brozeph / simple-socks

Simple SOCKS5 proxy server
280 stars 60 forks source link

High CPU Usage 100% when creating a simple socks proxy server #62

Open Eternal-Sunshine opened 11 months ago

Eternal-Sunshine commented 11 months ago

Proxy works for few days and then suddenly cpu spikes to 100% and doesn't come down i have to restart the machine to make it work again. I am attaching code and cpu snapshot. On this server nothing other than this code is running. I have same code running on 5 machines and all having same issue.

import socks5 from 'simple-socks';
let stats = {}; //not big object since I only proxy traffic to 5 domains
let AUTH = {user: 'test', pass: 'test'};

const options = {
    authenticate : function (username, password, socket, callback) {
        if (username === AUTH.user && password === AUTH.pass) {
            return setImmediate(callback);
        }

        return setImmediate(callback, new Error('incorrect username and password'));
    }
};

function updateStats(info) {
    const ip = info.address;
    stats[ip] = stats[ip] ? stats[ip]+1: 1;
    console.log('Proxy Stats:', stats);
}

const server = socks5.createServer(options);

// Better Logging and error handling
server.on('proxyConnect', (info, destination) => {
    updateStats(info);
    console.log(`[ProxyConnect] Connected to remote server at ${info.address}:${info.port}`);

    destination.on('data', (data) => {
        console.log(`[ProxyConnect] Data received from ${info.address}:${info.port}. Length: ${data.length} bytes`);
    });
});

server.on('proxyData', (data) => {
    console.log(`[ProxyData] Data received. Length: ${data.length} bytes`);
});

server.on('proxyError', (err) => {
    console.error('[ProxyError] Unable to connect to remote server.');
    console.error('Error:', err.message);
});

server.on('proxyDisconnect', (originInfo, destinationInfo, hadError) => {
    const errorStatus = hadError ? 'with error' : 'without error';
    console.log(
        `[ProxyDisconnect] Client ${originInfo.address}:${originInfo.port} request disconnected from remote server at ${destinationInfo.address}:${destinationInfo.port} ${errorStatus}`
    );
});

server.on('proxyEnd', (response, args) => {
    console.log(`[ProxyEnd] Socket closed with code ${response}. Args:`, args);
});

server.listen(8080, () => {
    console.log(`Server started at ${new Date().toISOString()}`);
});
image
brozeph commented 11 months ago

@Eternal-Sunshine can you share which version of the simple-socks module you're using? I'll try to setup a local test to see if I can replicate this.

Eternal-Sunshine commented 11 months ago

I am using this version "version": "3.1.0",

I tried to debug more and found that it is not closing sockets properly leading to too many active handles.