Hunter0x7c7
2022-08-11 3cd6b479d058b8ee96e1b773c8034f0ca8865f9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env sh
 
#
#ZM_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
#
#https://zonomi.com dns api
 
ZM_Api="https://zonomi.com/app/dns/dyndns.jsp"
 
########  Public functions #####################
 
#Usage: add  _acme-challenge.www.domain.com   "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_zonomi_add() {
  fulldomain=$1
  txtvalue=$2
 
  ZM_Key="${ZM_Key:-$(_readaccountconf_mutable ZM_Key)}"
 
  if [ -z "$ZM_Key" ]; then
    ZM_Key=""
    _err "You don't specify zonomi api key yet."
    _err "Please create your key and try again."
    return 1
  fi
 
  #save the api key to the account conf file.
  _saveaccountconf_mutable ZM_Key "$ZM_Key"
 
  _info "Get existing txt records for $fulldomain"
  if ! _zm_request "action=QUERY&name=$fulldomain"; then
    _err "error"
    return 1
  fi
 
  if _contains "$response" "<record"; then
    _debug "get and update records"
    _qstr="action[1]=SET&type[1]=TXT&name[1]=$fulldomain&value[1]=$txtvalue"
    _qindex=2
    for t in $(echo "$response" | tr -d "\r\n" | _egrep_o '<action.*</action>' | tr "<" "\n" | grep record | grep 'type="TXT"' | cut -d '"' -f 6); do
      _debug2 t "$t"
      _qstr="$_qstr&action[$_qindex]=SET&type[$_qindex]=TXT&name[$_qindex]=$fulldomain&value[$_qindex]=$t"
      _qindex="$(_math "$_qindex" + 1)"
    done
    _zm_request "$_qstr"
  else
    _debug "Just add record"
    _zm_request "action=SET&type=TXT&name=$fulldomain&value=$txtvalue"
  fi
 
}
 
#fulldomain txtvalue
dns_zonomi_rm() {
  fulldomain=$1
  txtvalue=$2
 
  ZM_Key="${ZM_Key:-$(_readaccountconf_mutable ZM_Key)}"
  if [ -z "$ZM_Key" ]; then
    ZM_Key=""
    _err "You don't specify zonomi api key yet."
    _err "Please create your key and try again."
    return 1
  fi
 
  _zm_request "action=DELETE&type=TXT&name=$fulldomain"
 
}
 
####################  Private functions below ##################################
#qstr
_zm_request() {
  qstr="$1"
 
  _debug2 "qstr" "$qstr"
 
  _zm_url="$ZM_Api?api_key=$ZM_Key&$qstr"
  _debug2 "_zm_url" "$_zm_url"
  response="$(_get "$_zm_url")"
 
  if [ "$?" != "0" ]; then
    return 1
  fi
  _debug2 response "$response"
  _contains "$response" "<is_ok>OK:"
}