-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
executable file
·73 lines (66 loc) · 2.25 KB
/
test.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
#!/bin/env -S qjs -m
import * as http from "./http-server.js";
import * as os from 'os';
var mainProcName = scriptArgs[0].match(/.*\/(.*)/)[1] || scriptArgs[0];
try {
http.setProcName(mainProcName);
http.start({
listen: "localhost",
port: 1202,
minWorkers: 1,
maxWorkers: 20,
workerTimeoutSec: 300,
requestHandler: handleRequest,
});
} catch (e) {
console.log(e);
console.log(e?.stack || "");
http.shutdown(); //shutdown workers
}
function handleRequest(r) {
// console.log(http.see(r));
return {
status: 302,
h: {
Host: "localhost",
"Content-Type": "text/plain; charset=utf-8",
Location: "https://meet.jit.si/protasenko",
},
postprocess: () => {
simpleSendMail("10.8.1.1", 587, "[email protected]", "[email protected]",
`jitsi visited from ${r.h["X-Real-IP"]}`, "Посетители в: https://meet.jit.si/protasenko");
}
};
}
function simpleFetchUrl(host, port, r) {
var conn = http.connect(host, port);
http.sendHttpRequest(conn, r);
var resp = http.recvHttpResponse(conn, r.maxBodySize || -1);
http.close(conn);
return resp;
}
function simpleSendMail(host, port, from, to, subj, text) {
var conn = http.connect("10.8.1.1", 587);
assertResp("220 ");
assertResp("250 ", `ehlo localhost\n`);
assertResp("250 ", `mail from: ${from}\n`);
assertResp("250 ", `rcpt to: ${to}\n`);
assertResp("354 ", "data\n");
assertResp("250 ", `Subject: ${subj}\nFrom: ${from}\nTo: ${to}\nContent-Type: text/plain; charset=utf-8;\n\n${text}\r\n.\r\n`)
http.sendString(conn, "quit\n");
http.close(conn);
function assertResp(respStart, cmd) {
if (cmd)
http.sendString(conn, cmd);
let resp = http.recvLine(conn);
if (resp.indexOf(respStart) != 0) {
while (1) { //could receive multiple lines in response to ehlo
if (resp.match(/^\d\d\d /mg))
break;
resp += http.recvLine(conn);
}
if (!resp.match(new RegExp(`^${respStart}`, "mg")))
throw new Error(`Unexpected reply: ${resp} in response to: ${cmd}`);
}
}
}