-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (102 loc) · 3.72 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
117
118
119
120
121
122
123
124
125
module.exports = function(options) {
this._routes = [];
this._log = () => {};
this.route = (path, options, cb) => {
let _path = path;
_path = _path.replaceAll("/", "\/");
_path = _path.replace(/\((.*?)\)/g, "(?<$1>.*)");
_path = _path.replaceAll("*", `(.*)`);
_path = new RegExp(_path);
this._routes.push({
path: _path,
options,
cb
});
}
this.body_parser = async (req, res, next) => {
if (req.method == "POST") {
try {
let no_data = true;
let body = "";
setTimeout(() => {
if (no_data) {
req.connection.destroy();
//res.setHeader('Content-Type', 'application/json');
//res.writeHead(200);
//res.end( JSON.stringify({ "connection": "timeout" }) );
this._log("TIMEOUT!");
return;
}
}, 100);
req.on('data', async (data) => {
try {
no_data = false;
body += data;
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6) {
// FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
req.connection.destroy();
}
req.on('end', async () => {
try {
req.body = body;
next();
//var obj = JSON.parse(body);
} catch (e) {
this._log("at req.on(end) ", e);
}
});
} catch (e) {
this._log("at req.on(data) ", e)
}
});
//next();
} catch (e) {
this._log("at body_parser ", e);
}
}
}
this.listen = async (req, res) => {
console.log("req")
let route = this._routes.filter(route => {
//reg = new RegExp(route.path);
if (route.options.method == "ANY" ? true :
route.options.method == req.method) {
let match = req.url.match(route.path);
if (match) {
this._log("checking", match);
if (match[0] == match.input) {
req.groups = match.groups;
return route;
}
}
}
});
for (let i = 0; i < route.length; i++) {
let nf = route[i + 1];
route[i].next = nf ?
() => {
nf.cb(req, res, nf.next);
} :
() => {
this._log("nothing is next")
};
}
this._log(route);
if (route.length > 0) route[0].cb(req, res, route[0].next);
this._log(`${req.socket.remoteAddress}:${req.socket.remotePort} -> ${req.method} '${req.url}'`);
}
//console.log(this.request_listener,"\n", this);
if (options) {
if (options.body_parser)
this.route("*", {
method: "POST"
}, this.body_parser);
this._log = options.debug ? console.log : () => {};
}
this.listen.route = this.route;
this.listen.body_parser = this.body_parser;
//this.request_listener.__proto__ = this;
//Object.setPrototypeOf(this.request_listener, this);
return this.listen;
};