Merge pull request #4037 from brvphoenix/hd-idle
[oweals/luci.git] / protocols / luci-proto-wireguard / htdocs / luci-static / resources / protocol / wireguard.js
1 'use strict';
2 'require uci';
3 'require form';
4 'require network';
5
6 function validateBase64(section_id, value) {
7         if (value.length == 0)
8                 return true;
9
10         if (value.length != 44 || !value.match(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/))
11                 return _('Invalid Base64 key string');
12
13         if (value[43] != "=" )
14                 return _('Invalid Base64 key string');
15
16         return true;
17 }
18
19 return network.registerProtocol('wireguard', {
20         getI18n: function() {
21                 return _('WireGuard VPN');
22         },
23
24         getIfname: function() {
25                 return this._ubus('l3_device') || this.sid;
26         },
27
28         getOpkgPackage: function() {
29                 return 'wireguard-tools';
30         },
31
32         isFloating: function() {
33                 return true;
34         },
35
36         isVirtual: function() {
37                 return true;
38         },
39
40         getDevices: function() {
41                 return null;
42         },
43
44         containsDevice: function(ifname) {
45                 return (network.getIfnameOf(ifname) == this.getIfname());
46         },
47
48         renderFormOptions: function(s) {
49                 var o, ss;
50
51                 // -- general ---------------------------------------------------------------------
52
53                 o = s.taboption('general', form.Value, 'private_key', _('Private Key'), _('Required. Base64-encoded private key for this interface.'));
54                 o.password = true;
55                 o.validate = validateBase64;
56                 o.rmempty = false;
57
58                 o = s.taboption('general', form.Value, 'listen_port', _('Listen Port'), _('Optional. UDP port used for outgoing and incoming packets.'));
59                 o.datatype = 'port';
60                 o.placeholder = _('random');
61                 o.optional = true;
62
63                 o = s.taboption('general', form.DynamicList, 'addresses', _('IP Addresses'), _('Recommended. IP addresses of the WireGuard interface.'));
64                 o.datatype = 'ipaddr';
65                 o.optional = true;
66
67                 o = s.taboption('general', form.Flag, 'nohostroute', _('No Host Routes'), _('Optional. Do not create host routes to peers.'));
68                 o.optional = true;
69
70                 // -- advanced --------------------------------------------------------------------
71
72                 o = s.taboption('advanced', form.Value, 'metric', _('Metric'), _('Optional'));
73                 o.datatype = 'uinteger';
74                 o.placeholder = '0';
75                 o.optional = true;
76
77                 o = s.taboption('advanced', form.Value, 'mtu', _('MTU'), _('Optional. Maximum Transmission Unit of tunnel interface.'));
78                 o.datatype = 'range(1280,1420)';
79                 o.placeholder = '1420';
80                 o.optional = true;
81
82                 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>.'));
83                 o.optional = true;
84                 o.validate = function(section_id, value) {
85                         if (value.length > 0 && !value.match(/^0x[a-fA-F0-9]{1,4}$/))
86                                 return _('Invalid hexadecimal value');
87
88                         return true;
89                 };
90
91
92                 // -- peers -----------------------------------------------------------------------
93
94                 try {
95                         s.tab('peers', _('Peers'), _('Further information about WireGuard interfaces and peers at <a href=\'http://wireguard.com\'>wireguard.com</a>.'));
96                 }
97                 catch(e) {}
98
99                 o = s.taboption('peers', form.SectionValue, '_peers', form.TypedSection, 'wireguard_%s'.format(s.section));
100                 o.depends('proto', 'wireguard');
101
102                 ss = o.subsection;
103                 ss.anonymous = true;
104                 ss.addremove = true;
105                 ss.addbtntitle = _('Add peer');
106
107                 ss.renderSectionPlaceholder = function() {
108                         return E([], [
109                                 E('br'),
110                                 E('em', _('No peers defined yet'))
111                         ]);
112                 };
113
114                 o = ss.option(form.Value, 'description', _('Description'), _('Optional. Description of peer.'));
115                 o.placeholder = 'My Peer';
116                 o.datatype = 'string';
117                 o.optional = true;
118
119                 o = ss.option(form.Value, 'public_key', _('Public Key'), _('Required. Base64-encoded public key of peer.'));
120                 o.validate = validateBase64;
121                 o.rmempty = false;
122
123                 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.'));
124                 o.password = true;
125                 o.validate = validateBase64;
126                 o.optional = true;
127
128                 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."));
129                 o.datatype = 'ipaddr';
130                 o.validate = function(section, value) {
131                         var opt = this.map.lookupOption('allowed_ips', section);
132                         var ips = opt[0].formvalue(section);
133                         if (ips.length == 0) {
134                                 return _('Value must not be empty');
135                         }
136                         return true;
137                 };
138
139                 o = ss.option(form.Flag, 'route_allowed_ips', _('Route Allowed IPs'), _('Optional. Create routes for Allowed IPs for this peer.'));
140
141                 o = ss.option(form.Value, 'endpoint_host', _('Endpoint Host'), _('Optional. Host of peer. Names are resolved prior to bringing up the interface.'));
142                 o.placeholder = 'vpn.example.com';
143                 o.datatype = 'host';
144
145                 o = ss.option(form.Value, 'endpoint_port', _('Endpoint Port'), _('Optional. Port of peer.'));
146                 o.placeholder = '51820';
147                 o.datatype = 'port';
148
149                 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.'));
150                 o.datatype = 'range(0,65535)';
151                 o.placeholder = '0';
152         },
153
154         deleteConfiguration: function() {
155                 uci.sections('network', 'wireguard_%s'.format(this.sid), function(s) {
156                         uci.remove('network', s['.name']);
157                 });
158         }
159 });