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.

159 lines
5.5 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. ################ Public functions ################
  6. # Usage: dns_world4you_add <fqdn> <value>
  7. dns_world4you_add() {
  8. fqdn="$1"
  9. value="$2"
  10. _info "Using world4you"
  11. _debug fulldomain "$fqdn"
  12. _debug txtvalue "$value"
  13. tld=$(echo "$fqdn" | _egrep_o '[^.]*\.[^.]*$')
  14. record=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#tld} - 1))")
  15. _login
  16. if [ "$?" != 0 ]; then
  17. return 1
  18. fi
  19. export _H1="Cookie: W4YSESSID=$sessid"
  20. paketnr=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht" | grep -B 3 "^\\s*$tld\$" | head -n 1 | sed 's/^.*>\([0-9][0-9]*\)<.*$/\1/')
  21. if [ -z "$paketnr" ]; then
  22. _err "Unable to parse paketnr"
  23. return 3
  24. fi
  25. _debug paketnr "$paketnr"
  26. export _H1="Cookie: W4YSESSID=$sessid"
  27. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  28. formiddp=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  29. formidttl=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  30. form_token=$(echo "$form" | grep 'AddDnsRecordForm\[_token\]' | sed 's/^.*name="AddDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  31. if [ -z "$formiddp" ]; then
  32. _err "Unable to parse form"
  33. return 3
  34. fi
  35. _ORIG_ACME_CURL="$_ACME_CURL"
  36. _ACME_CURL=$(echo "$_ACME_CURL" | sed 's/ -L / /')
  37. body="AddDnsRecordForm[name]=$record&AddDnsRecordForm[dnsType][type]=TXT&\
  38. AddDnsRecordForm[value]=$value&AddDnsRecordForm[aktivPaket]=$paketnr&AddDnsRecordForm[uniqueFormIdDP]=$formiddp&\
  39. AddDnsRecordForm[uniqueFormIdTTL]=$formidttl&AddDnsRecordForm[_token]=$form_token"
  40. _info "Adding record..."
  41. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/dns" '' POST 'application/x-www-form-urlencoded')
  42. _ACME_CURL="$_ORIG_ACME_CURL"
  43. success=$(grep '302\|200' <"$HTTP_HEADER")
  44. if [ "$success" ]; then
  45. return 0
  46. else
  47. _err "$(head -n 1 <"$HTTP_HEADER")"
  48. return 2
  49. fi
  50. }
  51. # Usage: dns_world4you_rm <fqdn> <value>
  52. dns_world4you_rm() {
  53. fqdn="$1"
  54. value="$2"
  55. _info "Using world4you"
  56. _debug fulldomain "$fqdn"
  57. _debug txtvalue "$value"
  58. tld=$(echo "$fqdn" | _egrep_o '[^.]*\.[^.]*$')
  59. record=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#tld} - 1))")
  60. _login
  61. if [ "$?" != 0 ]; then
  62. return 1
  63. fi
  64. export _H1="Cookie: W4YSESSID=$sessid"
  65. paketnr=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht" | grep -B 3 "^\\s*$tld\$" | head -n 1 | sed 's/^.*>\([0-9][0-9]*\).*$/\1/')
  66. if [ -z "$paketnr" ]; then
  67. _err "Unable to parse paketnr"
  68. return 3
  69. fi
  70. _debug paketnr "$paketnr"
  71. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  72. formiddp=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  73. formidttl=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  74. form_token=$(echo "$form" | grep 'DeleteDnsRecordForm\[_token\]' | sed 's/^.*name="DeleteDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  75. if [ -z "$formiddp" ]; then
  76. _err "Unable to parse form"
  77. return 3
  78. fi
  79. recordid=$(printf "TXT:%s.:\"%s\"" "$fqdn" "$value" | _base64)
  80. _debug recordid "$recordid"
  81. _ORIG_ACME_CURL="$_ACME_CURL"
  82. _ACME_CURL=$(echo "$_ACME_CURL" | sed 's/ -L / /')
  83. body="DeleteDnsRecordForm[recordId]=$recordid&DeleteDnsRecordForm[aktivPaket]=$paketnr&\
  84. DeleteDnsRecordForm[uniqueFormIdDP]=$formiddp&DeleteDnsRecordForm[uniqueFormIdTTL]=$formidttl&\
  85. DeleteDnsRecordForm[_token]=$form_token"
  86. _info "Removing record..."
  87. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/deleteRecord" '' POST 'application/x-www-form-urlencoded')
  88. _ACME_CURL="$_ORIG_ACME_CURL"
  89. success=$(grep '302\|200' <"$HTTP_HEADER")
  90. if [ "$success" ]; then
  91. return 0
  92. else
  93. _err "$(head -n 1 <"$HTTP_HEADER")"
  94. return 2
  95. fi
  96. }
  97. ################ Private functions ################
  98. # Usage: _login
  99. _login() {
  100. WORLD4YOU_USERNAME="${WORLD4YOU_USERNAME:-$(_readaccountconf_mutable WORLD4YOU_USERNAME)}"
  101. WORLD4YOU_PASSWORD="${WORLD4YOU_PASSWORD:-$(_readaccountconf_mutable WORLD4YOU_PASSWORD)}"
  102. if [ -z "$WORLD4YOU_USERNAME" ] || [ -z "$WORLD4YOU_PASSWORD" ]; then
  103. WORLD4YOU_USERNAME=""
  104. WORLD4YOU_PASSWORD=""
  105. _err "You don't specified world4you username and password yet."
  106. _err "Usage: export WORLD4YOU_USERNAME=<name>"
  107. _err "Usage: export WORLD4YOU_PASSWORD=<password>"
  108. return 2
  109. fi
  110. _saveaccountconf_mutable WORLD4YOU_USERNAME "$WORLD4YOU_USERNAME"
  111. _saveaccountconf_mutable WORLD4YOU_PASSWORD "$WORLD4YOU_PASSWORD"
  112. _info "Logging in..."
  113. username="$WORLD4YOU_USERNAME"
  114. password="$WORLD4YOU_PASSWORD"
  115. csrf_token=$(_get "$WORLD4YOU_API/login" | grep '_csrf_token' | sed 's/^.*<input[^>]*value=\"\([^"]*\)\".*$/\1/')
  116. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  117. export _H1="Cookie: W4YSESSID=$sessid"
  118. export _H2="X-Requested-With: XMLHttpRequest"
  119. body="_username=$username&_password=$password&_csrf_token=$csrf_token"
  120. ret=$(_post "$body" "$WORLD4YOU_API/login" '' POST 'application/x-www-form-urlencoded')
  121. unset _H2
  122. _debug ret "$ret"
  123. if _contains "$ret" "\"success\":true"; then
  124. _info "Successfully logged in"
  125. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  126. else
  127. _err "Unable to log in: $(echo "$ret" | sed 's/^.*"message":"\([^\"]*\)".*$/\1/')"
  128. return 1
  129. fi
  130. }