Skip to content

Instantly share code, notes, and snippets.

@ChenYFan
Created February 15, 2024 14:59
Show Gist options
  • Save ChenYFan/056e19ec873814883c36deb34fe6e19c to your computer and use it in GitHub Desktop.
Save ChenYFan/056e19ec873814883c36deb34fe6e19c to your computer and use it in GitHub Desktop.
这是一个简单的Fullcone网络打洞脚本,用于测试FullCone网络内网穿透的理论可行性
const http = require('http');
const localPort = 18000; //这是本地即将暴露的端口
const endpoint_url = '' //这是部署了server.js的服务器的ip地址
const endpoint_port = 13000 //这是部署了server.js的服务器的端口
const req = http.request({
host: endpoint_url,
port: endpoint_port,
method: 'GET',
localPort: localPort
}, (res) => {
res.on('data', (data) => {
const { ip, port } = JSON.parse(data.toString());
console.log(`127.0.0.1:${localPort} -> ${ip}:${port}`);
console.log(`http://${ip}:${port}`);
http.createServer((req, res) => {
console.log(`Request from ${req.connection.remoteAddress}`);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Working at ${new Date()}`);
}).listen(localPort); //通过server.js探测到映射对,然后在本地启动一个服务器,外网访问ip:port会被映射到本地的localPort,从而实现了内网穿透
})
});
req.end();
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
const ip = req.connection.remoteAddress.match(/\d+\.\d+\.\d+\.\d+/)[0];
const port = req.connection.remotePort;
console.log(`Request from ${ip}:${port}`);
setInterval(() => {
try {
const req = http.request({
host: ip,
port: port,
method: 'GET'
})
req.end();
} catch (e) { }
}, 1000); //该定时器会每秒向客户端发送一个请求,使得映射关系不会因为长时间没有通信而被销毁,从而实现锁定映射关系
res.end(JSON.stringify({ ip, port }));
}).listen(13000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment