protocols: add client side protocol handler implementations
[project/luci.git] / protocols / luci-proto-ppp / htdocs / luci-static / resources / protocol / ppp.js
1 'use strict';
2 'require rpc';
3 'require uci';
4 'require form';
5 'require network';
6
7 var callTTYDevices = rpc.declare({
8 object: 'luci',
9 method: 'getTTYDevices',
10 params: [ 'with_cdc', 'with_tts' ],
11 expect: { result: [] }
12 });
13
14 network.registerPatternVirtual(/^ppp-.+$/);
15
16 function write_keepalive(section_id, value) {
17 var f_opt = this.map.lookupOption('_keepalive_failure', section_id),
18 i_opt = this.map.lookupOption('_keepalive_interval', section_id),
19 f = (f_opt != null) ? +f_opt[0].formvalue(section_id) : null,
20 i = (i_opt != null) ? +i_opt[0].formvalue(section_id) : null;
21
22 if (f == null || f == '' || isNaN(f))
23 f = 0;
24
25 if (i == null || i == '' || isNaN(i) || i < 1)
26 i = 1;
27
28 if (f > 0)
29 uci.set('network', section_id, 'keepalive', '%d %d'.format(f, i));
30 else
31 uci.unset('network', section_id, 'keepalive');
32 }
33
34 return network.registerProtocol('ppp', {
35 getI18n: function() {
36 return _('PPP');
37 },
38
39 getIfname: function() {
40 return this._ubus('l3_device') || 'ppp-%s'.format(this.sid);
41 },
42
43 getOpkgPackage: function() {
44 return 'ppp';
45 },
46
47 isFloating: function() {
48 return true;
49 },
50
51 isVirtual: function() {
52 return true;
53 },
54
55 getDevices: function() {
56 return null;
57 },
58
59 containsDevice: function(ifname) {
60 return (network.getIfnameOf(ifname) == this.getIfname());
61 },
62
63 renderFormOptions: function(s) {
64 var dev = this.getL3Device() || this.getDevice(), o;
65
66 o = s.taboption('general', form.Value, 'device', _('Modem device'));
67 o.rmempty = false;
68 o.load = function(section_id) {
69 return callTTYDevices(true, true).then(L.bind(function(devices) {
70 if (Array.isArray(devices))
71 for (var i = 0; i < devices.length; i++)
72 this.value(devices[i]);
73
74 return form.Value.prototype.load.apply(this, [section_id]);
75 }, this));
76 };
77
78 s.taboption('general', form.Value, 'username', _('PAP/CHAP username'));
79
80 o = s.taboption('general', form.Value, 'password', _('PAP/CHAP password'));
81 o.password = true;
82
83 if (L.hasSystemFeature('ipv6')) {
84 o = s.taboption('advanced', form.ListValue, 'ipv6', _('Obtain IPv6-Address'), _('Enable IPv6 negotiation on the PPP link'));
85 o.value('auto', _('Automatic'));
86 o.value('0', _('Disabled'));
87 o.value('1', _('Manual'));
88 o.default = 'auto';
89 }
90
91 o = s.taboption('advanced', form.Flag, 'defaultroute', _('Use default gateway'), _('If unchecked, no default route is configured'));
92 o.default = o.enabled;
93
94 o = s.taboption('advanced', form.Flag, 'peerdns', _('Use DNS servers advertised by peer'), _('If unchecked, the advertised DNS server addresses are ignored'));
95 o.default = o.enabled;
96
97 o = s.taboption('advanced', form.DynamicList, 'dns', _('Use custom DNS servers'));
98 o.depends('peerdns', '0');
99 o.datatype = 'ipaddr';
100 o.cast = 'string';
101
102 o = s.taboption('advanced', form.Value, 'metric', _('Use gateway metric'));
103 o.placeholder = '0';
104 o.datatype = 'uinteger';
105
106 o = s.taboption('advanced', form.Value, '_keepalive_failure', _('LCP echo failure threshold'), _('Presume peer to be dead after given amount of LCP echo failures, use 0 to ignore failures'));
107 o.placeholder = '0';
108 o.datatype = 'uinteger';
109 o.write = write_keepalive;
110 o.remove = write_keepalive;
111 o.cfgvalue = function(section_id) {
112 var v = uci.get('network', section_id, 'keepalive');
113 if (typeof(v) == 'string' && v != '') {
114 var m = v.match(/^(\d+)[ ,]\d+$/);
115 return m ? m[1] : v;
116 }
117 };
118
119 o = s.taboption('advanced', form.Value, '_keepalive_interval', _('LCP echo interval'), _('Send LCP echo requests at the given interval in seconds, only effective in conjunction with failure threshold'));
120 o.placeholder = '5';
121 o.datatype = 'min(1)';
122 o.write = write_keepalive;
123 o.remove = write_keepalive;
124 o.cfgvalue = function(section_id) {
125 var v = uci.get('network', section_id, 'keepalive');
126 if (typeof(v) == 'string' && v != '') {
127 var m = v.match(/^\d+[ ,](\d+)$/);
128 return m ? m[1] : v;
129 }
130 };
131
132 o = s.taboption('advanced', form.Value, 'demand', _('Inactivity timeout'), _('Close inactive connection after the given amount of seconds, use 0 to persist connection'));
133 o.placeholder = '0';
134 o.datatype = 'uinteger';
135
136 o = s.taboption('advanced', form.Value, 'mtu', _('Override MTU'));
137 o.placeholder = dev ? (dev.getMTU() || '1500') : '1500';
138 o.datatype = 'max(9200)';
139 }
140 });