ipv6外网访问本地网络

使用的是电信光猫,外网访问本地地址(功能相当于ipv4的内网穿透)。

和安装光猫的师傅套套近乎,师傅应该就会把光猫的管理员账户和密码告诉我们。

登录光猫管理员账户后,在安全=>防火墙中把”启用IPV6 SESSION”关闭

就能在外网访问本地电脑的ipv6地址了。

打开本地电脑的windows的远程桌面开关,就可以在外网使用windows自带的远程桌面访问本地电脑了,无需安装第三方应用。

如果仍然无法访问本地ipv6地址,可能是路由器也有防火墙。一般路由器改成桥接模式可以关闭路由器的防火墙,从而外网使用ipv6访问本地地址。

再加上ddns动态域名解析,就可以实现使用域名访问本地电脑了。cloudflare ddns

或者使用nodejs(版本22),以下是更新dns的代码:

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
113
114
115
// 修改下面的四个变量
// https://dash.cloudflare.com/profile/api-tokens 在这个页面获取Global API Key
import {parse} from "url";
import http from "http";
import https from "https";
import fs from "fs";

var sub = ["域名前缀"]; // 如 ["www"] // 单个IP绑定多个域名则为多个元素的数组,如: ['a', 'b']
var domain = ["主域名"]; // 如 ["qq.com"]
var email = "你的邮箱"; // 如 "abc@qq.com"
var authKey = "Global API Key" // 如 "123456789..."

var auth = {
"X-Auth-Email": email,
"X-Auth-Key": authKey
};

function log(txt) {
fs.appendFile('ddns.txt', (txt + '\n'), err => {
if (err) {
console.error(err)
return
}
});
}


const requestIpv6 = http.get({ 'host': 'api6.ipify.org', 'port': 80, 'path': '/', family: 6 }, function (resp) {
if (resp.statusCode != 200) {
log("non-200 response status code:", resp.statusCode);
return;
}
else {
resp.on('data', function (ip) {
log('get ipv6 success:200');
log("My public IP address is: " + ip);


const url = parse("https://api.cloudflare.com/client/v4/zones/5926705fb3db4a7f866714500ef5a12c/dns_records");
if (url.hash) {
url.path += url.hash;
}
url.method = "GET";
url.headers = auth;

const getDNSList = https.request(url, (DNSListRes) => {
let DNSListBody = '';
DNSListRes.setEncoding("utf8");
DNSListRes.on("data", (resBody) => {
DNSListBody += resBody;
});
DNSListRes.on("end", () => {
log("dns list success");
const getDNSList = JSON.parse(DNSListBody).result;
for (let i = 0; i < sub.length; i++) {
let domainTotal = sub[i] + '.' + domain;
getDNSList.forEach((record) => {

if (record.name === domainTotal && ip.toString() !== record.content) {
makeRequest(ip, domainTotal, "PUT", record);
}
else if (record.name === domainTotal && ip.toString() === record.content) {
log(domainTotal + ":ipv6 doesn't need update.")
}
})
if (getDNSList.filter((record) => record.name === domainTotal).length === 0) {
makeRequest(ip, domainTotal, "POST", { id: '' }); // POST不需要id,设置为空
}


};
});

});
getDNSList.on("error", (error) => {
log(String(error));
});
getDNSList.end();
});
}
});
requestIpv6.on('error', (error) => {
log(String(error));
})

function makeRequest(ipA, domainTotalA, method, record) {
let putBody = {
"content": ipA.toString(),
"name": domainTotalA,
"proxied": false,
"type": "AAAA"
}

let urlPut = parse("https://api.cloudflare.com/client/v4/zones/5926705fb3db4a7f866714500ef5a12c/dns_records/" + record.id);
urlPut.method = method;
urlPut.headers = {
...auth,
"Content-Type": "application/json"
};
const putDNS = https.request(urlPut, (PutDNSRes) => {
let putDNSBody = '';
PutDNSRes.setEncoding("utf8");
PutDNSRes.on("data", (putRes) => {
putDNSBody += putRes;
});
PutDNSRes.on("end", () => {
log(JSON.stringify(JSON.parse(putDNSBody), null, '\t'));
});

});
putDNS.on("error", (error) => {
log(String(error));
});
putDNS.end(JSON.stringify(putBody));
}

如果使用windows系统,则可以使用任务计划程序,设置开机一分钟之后执行脚本、和设置重复间隔一小时执行脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

' 修改下面一行
Set objFile = objFSO.OpenTextFile([修改为nodejs的输出日志的绝对位置], ForAppending)

objFile.WriteLine Now
objFile.WriteLine("start")
objFile.Close

' 修改下面一行
strCommand = "cmd /c node [修改为nodejs的程序的绝对位置]"
CreateObject("Wscript.Shell").Run strCommand, 0, true