当前位置:   article > 正文

家庭宽带 动态公网IP,使用腾讯云DDNS实现实时更新DNS解析记录

腾讯云ddns

解决DDNS问题 动态公网IP

环境说明:
我是家庭宽带 只能申请到动态的公网ip,好处是不花钱,弊端是每次重启光猫,都会重新获取一个新的公网IP

为解决此办法,我尝试了很多个DDNS的方案。
包括但不限于。

1.云厂商DDNS(本教程使用)
2.购买一台云机nginx反代到本地,本地心跳检测域名,不通则立刻获取本地公网ip同步到云机nginx并重启。
3.路由器支持的DDNS功能
4. frp技术 花生壳、ngrok 等
5.软路由

下面我解释下为什么都排除了
2.成本高,还要保证一台转发云机。还要维持备案
3.路由器只支持部分的,例如花生壳的,看路由器兼容性
4. 研究了下花生壳,免费的只有2个,要么在花生壳买域名,要么push域名到贝锐(花生壳),要么每年40元解析费用,但都是需要实名认证的
5.软路由 ,需要设备,常开,具体功能我也没玩明白,以后研究明白了出教程

下面介绍一下通过腾讯云,云API调用,实现同步更新动态公网IP,解决痛点。
要用到的:
云API使用文档
借鉴了其他的文章

拓扑结构思路图

在这里插入图片描述

开始正文

首先要确定解析的域名
我的域名解析是这个
记录值是家宽公网ip
在这里插入图片描述

云api文档中

这个接口是用来修改记录值的,也就是DNS解析到的IP地址,在这里进行修改DNS记录值
在这里插入图片描述

上图有4个参数需要填写,
分别是

  • Domain
    顶级域名 例如: baidu.com
  • RecordType
    记录类型

在这里插入图片描述

  • RecordLine
    记录线路,通过 API 记录线路获得,中文,比如:默认。
  • Value
    记录值,如 IP : 200.200.200.200, CNAME : cname.dnspod.com., MX : mail.dnspod.com.。
  • RecordId
    记录 ID 。注意下图如何获取

举例:
在这里插入图片描述

获取记录信息 得到RecordId

文档:https://console.cloud.tencent.com/api/explorer?Product=dnspod&Version=2021-03-23&Action=DescribeRecord&SignVersion=

这里发起一下请求,找到我解析的m的记录信息中 的RecordId
在这里插入图片描述

复制上图红框内容,回到上面请求,这里进行调试
在这里插入图片描述
在这里插入图片描述

请求成功了,下面生成代码,进行部署
在这里插入图片描述
可以看到,生成的代码中,有2个字段需要填写,

密钥
SecretId
SecretKey

链接地址:https://console.cloud.tencent.com/cam/capi
新建即可
在这里插入图片描述
填入密钥
在这里插入图片描述

