简单ddns的实现

coredns +

package org.rx.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.rx.common.AppConfig;
import org.rx.core.Tasks;
import org.rx.net.http.HttpClient;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

import java.nio.charset.StandardCharsets;

import static org.rx.core.Contract.eq;
import static org.rx.core.Contract.quietly;

@Slf4j
@Service
@RequiredArgsConstructor
public class ScheduleService {
    private final AppConfig config;
    private volatile String lastIp;

    @PostConstruct
    public void init() {
        Tasks.schedule(() -> {
            HttpClient client = new HttpClient();

            quietly(() -> {
                String url = "https://api.myip.com";
                JSONObject json = JSON.parseObject(client.get(url));
                if (!eq(lastIp = json.getString("ip"), lastIp)) {
                    log.info("refresh dns db");
                    FileUtils.writeStringToFile(config.getDnsDb(), String.format("f-li.cn.\t\tIN\tSOA dns.f-li.cn. rx.f-li.cn. 2020020414 7200 3600 1209600 3600\n" +
                            "cloud.f-li.cn.\tIN\tA\t103.126.103.83\n" +
                            "v.f-li.cn.\t\tIN\tA\t%s\n", lastIp), StandardCharsets.UTF_8, false);
                }
            });
        }, config.getJobDelaySeconds() * 1000);
    }
}

 

package org.rx.service;
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import lombok.RequiredArgsConstructor;import lombok.extern.slf4j.Slf4j;import org.apache.commons.io.FileUtils;import org.rx.common.AppConfig;import org.rx.core.Tasks;import org.rx.net.http.HttpClient;import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import static org.rx.core.Contract.eq;import static org.rx.core.Contract.quietly;
@Slf4j@Service@RequiredArgsConstructorpublic class ScheduleService {    private final AppConfig config;    private volatile String lastIp;
    @PostConstruct    public void init() {        Tasks.schedule(() -> {            HttpClient client = new HttpClient();
            quietly(() -> {                String url = "https://api.myip.com";                JSONObject json = JSON.parseObject(client.get(url));                if (!eq(lastIp = json.getString("ip"), lastIp)) {                    log.info("refresh dns db");                    FileUtils.writeStringToFile(config.getDnsDb(), String.format("f-li.cn.\t\tIN\tSOA dns.f-li.cn. rx.f-li.cn. 2020020414 7200 3600 1209600 3600\n" +                            "cloud.f-li.cn.\tIN\tA\t103.126.103.83\n" +                            "v.f-li.cn.\t\tIN\tA\t%s\n", lastIp), StandardCharsets.UTF_8, false);                }            });        }, config.getJobDelaySeconds() * 1000);    }}

 

上一篇:NameSilo的DDNS动态域名解析脚本


下一篇:FortiGate为多ADSL接口创建多个对应指定的DDNS动态域名