Skip to content

Instantly share code, notes, and snippets.

@ttodua
Last active September 20, 2023 18:30
Show Gist options
  • Select an option

  • Save ttodua/7a66e5ca28e55deebc58b0dd8e0c39a2 to your computer and use it in GitHub Desktop.

Select an option

Save ttodua/7a66e5ca28e55deebc58b0dd8e0c39a2 to your computer and use it in GitHub Desktop.

Revisions

  1. ttodua revised this gist Sep 20, 2023. 1 changed file with 15 additions and 13 deletions.
    28 changes: 15 additions & 13 deletions sample-ws-proxy
    Original file line number Diff line number Diff line change
    @@ -38,14 +38,13 @@ wss.on('error', function message(err) {
    // ======================================================
    // ================= SAMPLE PROXY =================
    // ======================================================
    const proxy = require('proxy');
    const createProxy = proxy.createProxy;
    const createProxy = require('proxy').createProxy;
    const server = createProxy(require('http').createServer());
    const proxy_port = 3333;
    const proxy_host = '127.0.0.2';
    const proxy_PORT = 3333;
    const proxy_HOST = '127.0.0.2'; // works for localhost
    server.localAddress = '127.0.0.99'; // this line works only when testing on localhost (because all IPs in the 127th diapason is your localhost's virtual IPs)
    server.listen(proxy_port, proxy_host, () => {
    console.log('HTTP(s) proxy server listening on port', proxy_host, ':', server.address().port);
    server.listen(proxy_PORT, proxy_HOST, () => {
    console.log('HTTP(s) proxy server listening on port', proxy_HOST, ':', server.address().port);
    });
    // ======================================================
    // ======================================================
    @@ -59,13 +58,16 @@ server.listen(proxy_port, proxy_host, () => {
    const WebSocket = require ('ws').WebSocket;
    const HttpsProxyAgent = require ('https-proxy-agent').HttpsProxyAgent;

    const target_site = '127.0.0.1';
    const port_REMOTESERVER = 4444;
    const proxy_IP = '127.0.0.2';
    const port_PROXY = 3333;
    const site_address = '127.0.0.1';
    const site_port = 4444;
    const proxy_address = '127.0.0.2';
    const proxy_port = 3333;

    let agent = new HttpsProxyAgent('http://'+ proxy_IP + ':' + port_PROXY);
    const ws1 = new WebSocket('ws://' + target_site + ':'+ port_REMOTESERVER, {agent:agent});
    let agent = new HttpsProxyAgent('http://'+ proxy_address + ':' + proxy_port);
    const ws1 = new WebSocket('ws://' + site_address + ':'+ site_port, {agent:agent});
    ws1.on('open', function () { ws1.send('Hello from client!'); });
    ws1.on('error', (e)=>{ if (e) console.log('errrr', e); });
    ws1.on('message', function incoming(data) { console.log("receivv", data.toString()); });
    ws1.on('message', function incoming(data) { console.log("receivv", data.toString()); });
    // ======================================================
    // ======================================================
    // ======================================================
  2. ttodua created this gist Sep 20, 2023.
    71 changes: 71 additions & 0 deletions sample-ws-proxy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@

    // ======================================================
    // ================ SAMPLE SERVER =================
    // ======================================================
    const createServer = (require('http')).createServer;
    const WebSocketServer = require('ws').WebSocketServer;

    const server_host = '127.0.0.1';
    const server_port = 4444;
    const s = createServer((req, res) => {
    console.log('request', req.url, req.headers);
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('okay');
    });
    s.listen(server_port, server_host);
    const wss = new WebSocketServer({ server: s });
    wss.on('listening', () => {
    console.log('Wss server listening:', server_host, ':', server_port);
    });
    wss.on('connection', (ws, req) => {
    console.log('connected WSS', req.url, req.headers);
    console.log('REMOTE ADDR: ' + ws._socket.remoteAddress);
    ws.send("hi from server, you are " + ws._socket.remoteAddress, (e)=> {if (e) console.log('errrr', e);});
    });
    wss.on('message', function message(data) {
    console.log('received: %s', data);
    });
    wss.on('error', function message(err) {
    console.log('errr !!:', err);
    });
    // ======================================================
    // ======================================================
    // ======================================================




    // ======================================================
    // ================= SAMPLE PROXY =================
    // ======================================================
    const proxy = require('proxy');
    const createProxy = proxy.createProxy;
    const server = createProxy(require('http').createServer());
    const proxy_port = 3333;
    const proxy_host = '127.0.0.2';
    server.localAddress = '127.0.0.99'; // this line works only when testing on localhost (because all IPs in the 127th diapason is your localhost's virtual IPs)
    server.listen(proxy_port, proxy_host, () => {
    console.log('HTTP(s) proxy server listening on port', proxy_host, ':', server.address().port);
    });
    // ======================================================
    // ======================================================
    // ======================================================



    // ======================================================
    // ================== SAMPLE CLIENT =================
    // ======================================================
    const WebSocket = require ('ws').WebSocket;
    const HttpsProxyAgent = require ('https-proxy-agent').HttpsProxyAgent;

    const target_site = '127.0.0.1';
    const port_REMOTESERVER = 4444;
    const proxy_IP = '127.0.0.2';
    const port_PROXY = 3333;

    let agent = new HttpsProxyAgent('http://'+ proxy_IP + ':' + port_PROXY);
    const ws1 = new WebSocket('ws://' + target_site + ':'+ port_REMOTESERVER, {agent:agent});
    ws1.on('open', function () { ws1.send('Hello from client!'); });
    ws1.on('error', (e)=>{ if (e) console.log('errrr', e); });
    ws1.on('message', function incoming(data) { console.log("receivv", data.toString()); });