其他分享
首页 > 其他分享> > 简单ddns的实现

简单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);    }}

 

标签:cn,实现,简单,rx,lastIp,li,ddns,org,import
来源: https://www.cnblogs.com/Googler/p/14088474.html