Home DDNS

😑 Due to the shortage of IPv4 resources, the current IP address defaults to providing an intranet IP, which means that it can be accessed and cannot be accessed; even if we apply for a public IP, it is not a fixed static IP, and usually changes every other day. This brings us new troubles. We have to check the IP address every time. With DDNS technology, we can map dynamic IP to domain name. The domain name is unchanged, so we can directly access the host through the domain name. But the DDNS on the market is expensive. Although the DDNS function of the router is free, it is very unstable.

DDNS (Dynamic Domain Name Server, dynamic domain name service) is to map the user’s dynamic IP address to a fixed domain name resolution service. Every time the user connects to the network, the client program will transfer the host’s dynamic IP address through information transmission. It is sent to the server program located on the host of the service provider, and the server program is responsible for providing DNS services and implementing dynamic domain name resolution.

At first, I used a script running in the background of the host to monitor the IP address, and the changes were automatically sent to my mailbox. A few days ago, I found that I could operate the Alibaba Cloud domain name resolution service directly through the API interface, and finally I could automatically modify the domain name resolution once and for all.

This script is very simple, basically two functions

  1. Listening IP address
  2. Re-run domain name resolution

The program is placed on GitHub: https://github.com/lexinhu/ddns-micro/

It can be run by modifying the configuration information in application.yml, and it can be packaged and run on any platform.

The Linux startup command reference is as follows

 nohup java -jar -Xms128m -Xmx256m -XX:PermSize=128M -XX:MaxPermSize=256M ddns-micro.jar &

The best way to obtain the public IP address is to use the network to request the website. Here, we choose the website that obtains the IP address for free on the Internet, which saves a lot of effort.

 @Scheduled(fixedRate = 1800000) public void ipTask() { String ipv4 = ""; BufferedReader in = null; try { URL realUrl = new URL(pullAddress); // 打开和URL 之间的连接URLConnection connection = realUrl.openConnection(); // 定义BufferedReader 输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { ipv4 += line; } // TODO: 2022/5/10 更新阿里云域名解析aliyunDomain.updateDomainRecord(ipv4); } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } }

When modifying Alibaba Cloud domain name resolution, it should be noted that which record needs to be determined and whether the resolution content of the record needs to be changed, which cannot be neglected.

 // 修改阿里云域名解析记录public void updateDomainRecord(String ipv4) throws Exception { Client client = this.initialization(); DescribeDomainRecordsRequest domainRecordsRequest = new DescribeDomainRecordsRequest(); domainRecordsRequest.setDomainName(DOMAIN_NAME); // 解析记录列表DescribeDomainRecordsResponse domainRecordsResponse = client.describeDomainRecords(domainRecordsRequest); for (DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord record : domainRecordsResponse.body.domainRecords.record) { UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest(); // 找到指定的二级域名if (record.RR.equals(TWO_DOMAIN_NAME)) { // ip 变动则重新解析if (!record.value.equals(ipv4)) { BeanUtils.copyProperties(record, updateDomainRecordRequest); updateDomainRecordRequest.setValue(ipv4); client.updateDomainRecord(updateDomainRecordRequest); System.out.println(record.RR + "." + DOMAIN_NAME + "的主机记录更新为记录值: " + ipv4); } break; } } }

This article is reprinted from: https://www.xn2001.com/archives/692.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.