Skip to content

Instantly share code, notes, and snippets.

@ttodua
Last active September 20, 2023 18:30
Show Gist options
  • Save ttodua/7a66e5ca28e55deebc58b0dd8e0c39a2 to your computer and use it in GitHub Desktop.
Save ttodua/7a66e5ca28e55deebc58b0dd8e0c39a2 to your computer and use it in GitHub Desktop.
sample WS (WebSockets) & PROXY
// ======================================================
// ================ 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 createProxy = require('proxy').createProxy;
const server = createProxy(require('http').createServer());
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);
});
// ======================================================
// ======================================================
// ======================================================
// ======================================================
// ================== SAMPLE CLIENT =================
// ======================================================
const WebSocket = require ('ws').WebSocket;
const HttpsProxyAgent = require ('https-proxy-agent').HttpsProxyAgent;
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_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()); });
// ======================================================
// ======================================================
// ======================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment