Skip to content

Commit 74dcfb5

Browse files
committed
first commit
1 parent faa18c9 commit 74dcfb5

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) <year> <copyright holders>
3+
Copyright (c) <2023> <Ghink Universe Technology Inc.>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
# AliDDNS
22

3-
DDNS Plugin with Aliyun
3+
DDNS Plugin with Aliyun
4+
5+
## Installation
6+
7+
Install all requirments with follow command:
8+
9+
`pip install -r requirements.txt`
10+
11+
## Running
12+
13+
1. Configure class in `main.py`, write down your credential and domain.
14+
2. Keep this script running on your machine, like running through `systemctl`.
15+
3. Enjoy :)

main.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import time
2+
import threading
3+
import json
4+
5+
import requests
6+
7+
from aliyunsdkcore.client import AcsClient
8+
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest
9+
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
10+
11+
class AliDDNS(object):
12+
def __init__(self,
13+
domain, record,
14+
id, secret,
15+
sleep = 600,
16+
ipv4 = True, ipv6 = True,
17+
v4api = "https://ipv4.gh.ink",
18+
v6api = "https://ipv6.gh.ink"):
19+
20+
assert domain.count(".") == 1 and \
21+
not domain.startswith(".") and \
22+
not domain.endswith(".")
23+
24+
self.domain = domain
25+
self.record = record
26+
self.sleep = sleep
27+
self.ipv4 = ipv4
28+
self.ipv6 = ipv6
29+
self.v4api = v4api
30+
self.v6api = v6api
31+
32+
self.__client = AcsClient(id, secret, 'cn-hangzhou')
33+
34+
daemon = threading.Thread(target=self.__daemon, name="Daemon Thread")
35+
daemon.daemon = True
36+
daemon.start()
37+
38+
def __daemon(self):
39+
while True:
40+
self.refresh()
41+
time.sleep(self.sleep)
42+
43+
def refresh(self):
44+
# Get local public ipv4 address
45+
if self.ipv4:
46+
try:
47+
self.__v4 = requests.get(self.v4api).text
48+
except Exception as e:
49+
print("Failed to get local public ipv4 address,", e)
50+
else:
51+
print("Your ipv4 address is:", self.__v4)
52+
53+
describe_v4 = DescribeDomainRecordsRequest()
54+
describe_v4.set_accept_format('json')
55+
56+
describe_v4.set_DomainName(self.domain)
57+
describe_v4.set_RRKeyWord(self.record)
58+
describe_v4.set_TypeKeyWord("A")
59+
60+
describeback_v4 = self.__client.do_action_with_exception(describe_v4)
61+
62+
recordlist_v4 = json.loads(describeback_v4)
63+
recordid_v4 = recordlist_v4['DomainRecords']['Record'][0]['RecordId']
64+
value_v4 = recordlist_v4['DomainRecords']['Record'][0]['Value']
65+
66+
if value_v4 != self.__v4:
67+
print('Your ipv4 address has been changed, update now.')
68+
update_v4 = UpdateDomainRecordRequest()
69+
update_v4.set_accept_format('json')
70+
71+
update_v4.set_RecordId(recordid_v4)
72+
update_v4.set_Value(self.__v4)
73+
update_v4.set_Type("A")
74+
update_v4.set_RR(self.record)
75+
76+
self.__client.do_action_with_exception(update_v4)
77+
print('Your ipv4 record has been updated successfully.')
78+
79+
80+
# Get local public ipv6 address
81+
if self.ipv6:
82+
try:
83+
self.__v6 = requests.get(self.v6api).text
84+
except Exception as e:
85+
print("Failed to get local public ipv4 address,", e)
86+
else:
87+
print("Your ipv6 address is:", self.__v6)
88+
89+
describe_v6 = DescribeDomainRecordsRequest()
90+
describe_v6.set_accept_format('json')
91+
92+
describe_v6.set_DomainName(self.domain)
93+
describe_v6.set_RRKeyWord(self.record)
94+
describe_v6.set_TypeKeyWord("AAAA")
95+
96+
describeback_v6 = self.__client.do_action_with_exception(describe_v6)
97+
98+
recordlist_v6 = json.loads(describeback_v6)
99+
recordid_v6 = recordlist_v6['DomainRecords']['Record'][0]['RecordId']
100+
value_v6 = recordlist_v6['DomainRecords']['Record'][0]['Value']
101+
102+
if value_v6 != self.__v6:
103+
print('Your ipv6 address has been changed, update now.')
104+
update_v6 = UpdateDomainRecordRequest()
105+
update_v6.set_accept_format('json')
106+
107+
update_v6.set_RecordId(recordid_v6)
108+
update_v6.set_Value(self.__v6)
109+
update_v6.set_Type("AAAA")
110+
update_v6.set_RR(self.record)
111+
112+
self.__client.do_action_with_exception(update_v6)
113+
print('Your ipv6 record has been updated successfully.')
114+
115+
if __name__ == "__main__":
116+
dns = AliDDNS("MainDomain.com", "SubDomain", "AccessKey ID", "AccessKey Secret")
117+
118+
while True:
119+
cmd = input()
120+
if cmd == "exit":
121+
break
122+
elif cmd == "refresh":
123+
dns.refresh()
124+
else:
125+
print("Unknown command.")
126+

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests==2.26.0
2+
aliyun-python-sdk-core==2.13.36
3+
aliyun-python-sdk-alidns==3.0.1

0 commit comments

Comments
 (0)