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.

194 lines
6.0 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. ################ Public functions ################
  7. # Usage: dns_world4you_add <fqdn> <value>
  8. dns_world4you_add() {
  9. fqdn="$1"
  10. value="$2"
  11. _info "Using world4you"
  12. _debug fulldomain "$fqdn"
  13. _debug txtvalue "$value"
  14. tld=$(echo "$fqdn" | _egrep_o '[^.]*\.[^.]*$')
  15. record=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#tld} - 1))")
  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 "$tld" "$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. _ORIG_ACME_CURL="$_ACME_CURL"
  39. _ACME_CURL=$(echo "$_ACME_CURL" | sed 's/ -L / /')
  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. _ACME_CURL="$_ORIG_ACME_CURL"
  46. success=$(grep '302\|200' <"$HTTP_HEADER")
  47. if [ "$success" ]; then
  48. return 0
  49. else
  50. _err "$(head -n 1 <"$HTTP_HEADER")"
  51. return 2
  52. fi
  53. }
  54. # Usage: dns_world4you_rm <fqdn> <value>
  55. dns_world4you_rm() {
  56. fqdn="$1"
  57. value="$2"
  58. _info "Using world4you"
  59. _debug fulldomain "$fqdn"
  60. _debug txtvalue "$value"
  61. tld=$(echo "$fqdn" | _egrep_o '[^.]*\.[^.]*$')
  62. record=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#tld} - 1))")
  63. _login
  64. if [ "$?" != 0 ]; then
  65. return 1
  66. fi
  67. export _H1="Cookie: W4YSESSID=$sessid"
  68. form=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht")
  69. _get_paketnr "$tld" "$form"
  70. paketnr="$PAKETNR"
  71. if [ -z "$paketnr" ]; then
  72. _err "Unable to parse paketnr"
  73. return 3
  74. fi
  75. _debug paketnr "$paketnr"
  76. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  77. formiddp=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  78. formidttl=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  79. form_token=$(echo "$form" | grep 'DeleteDnsRecordForm\[_token\]' | sed 's/^.*name="DeleteDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  80. if [ -z "$formiddp" ]; then
  81. _err "Unable to parse form"
  82. return 3
  83. fi
  84. recordid=$(printf "TXT:%s.:\"%s\"" "$fqdn" "$value" | _base64)
  85. _debug recordid "$recordid"
  86. _ORIG_ACME_CURL="$_ACME_CURL"
  87. _ACME_CURL=$(echo "$_ACME_CURL" | sed 's/ -L / /')
  88. body="DeleteDnsRecordForm[recordId]=$recordid&DeleteDnsRecordForm[aktivPaket]=$paketnr&\
  89. DeleteDnsRecordForm[uniqueFormIdDP]=$formiddp&DeleteDnsRecordForm[uniqueFormIdTTL]=$formidttl&\
  90. DeleteDnsRecordForm[_token]=$form_token"
  91. _info "Removing record..."
  92. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/deleteRecord" '' POST 'application/x-www-form-urlencoded')
  93. _ACME_CURL="$_ORIG_ACME_CURL"
  94. success=$(grep '302\|200' <"$HTTP_HEADER")
  95. if [ "$success" ]; then
  96. return 0
  97. else
  98. _err "$(head -n 1 <"$HTTP_HEADER")"
  99. return 2
  100. fi
  101. }
  102. ################ Private functions ################
  103. # Usage: _login
  104. _login() {
  105. WORLD4YOU_USERNAME="${WORLD4YOU_USERNAME:-$(_readaccountconf_mutable WORLD4YOU_USERNAME)}"
  106. WORLD4YOU_PASSWORD="${WORLD4YOU_PASSWORD:-$(_readaccountconf_mutable WORLD4YOU_PASSWORD)}"
  107. if [ -z "$WORLD4YOU_USERNAME" ] || [ -z "$WORLD4YOU_PASSWORD" ]; then
  108. WORLD4YOU_USERNAME=""
  109. WORLD4YOU_PASSWORD=""
  110. _err "You don't specified world4you username and password yet."
  111. _err "Usage: export WORLD4YOU_USERNAME=<name>"
  112. _err "Usage: export WORLD4YOU_PASSWORD=<password>"
  113. return 2
  114. fi
  115. _saveaccountconf_mutable WORLD4YOU_USERNAME "$WORLD4YOU_USERNAME"
  116. _saveaccountconf_mutable WORLD4YOU_PASSWORD "$WORLD4YOU_PASSWORD"
  117. _info "Logging in..."
  118. username="$WORLD4YOU_USERNAME"
  119. password="$WORLD4YOU_PASSWORD"
  120. csrf_token=$(_get "$WORLD4YOU_API/login" | grep '_csrf_token' | sed 's/^.*<input[^>]*value=\"\([^"]*\)\".*$/\1/')
  121. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  122. export _H1="Cookie: W4YSESSID=$sessid"
  123. export _H2="X-Requested-With: XMLHttpRequest"
  124. body="_username=$username&_password=$password&_csrf_token=$csrf_token"
  125. ret=$(_post "$body" "$WORLD4YOU_API/login" '' POST 'application/x-www-form-urlencoded')
  126. unset _H2
  127. _debug ret "$ret"
  128. if _contains "$ret" "\"success\":true"; then
  129. _info "Successfully logged in"
  130. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  131. else
  132. _err "Unable to log in: $(echo "$ret" | sed 's/^.*"message":"\([^\"]*\)".*$/\1/')"
  133. return 1
  134. fi
  135. }
  136. # Usage _get_paketnr <tld> <form>
  137. _get_paketnr() {
  138. tld="$1"
  139. form="$2"
  140. domains=$(echo "$form" | _ggrep -E '^\s*([A-Za-z0-9_-]+\.)+[A-Za-z0-9_-]*$' | sed 's/^\s*\(\S*\)$/\1/')
  141. domain=''
  142. for domain in $domains; do
  143. if [ "$(echo "$domain" | grep -q "$tld\$")" ]; then
  144. break
  145. fi
  146. done
  147. if [ -z "$domain" ]; then
  148. return 1
  149. fi
  150. PAKETNR=$(echo "$form" | _ggrep -B 3 "^\\s*$domain\$" | head -n 1 | sed 's/^.*>\([0-9][0-9]*\).*$/\1/')
  151. return 0
  152. }
  153. _ggrep() {
  154. if _exists "ggrep"; then
  155. ggrep "$@"
  156. return $?
  157. else
  158. grep "$@"
  159. return $?
  160. fi
  161. }