-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver
executable file
·780 lines (654 loc) · 22.5 KB
/
server
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#!/usr/bin/env node
const fsL = require("fs"); // For functions unailable in fs.promises.
const fs = require("fs").promises;
const http = require("http");
const path = require("path");
const URL = require("url");
const spawn = require("child_process").spawn;
var port, hostname;
function regenerate() {
return new Promise((resolve, reject) => {
console.log("Regenerating index page");
let gen = spawn("scripts/gen", []);
gen.on("close", (code) => {
if (code == 0)
resolve();
else
reject();
});
gen.on("error", (err) => {
reject();
});
});
}
function cloneFollower(follower) {
return new Promise((resolve, reject) => {
let git = spawn("/usr/bin/git", [
"clone",
`${follower.url}git`,
path.join("repos", follower.email)
]);
git.on("close", () => {
regenerate()
.then(() => {
resolve();
})
.catch((err) => {
if (err)
console.log(err);
else
console.log("Failed page regeneration after git clone");
// Failed generation does not invalide the clone.
resolve();
});
});
git.on("error", (err) => {
reject(err);
});
});
}
// Run the local scripts/follow script to follow a new URL
function remoteFollow(targetUrl) {
return new Promise((resolve, reject) => {
let followScript = spawn("scripts/follow", [
targetUrl
]);
followScript.on("close", (code) => {
if (code != 0)
reject(new Error(`statusCode is ${code}`));
else
resolve();
});
followScript.on("error", (err) => {
reject(err);
});
});
}
// Run the local scripts/unfollow script to unfollow an URL
function remoteUnfollow(targetUrl) {
return new Promise((resolve, reject) => {
let unfollowScript = spawn("scripts/unfollow", [
targetUrl
]);
unfollowScript.on("close", (code) => {
if (code != 0)
reject(new Error(`statusCode is ${code}`));
else
resolve();
});
unfollowScript.on("error", (err) => {
reject(err);
});
});
}
function updateFollower(follower) {
let git = spawn("/usr/bin/git", ["pull"], {
cwd: path.join("repos", follower.email)
});
git.on("close", (code) => {
regenerate();
});
}
function isFollowing(storedFollows, targetUrl) {
for (let storedFollow of storedFollows)
if (storedFollow.url == targetUrl)
return true;
return false;
}
// Open the 'following' file and check it against data/following.
function checkFollowing() {
return Promise.all([
getFollowing(),
fs.readFile("following")
]).then((values) => {
let storedFollows = values[0];
let userFollowsUrls = new Set(values[1].toString()
.split("\n")
.filter((s) => {return s != "";})
.map((s) => {return s.trim();}));
for (let userFollowsUrl of userFollowsUrls) {
if (!isFollowing(storedFollows, userFollowsUrl)) {
// Trigger the follow process.
remoteFollow(userFollowsUrl)
.then(() => {
console.log(`Added follow URL: ${userFollowsUrl}`);
})
.catch(() => {
console.log(`Failed to add follow URL: ${userFollowsUrl}`);
});
}
}
// Scan the currently stored following URLs to see if one was removed.
for (let storedFollow of storedFollows) {
if (!userFollowsUrls.has(storedFollow.url)) {
// Trigger the unfollow process.
remoteUnfollow(storedFollow.url)
.then(() => {
console.log(`Removed follow URL: ${storedFollow.url}`);
})
.catch(() => {
console.log(`Failed to remove follow URL: ${storedFollow.url}`);
});
}
}
});
}
function gpgGetKeyData(keyData) {
let gpgpBins = ["gpg", "gpg2"];
return new Promise((resolve, reject) => {
let gpgBin = gpgBins.pop();
while (gpgBins) {
let gpg = spawn(gpgBin, ["--with-fingerprint", "--with-colons"], {
shell: true,
stdio: ['pipe', 'pipe', process.stderr]
});
let output = "";
let email = "";
let keyid = "";
gpg.stdout.on("data", (chunk) => {
output += chunk.toString();
});
gpg.on("exit", () => {
let gpgLines = output.split("\n");
for (let line of gpgLines) {
if (line.startsWith("pub"))
email = line.substring(line.indexOf("<") + 1, line.length - 2);
else if (line.startsWith("fpr:::::::::"))
keyid = line.substring(12, line.length - 1).trim();
}
if (email == null || email == "")
reject(new Error("email cannot be null or empty"));
if (keyid == null || keyid == "")
reject(new Error("keyid cannot be null or empty"));
resolve(email, keyid);
});
gpg.on("error", (err) => {
console.log(`Executing ${gpgBin} failed, trying another name`);
if (gpgBins)
gpgBin = gpgBins.pop();
else
reject(err);
});
gpg.stdin.end(keyData);
}
});
}
function readNodeDataFile(filename) {
return new Promise((resolve, reject) => {
// Open the following file.
let fin = fsL.createReadStream(filename);
let content = "";
fin.on("data", (chunk) => {
content += chunk.toString();
});
fin.on("end", () => {
let dataLines = content.split("\n");
let data = dataLines
.filter((dataLine) => {
return dataLine != "";
})
.map((dataLine) => {
let dataLineSplit = dataLine.split(" ");
return {
email: dataLineSplit[0].trim(),
url: dataLineSplit[1].trim(),
keyid: dataLineSplit[2].trim()
};
});
fin.close();
resolve(data);
});
});
}
function getFollowing() {
return readNodeDataFile(path.join("data", "following"));
}
function getFollowers() {
return readNodeDataFile(path.join("data", "followers"));
}
function saveFollowers(followers) {
let followersLines = followers.map((follower) => {
return `${follower.email} ${follower.url} ${follower.keyid}`;
});
return fs.writeFile(path.join("data", "followers"), followersLines.join("\n"));
}
function follow(keyData, url) {
let fin, content, email, keyid;
// Extract the key email.
return gpgGetKeyData(keyData)
.then((e, k) => {
email = e;
keyid = k;
return fs.writeFile(path.join("data", `${email}.gpg`), keyData);
})
.then(() => {
// Open the follower file.
fin = fsL.createReadStream(path.join("data", "followers"));
content = "";
return getFollowers();
})
.then((followers) => {
let doAdd = true;
for (let follower of followers) {
if (follower.email == email) {
doAdd = false;
break;
}
}
if (doAdd) {
followers.push({
email: email,
url: url,
keyid: keyid
});
}
return saveFollowers(followers);
})
.then(() => {
return regenerate();
});
}
function unfollow(keyData, url) {
let fin, content, email, keyid;
// Extract the key email.
return gpgGetKeyData(keyData)
.then((e, k) => {
email = e;
keyid = k;
return fs.writeFile(path.join("data", `${email}.gpg`), keyData);
})
.then(() => {
// Open the follower file.
fin = fsL.createReadStream(path.join("data", "followers"));
content = "";
return getFollowers();
})
.then((followers) => {
let doAdd = true;
followers = followers.filter((follower) => {
return follower.email != email && follower.keyid != keyid;
});
return saveFollowers(followers);
})
.then(() => {
return regenerate();
});
}
function readOptions() {
return new Promise((resolve, reject) => {
Promise.all([
fs.readFile(path.join("config", "port")),
fs.readFile(path.join("config", "hostname")),
fs.readFile(path.join("config", "user")),
fs.readFile(path.join("config", "password")),
fs.readFile(path.join("config", "title"))
]).then((values) => {
resolve({
port: values[0].toString().trim(),
hostname: values[1].toString().trim(),
user: values[2].toString().trim(),
password: values[3].toString().trim(),
title: values[4].toString().trim()
});
});
});
}
function handleUnfollowReq(req, res) {
let localUrl = URL.parse(req.url);
let localSp = new URLSearchParams(localUrl.search);
let targetUrl = URL.parse(localSp.get("url"));
const keyOpts = {
host: targetUrl.hostname,
port: targetUrl.port,
path: "/key"
};
console.log(`Unfollow request from: ${req.connection.remoteAddress}`);
// Lookup the public key of the follower.
const keyReq = http.request(keyOpts);
keyReq.end();
keyReq.on("response", (keyRes) => {
let keyData = "";
keyRes.on("data", (chunk) => {keyData += chunk;});
keyRes.on("end", () => {
unfollow(keyData, targetUrl.href);
});
if (keyRes.statusCode == 200) {
// Got the key!
console.log(`Got the key, accepting unfollow request from: ${req.connection.remoteAddress}`);
// Return the local key.
fs.readFile(path.join("data", "key.gpg"))
.then((data) => {
res.writeHead(200);
res.end(data);
});
} else {
res.writeHead(403);
res.end(`Failed to get follower key on ${targetUrl.href}: ${keyRes.statusCode}`);
}
});
keyReq.on("error", (err) => {
// No key, error out.
res.writeHead(403);
res.end(JSON.stringify(err));
});
}
function handleFollowReq(options, req, res) {
let localUrl = URL.parse(req.url);
let localSp = new URLSearchParams(localUrl.search);
let targetUrl = URL.parse(localSp.get("url"));
const keyOpts = {
host: targetUrl.hostname,
port: targetUrl.port,
path: "/key"
};
const pingOpts = {
host: targetUrl.hostname,
port: targetUrl.port,
path: `/ping?url=${options.hostname}:${options.port}`
};
console.log(`Follow request from: ${req.connection.remoteAddress}`);
// Lookup the public key of the follower.
const keyReq = http.request(keyOpts);
keyReq.end();
keyReq.on("response", (keyRes) => {
let keyData = "";
keyRes.on("data", (chunk) => {keyData += chunk;});
keyRes.on("end", () => {
follow(keyData, targetUrl.href)
.then(() => {
setTimeout(() => {
http.get(pingOpts);
}, 5000);
})
.catch((err) => {
if (err)
console.log(err);
else
console.log("Could not complete follow process");
});
});
if (keyRes.statusCode == 200) {
// Got the key!
console.log(`Got the key, accepting follow request from: ${targetUrl.href}`);
// Return the local key.
fs.readFile(path.join("data", "key.gpg"))
.then((data) => {
res.writeHead(200);
res.end(data);
});
} else {
res.writeHead(403);
res.end(`Failed to get follower key on ${targetUrl.href}: ${keyRes.statusCode}`);
}
});
keyReq.on("error", (err) => {
// No key, error out.
res.writeHead(403);
res.end(JSON.stringify(err));
});
}
function handlePingReq(req, res) {
let isFollower = false;
let localUrl = URL.parse(req.url);
let localSp = new URLSearchParams(localUrl.search);
let targetUrl = URL.parse(`http://${localSp.get("url")}`);
console.log(`Ping request from: ${req.connection.remoteAddress}`);
// Just answer since the caller won't wait for a result anyway.
res.writeHead(200);
res.end();
// Verify that the incoming request comes from a server
// we follow.
getFollowing().then((followings) => {
for (let following of followings) {
if (targetUrl.href.endsWith("/") && !following.url.endsWith("/"))
following.url += "/";
// Known followee? Great!
if (following.url == targetUrl.href) {
isFollower = true;
// Check if we already have this repository.
fs.access(path.join("repos", following.email))
.then(() => {
// Known repository, fetch it.
updateFollower(following);
})
.catch(() => {
// Unknown repository, clone it.
cloneFollower(following);
});
}
}
if (!isFollower)
console.log(`Ping from unknown followee ${targetUrl.href}`);
});
}
// Serve index page
function handleIndex(req, res) {
console.log(`Index request from: ${req.connection.remoteAddress}`);
fs.readFile(path.join("public", "index.html"))
.then((data) => {
res.writeHead(200);
res.end(data);
})
.catch((err) => {
res.writeHead(404);
res.end("Index not generated");
});
}
// Server private index path
function handlePrivIndex(options, req, res) {
let auth, authStr, authUser, authPwd;
auth = req.headers["authorization"];
if (auth && auth.startsWith("Basic ")) {
authStr = Buffer.from(auth.substring(6), "base64").toString("ascii");
authUser = authStr.substring(0, authStr.indexOf(":"));
authPwd = authStr.substring(authStr.indexOf(":") + 1, authStr.length);
if (authUser == options.user && authPwd == options.password) {
console.log(`Private index request from: ${req.connection.remoteAddress}`);
fs.readFile(path.join("public", "priv.html"))
.then((data) => {
res.writeHead(200);
res.end(data);
});
// Must exit here or we'll get password prompt again.
return;
}
}
res.setHeader("WWW-Authenticate", `Basic realm="Blarg"`);
res.writeHead(401, "Access Denied");
res.end();
}
function handlePostsReq(req, res) {
let url = URL.parse(req.url);
let fpath = url.pathname.substring(1);
console.log(`Static post request of ${fpath} from: ${req.connection.remoteAddress}`);
// Skip the first / in the pathname
fs.readFile(fpath)
.then((data) => {
res.writeHead(200);
res.end(data);
})
.catch((err) => {
res.writeHead(404);
res.end(`No file at path ${fpath}`);
});
}
function handleKeyReq(req, res) {
console.log(`Key request from: ${req.connection.remoteAddress}`);
fs.readFile(path.join("data", "key.gpg"))
.then((data) => {
res.writeHead(200);
res.end(data);
})
.catch((err) => {
res.writeHead(404);
res.end("No key.gpg file");
});
}
function handleStyleReq(req, res) {
console.log(`Style request from: ${req.connection.remoteAddress}`);
fs.readFile("style.css")
.then((data) => {
res.writeHead(200);
res.end(data);
})
.catch((err) => {
res.writeHead(404);
res.end("No style.css file");
});
}
function handleLogoReq(req, res) {
console.log(`Logo request from: ${req.connection.remoteAddress}`);
fs.readFile("logo.png")
.then((data) => {
res.writeHead(200);
res.end(data);
})
.catch((err) => {
res.writeHead(404);
res.end("No logo.png file");
});
}
// This code forwards HTTP requests to the Git repository.
// Heavily inspired by:
// https://github.com/TooTallNate/node-cgi/blob/master/cgi.js
// https://github.com/fgnass/gateway/blob/master/gateway.js
// but we want NO dependencies for now.
function handleGitReq(req, res) {
console.log(`Git request: ${req.connection.remoteAddress}`);
let statusExp = /^Status:\s*(\d{3}) (.*)$/i;
let url = URL.parse(req.url),
originalUrl = req.originalUrl ? URL.parse(req.originalUrl) : url;
let host = (req.headers.host || '').split(':');
let env = {
PATH: process.env.PATH,
GATEWAY_INTERFACE: 'CGI/1.1',
SERVER_PROTOCOL: 'HTTP/1.1',
SERVER_ROOT: process.cwd(),
DOCUMENT_ROOT: process.cwd(),
SERVER_NAME: host[0],
SERVER_PORT: host[1] || 80,
REDIRECT_STATUS: 200,
SCRIPT_NAME: originalUrl.pathname,
REQUEST_URI: originalUrl.path,
REQUEST_METHOD: req.method,
QUERY_STRING: url.query || '',
REMOTE_ADDR: req.connection.remoteAddress,
GIT_PROJECT_ROOT: path.join(process.cwd()),
PATH_INFO: url.pathname.replace("git", ".git")
};
for (let header in req.headers) {
let name = 'HTTP_' + header.toUpperCase().replace(/-/g, '_');
env[name] = req.headers[header];
}
if ('content-length' in req.headers)
env.CONTENT_LENGTH = req.headers['content-length'];
if ('content-type' in req.headers)
env.CONTENT_TYPE = req.headers['content-type'];
let body,
line = [],
statusCode,
reason,
exit,
end;
function error(code, reason, headers) {
res.writeHead(code || 500, reason, headers);
res.end();
}
function done() {
if (exit === undefined || !end) return
if (exit && !body) error(500, handler + ' exited with code ' + exit);
else res.end();
}
let child = spawn("/usr/bin/git", [
"http-backend",
], {
env: env,
stdio: ['pipe', 'pipe', process.stderr]
}).on('error', (err) => {
exit = -1;
done();
}).on('exit', (code) => {
exit = code;
done();
});
child.stdout.on('end', () => {
end = true;
done();
}).on('data', (buf) => {
if (body) return res.write(buf);
for (let i = 0; i < buf.length; i++) {
let c = buf[i];
if (c == 0xA) {
if (!line.length) {
body = true;
res.writeHead(statusCode || 200, reason);
res.write(buf.slice(i+1));
return;
}
let s = line.join('');
line = [];
if (!statusCode) {
let m = statusExp.exec(s);
if (m) {
statusCode = m[1];
reason = m[2];
continue;
}
}
let idx = s.indexOf(':');
res.setHeader(s.slice(0, idx), s.slice(idx+1).trim());
}
else if (c != 0xD)
line.push(String.fromCharCode(c));
}
});
req.pipe(child.stdin);
req.resume();
}
readOptions().then((options) => {
const server = http.createServer((req, res) => {
let url = URL.parse(req.url);
try {
// Index: public view of the site.
if (url.pathname == "/") handleIndex(req, res);
else if (url.pathname == "/logo") handleLogoReq(req, res);
// Style: local stylesheet
else if (url.pathname == "/style") handleStyleReq(req, res);
// Private section
else if (url.pathname == "/priv") handlePrivIndex(options, req, res);
// Allow to pull some static from posts.
else if (url.pathname.startsWith("/posts")) handlePostsReq(req, res);
// Handle git HTTP CGI
else if (url.pathname.startsWith("/git")) handleGitReq(req, res);
// Add an URL to the list of followers.
else if (url.pathname == "/follow") handleFollowReq(options, req, res);
// Remove an URL from the list of followers
else if (url.pathname == "/unfollow") handleUnfollowReq(req, res);
// Ping, technically from one of the servers we follow.
else if (url.pathname == "/ping") handlePingReq(req, res);
// Key lookup.
else if (url.pathname == "/key") handleKeyReq(req, res);
else {
res.writeHead(404);
res.end(`Unable to handle request to ${req.url}\n`);
}
} catch (ex) {
console.log(ex);
res.writeHead(500);
res.end(ex);
}
});
checkFollowing();
// Watch the 'following' file for changes, if it exists.
fs.access("following")
.then(() => {
fsL.watchFile("following", () => {
checkFollowing();
});
})
.catch(() => {
// Nevermind.
});
console.log(`Listening on ${options.hostname}:${options.port}`);
server.listen(options.port, options.hostname);
});