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.

188 lines
5.9 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"
  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. tld="$TLD"
  25. record="$RECORD"
  26. if [ -z "$paketnr" ]; then
  27. _err "Unable to parse paketnr"
  28. return 3
  29. fi
  30. _debug paketnr "$paketnr"
  31. export _H1="Cookie: W4YSESSID=$sessid"
  32. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  33. formiddp=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  34. formidttl=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  35. form_token=$(echo "$form" | grep 'AddDnsRecordForm\[_token\]' | sed 's/^.*name="AddDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  36. if [ -z "$formiddp" ]; then
  37. _err "Unable to parse form"
  38. return 3
  39. fi
  40. _ORIG_ACME_CURL="$_ACME_CURL"
  41. _ACME_CURL=$(echo "$_ACME_CURL" | sed 's/ -L / /')
  42. body="AddDnsRecordForm[name]=$record&AddDnsRecordForm[dnsType][type]=TXT&\
  43. AddDnsRecordForm[value]=$value&AddDnsRecordForm[aktivPaket]=$paketnr&AddDnsRecordForm[uniqueFormIdDP]=$formiddp&\
  44. AddDnsRecordForm[uniqueFormIdTTL]=$formidttl&AddDnsRecordForm[_token]=$form_token"
  45. _info "Adding record..."
  46. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/dns" '' POST 'application/x-www-form-urlencoded')
  47. _ACME_CURL="$_ORIG_ACME_CURL"
  48. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  49. return 0
  50. else
  51. _err "$(head -n 1 <"$HTTP_HEADER")"
  52. return 2
  53. fi
  54. }
  55. # Usage: dns_world4you_rm <fqdn> <value>
  56. dns_world4you_rm() {
  57. fqdn="$1"
  58. value="$2"
  59. _info "Using world4you"
  60. _debug fulldomain "$fqdn"
  61. _debug txtvalue "$value"
  62. tld=$(echo "$fqdn" | _egrep_o '[^.]*\.[^.]*$')
  63. record=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#tld} - 1))")
  64. _login
  65. if [ "$?" != 0 ]; then
  66. return 1
  67. fi
  68. export _H1="Cookie: W4YSESSID=$sessid"
  69. form=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht")
  70. _get_paketnr "$fqdn" "$form"
  71. paketnr="$PAKETNR"
  72. tld="$TLD"
  73. record="$RECORD"
  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. _ORIG_ACME_CURL="$_ACME_CURL"
  90. _ACME_CURL=$(echo "$_ACME_CURL" | sed 's/ -L / /')
  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. _ACME_CURL="$_ORIG_ACME_CURL"
  97. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  98. return 0
  99. else
  100. _err "$(head -n 1 <"$HTTP_HEADER")"
  101. return 2
  102. fi
  103. }
  104. ################ Private functions ################
  105. # Usage: _login
  106. _login() {
  107. WORLD4YOU_USERNAME="${WORLD4YOU_USERNAME:-$(_readaccountconf_mutable WORLD4YOU_USERNAME)}"
  108. WORLD4YOU_PASSWORD="${WORLD4YOU_PASSWORD:-$(_readaccountconf_mutable WORLD4YOU_PASSWORD)}"
  109. if [ -z "$WORLD4YOU_USERNAME" ] || [ -z "$WORLD4YOU_PASSWORD" ]; then
  110. WORLD4YOU_USERNAME=""
  111. WORLD4YOU_PASSWORD=""
  112. _err "You don't specified world4you username and password yet."
  113. _err "Usage: export WORLD4YOU_USERNAME=<name>"
  114. _err "Usage: export WORLD4YOU_PASSWORD=<password>"
  115. return 2
  116. fi
  117. _saveaccountconf_mutable WORLD4YOU_USERNAME "$WORLD4YOU_USERNAME"
  118. _saveaccountconf_mutable WORLD4YOU_PASSWORD "$WORLD4YOU_PASSWORD"
  119. _info "Logging in..."
  120. username="$WORLD4YOU_USERNAME"
  121. password="$WORLD4YOU_PASSWORD"
  122. csrf_token=$(_get "$WORLD4YOU_API/login" | grep '_csrf_token' | sed 's/^.*<input[^>]*value=\"\([^"]*\)\".*$/\1/')
  123. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  124. export _H1="Cookie: W4YSESSID=$sessid"
  125. export _H2="X-Requested-With: XMLHttpRequest"
  126. body="_username=$username&_password=$password&_csrf_token=$csrf_token"
  127. ret=$(_post "$body" "$WORLD4YOU_API/login" '' POST 'application/x-www-form-urlencoded')
  128. unset _H2
  129. _debug ret "$ret"
  130. if _contains "$ret" "\"success\":true"; then
  131. _info "Successfully logged in"
  132. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  133. else
  134. _err "Unable to log in: $(echo "$ret" | sed 's/^.*"message":"\([^\"]*\)".*$/\1/')"
  135. return 1
  136. fi
  137. }
  138. # Usage _get_paketnr <fqdn> <form>
  139. _get_paketnr() {
  140. fqdn="$1"
  141. form="$2"
  142. domains=$(echo "$form" | _egrep_o '^\s*([A-Za-z0-9_-]+\.)+[A-Za-z0-9_-]*$' | sed 's/^\s*\(\S*\)$/\1/')
  143. domain=''
  144. for domain in $domains; do
  145. if echo "$fqdn" | grep "$domain\$" >/dev/null; then
  146. break
  147. fi
  148. domain=''
  149. done
  150. if [ -z "$domain" ]; then
  151. return 1
  152. fi
  153. TLD="$domain"
  154. RECORD=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#TLD} - 1))")
  155. PAKETNR=$(echo "$form" | _egrep_o "data-textfilter=\" $domain .* [0-9]*" | head -n 1 | _egrep_o "[0-9]*")
  156. return 0
  157. }