IPV6 阿里DDNS
因为需要在家搭建一套环境,并且需要公网能访问。国内的ipv4的地址,各大运营商基本都不会分配ipv4地址(电信宽带好像有地方可以,但是听说很贵),而且是动态的,每过段时间就会改变。 发现移动宽带的公网ipv6地址是可以获取到的,但是也会动态刷新。想稳定访问就加上阿里的ddns的域名访问。 有时候电脑的mac地址在路由器注册上,公网会访问不了。我也不知道是什么原因。但是晚上凌晨4点或者5点时候定时重启路由器,基本上就没问题。
1.改光猫信息和设置路由器拨号
因为宽带接入家里基本是都是需要通过光猫拨号后,再接入路由器。这样就拿不到真实的公网ipv6地址,需要先将光猫中的设置改为桥接模式,再让路由器输入宽带账户和密码拨号。 改光猫和路由器这个步骤,确实很多坑,需要看网上的很多教程。光猫的型号和地区不一样,设置的方式也不一样,所以这一步就不详细介绍。
2.准备阿里云域名和获取阿里开发AccessKeyID和AccessKeySecret
2.1 准备域名
注册一个阿里云账号,购买域名,可以买个比较便宜的一级域名。购买完成后 购买以后需要看看dns服务器设置是否成功。阿里免费的ddns解析,修改ip后,大概10分钟左右生效。 然后进入左侧的 “域名解析” 菜单 主机记录是二级域名前缀,@表示是主域名 记录类型 A是ipv4地址 AAAA是ipv6地址 TTL 生效时间
2.2 获取阿里开发AccessKeyID和AccessKeySecret
3.开始脚本项目
3.1 引入阿里的sdk包
引入阿里现成的maven包,里面封装好的接口
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-alidns</artifactId>
<version>2.0.10</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.12</version>
</dependency>
</dependencies>
3.2 加载配置信息
域名,AccessKeyID和AccessKeySecret
初始化下配置信息
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class Config {
public static String host="";
public static String AccessKeyID="";
public static String AccessKeySecret="";
public static void initConfig(){
if(StrUtil.isNotBlank(host) && StrUtil.isNotBlank(AccessKeyID) && StrUtil.isNotBlank(AccessKeySecret)){
LogUtil.logOut("host:"+host);
LogUtil.logOut("AccessKeyID:"+AccessKeyID);
LogUtil.logOut("AccessKeySecret:"+AccessKeySecret);
return;
}
String jarPath= System.getProperty("user.dir");
System.out.println(jarPath);
File file=new File(jarPath+File.separator+"config.json");
if(!file.exists()){
LogUtil.logOut("initConfig config.json文件不存在");
return;
}
JSONObject json= JSONUtil.readJSONObject(file, StandardCharsets.UTF_8);
String host=json.getStr("host");
String AccessKeyID=json.getStr("AccessKeyID");
String AccessKeySecret=json.getStr("AccessKeySecret");
if(StrUtil.isBlank(host) || StrUtil.isBlank(AccessKeyID) || StrUtil.isBlank(AccessKeySecret)){
LogUtil.logOut("参数配置出现问题!!!!!!!!");
throw new RuntimeException("参数配置出现问题!!!!!!!!");
}
Config.host=host;
Config.AccessKeyID=AccessKeyID;
Config.AccessKeySecret=AccessKeySecret;
}
}
3.3 获取服务器ipv6地址
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IPv6 {
public static void main(String[] args) throws IOException {
getLocalIPv6Address();
}
public static String getLocalIPv6Address() throws SocketException {
InetAddress inetAddress =null;
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
outer:
while(networkInterfaces.hasMoreElements()) {
Enumeration<InetAddress> inetAds = networkInterfaces.nextElement().getInetAddresses();
while(inetAds.hasMoreElements()) {
inetAddress = inetAds.nextElement();
if(inetAddress instanceof Inet6Address&& !isReservedAddr(inetAddress)) {
break outer;
}
}
}
String ipAddr = inetAddress.getHostAddress();
int index = ipAddr.indexOf('%');
if(index>0) {
ipAddr = ipAddr.substring(0, index);
}
return ipAddr;
}
private static boolean isReservedAddr(InetAddress inetAddr) {
if(inetAddr.isAnyLocalAddress()||inetAddr.isLinkLocalAddress()||inetAddr.isLoopbackAddress())
{
return true;
}
return false;
}
public static boolean ping(String ipAddress, int pingTimes) {
BufferedReader in = null;
Runtime r = Runtime.getRuntime();
String pingCommand = "ping6 " + ipAddress +" -c " +pingTimes;
try {
LogUtil.logOut(pingCommand);
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int connectedCount = 0;
String line = null;
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
}
return connectedCount >= pingTimes;
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static int getCheckResult(String line) {
LogUtil.logOut("控制台输出的结果为:"+line);
String[] lines=line.split("=");
String lessStr=lines[lines.length-1].split(" ")[0];
try {
if(line.contains("Unreachable")){
return 0;
}
if(line.contains("unreachable")){
return 0;
}
if(Double.valueOf(lessStr)>0){
return 1;
}
}catch (Exception e){
return 0;
}
return 0;
}
}
3.4 main启动类
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.alidns.model.v20150109.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import java.io.IOException;
import java.net.SocketException;
import java.util.List;
public class DDNS {
private DescribeSubDomainRecordsResponse describeSubDomainRecords(DescribeSubDomainRecordsRequest request, IAcsClient client) {
try {
return client.getAcsResponse(request);
} catch (ClientException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
private UpdateDomainRecordResponse updateDomainRecord(UpdateDomainRecordRequest request, IAcsClient client) {
try {
return client.getAcsResponse(request);
} catch (ClientException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
private AddDomainRecordResponse addDomainRecord(AddDomainRecordRequest request, IAcsClient client) {
try {
return client.getAcsResponse(request);
} catch (ClientException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
public static void main(String[] args) throws InterruptedException, IOException {
Config.initConfig();
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
Config.AccessKeyID,
Config.AccessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
while (true) {
try {
ddnsOp(client);
} catch (Exception e) {
e.printStackTrace();
LogUtil.logOut(e);
}finally {
Thread.sleep(1000 * 300);
}
}
}
private static void ddnsOp(IAcsClient client) throws SocketException {
DDNS ddns = new DDNS();
DescribeSubDomainRecordsRequest describeSubDomainRecordsRequest = new DescribeSubDomainRecordsRequest();
describeSubDomainRecordsRequest.setSubDomain(Config.host);
DescribeSubDomainRecordsResponse describeSubDomainRecordsResponse = ddns.describeSubDomainRecords(describeSubDomainRecordsRequest, client);
LogUtil.log_print("describeSubDomainRecords", describeSubDomainRecordsResponse);
List<DescribeSubDomainRecordsResponse.Record> domainRecords = describeSubDomainRecordsResponse.getDomainRecords();
if (domainRecords.size() != 0) {
DescribeSubDomainRecordsResponse.Record record = domainRecords.get(0);
String recordId = record.getRecordId();
String recordsValue = record.getValue();
String currentHostIP = IPv6.getLocalIPv6Address();
LogUtil.logOut("-------------------------------当前主机公网IP为:" + currentHostIP + "-------------------------------");
if (!currentHostIP.equals(recordsValue)) {
LogUtil.logOut("与记录不一致,尝试修改地址!");
UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest();
updateDomainRecordRequest.setRR("@");
updateDomainRecordRequest.setRecordId(recordId);
updateDomainRecordRequest.setValue(currentHostIP);
updateDomainRecordRequest.setType("AAAA");
UpdateDomainRecordResponse updateDomainRecordResponse = ddns.updateDomainRecord(updateDomainRecordRequest, client);
LogUtil.log_print("updateDomainRecord", updateDomainRecordResponse);
}
}else{
String currentHostIP = IPv6.getLocalIPv6Address();
LogUtil.logOut("-------------------------------当前主机公网IP为:" + currentHostIP + "-------------------------------");
AddDomainRecordRequest addDomainRecordRequest=new AddDomainRecordRequest();
addDomainRecordRequest.setDomainName(Config.host);
addDomainRecordRequest.setRR("@");
addDomainRecordRequest.setValue(currentHostIP);
addDomainRecordRequest.setType("AAAA");
AddDomainRecordResponse updateDomainRecordResponse = ddns.addDomainRecord(addDomainRecordRequest, client);
LogUtil.log_print("updateDomainRecord", updateDomainRecordResponse);
}
}
}
3.5 config.json配置文件
***这个配置文件需要和jar包同级目录***
{
"host":"ljXXXX.top",
"AccessKeyID":"LTAI5tPXXXXXXX",
"AccessKeySecret":"FhkXXXXXX"
}
3.6 日志输出
import com.google.gson.Gson;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LogUtil {
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
public static void logOut(Object info){
String threadName=Thread.currentThread().getName();
System.out.println(dtf.format(LocalDateTime.now())+"--线程名"+threadName+":::"+info);
}
public static void log_print(String functionName, Object result) {
Gson gson = new Gson();
LogUtil.logOut("-------------------------------" + functionName + "-------------------------------");
LogUtil.logOut(gson.toJson(result));
}
}
3.7 启动脚本和stop脚本
aliDDns.sh 启动
#!/bin/bash
java -cp aliDDns-1.0-SNAPSHOT.jar DDNS start > log.file 2>&1 &
echo "开始解析"
stop.sh 停止
PROCESS=`ps -ef|grep aliDDns|grep -v grep|grep -v PPID|awk '{ print $2}'`
for i in $PROCESS
do
echo "Kill the $1 process [ $i ]"
kill -9 $i
done
总结
如果不是做java研发,这个脚本跑起来有点难。这个是打包好的DDNS ipv6 如果没有积分可留言 代码地址源码
声明:该项目只是做个人学习使用,并未将域名暴露他人。网友其他行为与本人无关!!!
|