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.

182 lines
5.5 KiB

  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/acmesh-official/acme.sh
  10. #
  11. ######## Public functions #####################
  12. # Please Read this guide first: https://github.com/acmesh-official/acme.sh/wiki/DNS-API-Dev-Guide
  13. # API Calls to be made
  14. # _get("https://api.corp-jamo.tech/dns/v1/records/exists.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  15. # _get("https://api.corp-jamo.tech/dns/v1/records/exists.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  16. # _get("https://api.corp-jamo.tech/dns/v1/records/add.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  17. # _get("https://api.corp-jamo.tech/dns/v1/records/add.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  18. # _get("https://api.corp-jamo.tech/dns/v1/records/remove.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  19. # _get("https://api.corp-jamo.tech/dns/v1/records/remove.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  20. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  21. dns_jamotech_add() {
  22. fulldomain=$1
  23. txtvalue=$2
  24. JTECH_ENDIP="${JTECH_ENDIP:-$(_readaccountconf_mutable JTECH_ENDIP)}"
  25. JTECH_KEY="${JTECH_KEY:-$(_readaccountconf_mutable JTECH_KEY)}"
  26. if [ "$JTECH_ENDIP" ]; then
  27. _saveaccountconf_mutable JTECH_ENDIP "$JTECH_ENDIP"
  28. else
  29. _err "You need to specify an end IP by running 'export JTECH_ENDIP=IP'"
  30. return 1
  31. fi
  32. if [ "$JTECH_KEY" ]; then
  33. _saveaccountconf_mutable JTECH_KEY "$JTECH_KEY"
  34. else
  35. _err "You need to specify an API Key by running 'export JTECH_KEY=APIKEY'"
  36. return 1
  37. fi
  38. _info "Using jamotech-register to add the TXT record"
  39. _get_root
  40. _create_record
  41. _debug fulldomain "$fulldomain"
  42. _debug txtvalue "$txtvalue"
  43. }
  44. #Usage: fulldomain txtvalue
  45. #Remove the txt record after validation.
  46. dns_jamotech_rm() {
  47. fulldomain=$1
  48. txtvalue=$2
  49. JTECH_ENDIP="${JTECH_ENDIP:-$(_readaccountconf_mutable JTECH_ENDIP)}"
  50. JTECH_KEY="${JTECH_KEY:-$(_readaccountconf_mutable JTECH_KEY)}"
  51. if [ "$JTECH_ENDIP" ]; then
  52. _saveaccountconf_mutable JTECH_ENDIP "$JTECH_ENDIP"
  53. else
  54. _err "You need to specify an end IP by running 'export JTECH_ENDIP=IP'"
  55. return 1
  56. fi
  57. if [ "$JTECH_KEY" ]; then
  58. _saveaccountconf_mutable JTECH_KEY "$JTECH_KEY"
  59. else
  60. _err "You need to specify an API Key by running 'export JTECH_KEY=APIKEY'"
  61. return 1
  62. fi
  63. _info "Using jamotech-clean to remove the TXT record"
  64. _get_root
  65. _remove_record
  66. _debug fulldomain "$fulldomain"
  67. _debug txtvalue "$txtvalue"
  68. }
  69. #################### Private functions below ##################################
  70. # _acme-challenge.www.domain.com
  71. # returns
  72. # _domain=domain.com
  73. # _txtdomain=_acme-challenge.www
  74. # _adomain=www
  75. _get_root() {
  76. domain=$fulldomain
  77. txtdomain=${domain%.jamo.tech}
  78. subdomain=$(echo "$txtdomain" | cut -d'.' -f2-)
  79. _debug "txtdomain = $txtdomain"
  80. _debug "subdomain = $subdomain"
  81. _debug "Domain: $domain TXTDomain: $txtdomain Subdomain: $subdomain"
  82. if [ -z "$domain" ] || [ -z "$txtdomain" ] || [ -z "$subdomain" ] ; then
  83. _err "We weren't able to determine the records which need to be created."
  84. return 1
  85. fi
  86. _txthost="$txtdomain"
  87. _subhost="$subdomain"
  88. _err "$domain not found"
  89. return 1
  90. }
  91. _check_record() {
  92. server_record="https://api.corp-jamo.tech/dns/v1/records/exists.php?access=$JTECH_KEY&hostname=$subdomain&target=$JTECH_ENDIP&type=A"
  93. txt_record="https://api.corp-jamo.tech/dns/v1/records/exists.php?access=$JTECH_KEY&hostname=$txtdomain&target=$txtvalue&type=TXT"
  94. _debug "API ENDPOINTS $server_record $txt_record"
  95. response="$(_get "$server_record")"
  96. if [ "$?" != "0" ]; then
  97. _err "error"
  98. return 1
  99. fi
  100. if _contains "$response" '"exists":"true"}'; then
  101. _err "Record already exists."
  102. return 1
  103. fi
  104. response="$(_get "$txt_record")"
  105. if [ "$?" != "0" ]; then
  106. _err "error"
  107. return 1
  108. fi
  109. if _contains "$response" '"exists":"true"}'; then
  110. _err "Record already exists."
  111. return 1
  112. fi
  113. }
  114. _create_record() {
  115. _check_record
  116. server_record="https://api.corp-jamo.tech/dns/v1/records/add.php?access=$JTECH_KEY&hostname=$subdomain&target=$JTECH_ENDIP&type=A"
  117. txt_record="https://api.corp-jamo.tech/dns/v1/records/add.php?access=$JTECH_KEY&hostname=$txtdomain&target=$txtvalue&type=TXT"
  118. _debug "API ENDPOINTS $server_record $txt_record"
  119. response="$(_get "$server_record")"
  120. if [ "$?" != "0" ]; then
  121. _err "error"
  122. return 1
  123. fi
  124. response="$(_get "$txt_record")"
  125. if [ "$?" != "0" ]; then
  126. _err "error"
  127. return 1
  128. fi
  129. return 0
  130. }
  131. _remove_record() {
  132. server_record="https://api.corp-jamo.tech/dns/v1/records/remove.php?access=$JTECH_KEY&hostname=$subdomain&target=$JTECH_ENDIP&type=A"
  133. txt_record="https://api.corp-jamo.tech/dns/v1/records/remove.php?access=$JTECH_KEY&hostname=$txtdomain&target=$txtvalue&type=TXT"
  134. _debug "API ENDPOINTS $server_record $txt_record"
  135. response="$(_get "$server_record")"
  136. if [ "$?" != "0" ]; then
  137. _err "error"
  138. return 1
  139. fi
  140. response="$(_get "$txt_record")"
  141. if [ "$?" != "0" ]; then
  142. _err "error"
  143. return 1
  144. fi
  145. return 0
  146. }