@@ -14,6 +14,8 @@ def main():
1414 parser .add_argument ('email' )
1515 parser .add_argument ('api_key' )
1616 parser .add_argument ('domain' )
17+ parser .add_argument ('--proxied' , '-p' , dest = 'proxied' , action = 'store_true' , default = False ,
18+ help = 'Enable the Cloudflare proxy for the record' )
1719 args = parser .parse_args ()
1820 cf = CloudFlare (** vars (args ))
1921 cf .sync_dns_from_my_ip ()
@@ -29,6 +31,8 @@ class CloudFlare:
2931
3032 api_key = ''
3133
34+ proxied = False
35+
3236 headers = None
3337
3438 domain = None
@@ -43,17 +47,19 @@ class CloudFlare:
4347 'https://ifconfig.co/json'
4448 )
4549
46- def __init__ (self , email : str , api_key : str , domain : str ):
50+ def __init__ (self , email : str , api_key : str , domain : str , proxied : bool ):
4751 """
4852 Initialization. It will set the zone information of the domain for operation.
4953 It will also get dns records of the current zone.
5054 :param email:
5155 :param api_key:
5256 :param domain:
57+ :param proxied:
5358 """
5459 self .email = email
5560 self .api_key = api_key
5661 self .domain = domain
62+ self .proxied = proxied
5763 self .headers = {
5864 'X-Auth-Key' : api_key ,
5965 'X-Auth-Email' : email ,
@@ -143,10 +149,10 @@ def create_record(self, dns_type, name, content, **kwargs):
143149 }
144150 if kwargs .get ('ttl' ) and kwargs ['ttl' ] != 1 :
145151 data ['ttl' ] = kwargs ['ttl' ]
146- if kwargs .get ('proxied' ) is False :
147- data ['proxied' ] = False
148- else :
152+ if kwargs .get ('proxied' ) is True :
149153 data ['proxied' ] = True
154+ else :
155+ data ['proxied' ] = False
150156 content = self .request (
151157 self .api_url + self .zone ['id' ] + '/dns_records' ,
152158 'post' ,
@@ -172,10 +178,10 @@ def update_record(self, dns_type, name, content, **kwargs):
172178 }
173179 if kwargs .get ('ttl' ) and kwargs ['ttl' ] != 1 :
174180 data ['ttl' ] = kwargs ['ttl' ]
175- if kwargs .get ('proxied' ) is False :
176- data ['proxied' ] = False
177- else :
181+ if kwargs .get ('proxied' ) is True :
178182 data ['proxied' ] = True
183+ else :
184+ data ['proxied' ] = False
179185 content = self .request (
180186 urllib .parse .urljoin (self .api_url , self .zone ['id' ] + '/dns_records/' + record ['id' ]),
181187 'put' ,
@@ -248,12 +254,18 @@ def sync_dns_from_my_ip(self, dns_type='A'):
248254 if len (self .domain .split ('.' )) == 3 \
249255 else self .get_record (dns_type , self .domain )
250256 except RecordNotFound :
251- self .create_record (dns_type , self .domain , ip_address )
257+ if self .proxied :
258+ self .create_record (dns_type , self .domain , ip_address , proxied = True )
259+ else :
260+ self .create_record (dns_type , self .domain , ip_address )
252261 print ('Successfully created new record with IP address {new_ip}'
253262 .format (new_ip = ip_address ))
254263 else :
255264 if record ['content' ] != ip_address :
256- self .update_record (dns_type , self .domain , ip_address )
265+ if self .proxied :
266+ self .update_record (dns_type , self .domain , ip_address , proxied = True )
267+ else :
268+ self .update_record (dns_type , self .domain , ip_address )
257269 print ('Successfully updated IP address from {old_ip} to {new_ip}'
258270 .format (old_ip = record ['content' ], new_ip = ip_address ))
259271 else :
0 commit comments