Merge pull request #3024 from TDT-AG/pr/20190829-material-logo
[oweals/luci.git] / protocols / luci-proto-wireguard / htdocs / luci-static / resources / protocol / wireguard.js
1 'use strict';
2 'require form';
3 'require network';
4
5 function validateBase64(section_id, value) {
6         if (value.length != 44 || !value.match(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/))
7                 return _('Invalid Base64 key string');
8
9         return true;
10 }
11
12 return network.registerProtocol('wireguard', {
13         getI18n: function() {
14                 return _('WireGuard VPN');
15         },
16
17         getIfname: function() {
18                 return this._ubus('l3_device') || this.sid;
19         },
20
21         getOpkgPackage: function() {
22                 return 'wireguard-tools';
23         },
24
25         isFloating: function() {
26                 return true;
27         },
28
29         isVirtual: function() {
30                 return true;
31         },
32
33         getDevices: function() {
34                 return null;
35         },
36
37         containsDevice: function(ifname) {
38                 return (network.getIfnameOf(ifname) == this.getIfname());
39         },
40
41         renderFormOptions: function(s) {
42                 var o, ss;
43
44                 // -- general ---------------------------------------------------------------------
45
46                 o = s.taboption('general', form.Value, 'private_key', _('Private Key'), _('Required. Base64-encoded private key for this interface.'));
47                 o.password = true;
48                 o.validate = validateBase64;
49                 o.rmempty = false;
50
51                 o = s.taboption('general', form.Value, 'listen_port', _('Listen Port'), _('Optional. UDP port used for outgoing and incoming packets.'));
52                 o.datatype = 'port';
53                 o.placeholder = _('random');
54                 o.optional = true;
55
56                 o = s.taboption('general', form.DynamicList, 'addresses', _('IP Addresses'), _('Recommended. IP addresses of the WireGuard interface.'));
57                 o.datatype = 'ipaddr';
58                 o.optional = true;
59
60
61                 // -- advanced --------------------------------------------------------------------
62
63                 o = s.taboption('advanced', form.Value, 'metric', _('Metric'), _('Optional'));
64                 o.datatype = 'uinteger';
65                 o.placeholder = '0';
66                 o.optional = true;
67
68                 o = s.taboption('advanced', form.Value, 'mtu', _('MTU'), _('Optional. Maximum Transmission Unit of tunnel interface.'));
69                 o.datatype = 'range(1280,1420)';
70                 o.placeholder = '1420';
71                 o.optional = true;
72
73                 o = s.taboption('advanced', form.Value, 'fwmark', _('Firewall Mark'), _('Optional. 32-bit mark for outgoing encrypted packets. Enter value in hex, starting with <code>0x</code>.'));
74                 o.optional = true;
75                 o.validate = function(section_id, value) {
76                         if (value.length > 0 && !value.match(/^0x[a-fA-F0-9]{1,4}$/))
77                                 return _('Invalid hexadecimal value');
78
79                         return true;
80                 };
81
82
83                 // -- peers -----------------------------------------------------------------------
84
85                 try {
86                         s.tab('peers', _('Peers'), _('Further information about WireGuard interfaces and peers at <a href=\'http://wireguard.com\'>wireguard.com</a>.'));
87                 }
88                 catch(e) {}
89
90                 o = s.taboption('peers', form.SectionValue, '_peers', form.TypedSection, 'wireguard_%s'.format(s.section));
91                 o.depends('proto', 'wireguard');
92
93                 ss = o.subsection;
94                 ss.anonymous = true;
95                 ss.addremove = true;
96                 ss.addbtntitle = _('Add peer');
97
98                 ss.renderSectionPlaceholder = function() {
99                         return E([], [
100                                 E('br'),
101                                 E('em', _('No peers defined yet'))
102                         ]);
103                 };
104
105                 o = ss.option(form.Value, 'description', _('Description'), _('Optional. Description of peer.'));
106                 o.placeholder = 'My Peer';
107                 o.datatype = 'string';
108                 o.optional = true;
109
110                 o = ss.option(form.Value, 'public_key', _('Public Key'), _('Required. Base64-encoded public key of peer.'));
111                 o.validate = validateBase64;
112                 o.rmempty = false;
113
114                 o = ss.option(form.Value, 'preshared_key', _('Preshared Key'), _('Optional. Base64-encoded preshared key. Adds in an additional layer of symmetric-key cryptography for post-quantum resistance.'));
115                 o.password = true;
116                 o.validate = validateBase64;
117                 o.optional = true;
118
119                 o = ss.option(form.DynamicList, 'allowed_ips', _('Allowed IPs'), _("Required. IP addresses and prefixes that this peer is allowed to use inside the tunnel. Usually the peer's tunnel IP addresses and the networks the peer routes through the tunnel."));
120                 o.datatype = 'ipaddr';
121                 o.rmempty = false;
122
123                 o = ss.option(form.Flag, 'route_allowed_ips', _('Route Allowed IPs'), _('Optional. Create routes for Allowed IPs for this peer.'));
124
125                 o = ss.option(form.Value, 'endpoint_host', _('Endpoint Host'), _('Optional. Host of peer. Names are resolved prior to bringing up the interface.'));
126                 o.placeholder = 'vpn.example.com';
127                 o.datatype = 'host';
128
129                 o = ss.option(form.Value, 'endpoint_port', _('Endpoint Port'), _('Optional. Port of peer.'));
130                 o.placeholder = '51820';
131                 o.datatype = 'port';
132
133                 o = ss.option(form.Value, 'persistent_keepalive', _('Persistent Keep Alive'), _('Optional. Seconds between keep alive messages. Default is 0 (disabled). Recommended value if this device is behind a NAT is 25.'));
134                 o.datatype = 'range(0,65535)';
135                 o.placeholder = '0';
136         }
137 });