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.

129 lines
3.6 KiB

5 years ago
  1. #!/usr/bin/env sh
  2. # Joker.com API for acme.sh
  3. #
  4. # This script adds the necessary TXT record to a domain in Joker.com.
  5. #
  6. # You must activate Dynamic DNS in Joker.com DNS configuration first.
  7. # Username and password below refer to Dynamic DNS authentication,
  8. # not your Joker.com login credentials.
  9. # See: https://joker.com/faq/content/11/427/en/what-is-dynamic-dns-dyndns.html
  10. #
  11. # NOTE: This script does not support wildcard certificates, because
  12. # Joker.com API does not support adding two TXT records with the same
  13. # subdomain. Adding the second record will overwrite the first one.
  14. # See: https://joker.com/faq/content/6/496/en/let_s-encrypt-support.html
  15. # "... this request will replace all TXT records for the specified
  16. # label by the provided content"
  17. #
  18. # Author: aattww (https://github.com/aattww/)
  19. #
  20. # JOKER_USERNAME="xxxx"
  21. # JOKER_PASSWORD="xxxx"
  22. JOKER_API="https://svc.joker.com/nic/replace"
  23. ######## Public functions #####################
  24. #Usage: dns_joker_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  25. dns_joker_add() {
  26. fulldomain=$1
  27. txtvalue=$2
  28. JOKER_USERNAME="${JOKER_USERNAME:-$(_readaccountconf_mutable JOKER_USERNAME)}"
  29. JOKER_PASSWORD="${JOKER_PASSWORD:-$(_readaccountconf_mutable JOKER_PASSWORD)}"
  30. if [ -z "$JOKER_USERNAME" ] || [ -z "$JOKER_PASSWORD" ]; then
  31. _err "No Joker.com username and password specified."
  32. return 1
  33. fi
  34. _saveaccountconf_mutable JOKER_USERNAME "$JOKER_USERNAME"
  35. _saveaccountconf_mutable JOKER_PASSWORD "$JOKER_PASSWORD"
  36. if ! _get_root "$fulldomain"; then
  37. _err "Invalid domain"
  38. return 1
  39. fi
  40. _info "Adding TXT record"
  41. if _joker_rest "username=$JOKER_USERNAME&password=$JOKER_PASSWORD&zone=$_domain&label=$_sub_domain&type=TXT&value=$txtvalue"; then
  42. if _startswith "$response" "OK"; then
  43. _info "Added, OK"
  44. return 0
  45. fi
  46. fi
  47. _err "Error adding TXT record."
  48. return 1
  49. }
  50. #fulldomain txtvalue
  51. dns_joker_rm() {
  52. fulldomain=$1
  53. txtvalue=$2
  54. JOKER_USERNAME="${JOKER_USERNAME:-$(_readaccountconf_mutable JOKER_USERNAME)}"
  55. JOKER_PASSWORD="${JOKER_PASSWORD:-$(_readaccountconf_mutable JOKER_PASSWORD)}"
  56. if ! _get_root "$fulldomain"; then
  57. _err "Invalid domain"
  58. return 1
  59. fi
  60. _info "Removing TXT record"
  61. # TXT record is removed by setting its value to empty.
  62. if _joker_rest "username=$JOKER_USERNAME&password=$JOKER_PASSWORD&zone=$_domain&label=$_sub_domain&type=TXT&value="; then
  63. if _startswith "$response" "OK"; then
  64. _info "Removed, OK"
  65. return 0
  66. fi
  67. fi
  68. _err "Error removing TXT record."
  69. return 1
  70. }
  71. #################### Private functions below ##################################
  72. #_acme-challenge.www.domain.com
  73. #returns
  74. # _sub_domain=_acme-challenge.www
  75. # _domain=domain.com
  76. _get_root() {
  77. fulldomain=$1
  78. i=1
  79. while true; do
  80. h=$(printf "%s" "$fulldomain" | cut -d . -f $i-100)
  81. _debug h "$h"
  82. if [ -z "$h" ]; then
  83. return 1
  84. fi
  85. # Try to remove a test record. With correct root domain, username and password this will return "OK: ..." regardless
  86. # of record in question existing or not.
  87. if _joker_rest "username=$JOKER_USERNAME&password=$JOKER_PASSWORD&zone=$h&label=jokerTXTUpdateTest&type=TXT&value="; then
  88. if _startswith "$response" "OK"; then
  89. _sub_domain="$(echo "$fulldomain" | sed "s/\\.$h\$//")"
  90. _domain=$h
  91. return 0
  92. fi
  93. fi
  94. i=$(_math "$i" + 1)
  95. done
  96. _debug "Root domain not found"
  97. return 1
  98. }
  99. _joker_rest() {
  100. data="$1"
  101. _debug data "$data"
  102. response="$(_post "$data" "$JOKER_API" "" "POST")"
  103. if [ "$?" != "0" ]; then
  104. _err "Error POSTing"
  105. return 1
  106. fi
  107. _debug response "$response"
  108. return 0
  109. }