-
Notifications
You must be signed in to change notification settings - Fork 143
/
cf-srv.py
executable file
·186 lines (166 loc) · 6.34 KB
/
cf-srv.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
import urllib.request
import json
import sys
# Natter notification script arguments
protocol, private_ip, private_port, public_ip, public_port = sys.argv[1:6]
cf_srv_service = "_minecraft"
cf_domain = "mc.example.com"
cf_auth_email = "[email protected]"
cf_auth_key = "d41d8cd98f00b204e9800998ecf8427e"
def main():
cf = CloudFlareDNS(cf_auth_email, cf_auth_key)
print(f"Setting {cf_domain} A record to {public_ip}...")
cf.set_a_record(cf_domain, public_ip)
print(f"Setting {cf_domain} SRV record to {protocol} port {public_port}...")
cf.set_srv_record(cf_domain, public_port, service=cf_srv_service, protocol=f"_{protocol}")
class CloudFlareDNS:
def __init__(self, auth_email, auth_key):
self.opener = urllib.request.build_opener()
self.opener.addheaders = [
("X-Auth-Email", auth_email),
("X-Auth-Key", auth_key),
("Content-Type", "application/json")
]
def set_a_record(self, name, ipaddr):
zone_id = self._find_zone_id(name)
if not zone_id:
raise ValueError("%s is not on CloudFlare" % name)
rec_id = self._find_a_record(zone_id, name)
if not rec_id:
rec_id = self._create_a_record(zone_id, name, ipaddr)
else:
rec_id = self._update_a_record(zone_id, rec_id, name, ipaddr)
return rec_id
def set_srv_record(self, name, port, service="_natter", protocol="_tcp"):
zone_id = self._find_zone_id(name)
if not zone_id:
raise ValueError("%s is not on CloudFlare" % name)
rec_id = self._find_srv_record(zone_id, name)
if not rec_id:
rec_id = self._create_srv_record(zone_id, name, service,
protocol, port, name)
else:
rec_id = self._update_srv_record(zone_id, rec_id, name, service,
protocol, port, name)
return rec_id
def _url_req(self, url, data=None, method=None):
data_bin = None
if data is not None:
data_bin = json.dumps(data).encode()
req = urllib.request.Request(url, data=data_bin, method=method)
try:
with self.opener.open(req, timeout=10) as res:
ret = json.load(res)
except urllib.error.HTTPError as e:
ret = json.load(e)
if "errors" not in ret:
raise RuntimeError(ret)
if not ret.get("success"):
raise RuntimeError(ret["errors"])
return ret
def _find_zone_id(self, name):
name = name.lower()
data = self._url_req(
f"https://api.cloudflare.com/client/v4/zones"
)
for zone_data in data["result"]:
zone_name = zone_data["name"]
if name == zone_name or name.endswith("." + zone_name):
zone_id = zone_data["id"]
return zone_id
return None
def _find_a_record(self, zone_id, name):
name = name.lower()
data = self._url_req(
f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records"
)
for rec_data in data["result"]:
if rec_data["type"] == "A" and rec_data["name"] == name:
rec_id = rec_data["id"]
return rec_id
return None
def _create_a_record(self, zone_id, name, ipaddr, proxied=False, ttl=120):
name = name.lower()
data = self._url_req(
f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records",
data={
"content": ipaddr,
"name": name,
"proxied": proxied,
"type": "A",
"ttl": ttl
},
method="POST"
)
return data["result"]["id"]
def _update_a_record(self, zone_id, rec_id, name, ipaddr, proxied=False, ttl=120):
name = name.lower()
data = self._url_req(
f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{rec_id}",
data={
"content": ipaddr,
"name": name,
"proxied": proxied,
"type": "A",
"ttl": ttl
},
method="PUT"
)
return data["result"]["id"]
def _find_srv_record(self, zone_id, name):
name = name.lower()
data = self._url_req(
f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records"
)
for rec_data in data["result"]:
if rec_data["type"] == "SRV" and rec_data["data"]["name"] == name:
rec_id = rec_data["id"]
return rec_id
return None
def _create_srv_record(self, zone_id, name, service, protocol, port, target,
priority=1, weight=10, ttl=120):
name = name.lower()
data = self._url_req(
f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records",
data={
"data": {
"name": name,
"port": port,
"priority": priority,
"proto": protocol,
"service": service,
"target": target,
"weight": weight
},
"proxied": False,
"type": "SRV",
"ttl": ttl
},
method="POST"
)
return data["result"]["id"]
def _update_srv_record(self, zone_id, rec_id, name, service, protocol, port, target,
priority=1, weight=10, ttl=120):
name = name.lower()
data = self._url_req(
f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{rec_id}",
data={
"data": {
"name": name,
"port": port,
"priority": priority,
"proto": protocol,
"service": service,
"target": target,
"weight": weight
},
"proxied": False,
"type": "SRV",
"ttl": ttl
},
method="PUT"
)
return data["result"]["id"]
if __name__ == "__main__":
main()