from aliyunsdkcore.client import AcsClient
from aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
import time
import json
import urllib.request
client = AcsClient( 'AccessKey ID' , 'Access Key' , 'cn-hangzhou' )
def get_internet_ip():
with urllib.request.urlopen( 'http://www.3322.org/dyndns/getip' ) as response:
html = response.read()
ip = str (html, encoding = 'utf-8' ).replace( "\n" , "")
return ip
def Describe_SubDomain_Records(client,record_type,subdomain):
request = DescribeSubDomainRecordsRequest()
request.set_accept_format( 'json' )
request.set_Type(record_type)
request.set_SubDomain(subdomain)
response = client.do_action_with_exception(request)
response = str (response, encoding = 'utf-8' )
relsult = json.loads(response)
return relsult
def add_record(client,priority,ttl,record_type,value,rr,domainname):
request = AddDomainRecordRequest()
request.set_accept_format( 'json' )
request.set_Priority(priority)
request.set_TTL(ttl)
request.set_Value(value)
request.set_Type(record_type)
request.set_RR(rr)
request.set_DomainName(domainname)
response = client.do_action_with_exception(request)
response = str (response, encoding = 'utf-8' )
relsult = json.loads(response)
return relsult
def update_record(client,priority,ttl,record_type,value,rr,record_id):
request = UpdateDomainRecordRequest()
request.set_accept_format( 'json' )
request.set_Priority(priority)
request.set_TTL(ttl)
request.set_Value(value)
request.set_Type(record_type)
request.set_RR(rr)
request.set_RecordId(record_id)
response = client.do_action_with_exception(request)
response = str (response, encoding = 'utf-8' )
return response
def wirte_to_file(path,ip):
f = open (path, 'w' )
f.write(ip)
f.close()
def domains(domain):
try :
des_relsult = Describe_SubDomain_Records(client, "A" , domain[ 'full' ])
if des_relsult[ "TotalCount" ] = = 0 :
add_relsult = add_record(client, "5" , "600" , "A" , ip, domain[ 'left' ], domain[ 'right' ])
record_id = add_relsult[ "RecordId" ]
print ( "域名解析新增成功!" )
elif des_relsult[ "TotalCount" ] = = 1 :
record_id = des_relsult[ "DomainRecords" ][ "Record" ][ 0 ][ "RecordId" ]
update_record(client, "5" , "600" , "A" , ip, domain[ 'left' ], record_id)
print ( "域名解析更新成功!" )
else :
record_id = 0
path = './RecordId'
wirte_to_file(path, record_id)
except Exception as e:
print (e)
while True :
ip = get_internet_ip()
with open ( "./ip" , 'r' ) as f:
old_ip = f.read()
if ip = = old_ip:
print ( "noupdate" + "\nnew_ip:" + ip + "\nold_ip:" + old_ip)
else :
wirte_to_file( "./ip" , ip)
domains({ 'full' : "*.tunnel.xxx.fun" , 'left' : "*.tunnel" , "right" : "xxx.fun" })
domains({ 'full' : "tunnel.xxx.fun" , 'left' : "tunnel" , "right" : "xxx.fun" })
domains({ 'full' : "*.frp.xxx.fun" , 'left' : "*.frp" , "right" : "xxx.fun" })
time.sleep( 10 )
|