|
| 1 | +import json |
| 2 | + |
| 3 | +import pyhttpx |
| 4 | +import requests |
| 5 | + |
| 6 | + |
| 7 | +def getCloudflareDomainNameContent(Token, DomainID, RecordID): |
| 8 | + """ |
| 9 | + :param Token: |
| 10 | + :param DomainID: |
| 11 | + :param RecordID: |
| 12 | + :return: result from Cloudflare dns_records |
| 13 | + """ |
| 14 | + headers = { |
| 15 | + "Authorization": "Bearer " + Token, |
| 16 | + "Content-Type": "application/json" |
| 17 | + } |
| 18 | + session = pyhttpx.HttpSession() |
| 19 | + res = session.get( |
| 20 | + url='https://api.cloudflare.com/client/v4/zones/' + DomainID + '/dns_records/' + RecordID, |
| 21 | + headers=headers) |
| 22 | + res = json.loads(res.text) |
| 23 | + return res |
| 24 | + |
| 25 | + |
| 26 | +def verifyCloudflareToken(Token: str): |
| 27 | + """ |
| 28 | + :param Token: |
| 29 | + :return: True or False |
| 30 | + """ |
| 31 | + headers = { |
| 32 | + "Authorization": "Bearer " + Token, |
| 33 | + "Content-Type": "application/json" |
| 34 | + } |
| 35 | + session = pyhttpx.HttpSession() |
| 36 | + res = session.get(url='https://api.cloudflare.com/client/v4/user/tokens/verify', headers=headers) |
| 37 | + res = json.loads(res.text) |
| 38 | + if res["result"]["status"] == "active": |
| 39 | + return True |
| 40 | + else: |
| 41 | + return False |
| 42 | + |
| 43 | + |
| 44 | +def updateCloudflareContent(Token: str, DDNSType: str, DDNSHostName: str, DDNSContent: str, DDNS_TTL: int, |
| 45 | + isProxied: bool, DomainID: str, |
| 46 | + RecordID: str): |
| 47 | + """ |
| 48 | + :param Token: str |
| 49 | + :param DDNSType: str |
| 50 | + :param DDNSHostName: str |
| 51 | + :param DDNSContent: str |
| 52 | + :param DDNS_TTL: int |
| 53 | + :param isProxied: bool |
| 54 | + :param DomainID: str |
| 55 | + :param RecordID: str |
| 56 | + :return: status_code |
| 57 | + """ |
| 58 | + headers = { |
| 59 | + "Authorization": "Bearer " + Token, |
| 60 | + "Content-Type": "application/json" |
| 61 | + } |
| 62 | + data = { |
| 63 | + "type": DDNSType, |
| 64 | + "name": DDNSHostName, |
| 65 | + "content": DDNSContent, |
| 66 | + "ttl": DDNS_TTL, |
| 67 | + "proxied": isProxied |
| 68 | + } |
| 69 | + res = requests.put( |
| 70 | + url='https://api.cloudflare.com/client/v4/zones/' + DomainID + '/dns_records/' + RecordID, |
| 71 | + headers=headers, json=data) |
| 72 | + return res.status_code |
| 73 | + |
| 74 | + |
| 75 | +def compareDDNSContent(NowContent: str, LastContent: str): |
| 76 | + """ |
| 77 | + :param NowContent: |
| 78 | + :param LastContent: you can use getCloudflareDomainNameContent()["result"]["content"] |
| 79 | + :return: True or False |
| 80 | + """ |
| 81 | + if NowContent == LastContent: |
| 82 | + return True |
| 83 | + else: |
| 84 | + return False |
0 commit comments