8 var generateKey
= rpc
.declare({
9 object
: 'luci.wireguard',
10 method
: 'generateKeyPair',
14 var getPublicAndPrivateKeyFromPrivate
= rpc
.declare({
15 object
: 'luci.wireguard',
16 method
: 'getPublicAndPrivateKeyFromPrivate',
21 var generateQrCode
= rpc
.declare({
22 object
: 'luci.wireguard',
23 method
: 'generateQrCode',
24 params
: ['privkey', 'psk', 'allowed_ips'],
25 expect
: { qr_code
: '' }
28 function validateBase64(section_id
, value
) {
29 if (value
.length
== 0)
32 if (value
.length
!= 44 || !value
.match(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/))
33 return _('Invalid Base64 key string');
35 if (value
[43] != "=" )
36 return _('Invalid Base64 key string');
41 function findSection(sections
, name
) {
42 for (var i
= 0; i
< sections
.length
; i
++) {
43 var section
= sections
[i
];
44 if (section
['.name'] == name
) return section
;
50 function generateDescription(name
, texts
) {
51 return E('li', { 'style': 'color: inherit;' }, [
53 E('ul', texts
.map(function (text
) {
54 return E('li', { 'style': 'color: inherit;' }, text
);
59 return network
.registerProtocol('wireguard', {
61 return _('WireGuard VPN');
64 getIfname: function() {
65 return this._ubus('l3_device') || this.sid
;
68 getOpkgPackage: function() {
69 return 'wireguard-tools';
72 isFloating: function() {
76 isVirtual: function() {
80 getDevices: function() {
84 containsDevice: function(ifname
) {
85 return (network
.getIfnameOf(ifname
) == this.getIfname());
88 renderFormOptions: function(s
) {
91 // -- general ---------------------------------------------------------------------
93 o
= s
.taboption('general', form
.Value
, 'private_key', _('Private Key'), _('Required. Base64-encoded private key for this interface.'));
95 o
.validate
= validateBase64
;
98 var sections
= uci
.sections('network');
99 var serverName
= this.getIfname();
100 var server
= findSection(sections
, serverName
);
102 o
= s
.taboption('general', form
.Value
, 'public_key', _('Public Key'), _('Base64-encoded public key of this interface for sharing.'));
104 o
.write = function() {/* write nothing */};
106 o
.load = function(s
) {
107 return getPublicAndPrivateKeyFromPrivate(server
.private_key
).then(
109 return keypair
.pub
|| '';
112 return _('Error getting PublicKey');
116 o
= s
.taboption('general', form
.Button
, 'generate_key', _('Generate Key'));
117 o
.inputstyle
= 'apply';
118 o
.onclick
= ui
.createHandlerFn(this, function(section_id
, ev
) {
119 return generateKey().then(function(keypair
) {
120 var keyInput
= document
.getElementById('widget.cbid.network.%s.private_key'.format(section_id
)),
121 changeEvent
= new Event('change'),
122 pubKeyInput
= document
.getElementById('widget.cbid.network.%s.public_key'.format(section_id
));
124 keyInput
.value
= keypair
.priv
|| '';
125 pubKeyInput
.value
= keypair
.pub
|| '';
126 keyInput
.dispatchEvent(changeEvent
);
130 o
= s
.taboption('general', form
.Value
, 'listen_port', _('Listen Port'), _('Optional. UDP port used for outgoing and incoming packets.'));
132 o
.placeholder
= _('random');
135 o
= s
.taboption('general', form
.DynamicList
, 'addresses', _('IP Addresses'), _('Recommended. IP addresses of the WireGuard interface.'));
136 o
.datatype
= 'ipaddr';
139 o
= s
.taboption('general', form
.Flag
, 'nohostroute', _('No Host Routes'), _('Optional. Do not create host routes to peers.'));
142 // -- advanced --------------------------------------------------------------------
144 o
= s
.taboption('advanced', form
.Value
, 'mtu', _('MTU'), _('Optional. Maximum Transmission Unit of tunnel interface.'));
145 o
.datatype
= 'range(1280,1420)';
146 o
.placeholder
= '1420';
149 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>.'));
151 o
.validate = function(section_id
, value
) {
152 if (value
.length
> 0 && !value
.match(/^0x[a-fA-F0-9]{1,8}$/))
153 return _('Invalid hexadecimal value');
159 // -- peers -----------------------------------------------------------------------
162 s
.tab('peers', _('Peers'), _('Further information about WireGuard interfaces and peers at <a href=\'http://wireguard.com\'>wireguard.com</a>.'));
166 o
= s
.taboption('peers', form
.SectionValue
, '_peers', form
.TypedSection
, 'wireguard_%s'.format(s
.section
));
167 o
.depends('proto', 'wireguard');
172 ss
.addbtntitle
= _('Add peer');
174 ss
.renderSectionPlaceholder = function() {
177 E('em', _('No peers defined yet'))
181 o
= ss
.option(form
.Flag
, 'disabled', _('Peer disabled'), _('Enable / Disable peer. Restart wireguard interface to apply changes.'));
184 o
= ss
.option(form
.Value
, 'description', _('Description'), _('Optional. Description of peer.'));
185 o
.placeholder
= 'My Peer';
186 o
.datatype
= 'string';
189 o
= ss
.option(form
.Value
, 'description', _('QR-Code'));
190 o
.render
= L
.bind(function (view
, section_id
) {
191 var sections
= uci
.sections('network');
192 var client
= findSection(sections
, section_id
);
193 var serverName
= this.getIfname();
194 var server
= findSection(sections
, serverName
);
196 var interfaceTexts
= [
197 'PrivateKey: ' + _('A random, on the fly generated "PrivateKey", the key will not be saved on the router')
201 'PublicKey: ' + _('The "PublicKey" of that wg interface'),
202 'AllowedIPs: ' + _('The list of this client\'s "AllowedIPs" or "0.0.0.0/0, ::/0" if not configured'),
203 'PresharedKey: ' + _('If available, the client\'s "PresharedKey"')
208 _('If there are any unsaved changes for this client, please save the configuration before generating a QR-Code'),
210 _('The QR-Code works per wg interface, it will be refreshed with every button click and transfers the following information:')
213 generateDescription('[Interface]', interfaceTexts
),
214 generateDescription('[Peer]', peerTexts
)
218 return E('div', { 'class': 'cbi-value' }, [
219 E('label', { 'class': 'cbi-value-title' }, _('QR-Code')),
221 'class': 'cbi-value-field',
222 'style': 'display: flex; flex-direction: column; align-items: baseline;',
223 'id': 'qr-' + section_id
226 'class': 'btn cbi-button cbi-button-apply',
227 'click': ui
.createHandlerFn(this, function (server
, client
, section_id
) {
228 var qrDiv
= document
.getElementById('qr-' + section_id
);
229 var qrEl
= qrDiv
.querySelector('value');
230 var qrBtn
= qrDiv
.querySelector('button');
231 var qrencodeErr
= '<b>%q</b>'.format(
232 _('For QR-Code support please install the qrencode package!'));
234 if (qrEl
.innerHTML
!= '' && qrEl
.innerHTML
!= qrencodeErr
) {
236 qrBtn
.innerHTML
= _('Generate New QR-Code')
238 qrEl
.innerHTML
= _('Loading QR-Code...');
240 generateQrCode(server
.private_key
, client
.preshared_key
,
241 client
.allowed_ips
).then(function (qrCode
) {
243 qrEl
.innerHTML
= qrencodeErr
;
245 qrEl
.innerHTML
= qrCode
;
246 qrBtn
.innerHTML
= _('Hide QR-Code');
250 }, server
, client
, section_id
)
251 }, _('Generate new QR-Code')),
253 'class': 'cbi-section',
254 'style': 'margin: 0;'
256 E('div', { 'class': 'cbi-value-description' }, description
)
261 o
= ss
.option(form
.Value
, 'public_key', _('Public Key'), _('Required. Base64-encoded public key of peer.'));
262 o
.validate
= validateBase64
;
265 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.'));
267 o
.validate
= validateBase64
;
270 o
= ss
.option(form
.DynamicList
, 'allowed_ips', _('Allowed IPs'), _("Optional. 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."));
271 o
.datatype
= 'ipaddr';
274 o
= ss
.option(form
.Flag
, 'route_allowed_ips', _('Route Allowed IPs'), _('Optional. Create routes for Allowed IPs for this peer.'));
276 o
= ss
.option(form
.Value
, 'endpoint_host', _('Endpoint Host'), _('Optional. Host of peer. Names are resolved prior to bringing up the interface.'));
277 o
.placeholder
= 'vpn.example.com';
280 o
= ss
.option(form
.Value
, 'endpoint_port', _('Endpoint Port'), _('Optional. Port of peer.'));
281 o
.placeholder
= '51820';
284 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.'));
285 o
.datatype
= 'range(0,65535)';
289 deleteConfiguration: function() {
290 uci
.sections('network', 'wireguard_%s'.format(this.sid
), function(s
) {
291 uci
.remove('network', s
['.name']);