You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.1 KiB

  1. #!/usr/bin/env sh
  2. # World4You - www.world4you.com
  3. # Lorenz Stechauner, 2020 - https://www.github.com/NerLOR
  4. WORLD4YOU_API="https://my.world4you.com/en"
  5. PAKETNR=''
  6. TLD=''
  7. RECORD=''
  8. ################ Public functions ################
  9. # Usage: dns_world4you_add <fqdn> <value>
  10. dns_world4you_add() {
  11. fqdn="$1"
  12. value="$2"
  13. _info "Using world4you to add record"
  14. _debug fulldomain "$fqdn"
  15. _debug txtvalue "$value"
  16. _login
  17. if [ "$?" != 0 ]; then
  18. return 1
  19. fi
  20. export _H1="Cookie: W4YSESSID=$sessid"
  21. form=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht")
  22. _get_paketnr "$fqdn" "$form"
  23. paketnr="$PAKETNR"
  24. if [ -z "$paketnr" ]; then
  25. _err "Unable to parse paketnr"
  26. return 3
  27. fi
  28. _debug paketnr "$paketnr"
  29. export _H1="Cookie: W4YSESSID=$sessid"
  30. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  31. formiddp=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  32. formidttl=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  33. form_token=$(echo "$form" | grep 'AddDnsRecordForm\[_token\]' | sed 's/^.*name="AddDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  34. if [ -z "$formiddp" ]; then
  35. _err "Unable to parse form"
  36. return 3
  37. fi
  38. _resethttp
  39. export ACME_HTTP_NO_REDIRECTS=1
  40. body="AddDnsRecordForm[name]=$RECORD&AddDnsRecordForm[dnsType][type]=TXT&\
  41. AddDnsRecordForm[value]=$value&AddDnsRecordForm[aktivPaket]=$paketnr&AddDnsRecordForm[uniqueFormIdDP]=$formiddp&\
  42. AddDnsRecordForm[uniqueFormIdTTL]=$formidttl&AddDnsRecordForm[_token]=$form_token"
  43. _info "Adding record..."
  44. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/dns" '' POST 'application/x-www-form-urlencoded')
  45. _resethttp
  46. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  47. msg=$(_get "$WORLD4YOU_API/$paketnr/dns" | tr '\n' '\t' | sed 's/.*<h3 class="mb-5">[^\t]*\t *\([^\t]*\)\t.*/\1/')
  48. if _contains "$msg" "successfully"; then
  49. return 0
  50. else
  51. _err "Unable to add record: $msg"
  52. return 1
  53. fi
  54. else
  55. _err "$(_head_n 1 <"$HTTP_HEADER")"
  56. return 1
  57. fi
  58. }
  59. # Usage: dns_world4you_rm <fqdn> <value>
  60. dns_world4you_rm() {
  61. fqdn="$1"
  62. value="$2"
  63. _info "Using world4you to remove record"
  64. _debug fulldomain "$fqdn"
  65. _debug txtvalue "$value"
  66. _login
  67. if [ "$?" != 0 ]; then
  68. return 1
  69. fi
  70. export _H1="Cookie: W4YSESSID=$sessid"
  71. form=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht")
  72. _get_paketnr "$fqdn" "$form"
  73. paketnr="$PAKETNR"
  74. if [ -z "$paketnr" ]; then
  75. _err "Unable to parse paketnr"
  76. return 3
  77. fi
  78. _debug paketnr "$paketnr"
  79. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  80. formiddp=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  81. formidttl=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  82. form_token=$(echo "$form" | grep 'DeleteDnsRecordForm\[_token\]' | sed 's/^.*name="DeleteDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  83. if [ -z "$formiddp" ]; then
  84. _err "Unable to parse form"
  85. return 3
  86. fi
  87. recordid=$(printf "TXT:%s.:\"%s\"" "$fqdn" "$value" | _base64)
  88. _debug recordid "$recordid"
  89. _resethttp
  90. export ACME_HTTP_NO_REDIRECTS=1
  91. body="DeleteDnsRecordForm[recordId]=$recordid&DeleteDnsRecordForm[aktivPaket]=$paketnr&\
  92. DeleteDnsRecordForm[uniqueFormIdDP]=$formiddp&DeleteDnsRecordForm[uniqueFormIdTTL]=$formidttl&\
  93. DeleteDnsRecordForm[_token]=$form_token"
  94. _info "Removing record..."
  95. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/deleteRecord" '' POST 'application/x-www-form-urlencoded')
  96. _resethttp
  97. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  98. msg=$(_get "$WORLD4YOU_API/$paketnr/dns" | tr '\n' '\t' | sed 's/.*<h3 class="mb-5">[^\t]*\t *\([^\t]*\)\t.*/\1/')
  99. if _contains "$msg" "successfully"; then
  100. return 0
  101. else
  102. _err "Unable to remove record: $msg"
  103. return 1
  104. fi
  105. else
  106. _err "$(_head_n 1 <"$HTTP_HEADER")"
  107. return 1
  108. fi
  109. }
  110. ################ Private functions ################
  111. # Usage: _login
  112. _login() {
  113. WORLD4YOU_USERNAME="${WORLD4YOU_USERNAME:-$(_readaccountconf_mutable WORLD4YOU_USERNAME)}"
  114. WORLD4YOU_PASSWORD="${WORLD4YOU_PASSWORD:-$(_readaccountconf_mutable WORLD4YOU_PASSWORD)}"
  115. if [ -z "$WORLD4YOU_USERNAME" ] || [ -z "$WORLD4YOU_PASSWORD" ]; then
  116. WORLD4YOU_USERNAME=""
  117. WORLD4YOU_PASSWORD=""
  118. _err "You didn't specify world4you username and password yet."
  119. _err "Usage: export WORLD4YOU_USERNAME=<name>"
  120. _err "Usage: export WORLD4YOU_PASSWORD=<password>"
  121. return 1
  122. fi
  123. _saveaccountconf_mutable WORLD4YOU_USERNAME "$WORLD4YOU_USERNAME"
  124. _saveaccountconf_mutable WORLD4YOU_PASSWORD "$WORLD4YOU_PASSWORD"
  125. _info "Logging in..."
  126. username="$WORLD4YOU_USERNAME"
  127. password="$WORLD4YOU_PASSWORD"
  128. csrf_token=$(_get "$WORLD4YOU_API/login" | grep '_csrf_token' | sed 's/^.*<input[^>]*value=\"\([^"]*\)\".*$/\1/')
  129. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  130. export _H1="Cookie: W4YSESSID=$sessid"
  131. export _H2="X-Requested-With: XMLHttpRequest"
  132. body="_username=$username&_password=$password&_csrf_token=$csrf_token"
  133. ret=$(_post "$body" "$WORLD4YOU_API/login" '' POST 'application/x-www-form-urlencoded')
  134. unset _H2
  135. _debug ret "$ret"
  136. if _contains "$ret" "\"success\":true"; then
  137. _info "Successfully logged in"
  138. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  139. else
  140. _err "Unable to log in: $(echo "$ret" | sed 's/^.*"message":"\([^\"]*\)".*$/\1/')"
  141. return 1
  142. fi
  143. }
  144. # Usage _get_paketnr <fqdn> <form>
  145. _get_paketnr() {
  146. fqdn="$1"
  147. form="$2"
  148. domains=$(echo "$form" | grep '^ *[A-Za-z0-9_\.-]*\.[A-Za-z0-9_-]*$' | sed 's/^\s*\(\S*\)$/\1/')
  149. domain=''
  150. for domain in $domains; do
  151. if echo "$fqdn" | grep "$domain\$" >/dev/null; then
  152. break
  153. fi
  154. domain=''
  155. done
  156. if [ -z "$domain" ]; then
  157. return 1
  158. fi
  159. TLD="$domain"
  160. RECORD=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#TLD} - 1))")
  161. PAKETNR=$(echo "$form" | grep "data-textfilter=\" $domain " | _head_n 1 | sed 's/^.* \([0-9]*\) .*$/\1/')
  162. return 0
  163. }