玩转NAS:黑群晖家用宽带公网IP和DNS解析同步

N年前装了个NAS,买了个域名,由于家用宽带的公网IP每过短时间电信就会换,导致有时候DNS解析的IP就过期了。然后就自己自己写了程序同步宽带IP和DNS服务商。(我这里是DNAPAD)阿里云同理。这里记录下

这里是腾讯的服务,阿里云万网也有 懒得写了 

secretId secretKey

       <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.901</version>
        </dependency>

这里是获取服务商当前绑定IP

 @Override
    public Long getRecordId() throws TencentCloudSDKException, IOException {
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("dnspod.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        DnspodClient client = new DnspodClient(cred, "", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeRecordListRequest req = new DescribeRecordListRequest();
        req.setDomain(myDomain);
        // 返回的resp是一个DescribeRecordListResponse的实例,与请求对象对应
        DescribeRecordListResponse resp = client.DescribeRecordList(req);
        // 输出json格式的字符串回包
        String respStr = DescribeRecordListResponse.toJsonString(resp);

        JSONArray array = JSONObject.parseObject(respStr).getJSONArray("RecordList");
        for (Object o : array) {
            JSONObject obj = (JSONObject) o;
            if (obj.getString("Name").equals("@") && obj.getString("Type").equals("A")) {
                if (!getGateIp().equals(obj.getString("Value"))) {
                    return obj.getLong("RecordId");
                }

            }
        }
        return null;
    }

修改服务商解析的IP

@Override
    public void updateIP(Long recordId) throws TencentCloudSDKException, IOException {
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("dnspod.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        DnspodClient client = new DnspodClient(cred, "", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        ModifyRecordRequest req = new ModifyRecordRequest();
        req.setDomain(myDomain);
        req.setRecordType("A");
        req.setRecordLine("默认");
        req.setValue(getGateIp());
        req.setRecordId(recordId);
        // 返回的resp是一个ModifyRecordResponse的实例,与请求对象对应
        ModifyRecordResponse resp = client.ModifyRecord(req);
        // 输出json格式的字符串回包
        System.out.println(ModifyRecordResponse.toJsonString(resp));
    }

docker安全运行了好几个月

源代码:已同步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值