-
-
Save execmd/fdcfa38d61785a1ac513c7f54ff0aaeb to your computer and use it in GitHub Desktop.
simple proxy server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| 'use strict'; | |
| const { createServer, connect } = require('net'); | |
| const log = (group) => (...args) => console.log.apply(console, ['[' + group + ']', ...args]); | |
| const err = (group) => (...args) => console.error.apply(console, ['[' + group + ']', ...args]); | |
| const l = log('server'); | |
| const e = err('server'); | |
| const server = createServer((socket) => { | |
| const id = parseInt(process.hrtime().join('')).toString(36); | |
| const l = log(id); | |
| const e = err(id); | |
| const client = connect(process.env.TARGET_PORT, process.env.TARGET_HOST || '127.0.0.1'); | |
| socket.on('error', (error) => { | |
| e('Server error: ', error); | |
| }); | |
| client.on('error', (error) => { | |
| e('Client error: ', error); | |
| }); | |
| socket.on('data', (chunk) => { | |
| l(' => ', chunk.toString('utf8')); | |
| client.write(chunk); | |
| }); | |
| client.on('data', (chunk) => { | |
| l(' <= ', chunk.toString('utf8')); | |
| socket.write(chunk); | |
| }); | |
| socket.on('end', () => { | |
| l('Server ended'); | |
| client.end(); | |
| }); | |
| client.on('end', () => { | |
| l('Server ended'); | |
| socket.end(); | |
| }); | |
| }); | |
| server.on('error', (err) => { | |
| e('Error on server', err); | |
| }); | |
| server.listen(process.env['HTTP_PORT'] || 3333, process.env['HTTP_IF'] || '0.0.0.0', () => { | |
| l('Server started', server.address()); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment