-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
1238 lines (1155 loc) · 44.1 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
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*@flow*/
/* global BigInt */
const Fs = require('fs');
const vpnfs = require('fs').promises;
const Http = require('http');
const Https = require('https');
const Crypto = require('crypto');
const IpAddr = require('ipaddr.js');
const Cjdns = require('cjdnsadmin');
const nThen = require('nthen');
const axios = require('axios');
const { exec } = require('child_process');
const { execSync } = require('child_process');
const lockfile = require('proper-lockfile');
const path = require('path');
const httpProxy = require('http-proxy');
const forbidden_vpn_ports = [22];
/*::
const BigInt = (n:number|string)=>Number(n);
type NetConfig_t = {
allocSize: number,
networkSize: number,
prefix: string,
}
type Config_t = {
cfg6: void | NetConfig_t,
cfg4: void | NetConfig_t,
serverPort: number,
dryrun: bool,
};
import type { IncomingMessage, ServerResponse } from 'http';
type ComputedConfig_t = {
slots4: number,
slots6: number,
baseAddr6: number,
baseAddr4: number,
};
type Lease_t = {
s4: number,
s6: number,
to: number, // timeout milliseconds
};
type Db_t = {
leases: {[string]:Lease_t},
slotmap4: {[number]:number},
slotmap6: {[number]:number},
}
type Context_t = {
cfg: Config_t,
db: Db_t,
cc: ComputedConfig_t,
mut: {
externalConfigs: {[string]:number},
lastSync: number,
sessions: { [string]:{ conn: number } },
cjdns: ?any,
coordinatorPubkey: string,
}
};
type CjdnsConn_t = {
"error": string,
"ip4Address": string,
"ip4Alloc": number,
"ip4Prefix": number,
"ip6Address": string,
"ip6Alloc": number,
"ip6Prefix": number,
"key": string,
"outgoing": number,
"txid": string,
// hacks
s4?: number,
s6?: number,
number: number,
};
type Session_t = {
ctx: Context_t,
req: IncomingMessage,
res: ServerResponse,
timeout: TimeoutID,
};
type Error_t = {
code: number,
error: string,
};
*/
const Config /*:Config_t*/ = require('./config.js');
const complete = (sess /*:Session_t*/, code /*:number*/, error /*:string|null*/, data) => {
sess.res.setHeader('Content-Type', 'application/json');
if (error) {
console.error(`Request error ${code} - ${error}`);
sess.res.statusCode = code;
sess.res.end(JSON.stringify({
status: "error",
message: error,
}, null, '\t'));
} else {
const s = JSON.stringify(data, (_, x) => {
// $FlowFixMe - new fancy js stuff
if (typeof x !== 'bigint') { return x; }
return x.toString();
}, '\t');
if (sess.req.url !== '/healthcheck') {
console.error(`Request result ${code} - ${String(s)}`);
}
sess.res.statusCode = code;
sess.res.end(s);
}
clearTimeout(sess.timeout);
};
const MARK128 = BigInt(2)**BigInt(132);
const MASK128 = BigInt(2)**BigInt(128)-BigInt(1);
const MARK32 = BigInt(2)**BigInt(36);
const MASK32 = BigInt(0xffffffff);
const alloc4 = (ctx) => (ctx.cfg.cfg4 && ctx.cfg.cfg4.allocSize) || 32;
const alloc6 = (ctx) => (ctx.cfg.cfg6 && ctx.cfg.cfg6.allocSize) || 128;
const addrForSlot6 = (ctx, slot) => {
return IpAddr.fromByteArray(
Buffer.from(
( ctx.cc.baseAddr6 + (BigInt(slot) << BigInt(128 - alloc6(ctx))) + MARK128).toString(16),
'hex'
).slice(1)
).toString();
};
const addrForSlot4 = (ctx, slot) => {
return IpAddr.fromByteArray(
Buffer.from(
( ctx.cc.baseAddr4 + (BigInt(slot) << BigInt(32 - alloc4(ctx))) + MARK32 ).toString(16),
'hex'
).slice(1)
).toString();
};
const slotForAddr4 = (ctx, addrStr) => {
const addr = BigInt('0x' + Buffer.from(IpAddr.parse(addrStr).toByteArray()).toString('hex'));
return Number( (addr - ctx.cc.baseAddr4) >> BigInt(32 - alloc4(ctx)) );
};
const slotForAddr6 = (ctx, addrStr) => {
const addr = BigInt('0x' + Buffer.from(IpAddr.parse(addrStr).toByteArray()).toString('hex'));
return Number( (addr - ctx.cc.baseAddr6) >> BigInt(128 - alloc6(ctx)) );
};
const addLease = (ctx, pubkey, lease, then) => {
if (!ctx.mut.cjdns) { return; }
const cjdns = ctx.mut.cjdns;
let ip6Prefix, ip6Alloc, ip6Address, ip4Prefix, ip4Alloc, ip4Address;
if (lease.s4 > -1 && ctx.cfg.cfg4) {
ip4Prefix = ctx.cfg.cfg4.networkSize;
ip4Alloc = ctx.cfg.cfg4.allocSize;
ip4Address = addrForSlot4(ctx, lease.s4);
}
if (lease.s6 > -1 && ctx.cfg.cfg6) {
ip6Prefix = ctx.cfg.cfg6.networkSize;
ip6Alloc = ctx.cfg.cfg6.allocSize;
ip6Address = addrForSlot6(ctx, lease.s6);
}
console.error('addLease() IpTunnel_allowConnection',
pubkey, ip6Prefix, ip6Alloc, ip6Address, ip4Prefix, ip4Alloc, ip4Address);
if (ctx.cfg.dryrun) {
return void then();
}
cjdns.IpTunnel_allowConnection(
pubkey, ip6Prefix, ip6Alloc, ip6Address, ip4Prefix, ip4Alloc, ip4Address, (err, res) => {
if (err) {
console.error('addLease() IpTunnel_allowConnection',
pubkey, ip6Prefix, ip6Alloc, ip6Address, ip4Prefix, ip4Alloc, ip4Address, '->', err);
then(err);
} else if (res.error !== "none") {
console.error('addLease() IpTunnel_allowConnection',
pubkey, ip6Prefix, ip6Alloc, ip6Address, ip4Prefix, ip4Alloc, ip4Address, '->', res);
then(res);
} else {
ctx.mut.sessions[pubkey] = { conn: res.connection };
then();
}
});
};
const now = () => +new Date();
const DAY_MS = 1000*60*60*24;
const syncSessions = (ctx, done) => {
console.error('syncSessions() start');
const fail = (w, err) => {
w.abort();
console.error("syncSessions() failed, trying in 3 seconds", err);
setTimeout(()=>syncSessions(ctx, done), 3000);
};
let numbers;
const connections /*:{[string]:CjdnsConn_t}*/ = {};
nThen((w) => {
if (!ctx.mut.cjdns) { return void fail(w, "cjdns missing"); }
ctx.mut.cjdns.IpTunnel_listConnections(w((err, ret) => {
if (err) { return void fail(w, err); }
if (ret.error !== 'none') { return void fail(w, "cjdns replied " + ret.error); }
numbers = ret.connections.map(Number);
}));
}).nThen((w) => {
console.error(`syncSessions() Getting [${numbers.length}] connections...`);
let nt = nThen;
numbers.forEach((n) => {
nt = nt((w) => {
if (!ctx.mut.cjdns) { return void fail(w, "cjdns missing"); }
ctx.mut.cjdns.IpTunnel_showConnection(n, w((err, ret) => {
if (err) { return void fail(w, err); }
if (ret.error !== 'none') { return void fail(w, "cjdns replied " + ret.error); }
if (ret.outgoing === 1) {
throw new Error("Cannot run a VPN server because this node is a VPN client " +
"with lease: " + JSON.stringify(ret, null, '\t'));
}
ret.number = n;
connections[n] = ret;
ctx.mut.sessions[ret.key] = { conn: n };
}));
}).nThen;
});
nt(w());
}).nThen((w) => {
const connByKey = {};
const externalConfigs = {};
for (const num in connections) {
const conn = connections[num];
connByKey[conn.key] = conn;
const lease = ctx.db.leases[conn.key];
if (conn.ip4Address) {
conn.s4 = slotForAddr4(ctx, conn.ip4Address);
// make sure we mark off these slots as used, even if they're allocated externally
if (conn.s4 > -1 && conn.s4 < ctx.cc.slots4) {
ctx.db.slotmap4[conn.s4] = 1;
}
}
if (conn.ip6Address) {
conn.s6 = slotForAddr6(ctx, conn.ip6Address);
if (conn.s6 > -1 && conn.s6 < ctx.cc.slots6) {
ctx.db.slotmap6[conn.s6] = 1;
}
}
if (!lease) {
console.error(`syncSessions() external ${conn.key} ${conn.ip4Address} ${conn.ip6Address} ${num}`);
externalConfigs[conn.key] = 1;
}
}
ctx.mut.externalConfigs = externalConfigs;
const leasesNeeded = {};
for (const key in ctx.db.leases) {
const conn = connByKey[key];
if (conn) {
const lease = ctx.db.leases[key];
console.error(`syncSessions() detected lease for ${key} at connection num ${conn.number}`);
if (conn.ip4Address && ctx.cfg.cfg4) {
if (conn.ip4Alloc !== ctx.cfg.cfg4.allocSize) {
console.error(conn, ctx.cfg.cfg4.allocSize);
throw new Error("Entry exists with cjdns which has a different alloc size");
}
if (lease.s4 !== conn.s4) {
const oldAddr = addrForSlot4(ctx, lease.s4);
// Always trust cjdns rather than the db because cjdns is what's in practice
console.error(`syncSessions() Warning: change of address for ` +
`[${conn.key}] [${oldAddr}] -> [${conn.ip4Address}] [${conn.s4}] -> [${lease.s4}]`);
delete ctx.db.slotmap4[lease.s4];
lease.s4 = conn.s4;
}
}
if (conn.ip6Address && ctx.cfg.cfg6) {
if (conn.ip6Alloc !== ctx.cfg.cfg6.allocSize) {
console.error(conn, ctx.cfg.cfg6.allocSize);
throw new Error("Entry exists with cjdns which has a different alloc size");
}
if (lease.s6 !== conn.s6) {
const oldAddr = addrForSlot6(ctx, lease.s6);
console.error(`syncSessions() Warning: change of address for ` +
`[${conn.key}] [${oldAddr}] -> [${conn.ip6Address}] [${conn.s6}] -> [${lease.s6}]`);
delete ctx.db.slotmap6[lease.s6];
lease.s6 = conn.s6;
}
}
} else {
console.error(`syncSessions() need lease for ${key}`);
leasesNeeded[key] = ctx.db.leases[key];
}
}
let nt = nThen;
Object.keys(leasesNeeded).forEach((pubkey) => {
nt = nt((w) => {
if (!ctx.mut.cjdns) { return void fail(w, "cjdns missing"); }
addLease(ctx, pubkey, leasesNeeded[pubkey], w());
}).nThen;
});
nt(w());
}).nThen((_) => {
console.error('syncSessions() complete');
ctx.mut.lastSync = now();
done();
});
};
// check if cjdns is up and running
const checkcjdns = (ctx, attemptNum, done) => {
if (!ctx.mut.cjdns) {
Cjdns.connect((err, cjdns) => {
if (err) {
console.error('checkcjdns()', err);
setTimeout(() => checkcjdns(ctx, 0, done), 2000);
} else {
console.error('checkcjdns() cjdns connection established');
ctx.mut.cjdns = cjdns;
// We need a sync to occur asap
ctx.mut.lastSync = 0;
setTimeout(() => checkcjdns(ctx, 0, done), 100);
}
});
return;
}
ctx.mut.cjdns.ping((err, ret) => {
if (!err && ret.q === 'pong') {
done();
} else if (attemptNum > 5) {
console.error('checkcjdns() cjdns connection lost');
ctx.mut.cjdns = undefined;
ctx.mut.sessions = {};
checkcjdns(ctx, attemptNum + 1, done);
} else {
console.error('checkcjdns() no connection, retrying');
setTimeout(() => checkcjdns(ctx, attemptNum + 1, done), 100);
}
});
};
const withCjdns = (sess, cb) => {
if (sess.ctx.mut.cjdns) {
cb(sess.ctx.mut.cjdns);
} else {
let i = 0;
const again = () => {
if (sess.ctx.mut.cjdns) {
cb(sess.ctx.mut.cjdns);
} else if (i < 5) {
i++;
setTimeout(again, 5000);
} else {
complete(sess, 500, "cjdns is not running");
}
};
setTimeout(again, 1000);
}
};
const readJson = (sess, then) => {
const data = [];
sess.req.on('data', (d) => data.push(d));
sess.req.on('end', () => {
let str = '';
if (Buffer.isBuffer(typeof(data[0]))) {
str = Buffer.concat(data).toString('utf8');
} else {
str = data.join('');
}
let o;
try {
o = JSON.parse(str);
} catch (e) {
return void complete(sess, 405, "could not parse json");
}
const auth = sess.req.headers['authorization'];
if (auth && auth.indexOf('cjdns ') === 0) {
const sig = auth.slice(6);
const hash = Crypto.createHash('sha256').update(Buffer.from(str, 'utf8')).digest('base64');
withCjdns(sess, (cjdns) => {
cjdns.Sign_checkSig(sig, hash, (err, ret) => {
if (err) {
return void complete(sess, 500, "Sign_checksig error " + err);
} else if (ret.error === 'invalid signature') {
return void complete(sess, 403, "Sign_checksig invalid sig");
} else if (ret.error !== 'none') {
return void complete(sess, 500, "Sign_checksig error " + ret.error);
} else {
then(o, ret);
}
});
});
} else {
then(o);
}
});
};
const needAuth = (sess, cb) => {
return (o, sig) => {
if (!sig) {
return void complete(sess, 403, "cjdns http signature required");
} else if (!o.date || typeof(o.date) !== 'number') {
return void complete(sess, 405, "date field required and must be a number");
} else if (now() - +new Date(o.date*1000) > DAY_MS) {
return void complete(sess, 405, "date is more than 1 day old");
} else {
cb(o, sig);
}
};
};
const storeDb = (ctx, then) => {
Fs.writeFile('./db.json', JSON.stringify(ctx.db, null, '\t'), 'utf8', (err) => {
if (err) { throw err; }
then();
});
};
const allocate = (sess, pubkey) => {
let s4 = -1;
if (sess.ctx.cc.slots4) {
if (Object.keys(sess.ctx.db.slotmap4).length >= sess.ctx.cc.slots4) {
return void complete(sess, 503, `IPv4 addresses exhausted`);
}
s4 = Math.floor(Math.random() * sess.ctx.cc.slots4);
while (sess.ctx.db.slotmap4[s4]) { s4 = (s4 + 1) % sess.ctx.cc.slots4; }
sess.ctx.db.slotmap4[s4] = 1;
}
let s6 = -1;
if (sess.ctx.cc.slots6) {
if (Object.keys(sess.ctx.db.slotmap6).length >= sess.ctx.cc.slots6) {
return void complete(sess, 503, `IPv6 addresses exhausted`);
}
s6 = Math.floor(Math.random() * sess.ctx.cc.slots6);
while (sess.ctx.db.slotmap6[s6]) { s6 = (s6 + 1) % sess.ctx.cc.slots6; }
sess.ctx.db.slotmap6[s6] = 1;
}
const lease = sess.ctx.db.leases[pubkey] = { s4, s6, to: now() + DAY_MS };
storeDb(sess.ctx, () => {
addLease(sess.ctx, pubkey, lease, (err) => {
if (err) {
return void complete(sess, 500, `Failed to add lease ${String(err)}`);
} else {
return void complete(sess, 201, null, {
status: "success",
message: "allocated",
expiresAt: Math.floor(lease.to / 1000),
});
}
});
});
};
const PUBKEY = 'clientPublicKey';
const httpRequestAuth = (sess) => {
readJson(sess, needAuth(sess, (o, sig) => {
console.log("---- Authorization request ------");
console.log(`from cjdns pubkey: ${o[PUBKEY]}`);
const formattedDateTime = new Date(o.date).toLocaleString("en-US")
console.log(`at datetime: ${formattedDateTime}`)
if (!o[PUBKEY]) {
return void complete(sess, 405, "expecting a cjdns public key");
} else if (sig.pubkey !== o[PUBKEY] && sig.pubkey !== sess.ctx.mut.coordinatorPubkey) {
return void complete(sess, 403,
`request can only be made (signed) by either ` +
`client (${o[PUBKEY]}) or coordinator (${sess.ctx.mut.coordinatorPubkey}) (signed by ${sig.pubkey})`);
} else if (sess.ctx.mut.externalConfigs[o[PUBKEY]]) {
return void complete(sess, 400, `cannot grant a lease because one was manually added`);
} else {
const l = sess.ctx.db.leases[o[PUBKEY]];
console.error(`Request from ${o[PUBKEY]}`);
if (l) {
l.to = now() + DAY_MS;
storeDb(sess.ctx, () => {
return void complete(sess, 200, null, {
status: "success",
message: "updated timeout",
expiresAt: Math.floor(l.to / 1000),
});
});
} else {
console.error(`Allocating Request from ${o[PUBKEY]}`);
allocate(sess, o[PUBKEY]);
}
}
}));
};
const httpRequestPremium = (sess) => {
console.log("---- Premium request ------");
const { req, res } = sess;
if (req.method !== 'POST') {
return void complete(sess, 400, "Bad Request");
}
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
try {
const request = JSON.parse(body);
if (!request.ip) {
return void complete(sess, 400, "Missing 'ip' property");
}
if (!request.address) {
return void complete(sess, 400, "Missing 'address' property");
}
console.log(`from ip: ${request.ip}`);
// Give Premium regardless of bcasttransaction success
// The handler will drop client after a few minutes if the transaction is not confirmed
givePremium(sess, request.ip);
// Update clients file
let clientFile = path.resolve(__dirname,"./clients.json");
Fs.readFile(clientFile, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
let parsedData;
try {
parsedData = JSON.parse(data);
let clients = parsedData.clients;
let ipExists = false;
const currentTime = Date.now();
for (let i = 0; i < clients.length; i++) {
if (clients[i].ip === request.ip) {
console.log(`Overwriting existing client information`);
ipExists = true;
clients[i].duration = 1;
clients[i].time = currentTime;
clients[i].transaction = request.transaction;
clients[i].txid = ""; // Let premium_handler handle the broadcast transaction, by leaving this empty
clients[i].address = request.address;
break;
}
}
if (!ipExists) {
console.log(`Appending new client information`);
var newClient = { ip: request.ip, duration: 1, time: currentTime, transaction: request.transaction, address: request.address, txid: "" };
clients.push(newClient);
}
// Write the updated data back to json
parsedData.clients = clients;
lockfile.lock(clientFile)
.then(() => {
Fs.writeFile(clientFile, JSON.stringify(parsedData), 'utf8', (err) => {
if (err) {
console.error(err);
}
console.log(`Updated ${clientFile}`);
});
return lockfile.unlock(clientFile);
});
} catch (error) {
console.log('Error parsing JSON:', error);
}
});
} catch (error) {
console.error(`Error parsing JSON: ${error}`);
return void complete(sess, 400, "Invalid JSON");
}
});
};
const givePremium = (sess, ip) => {
const envVars = {
PKTEER_IP: ip,
PKTEER_PAID: 'true'
};
const envVarString = Object.entries(envVars).map(([key, value]) => `${key}=${value}`).join(' ');
//-e "PKTEER_DURATION=$2" -e "PKTEER_CONN_TIME=$3"
const command = `/server/monitor_cjdns.sh`;
exec(`${envVarString} ${command}`, (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
// Handle the output of the handle_premium.js script if needed
console.log(stdout);
console.error(stderr);
return void complete(sess, 200, null,
{
status: "success",
message: "Premium VPN granted"
});
});
}
const httpRequestPremiumAddress = (sess) => {
console.log("---- Premium Address request ------");
const { req, res } = sess;
axios.post('http://localhost:8080/api/v1/wallet/address/create', {}, {
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
console.log('Response:', response.data);
const modifiedResponse = {
...response.data,
amount: parseInt(process.env.PKTEER_PREMIUM_PRICE, 10)
};
return void complete(sess, 200, null, modifiedResponse);
})
.catch(error => {
console.error('Error:', error.message);
return void complete(sess, 500, error.message);
});
};
const httpRequestReverseVPN = (sess) => {
console.log("---- Reverse VPN request ------");
const { req, res } = sess;
if (req.method !== 'POST') {
return void complete(sess, 400, "Bad Request");
}
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
try {
const request = JSON.parse(body);
if (!request.port) {
console.log('Missing port property');
return void complete(sess, 400, "Missing 'port' property");
}
if (!request.ip) {
console.log('Missing ip property');
return void complete(sess, 400, "Missing 'ip' property");
}
console.log(`Client IP: ${request.ip} requesting port: ${request.port}`);
setReverseVPN(sess, request.ip, request.port);
} catch (error) {
console.error(`Error parsing JSON: ${error}`);
return void complete(sess, 400, "Invalid JSON");
}
});
};
const setReverseVPN = (sess, ip, port) => {
//Check if port is forbidden
if (forbidden_vpn_ports.includes(port)) {
console.error(`Port ${port} is forbidden`);
return;
}
//Check if port is already allocated
exec(`netstat -tuln | grep :${port} || true`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return void complete(sess, 500, "Failed to check ports");
}
if (stdout) {
console.error(`Port ${port} is already allocated`);
return void complete(sess, 500, "Port "+port+" is already allocated");
}
//Add port to nftables
exec(`nft add element ip pfi s_reverse_ports { ${port} }`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return void complete(sess, 500, "Failed to allocate port");
}
});
exec(`nft add element ip pfi m_reverse_ports { ${port} : ${ip} }`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return void complete(sess, 500, "Failed to allocate port");
}
});
return void complete(sess, 200, null, {
status: "success",
message: "Port "+port+" allocated for "+ip
});
});
};
const httpsGet = (url) => {
return new Promise((resolve, reject) => {
Https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(JSON.parse(data));
});
}).on('error', (err) => {
reject(err);
});
});
};
async function addVpnClient(username) {
console.log(`Generating p12, sswan and mobileconfig files for ${username}`);
execSync(`/usr/bin/ikev2.sh --addclient ${username}`, (err, stdout, stderr) => {
if (err) {
console.error(err);
}
});
if (Fs.existsSync(`/server/createOpenVpnClient.sh`)) {
console.log(`Generating ovpn file for ${username}`);
execSync(`/server/createOpenVpnClient.sh ${username}`, (err, stdout, stderr) => {
if (err) {
console.error(err);
}
});
}
console.log(`Copying files to /server/vpnclients`);
vpnfs.copyFile(`/root/${username}.p12`, `/server/vpnclients/${username}.p12`);
vpnfs.copyFile(`/root/${username}.sswan`, `/server/vpnclients/${username}.sswan`);
vpnfs.copyFile(`/root/${username}.mobileconfig`, `/server/vpnclients/${username}.mobileconfig`);
}
async function isValidPaymentTxid(txid,pktAdress,acceptedAmount) {
let validPayment = false;
const explorerurl = `https://api.packetscan.io/api/v1/PKT/pkt/tx/${txid}`;
try {
const parsedData = await httpsGet(explorerurl);
if (parsedData.output) {
const outputArray = parsedData.output;
for (let i = 0; i < outputArray.length; i++) {
if (outputArray[i].address === pktAddress && parseInt(outputArray[i].value) >= acceptedAmount) {
console.log(`Transaction ${txid} is valid`);
validPayment = true;
break;
} else if (outputArray[i].address !== pktAddress) {
errormsg = `Transaction not for the correct PKT address. Should be for ${pktAddress}`;
} else if (parseInt(outputArray[i].value) < acceptedAmount) {
errormsg = `Transaction ${txid} is less than required 100 PKT`
}
}
}
} catch (err) {
console.error("Error: " + err.message);
}
return validPayment;
}
async function isValidPaymentAddress(address,requiredAmount) {
let validPayment = false;
const explorerurl = `https://api.packetscan.io/api/v1/PKT/pkt/address/${address}`
try {
const parsedData = await httpsGet(explorerurl);
if ((parsedData.balance) && parseInt(parsedData.balance) >= requiredAmount) {
console.log(`Address ${address} has a valid balance of ${parsedData.balance} PKT`);
validPayment = true;
} else if (parseInt(parsedData.balance) < requiredAmount) {
errormsg = `Address ${address} has less than ${requiredAmount} PKT`
console.error(errormsg);
}
} catch (err) {
console.error("Error: " + err.message);
}
return validPayment;
}
async function updateVPNClientFile(paymentAddress, username) {
let clientFile = path.resolve(__dirname,"./vpnclients.json");
console.log(`Updating VPN clients file: ${clientFile}`);
try {
const data = await vpnfs.readFile(clientFile, 'utf8');
let parsedData = JSON.parse(data);
let vpnclients = parsedData.clients || [];
const currentTime = Date.now();
vpnclients.push({
address: paymentAddress,
username: username,
timeCreated: currentTime
});
parsedData.clients = vpnclients;
await lockfile.lock(clientFile);
await vpnfs.writeFile(clientFile, JSON.stringify(parsedData, null, 2), 'utf8');
console.log(`Updated ${clientFile}`);
} catch (error) {
console.error(`Error updating VPN clients file: ${error}`);
} finally {
await lockfile.unlock(clientFile).catch(console.error);
}
}
const httpRequestVPNAccess = (sess) => {
console.log("---- Request VPN Access ------");
const { req, res } = sess;
if (req.method !== 'POST') {
return void complete(sess, 400, "Bad Request");
}
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', async () => {
let paymentAddress = "";
try {
const request = JSON.parse(body);
paymentAddress = request.address;
if (!request.address) {
return void complete(sess, 400, "Missing 'address' property");
}
} catch (error) {
console.error(`Error: ${error}`);
return void complete(sess, 400, error);
}
// Check against existing vpn clients
let addressExists = false;
let clientFile = path.resolve(__dirname,"./vpnclients.json");
try {
const clientsdata = await vpnfs.readFile(clientFile, 'utf8');
const parsedData = JSON.parse(clientsdata);
let vpnclients = parsedData.clients;
const currentTime = Date.now();
if (Array.isArray(vpnclients)) {
for (let i = 0; i < vpnclients.length; i++) {
if (vpnclients[i].address === paymentAddress) {
addressExists = true;
console.log(`Transaction has been already processed`);
filesMsg = "Transaction has been already processed, you can access your files at /vpnclients/"+vpnclients[i].username+".p12 /vpnclients/"+vpnclients[i].username+".sswan /vpnclients/"+vpnclients[i].username+".mobileconfig";
if (Fs.existsSync("/server/vpnclients/"+vpnclients[i].username+".ovpn")) {
filesMsg += " /vpnclients/"+vpnclients[i].username+".ovpn";
} else {
console.log(`ovpn file does not exist`);
}
return void complete(sess, 200, null, {
status: "success",
message: filesMsg,
});
}
}
}
} catch (error) {
console.log('Error parsing JSON:', error);
}
var requiredAmount = 100*1073741824; // 100 PKT
var validPayment = false;
let errormsg = "";
//Check blockchain for address - valid payment
validPayment = await isValidPaymentAddress(paymentAddress,requiredAmount);
if (!validPayment) {
console.error(errormsg);
return void complete(sess, 500, errormsg);
}
//Generate vpn client and files, using ikev2.sh
var usernameLength = 8;
var username = Crypto.randomBytes(usernameLength).toString('hex').slice(0, usernameLength);
await addVpnClient(username);
// Update VPN clients file
await updateVPNClientFile(paymentAddress,username);
filesMsg = `Get your vpnclient file at /vpnclients/${username}.p12 /vpnclients/${username}.sswan /vpnclients/${username}.mobileconfig`;
if (Fs.existsSync(`/server/vpnclients/${username}.ovpn`)) {
filesMsg += ` /vpnclients/${username}.ovpn`;
}
return void complete(sess, 200, null, {
status: "success",
message: filesMsg,
});
});
};
function isDomainPointingToIPv6(domain, ipv6) {
return new Promise((resolve, reject) => {
exec(`dig AAAA h.${domain} +short`, (error, stdout, stderr) => {
if (error) {
reject(`exec error: ${error}`);
return;
}
const outputIPv6 = stdout.trim();
resolve(outputIPv6 === ipv6);
});
});
}
const httpRequestRemoveDomain = (sess) => {
console.log("---- Remove Domain request ------");
const { req, res } = sess;
if (req.method !== 'POST') {
return void complete(sess, 400, "Bad Request");
}
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
try {
const { domain, cjdnsIpv6 } = JSON.parse(body);
const domainRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
const ipv6Regex = /^fc([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$/i;
if (!domainRegex.test(domain)) {
return void complete(sess, 400, "Invalid domain");
}
if (!ipv6Regex.test(cjdnsIpv6)) {
return void complete(sess, 400, "Invalid CJDNS IPv6 address");
}
exec(`/server/removedomain.sh ${domain} ${cjdnsIpv6}`,{ timeout: 5000 },(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
return void complete(sess, 200, null,
{
status: "success",
message: "Domain removed successfully"
});
});
} catch (error) {
console.error(`Error parsing JSON: ${error}`);
return void complete(sess, 400, "Invalid JSON");
}
});
}
const httpRequestAddDomain = (sess) => {
console.log("---- Add Domain request ------");
const { req, res } = sess;
if (req.method !== 'POST') {
return void complete(sess, 400, "Bad Request");
}
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
try {
const { domain, cjdnsIpv6 } = JSON.parse(body);
const domainRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
const ipv6Regex = /^fc([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$/i;
if (!domainRegex.test(domain)) {
return void complete(sess, 400, "Invalid domain");
}
if (!ipv6Regex.test(cjdnsIpv6)) {
return void complete(sess, 400, "Invalid CJDNS IPv6 address");
}
isDomainPointingToIPv6(domain, cjdnsIpv6).then(isValid => {
if(!isValid) {
return void complete(sess, 400, "Domain is not pointing to the given IPv6 address");
}
exec(`/server/adddomain.sh ${domain} ${cjdnsIpv6}`,{ timeout: 5000 },(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
return void complete(sess, 200, null,
{
status: "success",
message: "Domain added successfully"
});
});
}).catch(err => {
console.error(`Error checking domain: ${err}`);
return void complete(sess, 400, "Error checking domain");
});
} catch (error) {
console.error(`Error parsing JSON: ${error}`);
return void complete(sess, 400, "Invalid JSON");
}
});
}
const httpRequestStatus = (sess) => {
exec('/data/status.sh', (error, stdout, stderr) => {
if (error) {
console.error(`Error executing script: ${error}`);
return void complete(sess, 500, "Error in getting status");
}