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.

268 lines
8.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_1984hosting_info='1984.hosting
  4. Domains: 1984.is
  5. Site: 1984.hosting
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_1984hosting
  7. Options:
  8. One984HOSTING_Username Username
  9. One984HOSTING_Password Password
  10. Issues: github.com/acmesh-official/acme.sh/issues/2851
  11. Author: Adrian Fedoreanu
  12. '
  13. ######## Public functions #####################
  14. # Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. # Add a text record.
  16. dns_1984hosting_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. _info "Add TXT record using 1984Hosting."
  20. _debug fulldomain "$fulldomain"
  21. _debug txtvalue "$txtvalue"
  22. if ! _1984hosting_login; then
  23. _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
  24. return 1
  25. fi
  26. _debug "First detect the root zone."
  27. if ! _get_root "$fulldomain"; then
  28. _err "Invalid domain '$fulldomain'."
  29. return 1
  30. fi
  31. _debug _sub_domain "$_sub_domain"
  32. _debug _domain "$_domain"
  33. _debug "Add TXT record $fulldomain with value '$txtvalue'."
  34. value="$(printf '%s' "$txtvalue" | _url_encode)"
  35. url="https://1984.hosting/domains/entry/"
  36. postdata="entry=new"
  37. postdata="$postdata&type=TXT"
  38. postdata="$postdata&ttl=900"
  39. postdata="$postdata&zone=$_domain"
  40. postdata="$postdata&host=$_sub_domain"
  41. postdata="$postdata&rdata=%22$value%22"
  42. _debug2 postdata "$postdata"
  43. _authpost "$postdata" "$url"
  44. if _contains "$_response" '"haserrors": true'; then
  45. _err "1984Hosting failed to add TXT record for $_sub_domain bad RC from _post."
  46. return 1
  47. elif _contains "$_response" "html>"; then
  48. _err "1984Hosting failed to add TXT record for $_sub_domain. Check $HTTP_HEADER file."
  49. return 1
  50. elif _contains "$_response" '"auth": false'; then
  51. _err "1984Hosting failed to add TXT record for $_sub_domain. Invalid or expired cookie."
  52. return 1
  53. fi
  54. _info "Added acme challenge TXT record for $fulldomain at 1984Hosting."
  55. return 0
  56. }
  57. # Usage: fulldomain txtvalue
  58. # Remove the txt record after validation.
  59. dns_1984hosting_rm() {
  60. fulldomain=$1
  61. txtvalue=$2
  62. _info "Delete TXT record using 1984Hosting."
  63. _debug fulldomain "$fulldomain"
  64. _debug txtvalue "$txtvalue"
  65. if ! _1984hosting_login; then
  66. _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
  67. return 1
  68. fi
  69. _debug "First detect the root zone."
  70. if ! _get_root "$fulldomain"; then
  71. _err "Invalid domain '$fulldomain'."
  72. return 1
  73. fi
  74. _debug _sub_domain "$_sub_domain"
  75. _debug _domain "$_domain"
  76. _debug "Delete $fulldomain TXT record."
  77. url="https://1984.hosting/domains"
  78. if ! _get_zone_id "$url" "$_domain"; then
  79. _err "Invalid zone '$_domain'."
  80. return 1
  81. fi
  82. _htmlget "$url/$_zone_id" "$txtvalue"
  83. entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
  84. _debug2 entry_id "$entry_id"
  85. if [ -z "$entry_id" ]; then
  86. _err "Error getting TXT entry_id for $1."
  87. return 1
  88. fi
  89. _authpost "entry=$entry_id" "$url/delentry/"
  90. if ! _contains "$_response" '"ok": true'; then
  91. _err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post."
  92. return 1
  93. fi
  94. _info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting."
  95. return 0
  96. }
  97. #################### Private functions below ##################################
  98. _1984hosting_login() {
  99. if ! _check_credentials; then return 1; fi
  100. if _check_cookies; then
  101. _debug "Already logged in."
  102. return 0
  103. fi
  104. _debug "Login to 1984Hosting as user $One984HOSTING_Username."
  105. username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
  106. password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
  107. url="https://1984.hosting/api/auth/"
  108. _get "https://1984.hosting/accounts/login/" | grep "csrfmiddlewaretoken"
  109. csrftoken="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'csrftoken=[^;]*;' | tr -d ';')"
  110. sessionid="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
  111. if [ -z "$csrftoken" ] || [ -z "$sessionid" ]; then
  112. _err "One or more cookies are empty: '$csrftoken', '$sessionid'."
  113. return 1
  114. fi
  115. export _H1="Cookie: $csrftoken; $sessionid"
  116. export _H2="Referer: https://1984.hosting/accounts/login/"
  117. csrf_header=$(echo "$csrftoken" | sed 's/csrftoken=//' | _head_n 1)
  118. export _H3="X-CSRFToken: $csrf_header"
  119. response="$(_post "username=$username&password=$password&otpkey=" $url)"
  120. response="$(echo "$response" | _normalizeJson)"
  121. _debug2 response "$response"
  122. if _contains "$response" '"loggedin": true'; then
  123. One984HOSTING_SESSIONID_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
  124. One984HOSTING_CSRFTOKEN_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'csrftoken=[^;]*;' | tr -d ';')"
  125. export One984HOSTING_SESSIONID_COOKIE
  126. export One984HOSTING_CSRFTOKEN_COOKIE
  127. _saveaccountconf_mutable One984HOSTING_Username "$One984HOSTING_Username"
  128. _saveaccountconf_mutable One984HOSTING_Password "$One984HOSTING_Password"
  129. _saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
  130. _saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
  131. return 0
  132. fi
  133. return 1
  134. }
  135. _check_credentials() {
  136. One984HOSTING_Username="${One984HOSTING_Username:-$(_readaccountconf_mutable One984HOSTING_Username)}"
  137. One984HOSTING_Password="${One984HOSTING_Password:-$(_readaccountconf_mutable One984HOSTING_Password)}"
  138. if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
  139. One984HOSTING_Username=""
  140. One984HOSTING_Password=""
  141. _clearaccountconf_mutable One984HOSTING_Username
  142. _clearaccountconf_mutable One984HOSTING_Password
  143. _err "You haven't specified 1984Hosting username or password yet."
  144. _err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again."
  145. return 1
  146. fi
  147. return 0
  148. }
  149. _check_cookies() {
  150. One984HOSTING_SESSIONID_COOKIE="${One984HOSTING_SESSIONID_COOKIE:-$(_readaccountconf_mutable One984HOSTING_SESSIONID_COOKIE)}"
  151. One984HOSTING_CSRFTOKEN_COOKIE="${One984HOSTING_CSRFTOKEN_COOKIE:-$(_readaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE)}"
  152. if [ -z "$One984HOSTING_SESSIONID_COOKIE" ] || [ -z "$One984HOSTING_CSRFTOKEN_COOKIE" ]; then
  153. _debug "No cached cookie(s) found."
  154. return 1
  155. fi
  156. _authget "https://1984.hosting/api/auth/"
  157. if _contains "$_response" '"ok": true'; then
  158. _debug "Cached cookies still valid."
  159. return 0
  160. fi
  161. _debug "Cached cookies no longer valid. Clearing cookies."
  162. One984HOSTING_SESSIONID_COOKIE=""
  163. One984HOSTING_CSRFTOKEN_COOKIE=""
  164. _clearaccountconf_mutable One984HOSTING_SESSIONID_COOKIE
  165. _clearaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE
  166. return 1
  167. }
  168. # _acme-challenge.www.domain.com
  169. # Returns
  170. # _sub_domain=_acme-challenge.www
  171. # _domain=domain.com
  172. _get_root() {
  173. domain="$1"
  174. i=1
  175. p=1
  176. while true; do
  177. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  178. # not valid
  179. if [ -z "$h" ]; then
  180. return 1
  181. fi
  182. _authget "https://1984.hosting/domains/zonestatus/$h/?cached=no"
  183. if _contains "$_response" '"ok": true'; then
  184. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  185. _domain="$h"
  186. return 0
  187. fi
  188. p=$i
  189. i=$(_math "$i" + 1)
  190. done
  191. return 1
  192. }
  193. # Usage: _get_zone_id url domain.com
  194. # Returns zone id for domain.com
  195. _get_zone_id() {
  196. url=$1
  197. domain=$2
  198. _htmlget "$url" "$domain"
  199. _zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+' | _head_n 1)"
  200. _debug2 _zone_id "$_zone_id"
  201. if [ -z "$_zone_id" ]; then
  202. _err "Error getting _zone_id for $2."
  203. return 1
  204. fi
  205. return 0
  206. }
  207. # Add extra headers to request
  208. _authget() {
  209. export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
  210. _response=$(_get "$1" | _normalizeJson)
  211. _debug2 _response "$_response"
  212. }
  213. # Truncate huge HTML response
  214. _htmlget() {
  215. export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
  216. _response=$(_get "$1" | grep "$2")
  217. if _contains "$_response" "@$2"; then
  218. _response=$(echo "$_response" | grep -v "[@]" | _head_n 1)
  219. fi
  220. _debug2 _response "$_response"
  221. }
  222. # Add extra headers to request
  223. _authpost() {
  224. url="https://1984.hosting/domains"
  225. _get_zone_id "$url" "$_domain"
  226. csrf_header="$(echo "$One984HOSTING_CSRFTOKEN_COOKIE" | _egrep_o "=[^=][0-9a-zA-Z]*" | tr -d "=")"
  227. export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
  228. export _H2="Referer: https://1984.hosting/domains/$_zone_id"
  229. export _H3="X-CSRFToken: $csrf_header"
  230. _response="$(_post "$1" "$2" | _normalizeJson)"
  231. _debug2 _response "$_response"
  232. }