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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
|
var iface = 'wg0';
function parse_placeholder(val) {
return val;
}
function parse_boolean(val) {
if (val === 'true')
return true;
if (val === 'false')
return false;
throw new Error('Cannot parse as a boolean.');
}
function parse_boolean_or_string(val) {
if (typeof val == 'boolean') {
return val;
}
return parse_boolean(val);
}
function try_parse_boolean_or_string(val) {
try {
return parse_boolean_or_string(val);
} catch (err) {
return null;
}
}
function parse_positive_integer(src) {
var val = parseInt(src);
if (isNaN(val) || val < 1)
throw new Error('Cannot parse as a positive integer.');
return val;
}
function parse_keepalive(src) {
return parse_positive_integer(src);
}
function parse_tunnel_everything(src) {
return parse_boolean_or_string(src);
}
function parse_endpoint(val) {
val = val.trim();
if (val.length == 0)
throw new Error('Server endpoint cannot be an empty string.');
if (!val.match(/^.+:[0-9]+$/))
throw new Error('Please specify a host and a port in the HOST:PORT format.');
return val;
}
function parse_key(val) {
val = val.trim();
if (val.length == 0)
throw new Error('Key as used by WireGuard cannot be an empty string.');
if (!val.match(/^[0-9a-zA-Z+/=]+$/))
throw new Error('Key as used by WireGuard must be Base64-encoded.');
return val;
}
var ip_address_lib = require('ip-address');
function parse_ip_internal(src, parser) {
src = src.trim();
if (src.length == 0)
throw new Error('IP address cannot be an empty string.');
try {
var result = new parser(src);
} catch (err) {
throw new Error('Sorry, IP address validation failed with the following error:\n' + err.message);
}
return result;
}
function parse_ip(src, parser) {
var result = parse_ip_internal(src, parser);
if (result.parsedSubnet)
throw new Error('Please specify address without the netmask.')
return result.correctForm();
}
function parse_ip_cidr(src, parser) {
var result = parse_ip_internal(src, parser);
if (!result.parsedSubnet)
throw new Error('Please specify address using the CIDR format (including the netmask).');
var addr = result.correctForm();
var netmask = result.subnetMask;
var network_id = result.startAddress().correctForm();
return [addr, netmask, network_id];
}
function parse_ipv4(src) {
var result = parse_ip(src, ip_address_lib.Address4);
return result;
}
function parse_ipv4_cidr(src) {
var parser = ip_address_lib.Address4;
var result = parse_ip_cidr(src, ip_address_lib.Address4);
var addr = result[0];
var netmask = result[1];
var network_id = result[2];
var broadcast_addr = new parser(src).endAddress().correctForm();
if (addr == network_id && netmask != 32)
throw new Error('Please use a real host IP address, not a network identifier.');
if (addr == broadcast_addr && netmask != 32)
throw new Error('Please use a real host IP address, not a broadcast address.');
return result;
}
function parse_ipv6(val) {
return parse_ip(val, ip_address_lib.Address6);
}
function parse_ipv6_cidr(val) {
return parse_ip_cidr(val, ip_address_lib.Address6);
}
function parse_ip_list(val, addr_parser) {
var parts = val.split(/, */);
var result = [];
parts.forEach(function(part) {
result.push(addr_parser(part));
});
return result;
}
var Input = function(name) {
this.name = name;
if (this.is_checkbox()) {
// Keep the checked property and the value consistent.
$(this.input_id()).change(function(evt) {
var elem = evt.target;
elem.value = elem.checked;
});
}
}
Input.prototype.input_id = function() {
return '#param_' + this.name;
}
Input.prototype.error_id = function() {
return this.input_id() + '_error';
}
Input.prototype.container_id = function() {
return this.input_id() + '_container';
}
Input.prototype.is_checkbox = function() {
return $(this.input_id()).attr('type') == 'checkbox';
}
Input.prototype.value = function() {
return $(this.input_id()).val();
}
Input.prototype.has_value = function() {
return this.value().length > 0;
}
Input.prototype.set = function(value) {
if (this.is_checkbox() && try_parse_boolean_or_string(value))
$(this.input_id()).prop('checked', true);
$(this.input_id()).val(value);
}
Input.prototype.show = function() {
$(this.container_id()).show();
}
Input.prototype.hide = function() {
$(this.container_id()).hide();
}
Input.prototype.show_error = function(msg) {
this.show();
$(this.error_id()).text(msg);
$(this.error_id()).show();
}
Input.prototype.hide_error = function() {
$(this.error_id()).hide();
}
var Value = function(name, parser) {
this.name = name;
this.input = new Input(name);
this.parser = parser;
this.optional = false;
this.advanced = false;
this.value = null;
this.error = null;
}
Value.prototype.parse = function() {
return this.parse_from(this.input.value());
}
Value.prototype.parse_from = function(src) {
try {
var value = src;
switch (typeof value) {
case 'boolean':
value = this.parser(value);
break;
default:
if (this.optional && value.length == 0)
break;
value = this.parser(value);
break;
}
this.input.set(src);
this.set_value(value);
return true;
} catch (err) {
this.set_error(err.message);
return false;
}
}
Value.prototype.set_value = function(value) {
this.value = value;
this.error = null;
this.input.hide_error();
}
Value.prototype.set_error = function(msg) {
this.value = null;
this.error = msg;
this.input.show_error(this.error);
}
Value.prototype.is_set = function() {
return this.input.has_value();
}
Value.prototype.show = function() {
this.input.show();
}
Value.prototype.hide = function() {
this.input.hide();
}
var Endpoint = function(name) {
Value.call(this, name, parse_endpoint);
}
Endpoint.prototype = Object.create(Value.prototype);
Endpoint.prototype.constructor = Endpoint;
var Key = function(name) {
Value.call(this, name, parse_key);
}
Key.prototype = Object.create(Value.prototype);
Key.prototype.constructor = Key;
var IPAddr = function(name, parser) {
Value.call(this, name, parser);
}
IPAddr.prototype = Object.create(Value.prototype);
IPAddr.prototype.constructor = IPAddr;
IPAddr.prototype.set_value = function(value) {
Value.prototype.set_value.call(this, value);
this.addr = this.value[0];
this.netmask = this.value[1];
this.network_id = this.value[2];
}
IPAddr.prototype.full_address = function() {
return this.addr + '/' + this.netmask;
}
IPAddr.prototype.allowed_ips_client = function() {
// As of this writing, _every_ tool accepts something like 192.168.0.1/24
// here, _except_ for the Android app, which _requires_ the least
// significant bits to be zeroed out.
return this.network_id + '/' + this.netmask;
}
var IPv4 = function(name) {
IPAddr.call(this, name, parse_ipv4_cidr);
}
IPv4.prototype = Object.create(IPAddr.prototype);
IPv4.prototype.constructor = IPv4;
IPv4.prototype.allowed_ips_server = function() {
return this.addr + '/' + 32;
}
var IPv6 = function(name) {
IPAddr.call(this, name, parse_ipv6_cidr);
}
IPv6.prototype = Object.create(IPAddr.prototype);
IPv6.prototype.constructor = IPv6;
IPv6.prototype.allowed_ips_server = function() {
return this.addr + '/' + 128;
}
var Keepalive = function(name) {
Value.call(this, name, parse_keepalive);
this.optional = true;
this.advanced = true;
}
Keepalive.prototype = Object.create(Value.prototype);
Keepalive.prototype.constructor = Keepalive;
var TunnelEverything = function(name) {
Value.call(this, name, parse_tunnel_everything);
this.optional = true;
this.advanced = true;
}
TunnelEverything.prototype = Object.create(Value.prototype);
TunnelEverything.prototype.constructor = TunnelEverything;
var IPAddrList = function(name, parser) {
Value.call(this, name, function(val) {
return parse_ip_list(val, parser);
});
}
IPAddrList.prototype = Object.create(Value.prototype);
IPAddrList.prototype.constructor = IPAddrList;
var DNS_IPv4 = function(name) {
IPAddrList.call(this, name, parse_ipv4);
this.optional = true;
this.advanced = true;
}
DNS_IPv4.prototype = Object.create(IPAddrList.prototype);
DNS_IPv4.prototype.constructor = DNS_IPv4;
var DNS_IPv6 = function(name) {
IPAddrList.call(this, name, parse_ipv6);
this.optional = true;
this.advanced = true;
}
DNS_IPv6.prototype = Object.create(IPAddrList.prototype);
DNS_IPv6.prototype.constructor = DNS_IPv6;
var Data = function() {
this.server_public = new Key('server_public');
this.server_endpoint = new Endpoint('server_endpoint');
this.preshared = new Key('preshared');
this.client_public = new Key('client_public');
this.client_private = new Key('client_private');
this.client_ipv4 = new IPv4('client_ipv4');
this.client_ipv6 = new IPv6('client_ipv6');
this.keepalive = new Keepalive('keepalive');
this.tunnel_everything = new TunnelEverything('tunnel_everything');
this.dns_ipv4 = new DNS_IPv4('dns_ipv4');
this.dns_ipv6 = new DNS_IPv6('dns_ipv6');
this.values = [
this.server_public,
this.server_endpoint,
this.preshared,
this.client_public,
this.client_private,
this.client_ipv4,
this.client_ipv6,
this.keepalive,
this.tunnel_everything,
this.dns_ipv4,
this.dns_ipv6
];
}
Data.prototype.set_from_url = function(url) {
var url = new URL(url);
var params = new URLSearchParams(url.search);
var has = false;
this.values.forEach(function(value) {
if (params.has(value.name)) {
has = true;
value.input.set(params.get(value.name));
}
});
return has;
}
Data.prototype.set_from_this_url = function() {
return this.set_from_url(window.location.href);
};
Data.prototype.get_perma_url = function() {
var url = new URL(window.location.href);
url.search = '';
var params = new URLSearchParams();
this.values.forEach(function(value) {
params.append(value.name, value.input.value());
});
url.search = params.toString();
return url.toString();
}
Data.prototype.parse = function() {
var success = true;
this.values.forEach(function(value) {
if (!value.parse())
success = false;
});
if (success) {
if (!$('#advanced_params').prop('checked'))
this.hide_advanced();
this.hide_error();
} else {
this.show_error();
}
return success;
}
Data.prototype.show_error = function() {
$('#params_error').text('Please correct the input errors above first.');
$('#params_error').show();
}
Data.prototype.hide_error = function() {
$('#params_error').hide();
}
Data.prototype.show_advanced = function() {
this.values.forEach(function(value) {
if (!value.advanced)
return;
value.show();
});
}
Data.prototype.hide_advanced = function() {
this.values.forEach(function(value) {
if (!value.advanced)
return;
value.hide();
});
}
function edit_btn_init(btn) {
btn.empty();
btn.append('<span class="glyphicon glyphicon-pencil"/>');
}
function edit_btn_save(btn) {
btn.empty();
btn.append('<span class="glyphicon glyphicon-floppy-disk"/>');
}
function edit_btn_on_click(btn, pre) {
var editable = pre.prop('isContentEditable');
pre.prop('contentEditable', !editable);
if (editable) {
edit_btn_init(btn);
btn.blur(); // a.k.a. unfocus
} else {
edit_btn_save(btn);
pre.focus();
}
}
function dload_btn_init(btn) {
btn.empty();
btn.append('<span class="glyphicon glyphicon-download-alt"/>');
}
function basename(path) {
return path.substring(path.lastIndexOf('/') + 1);
}
function dload_btn_on_click(btn, path, pre) {
// The type must be application/octet-stream; if it's text/plain, the
// Android Chrome appends the .txt suffix to the downloaded file names.
var blob = new Blob([pre.text()], {type: 'application/octet-stream'});
var url = URL.createObjectURL(blob);
var link = $('<a/>');
link.prop('href', url);
link.prop('download', basename(path));
// Whoever thought of this [0] is fucking crazy:
// https://stackoverflow.com/a/36483380/514684
// It literally silently does nothing if this is omitted.
link[0].click();
}
function copy_to_clipboard(elem) {
// This is rather neat, thanks:
// https://stackoverflow.com/a/22581382/514684
// Apparently, you can only do execCommand('copy') from <input> and
// <textarea> elements. Another stupidity, I'm so surprised.
var is_input = elem.tagName === 'INPUT' || elem.tagName === 'TEXTAREA';
var orig_select_start, orig_select_end;
if (is_input) {
var target = elem;
orig_select_start = elem.selectionStart;
orig_select_end = elem.selectionEnd;
} else {
var target = document.createElement('textarea');
target.textContent = elem.textContent;
document.body.appendChild(target);
}
var current_focus = document.activeElement;
target.focus();
target.setSelectionRange(0, target.value.length);
var succeed;
try {
// Supposedly, this is deprecated. But the "proper" method sucks balls,
// so I'm using this one:
// https://stackoverflow.com/a/30810322/514684
succeed = document.execCommand('copy');
} catch (e) {
succeed = false;
}
if (current_focus && typeof current_focus.focus === "function") {
current_focus.focus();
}
if (is_input) {
elem.setSelectionRange(orig_select_start, orig_select_end);
} else {
document.body.removeChild(target);
}
return succeed;
}
function copy_btn_init(btn) {
btn.empty();
btn.append('<span class="glyphicon glyphicon-copy"/>');
}
function copy_btn_on_click(btn, pre) {
copy_to_clipboard(pre[0]);
}
function make_pre_buttons(path, pre) {
var edit_btn = $('<button class="btn btn-default" type="button" title="Edit"/>');
edit_btn_init(edit_btn);
edit_btn.click(function() {
edit_btn_on_click(edit_btn, pre);
});
var dload_btn = $('<button class="btn btn-default" type="button" title="Download"/>');
dload_btn_init(dload_btn);
dload_btn.click(function() {
dload_btn_on_click(dload_btn, path, pre);
});
var copy_btn = $('<button class="btn btn-default" type="button" title="Copy"/>');
copy_btn_init(copy_btn);
copy_btn.click(function() {
copy_btn_on_click(copy_btn, pre);
});
return $('<div class="pre_buttons btn-group btn-group-xs" role="group"/>')
.append(copy_btn)
.append(dload_btn)
.append(edit_btn);
}
function format_pre_text(pre, name) {
return $('<div class="pre_container"/>')
.append(pre)
.append(make_pre_buttons(name, pre));
}
var ConfigFile = function(name, text) {
this.name = name;
this.text = text;
this.pre = $('<pre/>').text(text);
}
ConfigFile.prototype.format = function() {
return format_pre_text(this.pre, this.name);
}
var QRCode = function(pre) {
this.pre = pre;
}
function draw_qr_code(container, text) {
var canvas = $('<canvas/>');
var qr = new QRious({
element: canvas[0],
value: text,
size: 350
});
container.append(canvas);
}
QRCode.prototype.format = function() {
var container = $('<div class="text-center"/>');
var pre = this.pre;
pre.on('blur', function() {
container.empty();
draw_qr_code(container, pre.text());
});
draw_qr_code(container, pre.text());
return container;
}
var catchall_ipv4 = '0.0.0.0/0';
var catchall_ipv6 = '::/0';
function wg_quick_client_file(data) {
var path = `/etc/wireguard/${iface}.conf`;
var contents =
`# On the client, put this to
# ${path}
# and either
# * start the wg-quick@${iface} systemd service,
# * or run \`wg-quick up ${iface}\`.
[Interface]
PrivateKey = ${data.client_private.value}
Address = ${data.client_ipv4.full_address()}, ${data.client_ipv6.full_address()}
`;
if (data.tunnel_everything.value) {
contents +=
`DNS = ${(data.dns_ipv4.value.concat(data.dns_ipv6.value)).join(',')}
`;
}
contents +=
`
[Peer]
Endpoint = ${data.server_endpoint.value}
PublicKey = ${data.server_public.value}
PresharedKey = ${data.preshared.value}
`;
if (data.tunnel_everything.value) {
contents +=
`AllowedIPs = ${catchall_ipv4}, ${catchall_ipv6}
`;
} else {
contents +=
`AllowedIPs = ${data.client_ipv4.allowed_ips_client()}, ${data.client_ipv6.allowed_ips_client()}
`;
}
if (data.keepalive.is_set()) {
contents +=
`PersistentKeepalive = ${data.keepalive.value}
`;
}
return new ConfigFile(path, contents);
}
function wg_quick_server_file(data) {
var path = `/etc/wireguard/${iface}.conf`;
return new ConfigFile(path,
`# On the server, add this to
# ${path}
# and restart the wg-quick@${iface} systemd service.
# Previous contents goes here...
[Peer]
PublicKey = ${data.client_public.value}
PresharedKey = ${data.preshared.value}
AllowedIPs = ${data.client_ipv4.allowed_ips_server()}, ${data.client_ipv6.allowed_ips_server()}
`);
}
function systemd_client_netdev_file(data) {
var path = `/etc/systemd/network/${iface}.netdev`;
var contents =
`# On the client, you need two files. Put this into
# ${path}
# and after you're done with both files, run
# systemctl daemon-reload
# systemctl restart systemd-networkd
[NetDev]
Name = ${iface}
Kind = wireguard
[WireGuard]
PrivateKey = ${data.client_private.value}
`;
if (data.tunnel_everything.value) {
contents +=
`FirewallMark = 0x8888
`;
}
contents +=
`
[WireGuardPeer]
Endpoint = ${data.server_endpoint.value}
PublicKey = ${data.server_public.value}
PresharedKey = ${data.preshared.value}
`;
if (data.tunnel_everything.value) {
contents +=
`AllowedIPs = ${catchall_ipv4}, ${catchall_ipv6}
`;
} else {
contents +=
`AllowedIPs = ${data.client_ipv4.allowed_ips_client()}, ${data.client_ipv6.allowed_ips_client()}
`;
}
if (data.keepalive.is_set()) {
contents +=
`PersistentKeepalive = ${data.keepalive.value}
`;
}
return new ConfigFile(path, contents);
}
function systemd_client_network_file(data) {
var path = `/etc/systemd/network/${iface}.network`;
var contents =
`# This is the second file. Put this into
# ${path}
# and if you're done with the first file already, run
# systemctl daemon-reload
# systemctl restart systemd-networkd
[Match]
Name = ${iface}
[Network]
Address = ${data.client_ipv4.full_address()}
Address = ${data.client_ipv6.full_address()}
`;
if (data.tunnel_everything.value) {
data.dns_ipv4.value.forEach(function(val) {
contents +=
`DNS = ${val}
`;
});
data.dns_ipv6.value.forEach(function(val) {
contents +=
`DNS = ${val}
`;
});
contents +=
`Domains = ~.
DNSDefaultRoute = true
[RoutingPolicyRule]
FirewallMark = 0x8888
InvertRule = true
Table = 1000
Priority = 10
`;
}
// I know this doesn't work, but I don't care, systemd-networkd is a
// fucking nightmare.
return new ConfigFile(path, contents);
}
function systemd_server_netdev_file(data) {
var path = `/etc/systemd/network/${iface}.netdev`;
return new ConfigFile(path,
`# On the server, add this to
# ${path}
# and run
# systemctl daemon-reload
# systemctl restart systemd-networkd
# Previous contents goes here...
[WireGuardPeer]
PublicKey = ${data.client_public.value}
PresharedKey = ${data.preshared.value}
AllowedIPs = ${data.client_ipv4.allowed_ips_server()}, ${data.client_ipv6.allowed_ips_server()}
`);
}
function nmcli_client_file(data) {
var path = `/etc/NetworkManager/system-connections/${iface}.nmconnection`;
var contents =
`# On the client, put this to
# ${path}
# and run
# chmod 0600 ${path}
# nmcli c reload
# nmcli c up ${iface}
[connection]
id=${iface}
type=wireguard
interface-name=${iface}
[wireguard]
private-key=${data.client_private.value}
private-key-flags=0
[wireguard-peer.${data.server_public.value}]
endpoint=${data.server_endpoint.value}
preshared-key=${data.preshared.value}
preshared-key-flags=0
`;
if (data.tunnel_everything.value) {
contents +=
`allowed-ips=${catchall_ipv4};${catchall_ipv6};
`;
} else {
contents +=
`allowed-ips=${data.client_ipv4.allowed_ips_client()};${data.client_ipv6.allowed_ips_client()};
`;
}
if (data.keepalive.is_set()) {
contents +=
`persistent-keepalive=${data.keepalive.value}
`;
}
contents +=
`
[ipv4]
address1=${data.client_ipv4.full_address()}
method=manual
`;
if (data.tunnel_everything.value) {
contents +=
`dns=${data.dns_ipv4.value.join(';')};
dns-priority=-10
dns-search=~;
`;
}
contents +=
`
[ipv6]
address1=${data.client_ipv6.full_address()}
method=manual
`;
if (data.tunnel_everything.value) {
contents +=
`dns=${data.dns_ipv6.value.join(';')};
dns-priority=-10
dns-search=~;
`;
}
return new ConfigFile(path, contents);
}
function nmcli_server_file(data) {
var path = `/etc/NetworkManager/system-connections/${iface}.nmconnection`;
return new ConfigFile(path,
`# On the server, add this to
# ${path}
# and run
# nmcli c reload
# nmcli c up ${iface}
# Previous contents goes here...
[wireguard-peer.${data.client_public.value}]
preshared-key=${data.preshared.value}
preshared-key-flags=0
allowed-ips=${data.client_ipv4.allowed_ips_server()};${data.client_ipv6.allowed_ips_server()};
`);
}
var shell_info =
`# Make sure your .bash_history or whatever is
# secure before running this.
# Better yet, put into a file and run (possibly, using sudo).`;
function manual_client_script(data) {
var contents =
`# On the client, run this to set up a connection.
${shell_info}
ip link add dev ${iface} type wireguard
ip addr add ${data.client_ipv4.full_address()} dev ${iface}
ip addr add ${data.client_ipv6.full_address()} dev ${iface}
wg set ${iface} \\
private-key <( echo ${data.client_private.value} )
wg set ${iface} \\
peer ${data.server_public.value} \\
preshared-key <( echo ${data.preshared.value} ) \\
endpoint ${data.server_endpoint.value} \\
`;
if (data.tunnel_everything.value) {
contents +=
` allowed-ips ${catchall_ipv4},${catchall_ipv6}
`;
} else {
contents +=
` allowed-ips ${data.client_ipv4.allowed_ips_client()},${data.client_ipv6.allowed_ips_client()}
`;
}
contents +=
`ip link set ${iface} up
`;
return new ConfigFile('client_setup.sh', contents);
}
function manual_server_script(data) {
return new ConfigFile('server_setup.sh',
`# On the server, run this to tweak an existing connection.
${shell_info}
wg set ${iface} \\
peer ${data.client_public.value} \\
preshared-key <( echo ${data.preshared.value} ) \\
allowed-ips ${data.client_ipv4.allowed_ips_server()},${data.client_ipv6.allowed_ips_server()}
`);
}
var Guide = function(name) {
this.name = name;
}
Guide.prototype.format = function(data) {
var container = $('<div/>');
container.append($('<h2/>').html(this.name));
return container;
}
var GuidePermaURL = function() {
Guide.call(this, 'Permanent URL');
}
GuidePermaURL.prototype = Object.create(Guide.prototype);
GuidePermaURL.prototype.constructor = GuidePermaURL;
GuidePermaURL.prototype.format = function(data) {
var container = Guide.prototype.format.call(this, data);
container
.append($('<p/>').text('Use the following URL to share this configuration. Be careful: it probably contains sensitive data, like your private keys.'))
.append($('<pre/>').append($('<a/>', {href: data.get_perma_url()}).text(data.get_perma_url())));
return container;
}
var GuideServerClient = function(name) {
Guide.call(this, name);
}
GuideServerClient.prototype = Object.create(Guide.prototype);
GuideServerClient.prototype.constructor = GuideServerClient;
GuideServerClient.prototype.for_server = function(data) { return []; }
GuideServerClient.prototype.for_client = function(data) { return []; }
GuideServerClient.prototype.join_guides = function(guides) {
var container = $('<div/>');
guides.forEach(function(guide) {
container.append(guide.format());
});
return container;
}
GuideServerClient.prototype.format = function(data) {
var container = Guide.prototype.format.call(this, data);
container
.append($('<div class="row"/>')
.append($('<div class="col-md-6"/>')
.append(this.join_guides(this.for_server(data))))
.append($('<div class="col-md-6"/>')
.append(this.join_guides(this.for_client(data)))));
return container;
}
var GuideWgQuick = function() {
GuideServerClient.call(this, 'wg-quick');
}
GuideWgQuick.prototype = Object.create(GuideServerClient.prototype);
GuideWgQuick.prototype.constructor = GuideWgQuick;
GuideWgQuick.prototype.for_client = function(data) {
var config = wg_quick_client_file(data);
var qr = new QRCode(config.pre);
return [config, qr];
}
GuideWgQuick.prototype.for_server = function(data) {
return [wg_quick_server_file(data)];
}
var GuideSystemd = function() {
GuideServerClient.call(this, 'systemd-networkd');
}
GuideSystemd.prototype = Object.create(GuideServerClient.prototype);
GuideSystemd.prototype.constructor = GuideSystemd;
GuideSystemd.prototype.for_client = function(data) {
return [systemd_client_netdev_file(data), systemd_client_network_file(data)];
}
GuideSystemd.prototype.for_server = function(data) {
return [systemd_server_netdev_file(data)];
}
var GuideNetworkManager = function() {
GuideServerClient.call(this, 'NetworkManager');
}
GuideNetworkManager.prototype = Object.create(GuideServerClient.prototype);
GuideNetworkManager.prototype.constructor = GuideNetworkManager;
GuideNetworkManager.prototype.for_client = function(data) {
return [nmcli_client_file(data)];
}
GuideNetworkManager.prototype.for_server = function(data) {
return [nmcli_server_file(data)];
}
var GuideManual = function() {
GuideServerClient.call(this, '<code>ip</code> & <code>wg</code>');
}
GuideManual.prototype = Object.create(GuideServerClient.prototype);
GuideManual.prototype.constructor = GuideManual;
GuideManual.prototype.for_client = function(data) {
return [manual_client_script(data)];
}
GuideManual.prototype.for_server = function(data) {
return [manual_server_script(data)];
}
function clear_guides() {
$('#guides').empty();
}
function add_guide(guide, data) {
$('#guides').append(guide.format(data));
}
function guides_from_data(data) {
if (!data.parse()) {
return;
}
var guides = [
new GuidePermaURL(),
new GuideWgQuick(),
new GuideSystemd(),
new GuideNetworkManager(),
new GuideManual()
];
guides.forEach(function(guide) {
add_guide(guide, data);
});
}
function form_on_submit() {
clear_guides();
var data = new Data();
guides_from_data(data);
return false;
}
function advanced_params_on_click(input) {
var data = new Data();
if (input.checked)
data.show_advanced();
else
data.hide_advanced();
}
function main() {
var data = new Data();
if (data.set_from_this_url())
guides_from_data(data);
}
$(function() {
main();
});
|