Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Eternal-Sunshine opened this issue Nov 28, 2023 · 2 comments
Open

Comments

@Eternal-Sunshine
Copy link

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
Copy link
Owner

brozeph commented Nov 29, 2023

@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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants