家庭公网IP动态解析至阿里云DNS
生活随笔
收集整理的這篇文章主要介紹了
家庭公网IP动态解析至阿里云DNS
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
家庭公網IP動態解析之阿里云DNS
此服務使用Java開發,每隔10分鐘進行阿里云dns解析。如果解析地址未變更,則不出發修改解析操作。
代碼
1. AliClient 代碼 獲取指定域名的解析記錄和修改
/*** 阿里云客戶端** @author Created by Harry Ma on 2021-01-26*/ @Component public class AliClient {private static IAcsClient client;public AliClient() {IClientProfile profile = DefaultProfile.getProfile(Constants.REGION_ID, AliDnsApplication.aliConfig.getAccessKey(), AliDnsApplication.aliConfig.getAccessSecret());// 若報Can not find endpoint to access異常,請添加以下此行代碼// DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Alidns", "alidns.aliyuncs.com");client = new DefaultAcsClient(profile);}public String getFirstDomain() {DescribeDomainsRequest request = new DescribeDomainsRequest();DescribeDomainsResponse response;try {response = client.getAcsResponse(request);List<DescribeDomainsResponse.Domain> domains = response.getDomains();if (domains.size() > 0) {return domains.get(0).getDomainName();}} catch (ClientException e) {e.printStackTrace();}return "";}/*** 獲取指定解析記錄** @param domainName 域名* @param filterRR 獲取的解析記錄* @return 解析記錄*/public DescribeDomainRecordsResponse.Record getRecord(String domainName, String filterRR) {DescribeDomainRecordsRequest describeDomainRecordsRequest = new DescribeDomainRecordsRequest();describeDomainRecordsRequest.setDomainName(domainName);DescribeDomainRecordsResponse describeSubDomainRecordsResponse;try {describeSubDomainRecordsResponse = client.getAcsResponse(describeDomainRecordsRequest);for (DescribeDomainRecordsResponse.Record domainRecord : describeSubDomainRecordsResponse.getDomainRecords()) {if (filterRR != null && filterRR.equals(domainRecord.getRR())) {return domainRecord;}}} catch (ClientException e) {e.printStackTrace();}return null;}public void updateRecordIp(String ip, String recordId, String rR) {UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();request.setRR(rR);request.setRecordId(recordId);request.setType(Constants.TYPE);request.setValue(ip);try {HttpResponse response = client.doAction(request);if (response.isSuccess()) {System.out.println("DNS解析成功^_^");return;}if (response.getHttpContentString().contains("The DNS record already exists.")){System.err.println("當前解析記錄已存在...");}System.err.println("DNS解析失敗,請檢查$_$");} catch (ClientException e) {System.err.println(e.getErrMsg());}} }2. 定時任務類
/*** 定時修改阿里云動態DNS** @author Created by Harry Ma on 2021-01-26*/ @Component @EnableScheduling public class DnsSchedule {@Resourceprivate AliClient aliClient;/*** 每10分鐘修改一次云解析記錄*/@Scheduled(fixedRate = 1000 * 60 * 10)private void dnsTasks() {String nowIp = getNowIp();if (nowIp == null || nowIp.equals("")) {System.err.println("獲取當前IP為空,請檢查");return;}String firstDomain = aliClient.getFirstDomain();System.out.printf("獲取到的domain為: %s%n", firstDomain);DescribeDomainRecordsResponse.Record record = aliClient.getRecord(firstDomain, AliDnsApplication.aliConfig.getRr());System.out.printf("獲取到指定RR(%s)為: %s%n", AliDnsApplication.aliConfig.getRr(), JSONObject.toJSONString(record));if (!nowIp.equals(record.getValue())){aliClient.updateRecordIp(nowIp, record.getRecordId(), AliDnsApplication.aliConfig.getRr());}System.out.println("當前IP與解析IP相同,無需更新...");}private String getNowIp() {HttpGet httpGet = new HttpGet(Constants.REQUEST_IP_URL);httpGet.addHeader(Constants.ACCEPT_STR, Constants.ACCEPT_VALUE);httpGet.addHeader(Constants.CONNECTION_STR, Constants.CONNECTION_VALUE);httpGet.addHeader(Constants.USER_AGENT_STR, Constants.USER_AGENT_VALUE);CloseableHttpClient client = HttpClients.createDefault();try {CloseableHttpResponse response = client.execute(httpGet);if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {String result = EntityUtils.toString(response.getEntity());return Objects.requireNonNull(JSONObject.parseObject(result)).getString(Constants.IP_STR);}} catch (IOException e) {System.err.println(e.getMessage());}return "";} }3. 常量類
/*** @author Created by Harry Ma on 2021-01-26*/ public interface Constants {String TYPE = "A";String REGION_ID = "cn-hangzhou"; //地域ID 默認String IP_STR = "ip";String REQUEST_IP_URL = "https://api.ipify.org/?format=json";String ACCEPT_STR = "Accept";String CONNECTION_STR = "Connection";String USER_AGENT_STR = "User-Agent";String ACCEPT_VALUE = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";String CONNECTION_VALUE = "Keep-Alive";String USER_AGENT_VALUE = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0";String SEPARATOR_STR = ":";String COMMA = ",";String BLANK_STR = ""; }如有不依賴jdk運行需求,請自行查找解決方法
本代碼已上傳至gitee和github
總結
以上是生活随笔為你收集整理的家庭公网IP动态解析至阿里云DNS的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mybatis知识
- 下一篇: Microsoft.VisualStud