-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-single.js
72 lines (53 loc) · 1.76 KB
/
proxy-single.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'
const net = require('net');
const chalk = require('chalk');
// Waits for external client connections and forwards to connected client process.
function startServer() {
console.log('Starting proxy client');
const server = net.createServer();
server.listen(8000, '127.0.0.1', () => {
console.log('TCP server running...');
});
let globalSocketId = 0;
server.on('connection', (sock) => {
const socketId = globalSocketId;
++globalSocketId;
console.log(chalk.blue('New TCP connection ' + socketId));
const proxySock = new net.Socket();
proxySock.connect(7010, 'localhost', () => {
proxySock.on('data', (data) => {
console.log(socketId + ': ' + chalk.yellow('Proxy Data: ' + data.length));
sock.write(data);
});
proxySock.on('close', (hadError) => {
console.log(socketId + ': ' + chalk.yellow('Proxy: close'));
});
proxySock.on('error', (hadError) => {
console.log(socketId + ': ' + chalk.yellow('Proxy: ') + chalk.red('error'));
});
proxySock.on('end', (hadError) => {
console.log(socketId + ': ' + chalk.yellow('Proxy: end'));
sock.destroy();
});
sock.on('data', (data) => {
console.log(socketId + ': ' + chalk.blue('Socket Data: ' + data.length));
proxySock.write(data);
});
sock.on('close', (hadError) => {
console.log(socketId + ': ' + chalk.blue('Socket: close'));
});
sock.on('end', (data) => {
// Is this always called before close?
console.log(socketId + ': ' + chalk.blue('Socket: end'));
proxySock.destroy();
});
sock.on('error', (data) => {
console.log(socketId + ': ' + chalk.blue('Proxy: ') + chalk.red('error'));
});
});
});
}
// Connects to external server process and forwards to local TCP server.
function startClient() {
}
startServer();