| |
| const net = require('net'); |
| const dgram = require('dgram'); |
| const { exec } = require('child_process'); |
| const { promisify } = require('util'); |
|
|
| const execAsync = promisify(exec); |
|
|
| |
| |
| |
| |
| |
| |
| function createProtectedTCPConnection(host, port) { |
| console.log(`创建受保护的TCP连接到 ${host}:${port}`); |
| |
| |
| const socket = new net.Socket(); |
| |
| |
| socket.setNoDelay(true); |
| socket.setTimeout(10000); |
| |
| |
| try { |
| |
| |
| socket.setKeepAlive(true, 1000); |
| } catch (error) { |
| console.warn('设置TCP套接字选项时出错:', error.message); |
| } |
| |
| |
| socket.connect(port, host, () => { |
| console.log(`已连接到 ${host}:${port}`); |
| }); |
| |
| return socket; |
| } |
|
|
| |
| |
| |
| |
| function createProtectedUDPSocket() { |
| console.log('创建受保护的UDP套接字'); |
| |
| |
| const socket = dgram.createSocket('udp4'); |
| |
| return socket; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async function sendICMPPacketViaSystem(destinationIP, packet) { |
| console.log(`通过系统命令发送ICMP数据包到 ${destinationIP}`); |
| |
| try { |
| |
| const fs = require('fs'); |
| const path = require('path'); |
| const tempDir = '/tmp'; |
| const tempFile = path.join(tempDir, `icmp_packet_${Date.now()}.bin`); |
| |
| |
| fs.writeFileSync(tempFile, packet); |
| |
| |
| |
| const command = `hping3 -0 -c 1 -d ${packet.length} -E ${tempFile} ${destinationIP}`; |
| |
| console.log(`执行命令: ${command}`); |
| const { stdout, stderr } = await execAsync(command); |
| |
| if (stderr) { |
| console.warn('发送ICMP数据包时的警告:', stderr); |
| } |
| |
| console.log('ICMP数据包发送结果:', stdout); |
| |
| |
| fs.unlinkSync(tempFile); |
| } catch (error) { |
| console.error('通过系统命令发送ICMP数据包时出错:', error.message); |
| |
| |
| console.log('回退到普通UDP发送'); |
| const client = dgram.createSocket('udp4'); |
| client.send(packet, 0, packet.length, 7, destinationIP, (err) => { |
| if (err) { |
| console.error('发送ICMP数据包时出错:', err.message); |
| } else { |
| console.log(`ICMP数据包已发送到 ${destinationIP}:7`); |
| } |
| client.close(); |
| }); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| function isLocalAddress(ip) { |
| |
| if (ip.startsWith('127.') || ip === 'localhost') { |
| return true; |
| } |
| |
| |
| if (ip.startsWith('10.') || |
| ip.startsWith('192.168.') || |
| (ip.startsWith('172.') && parseInt(ip.split('.')[1]) >= 16 && parseInt(ip.split('.')[1]) <= 31)) { |
| return true; |
| } |
| |
| return false; |
| } |
|
|
| |
| |
| |
| |
| async function getDefaultGateway() { |
| return new Promise((resolve, reject) => { |
| require('child_process').exec('route get default | grep gateway | awk \'{print $2}\'', (error, stdout, stderr) => { |
| if (error) { |
| reject(error); |
| } else { |
| const gateway = stdout.trim(); |
| resolve(gateway); |
| } |
| }); |
| }); |
| } |
|
|
| |
| |
| |
| |
| async function getNetworkInterfaces() { |
| return new Promise((resolve, reject) => { |
| require('child_process').exec('ifconfig -l', (error, stdout, stderr) => { |
| if (error) { |
| reject(error); |
| } else { |
| const interfaces = stdout.trim().split(' '); |
| resolve(interfaces); |
| } |
| }); |
| }); |
| } |
|
|
| module.exports = { |
| createProtectedTCPConnection, |
| createProtectedUDPSocket, |
| isLocalAddress, |
| getDefaultGateway, |
| getNetworkInterfaces |
| }; |