服务器没有公网ivp4地址,只有ivp6地址,通过ddns可以实现ivp6访问到服务器,可是一般的网络没有配备ivp6,只有ivp4网络,这时可以使用Cloudflare的ivp6转ivp4实现访问,通过java定时调用Cloudflare接口更新ivp6地址值实现,前提是有域名

导入依赖

        <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.78</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.6</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.10</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.5.6</version></dependency>

需要填入url,秘钥,账号

package com.example.ddns;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.io.*;
import java.net.SocketException;
import java.util.*;@Component
public class CloudFlareSetIp {private static final String TYPE = "AAAA";private static final String URL = ""; //这里填你的网址private static final String X_AUTH_KEY = "";//这里填你的秘钥private static final String X_AUTH_EMAIL = "";//这里填你的邮箱账号private static final String CONTENT_TYPE = "application/json";private static final int TTL = 1;private static final boolean PROXIED = true;@Autowiredprivate Ipv6Util ipv6Util;/*** 定时任务*/@Scheduled(cron = "0 0/15 * * * ?")public void setIp() throws IOException {String zoneUrl = "https://api.cloudflare.com/client/v4/zones?name=";zoneUrl += URL + "&status=active&page=1&per_page=20&order=status&direction=desc&match=all";String zoneId = null;String dnsId = null;String content = null;Map map = JSON.parseObject(sendGet(zoneUrl), Map.class);List<Map> result = (List) map.get("result");for (Map map1 : result) {zoneId = (String) map1.get("id");}String dnsUrl = "https://api.cloudflare.com/client/v4/zones/" + zoneId + "/dns_records?";Map map1 = JSON.parseObject(sendGet(dnsUrl), Map.class);List<Map> result1 = (List) map1.get("result");for (Map map2 : result1) {dnsId = (String) map2.get("id");content = (String) map2.get("content");}String updateUrl = "https://api.cloudflare.com/client/v4/zones/" + zoneId + "/dns_records/" + dnsId;if (ipv6Util.getIpv6().equals(content)) {System.out.println("dns地址未变动,不执行更新");return;}String s = doPut(updateUrl);System.out.println("接口返回信息:" + s);Map map2 = JSON.parseObject(s, Map.class);if (true == ((boolean) map2.get("success"))) {System.out.println("执行更新dns成功");} else {System.out.println("执行更新dns失败");}}public String sendGet(String url) {CloseableHttpClient httpClient = HttpClients.createDefault();HttpGet httpGet = new HttpGet(url);httpGet.setHeader("Content-type", CONTENT_TYPE);httpGet.setHeader("DataEncoding", "UTF-8");httpGet.setHeader("X-Auth-Email", X_AUTH_EMAIL);httpGet.setHeader("X-Auth-Key", X_AUTH_KEY);RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();httpGet.setConfig(requestConfig);CloseableHttpResponse httpResponse = null;try {httpResponse = httpClient.execute(httpGet);HttpEntity entity = httpResponse.getEntity();String result = EntityUtils.toString(entity);return result;} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (httpResponse != null) {try {httpResponse.close();} catch (IOException e) {e.printStackTrace();}}if (null != httpClient) {try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}return null;}/*** 原生字符串发送put请求** @param url* @return* @throws ClientProtocolException* @throws IOException*/public String doPut(String url) throws SocketException {CloseableHttpClient httpClient = HttpClients.createDefault();HttpPut httpPut = new HttpPut(url);RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();httpPut.setConfig(requestConfig);httpPut.setHeader("Content-type", CONTENT_TYPE);httpPut.setHeader("DataEncoding", "UTF-8");httpPut.setHeader("X-Auth-Email", X_AUTH_EMAIL);httpPut.setHeader("X-Auth-Key", X_AUTH_KEY);//bodyJSONObject contentMap = new JSONObject();contentMap.put("type", TYPE);contentMap.put("name", URL);contentMap.put("content", ipv6Util.getIpv6());contentMap.put("ttl", TTL);contentMap.put("proxied", PROXIED);CloseableHttpResponse httpResponse = null;try {httpPut.setEntity(new StringEntity(contentMap.toJSONString()));httpResponse = httpClient.execute(httpPut);HttpEntity entity = httpResponse.getEntity();String result = EntityUtils.toString(entity);return result;} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (httpResponse != null) {try {httpResponse.close();} catch (IOException e) {e.printStackTrace();}}if (null != httpClient) {try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}return null;}}

获取网卡ipv6地址,需要先查看服务器网卡名称填入即可

package com.example.ddns;import org.springframework.stereotype.Component;import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;@Component
public class Ipv6Util {/*** 获取ipv6地址信息** @return* @throws SocketException*/public String getIpv6() throws SocketException {try {InetAddress ads = null;Enumeration<?> adds = NetworkInterface.getNetworkInterfaces();String myIp = null;while (adds.hasMoreElements()) {NetworkInterface netInterface = (NetworkInterface) adds.nextElement();Enumeration<?> inetAds = netInterface.getInetAddresses();while (inetAds.hasMoreElements()) {ads = (InetAddress) inetAds.nextElement();//判断是否与InetAddress相同且非保留地址if (ads instanceof InetAddress && !isReservedAddr(ads)) {//ip地址String ipAddress = ads.getHostAddress();//对应ip的网卡名称String eth = netInterface.getName();//System.out.println(eth + "-" + ipAddress);//网卡信息过滤//enp2s0是我的网卡名称,需要填入自己的网卡名称if ("enp2s0".equals(eth) && ipAddress.length() > 20) {myIp = ipAddress.replace("%enp2s0", "");}}}}System.out.println("ipv6地址是:" + myIp);return myIp;} catch (Exception e) {e.printStackTrace();System.out.println("获取ipv6地址失败-" + e);return null;}}private boolean isReservedAddr(InetAddress inetAddr) {if (inetAddr.isAnyLocalAddress() || inetAddr.isLinkLocalAddress() || inetAddr.isLoopbackAddress()) {return true;}return false;}
}

打包后在linux后台运行jar包即可

【ivp6服务器可通过ivp4网络访问教程】相关推荐

