protocols: add client side protocol handler implementations
[project/luci.git] / protocols / luci-proto-relay / htdocs / luci-static / resources / protocol / relay.js
1 'use strict';
2 'require uci';
3 'require form';
4 'require network';
5 'require tools.widgets as widgets';
6
7 network.registerPatternVirtual(/^relay-.+$/);
8
9 var RelayDevicePrototype = {
10 __init__: function(ifname, network) {
11 this.ifname = ifname;
12 this.network = network;
13 },
14
15 _aggregateDevices: function(fn, first) {
16 var devices = this.network ? this.network.getDevices() : [],
17 rv = 0;
18
19 for (var i = 0; i < devices.length; i++) {
20 var v = devices[i][fn].apply(devices[i]);
21
22 if (v != null) {
23 if (first)
24 return v;
25
26 rv += v;
27 }
28 }
29
30 return first ? null : [ rv, devices.length ];
31 },
32
33 getPorts: function() { return this.network ? this.network.getDevices() : [] },
34
35 getType: function() { return 'tunnel' },
36 getTypeI18n: function() { return _('Relay Bridge') },
37
38 getShortName: function() {
39 return '%s "%h"'.format(_('Relay'), this.ifname);
40 },
41
42 isUp: function() {
43 var res = this._aggregateDevices('isUp');
44 return (res[1] > 0 && res[0] == res[1]);
45 },
46
47 getTXBytes: function() { return this._aggregateDevices('getTXBytes')[0] },
48 getRXBytes: function() { return this._aggregateDevices('getRXBytes')[0] },
49 getTXPackets: function() { return this._aggregateDevices('getTXPackets')[0] },
50 getRXPackets: function() { return this._aggregateDevices('getRXPackets')[0] },
51
52 getMAC: function() { return this._aggregateDevices('getMAC', true) },
53
54 getIPAddrs: function() {
55 var ipaddr = this.network ? L.toArray(uci.get('network', this.network.getName(), 'ipaddr'))[0] : null;
56 return (ipaddr != null ? [ ipaddr ] : []);
57 },
58
59 getIP6Addrs: function() { return [] }
60 };
61
62 return network.registerProtocol('relay', {
63 getI18n: function() {
64 return _('Relay bridge');
65 },
66
67 getIfname: function() {
68 return 'relay-%s'.format(this.sid);
69 },
70
71 getOpkgPackage: function() {
72 return 'relayd';
73 },
74
75 isFloating: function() {
76 return true;
77 },
78
79 isVirtual: function() {
80 return true;
81 },
82
83 containsDevice: function(ifname) {
84 return (network.getIfnameOf(ifname) == this.getIfname());
85 },
86
87 isUp: function() {
88 var dev = this.getDevice();
89 return (dev ? dev.isUp() : false);
90 },
91
92 getDevice: function() {
93 return network.instantiateDevice(this.sid, this, RelayDevicePrototype);
94 },
95
96 getDevices: function() {
97 if (this.devices)
98 return this.devices;
99
100 var networkNames = L.toArray(uci.get('network', this.sid, 'network')),
101 deviceNames = L.toArray(uci.get('network', this.sid, 'ifname')),
102 devices = {},
103 rv = [];
104
105 for (var i = 0; i < networkNames.length; i++) {
106 var net = network.instantiateNetwork(networkNames[i]),
107 dev = net ? net.getDevice() : null;
108
109 if (dev)
110 devices[dev.getName()] = dev;
111 }
112
113 for (var i = 0; i < deviceNames.length; i++) {
114 var dev = network.getDevice(deviceNames[i]);
115
116 if (dev)
117 devices[dev.getName()] = dev;
118 }
119
120 deviceNames = Object.keys(devices);
121 deviceNames.sort();
122
123 for (var i = 0; i < deviceNames.length; i++)
124 rv.push(devices[deviceNames[i]]);
125
126 this.devices = rv;
127
128 return rv;
129 },
130
131 getUptime: function() {
132 var networkNames = L.toArray(uci.get('network', this.sid, 'network')),
133 uptime = 0;
134
135 for (var i = 0; i < networkNames.length; i++) {
136 var net = network.instantiateNetwork(networkNames[i]);
137 if (net)
138 uptime = Math.max(uptime, net.getUptime());
139 }
140
141 return uptime;
142 },
143
144 getErrors: function() {
145 return null;
146 },
147
148 renderFormOptions: function(s) {
149 var o;
150
151 o = s.taboption('general', form.Value, 'ipaddr', _('Local IPv4 address'), _('Address to access local relay bridge'));
152 o.datatype = 'ip4addr("nomask")';
153
154 o = s.taboption('general', widgets.NetworkSelect, 'network', _('Relay between networks'));
155 o.exclude = s.section;
156 o.multiple = true;
157 o.nocreate = true;
158 o.nobridges = true;
159 o.novirtual = true;
160
161 o = s.taboption('advanced', form.Flag, 'forward_bcast', _('Forward broadcast traffic'));
162 o.default = o.enabled;
163
164 o = s.taboption('advanced', form.Flag, 'forward_dhcp', _('Forward DHCP traffic'));
165 o.default = o.enabled;
166
167 o = s.taboption('advanced', form.Value, 'gateway', _('Use DHCP gateway'), _('Override the gateway in DHCP responses'));
168 o.datatype = 'ip4addr("nomask")';
169 o.depends('forward_dhcp', '1');
170
171 o = s.taboption('advanced', form.Value, 'expiry', _('Host expiry timeout'), _('Specifies the maximum amount of seconds after which hosts are presumed to be dead'));
172 o.placeholder = '30';
173 o.datatype = 'min(1)';
174
175 o = s.taboption('advanced', form.Value, 'retry', _('ARP retry threshold'), _('Specifies the maximum amount of failed ARP requests until hosts are presumed to be dead'));
176 o.placeholder = '5';
177 o.datatype = 'min(1)';
178
179 o = s.taboption('advanced', form.Value, 'table', _('Use routing table'), _('Override the table used for internal routes'));
180 o.placeholder = '16800';
181 o.datatype = 'range(0,65535)';
182 }
183 });