Java动态修改dns(DDNS基于DNSPot和IPv6)

2021-02-06  本文已影响0人  TinyThing

废话不多说,直接上源码:

    @Autowired
    private RestTemplate restTemplate;

    private String ipv6;

    private static final String RECORD_LIST = "https://dnsapi.cn/Record.List";
    private static final String RECORD_DDNS = "https://dnsapi.cn/Record.Ddns";
    private static final String RECORD_MODIFY = "https://dnsapi.cn/Record.Modify";

    private static final String TOKEN = "你的token";
    private static final String DOMAIN = "你的域名";
    private static final String SUB_DOMAIN = "你要修改的子域名";
    private static final String RECORD_ID = "记录id";

    @PostConstruct
    public void init() {
        ipv6 = getLocalIPv6Address();
        log.info("- local ipv6 = {}", ipv6);
    }


    @Scheduled(fixedRate = 5000)
    public void checkIP() {
        String currentIPv6 = getLocalIPv6Address();

        if (ipv6.equals(currentIPv6)) {
            return;
        }

        modifyDns();
    }

    public String getRecordList() {
        MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
        return postForm(RECORD_LIST, params);
    }


    public String modifyDns() {
        MultiValueMap<String, String> params= new LinkedMultiValueMap<>();


        params.add("record_id", RECORD_ID);
        params.add("sub_domain", SUB_DOMAIN);
        params.add("record_type", "AAAA");
        params.add("record_line_id", "0");
        params.add("ttl", "600");
        params.add("value", getLocalIPv6Address());

        return postForm(RECORD_MODIFY, params);
    }


    private String postForm(String url, MultiValueMap<String, String> params) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        params.add("login_token", TOKEN);
        params.add("format", "json");
        params.add("domain", DOMAIN);

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

        return response.getBody();
    }

    public String getLocalIPv6Address() {
        InetAddress inetAddress = null;

        Enumeration<NetworkInterface> networkInterfaces = null;
        try {
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        outer:
        while(networkInterfaces.hasMoreElements()) {
            Enumeration<InetAddress> addressEnumeration = networkInterfaces.nextElement().getInetAddresses();
            while(addressEnumeration.hasMoreElements()) {
                inetAddress = addressEnumeration.nextElement();
                //检查此地址是否是IPv6地址以及是否是保留地址
                if(inetAddress instanceof Inet6Address && !isReservedAddr(inetAddress)) {
                    break outer;
                }
            }
        }
        String ipAddr = inetAddress.getHostAddress();
        //过滤网卡
        int index = ipAddr.indexOf('%');
        if(index > 0) {
            ipAddr = ipAddr.substring(0, index);
        }

        return ipAddr;
    }

    private boolean isReservedAddr(InetAddress inetAddr) {
        return inetAddr.isAnyLocalAddress()
                || inetAddr.isLinkLocalAddress()
                || inetAddr.isLoopbackAddress();
    }
上一篇下一篇

猜你喜欢

热点阅读