-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweaver.js
142 lines (122 loc) · 4.54 KB
/
weaver.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
/*
Weaver JavaScript Library v 0.3.0
Copyright (C) 2025 Modula.dev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module.exports = {
listen,
respond,
router,
routeMatch,
routeUri,
routeQueryObject,
routeMethod,
routeHoneypot,
pushHoneypot,
serveFile,
serveRedirect,
serveError,
hashId
};
const fs = require('fs');
const http = require('http');
// ================================================== Weaver Core Functionality
var router_function = undefined;
function router (func) { router_function = func; }
function respond (handler, response) {
handler.writeHead(response.code, {'Content-Type': response.mime});
handler.end(response.body);
}
function listen (port) {
const service = http.createServer(function(request, handler) {
if (router_function == undefined) {
error(`fatal error, weaver.router has not been initialized`);
respond(handler, serveError(500, "")); }
router_function(request, handler);
});
service.listen(port);
}
function error(text){
console.error(text);
fs.appendFileSync('error.log', text+'\n')
process.exit(1)
}
function warn(text){
console.error(text);
fs.appendFileSync('error.log', text+'\n')
}
// ================================================== Weaver Routing Functions
function hashId(request) {
try { ip = request.headers['x-forwarded-for'].split(',')[0]} catch(err) { warn('request missing source IP address'); return undefined; }
try { user = request.headers['user-agent']} catch(err) { warn('request missing user-agent'); return undefined; }
try { url = request.url } catch(err) { warn('request missing url header'); return undefined; }
try { method = request.method } catch(err) { warn('request missing method header'); return undefined; }
var string = ip + user; var roll = 0; var len=string.length; var sum=0;
for(let i=0; i<string.length;i++){ sum += string.charCodeAt(i) }
roll = sum % 256
return parseInt(''+roll+len+sum)
}
function routeUri(request) {
const uri = request.url;
const a = uri.split("?");
return decodeURIComponent(a[0]);
}
function routeMethod(request) {
return request.method;
}
function routeQueryObject(request) {
const object = {};
const query = ((request.url.split("?")).slice(1).join(""));
const entries = query.split("&");
for (const entry of entries) {
const [key, value] = entry.split("=");
if (!key) { continue; } object[decodeURIComponent(key)] = decodeURIComponent(value);
} return object;
}
function routeMatch(request, regArray, flags) {
const uri = routeUri(request);
for(let i=0; i<regArray.length; i++) {
const test = new RegExp(regArray[i], flags);
if (test.test(uri)) { return i; }
} return -1;
}
const honeypots = [
"^(https?:\/\/)?[^\/]*\/wp-.*",
"^(https?:\/\/)?[^\/]*\/\.?[Aa][Ww][Ss]_?.*"
];
function pushHoneypot(regex) { honeypots.push(regex); }
function routeHoneypot(request) {
if ( routeMatch(request, honeypots) != -1 ) { return serveError(404, request.url); }
else { return undefined; }
}
// ================================================== Weaver Include Functions
function serveFile(mime, path) {
try { return {
code: 200,
mime: mime,
body: fs.readFileSync(path) // {encoding: "utf-8"}
}}
catch(err) {
error(`cannot serve missing file ${path}`);
return { code: 404, mime: "text/raw", body: `HTTP 404: told to serve file ${path}, file does not exist` }
}
}
function serveRedirect(path) {
return {
code: 303,
mime: "text/html",
body: `<script>function redirect() { window.location.replace("${path}"); }</script><body onload="redirect()">HTTP 303: <a href="${path}">Click here if not automatically redirected</a></body>`
}
}
function serveError(code, path) {
return { code: code, mime: "text/raw", body: `HTTP ${code}: uri ${path} ${errorDescription[code]}` }
}