udp server
MeProm opened this issue · 0 comments
I have set up a server, and the client can connect via UDP on this server machine. However, when I use local network penetration, the UDP server cannot establish a connection. So, I wrote a piece of Node.js code to listen for UDP requests on some local ports, and indeed, I am able to capture these UDP requests. Therefore, my speculation is as follows:
Server -> UDP server port -> Client -> Local port -> Request connection
I am not sure how to resolve this issue, but I can provide my source code for reference: const dgram = require('dgram');
// 本地监听的UDP端口
const proxyPort = 25577;
// 目标地址和端口
const targetHost = '139.155.138.49';
const targetPort = 2333;
// 创建UDP服务器
const proxyServer = dgram.createSocket('udp4');
// 监听本地端口
proxyServer.on('listening', () => {
const address = proxyServer.address();
console.log(UDP Proxy Server listening on ${address.address}:${address.port}
);
});
// 处理收到的UDP消息
proxyServer.on('message', (message, remote) => {
console.log(Received message from ${remote.address}:${remote.port}: ${message}
);
// 转发消息到目标地址和端口
const client = dgram.createSocket('udp4');
client.send(message, 0, message.length, targetPort, targetHost, (err) => {
if (err) {
console.error(`Error sending message to ${targetHost}:${targetPort}: ${err}`);
}
// 监听目标返回的消息
client.on('message', (responseMessage, responseRemote) => {
console.log(`Received response from ${responseRemote.address}:${responseRemote.port}: ${responseMessage}`);
// 将返回消息发送回原始请求的客户端
proxyServer.send(responseMessage, 0, responseMessage.length, remote.port, remote.address, (sendErr) => {
if (sendErr) {
console.error(`Error sending response to ${remote.address}:${remote.port}: ${sendErr}`);
}
// 关闭UDP客户端
client.close();
});
});
});
});
// 绑定本地代理端口
proxyServer.bind(proxyPort);
I am chinese.