添加代码:
获取本地的公网ip地址的方法

   public static String getIpV4(){
        String ipV4 = "";
        try{
            String ipStr = HttpUtil.get("https://www.taobao.com/help/getip.php");
            ipV4 = ipStr.split("\"")[1];
        }catch(Exception e){
            e.printStackTrace();
            ipV4 = "127.0.0.1";
        }
        return ipV4;

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

pom添加

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.1.2</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述
开始请求,启动main方法

在这里插入图片描述
在这里插入图片描述
成功啦!

下面解决心跳机制的问题。
想过几个方案

1.定时任务
2.定时ping 当ping不同的时候调用ddns方法
3.生成jar包,启动main方法,一直后台运行

我采用了方案2
操作如下
1.添加定时ping的任务
2.启动的jar包,开启后台守护程序
3.生成日志,重启记录发送邮箱
当请求超时时,说明ip不通了,需要更新一下,就调用一下方法

增加了如下代码
1.定时任务

    //动态定时任务
    public static void main(String[] args) {
        //每10秒执行一次
        CronUtil.schedule("*/10 * * * * *", new Task() {
            @Override
            public void execute() {

                Console.log("这里是动态添加定时任务!.");

            }
        });

        // 支持秒级别定时任务
        CronUtil.setMatchSecond(true);
        CronUtil.start();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.ping不同的时候调用ddns方法

    public static void main(String[] args) {
        System.out.println( ping("m.2048.top"));
    }

    /**
     * 检测IP地址是否能ping通
     *
     * @param ip IP地址
     * @return 返回是否ping通
     */
    public static boolean ping(String ip) {
        return ping(ip, 200);
    }

    /**
     * 检测IP地址是否能ping通
     *
     * @param ip      IP地址
     * @param timeout 检测超时(毫秒)
     * @return 是否ping通
     */
    public static boolean ping(String ip, int timeout) {
        try {
            return InetAddress.getByName(ip).isReachable(timeout); // 当返回值是true时,说明host是可用的,false则不可。
        } catch (Exception ex) {
            return false;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

最后合起来的代码

package com.tencent;

import cn.hutool.core.lang.Console;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import cn.hutool.http.HttpUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.dnspod.v20210323.DnspodClient;
import com.tencentcloudapi.dnspod.v20210323.models.*;

import java.net.InetAddress;

public class ModifyRecord
{

    public static String ddns() {
        try{
            // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential("密钥id", "密钥key");
            // 实例化一个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("2048.top");
            req.setSubDomain("m");
            req.setRecordType("A");
            req.setRecordLine("默认");
            //获取本机的公网ipv4地址
            String ip=getIpV4();
            req.setValue(ip);
            Console.log("新的ip是:"+ip);
            req.setRecordId(纯数字);
            // 返回的resp是一个ModifyRecordResponse的实例,与请求对象对应
            ModifyRecordResponse resp = client.ModifyRecord(req);
            // 输出json格式的字符串回包
//            Console.log(ModifyRecordResponse.toJsonString(resp));
            return ModifyRecordResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            Console.log(e.toString());
        }
        return null;
    }
    public static String getIpV4(){
        String ipV4 = "";
        try{
            String ipStr = HttpUtil.get("https://www.taobao.com/help/getip.php");
            ipV4 = ipStr.split("\"")[1];
        }catch(Exception e){
            e.printStackTrace();
            ipV4 = "127.0.0.1";
        }
        return ipV4;

    }

    /**
     * 检测IP地址是否能ping通
     *
     * @param ip IP地址
     * @return 返回是否ping通
     */
    public static boolean ping(String ip) {
        return ping(ip, 200);
    }

    /**
     * 检测IP地址是否能ping通
     *
     * @param ip      IP地址
     * @param timeout 检测超时(毫秒)
     * @return 是否ping通
     */
    public static boolean ping(String ip, int timeout) {
        try {
            return InetAddress.getByName(ip).isReachable(timeout); // 当返回值是true时,说明host是可用的,false则不可。
        } catch (Exception ex) {
            return false;
        }
    }


    public static void main(String[] args) {
        //动态定时任务
        //每10秒执行一次
        CronUtil.schedule("*/10 * * * * *", new Task() {
            @Override
            public void execute() {
//                Console.log("这里是动态添加定时任务!");
                //如果不通了  返回false
                if (!ping("m.2048.top")){
                    //就进行ddns方法
                    Console.log(ddns());
                }
            }
        });
        // 支持秒级别定时任务
        CronUtil.setMatchSecond(true);
        CronUtil.start();
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112

package打包成jar包
在这里插入图片描述
运行下面的这个jar包
启动命令 其实就是启动一个main方法

java -cp java-sdk-1.0-SNAPSHOT-jar-with-dependencies.jar com.tencent.ModifyRecord start
  • 1

在这里插入图片描述
运行正常
后期放到docker中使用

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/404421
推荐阅读
相关标签
  

闽ICP备14008679号