-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
144 lines (127 loc) · 3.45 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fetch = require("node-fetch");
const https = require("https");
const WebSocket = require("ws");
const winston = require("winston");
let openSocket = [];
const logger = winston.createLogger({
level: "info",
format: winston.format.simple(),
transports: [new winston.transports.File({ filename: "provider-proxy.log" })],
});
logger.info("Start");
process.on("uncaughtException", (error) => {
logger.error(error);
});
process.on("message", async (message) => {
logger.info("Received message from parent process: ", {
...message,
certPem: "***REDACTED***",
keyPem: "***REDACTED***",
});
try {
if (message.type === "fetch") {
const response = await makeProviderRequest(
message.url,
message.method,
message.body,
message.certPem,
message.keyPem
);
logger.info("Sending response: ", response);
process.send({
id: message.id,
type: "fetch",
response: response,
});
} else if (message.type === "websocket") {
providerWebSocket(
message.id,
message.url,
message.certPem,
message.keyPem,
(socketMessage) => {
if (socketMessage) {
logger.info("Sending message: " + socketMessage);
process.send({
id: message.id,
type: "websocket",
message: socketMessage,
});
}
},
(error) => {
logger.error(error);
}
);
} else if (message.type === "websocket_close") {
logger.info("Closing socket: " + message.id);
openSocket[message.id].terminate();
delete openSocket[message.id];
} else {
throw "Invalid message type: " + message.type;
}
} catch (err) {
logger.info("Sending error: " + err);
process.send({
id: message.id,
error: err.message || err,
type: message.type,
});
}
});
async function makeProviderRequest(url, method, body, certPem, keyPem) {
try {
var myHeaders = new fetch.Headers();
myHeaders.append("Content-Type", "application/json");
const httpsAgent = new https.Agent({
cert: certPem,
key: keyPem,
rejectUnauthorized: false,
});
const response = await fetch(url, {
method: method,
body: body,
headers: myHeaders,
agent: httpsAgent,
});
if (response.status === 200) {
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return await response.json();
} else {
return await response.text();
}
} else {
const res = await response.text();
throw new Error(
"Status code was not success (" + response.status + ") : " + res
);
}
} catch (err) {
logger.info(err);
throw err;
}
}
function providerWebSocket(id, url, certPem, keyPem, onMessage) {
url = url.replace("https://", "wss://");
console.log(url);
const ws = new WebSocket(url, {
cert: certPem,
key: keyPem,
rejectUnauthorized: false,
});
openSocket[id] = ws;
ws.on("open", function open() {
console.log("connected");
});
ws.on("message", function incoming(data) {
console.log(data);
onMessage(data);
});
ws.on("error", (_, err) => {
logger.error("Websocket received an error: " + err);
});
ws.on("close", (_, code, reason) => {
logger.info("Websocket was closed [" + code + "]: " + reason);
});
}