def main(): """ Main routine """ config = DDNSConfig() record_manager = DDNSDomainRecordManager(config) for local_record in record_manager.local_record_list: family = AF_INET if local_record.type == 'A' else AF_INET6 interface = local_record.interface if interface is None: current_public_ip = { AF_INET: DDNSUtils.get_current_public_ip(), AF_INET6: DDNSUtils.get_current_public_ipv6() } else: current_public_ip = { AF_INET: DDNSUtils.get_interface_address(interface), AF_INET6: DDNSUtils.get_interface_address(interface, AF_INET6) } if not current_public_ip: DDNSUtils.info( "Unable to get current IP for [{rec.subdomain}.{rec.domainname}.{rec.type}]" .format(rec=local_record)) continue dns_resolved_ip = DDNSUtils.get_dns_resolved_ip( local_record.subdomain, local_record.domainname, family) if current_public_ip[family] == dns_resolved_ip: DDNSUtils.info("Skipped as no changes for DomainRecord" \ "[{rec.subdomain}.{rec.domainname}.{rec.type}]".format(rec=local_record)) continue # If current public IP doesn't equal to current DNS resolved ip, only in three cases: # 1. The new synced IP for remote record in Aliyun doesn't take effect yet # 2. remote record's IP in Aliyun server has changed # 3. current public IP is changed remote_record = record_manager.fetch_remote_record(local_record) if not remote_record: DDNSUtils.err("Failed finding remote DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) continue if current_public_ip[family] == remote_record.value: DDNSUtils.info("Skipped as we already updated DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) continue # if we can fetch remote record and record's value doesn't equal to public IP record_type = 'A' if family == AF_INET else 'AAAA' sync_result = record_manager.update(remote_record, current_public_ip[family], record_type) if not sync_result: DDNSUtils.err("Failed updating DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) else: DDNSUtils.info("Successfully updated DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
def main(): """ Main routine """ config = DDNSConfig() record_manager = DDNSDomainRecordManager(config) # get current public ip for this server if config.pifn_enable: current_public_ip = DDNSUtils.get_interface_address( config.pifn_interface) else: current_public_ip = DDNSUtils.get_current_public_ip() if not current_public_ip: DDNSUtils.err_and_exit("Failed to get current public IP") for local_record in record_manager.local_record_list: if local_record.subdomain == '*': dns_resolved_ip = DDNSUtils.get_dns_resolved_ip( 'xxx', local_record.domainname) else: dns_resolved_ip = DDNSUtils.get_dns_resolved_ip( local_record.subdomain, local_record.domainname) if local_record.type == "AAAA": current_ip = DDNSUtils.get_interface_ipv6_address( local_record.interface) else: current_ip = current_public_ip if current_ip == dns_resolved_ip: DDNSUtils.info("Skipped as no changes for DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) continue # If current public IP doesn't equal to current DNS resolved ip, only in three cases: # 1. The new synced IP for remote record in Aliyun doesn't take effect yet # 2. remote record's IP in Aliyun server has changed # 3. current public IP is changed remote_record = record_manager.fetch_remote_record(local_record) if not remote_record: DDNSUtils.err("Failed finding remote DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) continue if current_ip == remote_record.value: DDNSUtils.info("Skipped as we already updated DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) continue # if we can fetch remote record and record's value doesn't equal to public IP sync_result = record_manager.update(remote_record, current_ip, local_record.type) if not sync_result: DDNSUtils.err("Failed updating DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) else: DDNSUtils.info("Successfully updated DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
def perform_ddns(self): """ Perform the ddns process when everything is ready """ current_public_ip = DDNSUtils.get_current_public_ip() if not current_public_ip: DDNSUtils.err_and_exit("Failed to get local public IP") DDNSUtils.info( "Local public ip address read: [{0}]".format(current_public_ip)) for record_to_update in self.configuration.recordsToUpdate: dns_resolved_ip = record_to_update.get_dns_resolved_ip() if current_public_ip == dns_resolved_ip: DDNSUtils.info( "Skipped as no changes for DomainRecord: [{rec.subDomainName}.{rec.domainName}]" .format(rec=record_to_update)) continue # If current public IP doesn't equal to current DNS resolved ip, only in three cases: # 1. The new synchronized IP for remote record in api provider doesn't take effect yet # 2. remote record's IP in Aliyun server has changed # 3. current public IP is changed dns_record = self.get_dns_record(record_to_update) if not dns_record: DDNSUtils.err( "Failed to get dns resolution record for [{rec.subDomainName}.{rec.domainName}]" .format(rec=record_to_update)) continue if current_public_ip == dns_record.value: DDNSUtils.info( "Skipped: dns record already updated: [{rec.subDomainName}.{rec.domainName}]" .format(rec=record_to_update)) continue dns_record.value = current_public_ip result = self.update_dns_record(dns_record, current_public_ip) if not result: DDNSUtils.err( "Failed to update dns record: [{rec.subDomainName}.{rec.domainName}]" .format(rec=record_to_update)) else: DDNSUtils.info( "Successfully update dns record: [{rec.subDomainName}.{rec.domainName}]" .format(rec=record_to_update))
def main(): """ Main routine """ config = DDNSConfig() record_manager = DDNSDomainRecordManager(config) # get current public ip for this server current_public_ip = DDNSUtils.get_current_public_ip() if not current_public_ip: DDNSUtils.err_and_exit("Failed to get current public IP") for local_record in record_manager.local_record_list: dns_resolved_ip = DDNSUtils.get_dns_resolved_ip(local_record.subdomain, local_record.domainname) if current_public_ip == dns_resolved_ip: DDNSUtils.info("Skipped as no changes for DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) continue # If current public IP doesn't equal to current DNS resolved ip, only in three cases: # 1. The new synced IP for remote record in Aliyun doesn't take effect yet # 2. remote record's IP in Aliyun server has changed # 3. current public IP is changed remote_record = record_manager.fetch_remote_record(local_record) if not remote_record: DDNSUtils.err("Failed finding remote DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) continue if current_public_ip == remote_record.value: DDNSUtils.info("Skipped as we already updated DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) continue # if we can fetch remote record and record's value doesn't equal to public IP sync_result = record_manager.update(remote_record, current_public_ip) if not sync_result: DDNSUtils.err("Failed updating DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record)) else: DDNSUtils.info("Successfully updated DomainRecord" \ "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
def switch(m): switcher = { "net": lambda: DDNSUtils.get_current_public_ip(), "static": lambda: args[0], } return switcher.get(m, lambda: "net|static 10.0.0.1")