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.

206 lines
6.4 KiB

4 years ago
  1. #!/usr/bin/env sh
  2. #Here is a sample custom api script.
  3. #This file name is "dns_myapi.sh"
  4. #So, here must be a method dns_myapi_add()
  5. #Which will be called by acme.sh to add the txt record to your api system.
  6. #returns 0 means success, otherwise error.
  7. #
  8. #Author: Neilpang
  9. #Report Bugs here: https://github.com/Neilpang/acme.sh
  10. #
  11. ######## Public functions #####################
  12. # Please Read this guide first: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  13. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_dnsever_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. _info "Using dnsever add"
  18. _debug fulldomain "$fulldomain"
  19. _debug txtvalue "$txtvalue"
  20. DNSEVER_ID="${DNSEVER_ID:-$(_readaccountconf_mutable DNSEVER_ID)}"
  21. DNSEVER_PW="${DNSEVER_PW:-$(_readaccountconf_mutable DNSEVER_PW)}"
  22. if [ "$DNSEVER_ID" ]; then
  23. _saveaccountconf_mutable DNSEVER_ID "$DNSEVER_ID"
  24. _saveaccountconf_mutable DNSEVER_PW "$DNSEVER_PW"
  25. else
  26. if [ -z "$DNSEVER_ID" ] || [ -z "$DNSEVER_PW" ]; then
  27. DNSEVER_ID=""
  28. DNSEVER_PW=""
  29. _err "You didn't specify a DNSEVER ID and PW yet."
  30. return 1
  31. fi
  32. fi
  33. dnsever_domain_txt "add" "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
  34. #save the api key and email to the account conf file.
  35. return $?
  36. }
  37. #Usage: fulldomain txtvalue
  38. #Remove the txt record after validation.
  39. dns_dnsever_rm() {
  40. fulldomain=$1
  41. txtvalue=$2
  42. _info "Using dnsever remove"
  43. _debug fulldomain "$fulldomain"
  44. _debug txtvalue "$txtvalue"
  45. DNSEVER_ID="${DNSEVER_ID:-$(_readaccountconf_mutable DNSEVER_ID)}"
  46. DNSEVER_PW="${DNSEVER_PW:-$(_readaccountconf_mutable DNSEVER_PW)}"
  47. if [ -z "$DNSEVER_ID" ] || [ -z "$DNSEVER_PW" ]; then
  48. DNSEVER_ID=""
  49. DNSEVER_PW=""
  50. return 1
  51. fi
  52. dnsever_domain_txt "del" "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
  53. return $?
  54. }
  55. #################### Private functions below ##################################
  56. #_acme-challenge.www.domain.com
  57. #returns
  58. # _sub_domain=_acme-challenge.www
  59. # _domain=domain.com
  60. _get_root() {
  61. domain=$1
  62. i=2
  63. p=1
  64. while true; do
  65. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  66. if [ -z "$h" ]; then
  67. #not valid
  68. return 1
  69. fi
  70. #<OPTION value="flywithu.com" selected>flywithu.com</OPTION>
  71. domains=$(printf "%s\n" "$response" | _egrep_o "OPTION value=\".+\"" | tr -d '\n')
  72. _debug2 "h" "$h"
  73. _debug2 "domains" "$domains"
  74. if _contains "$domains" "$h"; then
  75. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  76. _domain="$h"
  77. return 0
  78. fi
  79. p=$i
  80. i=$(_math "$i" + 1)
  81. done
  82. return 1
  83. }
  84. _get_check_count() {
  85. domain=$1
  86. _err "res" "$response"
  87. }
  88. dnsever_domain_txt() {
  89. action="$1"
  90. login_id="$2"
  91. login_password="$3"
  92. domain_name="$4"
  93. domain_txt="$5"
  94. response=$(_post "login_id=$login_id&login_password=$login_password" "https://kr.dnsever.com/index.html")
  95. result=$?
  96. if [ $result != 0 ] || [ -z "$response" ]; then
  97. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/index.html with login_id=$login_id login_password=$login_password"
  98. return 1
  99. fi
  100. _H1="$(grep PHPSESSID "$HTTP_HEADER" | sed s/^Set-//)"
  101. export _H1
  102. response=$(_post "" "https://kr.dnsever.com/start.html")
  103. result=$?
  104. if [ $result != 0 ] || [ -z "$response" ]; then
  105. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/start.html after login"
  106. return 1
  107. fi
  108. # newhref=$(echo "$response" | sed -E "s/.*\'(.*)\'<.*/\1/")
  109. newhref=$(printf "%s\n" "$response" | _egrep_o "'.+'" | cut -d\' -f2)
  110. response=$(_post "" "$newhref")
  111. result=$?
  112. if [ $result != 0 ] || [ -z "$response" ]; then
  113. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/start.html after login"
  114. return 1
  115. fi
  116. # newhref=$(echo "$response" | sed -E "s/.*action=\"(.*)\" .*/\1/")
  117. newhref=$(printf "%s\n" "$response" | _egrep_o "https.+\" " | cut -d\" -f1)
  118. response=$(_post "" "$newhref")
  119. result=$?
  120. if [ $result != 0 ] || [ -z "$response" ]; then
  121. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/start.html after login"
  122. return 1
  123. fi
  124. response=$(_post "" "https://kr.dnsever.com/start.html")
  125. result=$?
  126. if [ $result != 0 ] || [ -z "$response" ]; then
  127. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/start.html after login"
  128. return 1
  129. fi
  130. skey=$(printf "%s\n" "$response" | _egrep_o "name=\"skey\" value=\".+\"" | cut -f3 -d= | tr -d \")
  131. _debug skey "$skey"
  132. if [ -z "$skey" ]; then
  133. _err "dnsever_txt:$action ERROR login failed with login_id=$login_id login_password=$login_password"
  134. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  135. return 1
  136. fi
  137. _get_root "$domain_name"
  138. _debug2 "fulldomain" "$domain_name"
  139. _debug2 "domain" "$_domain"
  140. _debug2 "subdomain" "$_sub_domain"
  141. _debug2 "txt" "$domain_txt"
  142. if [ "$action" = "add" ]; then
  143. ##https://kr.dnsever.com/start.html?user_domain=flywithu.com&selected_menu=edittxt&skey=flywithu:f80f523d2254f1e2c56462ace327f256
  144. # subname=$(echo "$domain_name" | sed "s/\.$user_domain\$//")
  145. response=$(_post "skey=$skey&user_domain=$_domain&selected_menu=edittxt&command=add_txt&subname=$_sub_domain&new_txt=$domain_txt" "https://kr.dnsever.com/start.html")
  146. result=$?
  147. if [ $result != 0 ] || [ -z "$response" ]; then
  148. _err "dnsever_txt:$action ERROR failed to add_text $domain_name=$domain_txt"
  149. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  150. fi
  151. elif [ "$action" = "del" ]; then
  152. #https://kr.dnsever.com/start.html?user_domain=flywithu.com&selected_menu=edittxt&skey=flywithu:41e3390a9b7aee2cce36c0012bb042b6
  153. response=$(_post "skey=$skey&user_domain=$_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html")
  154. # _debug2 "response" "$response" |cut -d\" -f1
  155. seq_1=$(printf "%s\n" "$response" | _egrep_o "name=\"seq_1\" value=\".+\"" | cut -f3 -d= | tr -d \")
  156. response=$(_post "skey=$skey&user_domain=$_domain&selected_menu=edittxt&command=delete_txt&domain_for_txt_1=$domain_name&old_txt_1=$domain_txt&txt_1=$domain_txt&check[]=1&seq_1=$seq_1&subname=&new_txt=" "https://kr.dnsever.com/start.html")
  157. result=$?
  158. if [ $result != 0 ] || [ -z "$response" ]; then
  159. _err "dnsever_txt:$action ERROR failed to delete $domain_name=$domain_txt from DNSEver"
  160. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  161. return 1
  162. fi
  163. fi
  164. return 0
  165. }