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