-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (43 loc) · 1.78 KB
/
main.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
import requests
import sys
import json
#---------------------
# Ngrok API
# https://dashboard.ngrok.com/api-keys
ngrokApiKey = ''
ngrokIP = ''
ngrokPort = 0
# Cloudflare API
# https://developers.cloudflare.com/api/
# https://dash.cloudflare.com/profile/api-tokens
cfApiKey = ''
cfEmail = ''
# Domain Details
cfZoneID = ''
cfDNSRecordID = ''
cfDNSname = ''
#---------------------
if ((cfZoneID == '' and cfApiKey == '' and cfEmail == '') or (cfZoneID == '') or (cfApiKey == '') or (cfEmail == '')):
print('CF API Key and Email are required')
sys.exit()
if ngrokApiKey == '':
print('Ngrok Token is required')
sys.exit()
def makeGetRequest(url, headers):
response = requests.request("GET", url, headers=headers)
return response.json()
def makePatchRequest(url, headers, data):
response = requests.request("PATCH", url, json=data, headers=headers)
return response.json()
ngrokResult = makeGetRequest('https://api.ngrok.com/endpoints', headers = {'Ngrok-Version': '2', 'Authorization': f'Bearer {ngrokApiKey}'})
ngrokValue = ngrokResult['endpoints'][0]['hostport']
ngrokIP, ngrokPort = ngrokValue.split(":")
updateData = { "type": "SRV", "name": cfDNSname, "data": { "port": ngrokPort, "priority": 1, "weight": 1, "target": ngrokIP }, "comment": "update port by SF" }
headers = { "Content-Type": "application/json", "X-Auth-Email": cfEmail, "X-Auth-Key": cfApiKey }
url = 'https://api.cloudflare.com/client/v4/zones/' + cfZoneID + '/dns_records/' + cfDNSRecordID
responseCF = makePatchRequest(url, headers={"Content-Type": "application/json", 'X-Auth-Email': cfEmail, 'X-Auth-Key': cfApiKey}, data=updateData)
if(responseCF['success'] == 'true' or responseCF['success'] == True):
print('DNS Record Updated')
else:
print('Failed to update DNS Record')
print(responseCF)