  1. Docker教程(三) Docker网络访问和数据管理

    Docker教程(三) Docker网络访问和数据管理 本文链接:https://blog.csdn.net/yuan_xw/article/details/77504077 Docker教程(三) ...

  2. 海康监控虚拟服务器设置,海康监控如何连接网络设置教程

    海康监控如何连接网络设置教程 [2021-02-14 17:49:08]  简介: 建站服务器 这篇文章给大家分享的是有关通过ECS自带监控服务和云监控服务监控实例的方法的内容.小编觉得挺实用的,因此 ...

  3. 访问samba服务器提示“无任何网络提供程序接受指定的网络路径”的一个解决办法

    我刚开始的配置信息如下: [global] workgroup = WORKGROUP server string = %h server \(Samba, Ubuntu\) security = s ...

  4. 登录wd云盘显示内部服务器错误,网络访问失败,请检查你的网络。360云盘登陆不上!...

    http://help.360.cn/5030804/256704945.html 服务器不能登录360云盘 提示网络连接失败 请检测网络 有网络的 但是不能登录 可以访问网页 2013-01-31 ...

  5. 拒绝从网络访问这台计算机没有用户,拒绝从网络服务器访问此 (Windows 10) - Windows security | Microsoft Docs...

    拒绝从网络访问这台计算机 05/19/2021 本文内容 适用范围 Windows 10 介绍"拒绝从网络安全策略访问此计算机"设置的最佳方案.位置.值.策略**** 管理和安全注 ...

  6. 电脑小米路由器设置虚拟服务器,小米路由器在Win7系统中设置网络映射教程

    小米路由器怎么在Win7系统中设置网络映射?小米路由器,自带有一个文件存储芯片,用于存储用户使用小米路由器下载的各类文件(包括电影.音乐等文件),如果需要在计算机中看到小米路由器的文件夹,则需要将小米 ...

  7. xp系统宽带连接断开怎么连接服务器,winxp的怎样断开和启用连接_winxp系统如何随意断开和启用连接网络图文教程...

    最近有些朋友在询问小编winxp的怎样断开和启用连接,对于这个问题,还有很多朋友不太明白.有的时候我们需要断开电脑上的网络,拔网线会很麻烦,这时候我们就可以通过一些设置来实现断开和启用连接,具体应该如 ...

  8. .NET开发框架(八)-服务器集群之网络负载平衡(视频)

    [视频教程在文章底部],本文讲解Windows服务器集群的网络负载平衡NLB的作用,以及在.NET开发框架的架构设计中,如何应用NLB与ARR,使用它们各有什么优点. 视频教程目录: 1.讲解NLB概 ...

  9. 使用ip小魔棒让外部网络访问内网中的资源

    首先说下需求,因公司是做嵌入式硬件开发的,而且公司网路没有公网ip,发布到嵌入式硬件中的web项目,无法在外部网路访问,于是就buy了个ip小魔棒.下面以Windows做例子,我先在Windows电脑 ...

最新文章

  1. list在codeblocks和vs2013中编译提示不同
  2. Linux入门(9)——Ubuntu16.04安装flash player
  3. 获取前台HTML控件的值(select)
  4. 《Effective Java2》笔录
  5. 【报错笔记】 启动tomcat服务器报错Context initialization failed
  6. 全方位了解超宽带信号高速采集记录回放系统
  7. 使用Anaconda3安装tensorflow,opencv,使其可以在spyder中运行
  8. Java线程那点事儿
  9. 软件设计师12-数据库(范式)
  10. 【安全牛学习笔记】手动漏洞挖掘(二)
  11. VS 2015 64位CMake编译openCV3.1.0必备文件
  12. oracle sql语句加减,实现四则运算的一条sql语句
  13. 计算机 继续教育培训心得体会,继续教育培训总结.doc
  14. CEC2018:动态多目标测试函数DF6~DF9的PS及PF(提供Matlab代码)
  15. 数据挖掘从业人员的愿景
  16. Internet Explorer无法打开internet站点,已终止操作的解决方法合集
  17. Docker mysql [Warning] World-writable config file ‘.cnf‘ is is ignored
  18. python基础:面向对象的应用--搬家具。
  19. java 浏览器设置字体大小_Sass可以在不使用REM的情况下访问浏览器的默认字体大小吗?...
  20. MySQL 数据库基础

热门文章

  1. 动网php_动网(DVBBS)PHP论坛preview.php代码执行漏洞
  2. OC 主线程刷新UI
  3. oppo--三面HR面试总结(三)
  4. 近8年的测试人员跟你细谈如何从一个测试小白到大佬的转变
  5. 强化学习——探索与利用基本方法
  6. Java三个月速成学习路线图
  7. 搭建个人博客,东半球最强教程
  8. 浙江杭州工程师职称评审论文要求
  9. 最简单的理财:定投与资产配置
  10. Linux基础指令的基本操作(一)