treewide: remove rendundant proto handler options
[project/luci.git] / protocols / luci-proto-openconnect / htdocs / luci-static / resources / protocol / openconnect.js
1 'use strict';
2 'require rpc';
3 'require form';
4 'require network';
5
6 var callGetCertificateFiles = rpc.declare({
7 object: 'luci.openconnect',
8 method: 'getCertificates',
9 params: [ 'interface' ],
10 expect: { '': {} }
11 });
12
13 var callSetCertificateFiles = rpc.declare({
14 object: 'luci.openconnect',
15 method: 'setCertificates',
16 params: [ 'interface', 'user_certificate', 'user_privatekey', 'ca_certificate' ],
17 expect: { '': {} }
18 });
19
20 network.registerPatternVirtual(/^vpn-.+$/);
21
22 function sanitizeCert(s) {
23 if (typeof(s) != 'string')
24 return null;
25
26 s = s.trim();
27
28 if (s == '')
29 return null;
30
31 s = s.replace(/\r\n?/g, '\n');
32
33 if (!s.match(/\n$/))
34 s += '\n';
35
36 return s;
37 }
38
39 function validateCert(priv, section_id, value) {
40 var beg = priv ? /^-----BEGIN RSA PRIVATE KEY-----$/ : /^-----BEGIN CERTIFICATE-----$/,
41 end = priv ? /^-----END RSA PRIVATE KEY-----$/ : /^-----END CERTIFICATE-----$/,
42 lines = value.trim().split(/[\r\n]/),
43 start = false,
44 i;
45
46 if (value === null || value === '')
47 return true;
48
49 for (i = 0; i < lines.length; i++) {
50 if (lines[i].match(beg))
51 start = true;
52 else if (start && !lines[i].match(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/))
53 break;
54 }
55
56 if (!start || i < lines.length - 1 || !lines[i].match(end))
57 return _('This does not look like a valid PEM file');
58
59 return true;
60 }
61
62 return network.registerProtocol('openconnect', {
63 getI18n: function() {
64 return _('OpenConnect (CISCO AnyConnect)');
65 },
66
67 getIfname: function() {
68 return this._ubus('l3_device') || 'vpn-%s'.format(this.sid);
69 },
70
71 getOpkgPackage: function() {
72 return 'openconnect';
73 },
74
75 isFloating: function() {
76 return true;
77 },
78
79 isVirtual: function() {
80 return true;
81 },
82
83 getDevices: function() {
84 return null;
85 },
86
87 containsDevice: function(ifname) {
88 return (network.getIfnameOf(ifname) == this.getIfname());
89 },
90
91 renderFormOptions: function(s) {
92 var dev = this.getDevice().getName(),
93 certLoadPromise = null,
94 o;
95
96 o = s.taboption('general', form.ListValue, 'vpn_protocol', _('VPN Protocol'));
97 o.value('anyconnect', 'Cisco AnyConnect SSL VPN');
98 o.value('nc', 'Juniper Network Connect');
99 o.value('gp', 'GlobalProtect SSL VPN');
100 o.value('pulse', 'Pulse Connect Secure SSL VPN');
101
102 o = s.taboption('general', form.Value, 'server', _('VPN Server'));
103 o.datatype = 'host(0)';
104
105 o = s.taboption('general', form.Value, 'port', _('VPN Server port'));
106 o.placeholder = '443';
107 o.datatype = 'port';
108
109 s.taboption('general', form.Value, 'serverhash', _("VPN Server's certificate SHA1 hash"));
110 s.taboption('general', form.Value, 'authgroup', _('Auth Group'));
111 s.taboption('general', form.Value, 'usergroup', _('User Group'));
112 s.taboption("general", form.Value, "username", _("Username"));
113
114 o = s.taboption('general', form.Value, 'password', _('Password'));
115 o.password = true;
116
117 o = s.taboption('general', form.Value, 'password2', _('Password2'));
118 o.password = true;
119
120 o = s.taboption('general', form.TextValue, 'usercert', _('User certificate (PEM encoded)'));
121 o.rows = 10;
122 o.monospace = true;
123 o.validate = L.bind(validateCert, o, false);
124 o.load = function(section_id) {
125 certLoadPromise = certLoadPromise || callGetCertificateFiles(section_id);
126 return certLoadPromise.then(function(certs) { return certs.user_certificate });
127 };
128 o.write = function(section_id, value) {
129 return callSetCertificateFiles(section_id, sanitizeCert(value), null, null);
130 };
131
132 o = s.taboption('general', form.TextValue, 'userkey', _('User key (PEM encoded)'));
133 o.rows = 10;
134 o.monospace = true;
135 o.validate = L.bind(validateCert, o, true);
136 o.load = function(section_id) {
137 certLoadPromise = certLoadPromise || callGetCertificateFiles(section_id);
138 return certLoadPromise.then(function(certs) { return certs.user_privatekey });
139 };
140 o.write = function(section_id, value) {
141 return callSetCertificateFiles(section_id, null, sanitizeCert(value), null);
142 };
143
144 o = s.taboption('general', form.TextValue, 'ca', _('CA certificate; if empty it will be saved after the first connection.'));
145 o.rows = 10;
146 o.monospace = true;
147 o.validate = L.bind(validateCert, o, false);
148 o.load = function(section_id) {
149 certLoadPromise = certLoadPromise || callGetCertificateFiles(section_id);
150 return certLoadPromise.then(function(certs) { return certs.ca_certificate });
151 };
152 o.write = function(section_id, value) {
153 return callSetCertificateFiles(section_id, null, null, sanitizeCert(value));
154 };
155
156 o = s.taboption('advanced', form.Value, 'mtu', _('Override MTU'));
157 o.optional = true;
158 o.placeholder = 1406;
159 o.datatype = 'range(68, 9200)';
160 }
161 });