Make a TCP proxy forwarding script in nodejs, 0.0.0.0:1000 to > 1.1.1.1:2000 as example.
- User
Make a TCP proxy forwarding script in nodejs, 0.0.0.0:1000 to > 1.1.1.1:2000 as example.
- Assistant
Make a TCP proxy forwarding script in nodejs, 0.0.0.0:1000 to > 1.1.1.1:2000 as example.
Certainly! Below is a Node.js script that acts as a TCP proxy. It listens on 0.0.0.0:1000 and forwards all incoming TCP traffic to 1.1.1.1:2000.
const net = require('net');
const LOCAL_PORT = 1000; // Local port to listen on
const REMOTE_PORT = 2000; // Remote port to forward to
const REMOTE_ADDR = '1.1.1.1'; // Remote address to forward to
// Create a server instance and listen for connections
const server = net.createServer((clientSocket) => {
console.log(`Client connected from ${clientSocket.remoteAddress}:${clientSocket.remotePort}`);
// Create a connection to the remote server
const remoteSocket = new net.Socket();
remoteSocket.connect(REMOTE_PORT, REMOTE_ADDR, () => {
console.log(`Connected to remote server at ${REMOTE_ADDR}:${REMOTE_PORT}`);
});
// Forward data from client to remote server
clientSocket.on('data', (data) => {
remoteSocket.write(data);
});
// Forward data from remote server back to client
remoteSocket.on('data', (data) => {
clientSocket.write(data);
});
// Handle client socket closure
clientSocket.on('close', () => {
console.log('Client connection closed');
remoteSocket.end();
});
// Handle remote socket closure
remoteSocket.on('close', () => {
console.log('Remote connection closed');
clientSocket.end();
});
// Handle client socket errors
clientSocket.on('error', (err) => {
console.error(`Client socket error: ${err.message}`);
remoteSocket.end();
});
// Handle remote socket errors
remoteSocket.on('error', (err) => {
console.error(`Remote socket error: ${err.message}`);
clientSocket.end();
});
});
// Start the server
server.listen(LOCAL_PORT, '0.0.0.0', () => {
console.log(`TCP proxy server listening on 0.0.0.0:${LOCAL_PORT}`);
});
Explanation:
Imports the net module: This module provides an asynchronous network API for creating stream-based TCP servers and clients.
Defines constants:
LOCAL_PORT is the local port where the proxy server listens for incoming connections.REMOTE_PORT and REMOTE_ADDR specify the remote address and port to forward the traffic to.Creates a TCP server:
clientSocket), it establishes a new connection to the remote server (remoteSocket).Data forwarding:
Handles socket events:
data event: Forwards data between the client and remote server.close event: Closes the corresponding socket when one end disconnects.error event: Logs the error and ensures both sockets are properly closed.Starts listening:
0.0.0.0:1000 and logs a message indicating that it's running.Usage:
Install Node.js: Ensure you have Node.js installed on your system.
Save the script: Save the above code in a file, e.g., tcp-proxy.js.
Run the script:
node tcp-proxy.js
Test the proxy:
localhost:1000.1.1.1.1:2000.Note: Replace 1.1.1.1:2000 with the actual remote address and port you wish to forward traffic to.
Security Considerations:
Firewall Rules: Ensure that your firewall settings allow traffic on the specified local port (1000) and that outgoing connections to the remote server are permitted.
Access Control: This simple proxy does not implement any access control. For production use, consider adding authentication or IP restrictions.
Error Handling: While basic error handling is included, you might want to enhance it based on your use case.
Feel free to customize the script according to your requirements!
Not yet included in a published release.