-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·116 lines (116 loc) · 3.21 KB
/
index.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
const WebSocket = require("./lib/ws/websocket");
const WebSocketServer = require("./lib/ws/websocket-server");
const AvailableSecret = require("./lib/processing/AvailableSecret");
class ipc {
#secret;
#port;
#listening;
#listening_event;
constructor(key) {
process.cacheipc = process.cacheipc || [];
this.#secret = `${key}`;
this.#port = typeof key === "number" ? key : 0;
this.#listening = false;
if (typeof key === "number") {
if (key < 1024 || key > 65535) {
throw new Error("Must be between 1024 and 65535");
}
}
this.#listening_event = false;
}
async on(event, callback) {
const secret = this.#secret;
if (!["receive", "listening"].includes(event))
throw new Error("Invalid event");
if (event === "receive") {
if (await AvailableSecret(secret)) {
throw new Error(
`Error: Error: listen EADDRINUSE: address already in use :::${
this.#secret
}`
);
}
const wss = new WebSocketServer({
path:
"/ipc_9ajaxVjVtI0L0LtV_04jtGF0710V7L5020Im1D307F0gvF0N5AMx5gxo0_g0og6dx06g0g" +
secret,
port: (await AvailableSecret(secret)) || this.#port,
});
if (this.#listening || process.cacheipc.includes(secret)) {
throw new Error(`Already used on this secret ${this.#secret}`);
}
process.cacheipc.push(secret);
this.#listening = true;
wss.on("listening", (err) => {
this.#port = wss.address().port;
if (this.#listening_event) {
this.#listening_event();
}
});
wss.on("error", (err) => {
throw new Error(err.toString());
});
wss.on("connection", (ws) => {
ws.on("message", (message) => {
const data = JSON.parse(message);
if (data.secret !== secret) return;
callback({
res: data.res,
resolve: (data) => {
if (data === undefined) data = undefined;
ws.send(
JSON.stringify({
secret: secret,
return: data,
})
);
},
});
});
});
} else {
this.#listening_event = callback;
}
}
port() {
return this.#port;
}
}
const client = async (secret, res) => {
return new Promise(async (resolve, reject) => {
secret = secret.toString();
const $$PORT = await AvailableSecret(secret);
if (!$$PORT) throw new Error("Invalid secret");
const ws = new WebSocket(
"ws://localhost:" +
$$PORT +
"/ipc_9ajaxVjVtI0L0LtV_04jtGF0710V7L5020Im1D307F0gvF0N5AMx5gxo0_g0og6dx06g0g" +
secret
);
ws.on("error", (err) => {
if (err.message.includes("ECONNREFUSED")) {
reject(new Error("Connection refused, make sure you have started ipc"));
return;
}
reject(err);
});
ws.on("open", () => {
ws.send(
JSON.stringify({
secret,
res,
})
);
ws.on("message", (message) => {
const data = JSON.parse(message);
if (data.secret !== secret) return;
resolve(data.return);
ws.close();
});
});
});
};
module.exports = {
ipc,
client,
};