forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirefox-proxy
More file actions
executable file
·77 lines (65 loc) · 2.04 KB
/
firefox-proxy
File metadata and controls
executable file
·77 lines (65 loc) · 2.04 KB
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
72
73
74
75
76
77
#!/usr/bin/env node
"use strict";
const minimist = require("minimist");
const ws = require("ws");
const net = require("net");
function proxy(webSocketPort, tcpPort, logging) {
console.log("Listening for WS on *:" + webSocketPort);
console.log("Will proxy to TCP on *:" + tcpPort + " on first WS connection");
if (!logging) {
console.log("Protocol messages can be logged by enabling " +
"logging.firefoxProtocol in your local.json config");
}
let wsServer = new ws.Server({ port: webSocketPort });
wsServer.on("connection", function onConnection(wsConnection) {
let tcpClient = net.connect({ port: tcpPort });
tcpClient.setEncoding("utf8");
tcpClient.on("connect", () => {
console.log("TCP connection succeeded");
});
tcpClient.on("error", e => {
wsConnection.close();
console.log("TCP connection failed: " + e);
});
tcpClient.on("data", data => {
if (logging) {
console.log("TCP -> WS: " + data);
}
try {
wsConnection.send(data);
} catch (e) {
tcpClient.end();
console.log("WS send failed, disconnected from TCP");
}
});
wsConnection.on("message", msg => {
if (logging) {
console.log("WS -> TCP: " + msg);
}
tcpClient.write(msg);
});
wsConnection.on("close", () => {
tcpClient.end();
console.log("WS connection closed, disconnected from TCP");
});
wsConnection.on("error", () => {
tcpClient.end();
console.log("WS connection error, disconnected from TCP");
});
});
}
const args = minimist(process.argv.slice(2));
const WEB_SOCKET_PORT = args["web-socket-port"] || 9000;
const TCP_PORT = args["tcp-port"] || 6080;
const shouldStart = args.start;
function start(options) {
const webSocketPort = options.webSocketPort || 9000;
const tcpPort = options.tcpPort || 6080;
const logging = !!options.logging;
proxy(webSocketPort, tcpPort, logging);
}
if (shouldStart) {
start({ webSocketPort: WEB_SOCKET_PORT, tcpPort: TCP_PORT });
} else {
module.exports = start;
}