Пример #1
0
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))
Пример #2
0
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))