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安全运行了好几个月
源代码:已同步