cc59cfe31837a98c00e56d04ef2429078c538c9a
[openwrt-luci.git] /
1 'use strict';
2 'require view';
3 'require poll';
4 'require form';
5 'require uci';
6 'require fs';
7 'require network';
8 'require rpc';
9 'require shadowsocks-libev as ss';
10
11 var conf = 'shadowsocks-libev';
12 var cfgtypes = ['ss_local', 'ss_redir', 'ss_server', 'ss_tunnel'];
13
14 var callServiceList = rpc.declare({
15         object: 'service',
16         method: 'list',
17         params: [ 'name' ],
18         expect: { '': {} }
19 });
20
21 return view.extend({
22         render: function(stats) {
23                 var m, s, o;
24
25                 m = new form.Map(conf,
26                         _('Local Instances'),
27                         _('Instances of shadowsocks-libev components, e.g. ss-local, \
28                            ss-redir, ss-tunnel, ss-server, etc.  To enable an instance it \
29                            is required to enable both the instance itself and the remote \
30                            server it refers to.'));
31
32                 s = m.section(form.GridSection);
33                 s.addremove = true;
34                 s.cfgsections = function() {
35                         return this.map.data.sections(this.map.config)
36                                 .filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
37                                 .map(function(s) { return s['.name']; });
38                 };
39                 s.sectiontitle = function(section_id) {
40                         var s = uci.get(conf, section_id);
41                         return (s ? s['.type'] + '.' : '') + section_id;
42                 };
43                 s.renderSectionAdd = function(extra_class) {
44                         var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
45                                 optionEl = [E('option', { value: '_dummy' }, [_('-- instance type --')])];
46                         cfgtypes.forEach(function(t) {
47                                 optionEl.push(E('option', { value: t }, [t.replace('_', '-')]));
48                         });
49                         var selectEl = E('select', {
50                                 class: 'cbi-input-select',
51                                 change: function(ev) {
52                                         ev.target.parentElement.nextElementSibling.nextElementSibling
53                                                 .toggleAttribute('disabled', ev.target.value === '_dummy');
54                                 }
55                         }, optionEl);
56                         el.lastElementChild.setAttribute('disabled', '');
57                         el.prepend(E('div', {}, selectEl));
58                         return el;
59                 };
60                 s.handleAdd = function(ev, name) {
61                         var selectEl = ev.target.parentElement.firstElementChild.firstElementChild,
62                                 type = selectEl.value;
63                         this.sectiontype = type;
64                         var promise = form.GridSection.prototype.handleAdd.apply(this, arguments);
65                         this.sectiontype = undefined;
66                         return promise;
67                 };
68                 s.addModalOptions = function(s, section_id, ev) {
69                         var sdata = uci.get(conf, section_id),
70                                 stype = sdata ? sdata['.type'] : null;
71                         if (stype) {
72                                 s.sectiontype = stype;
73                                 return Promise.all([
74                                         L.resolveDefault(fs.stat('/usr/bin/' + stype.replace('_', '-')), null),
75                                         network.getDevices()
76                                 ]).then(L.bind(function(res) {
77                                         s.tab('general', _('General Settings'));
78                                         s.tab('advanced', _('Advanced Settings'));
79                                         s.taboption('general', form.Flag, 'disabled', _('Disable'));
80                                         if (!res[0]) {
81                                                 ss.option_install_package(s, 'general');
82                                         }
83                                         ss.options_common(s, 'advanced');
84
85                                         if (stype === 'ss_server') {
86                                                 ss.options_server(s, { tab: 'general' });
87                                                 o = s.taboption('advanced', form.Value, 'local_address',
88                                                         _('Local address'),
89                                                         _('The address ss-server will initiate connections from'));
90                                                 o.datatype = 'ipaddr';
91                                                 ss.values_ipaddr(o, res[1]);
92                                                 o = s.taboption('advanced', form.Value, 'local_ipv4_address',
93                                                         _('Local IPv4 address'),
94                                                         _('The IPv4 address ss-server will initiate IPv4 connections from'));
95                                                 o.datatype = 'ip4addr';
96                                                 ss.values_ip4addr(o, res[1]);
97                                                 o = s.taboption('advanced', form.Value, 'local_ipv6_address',
98                                                         _('Local IPv6 address'),
99                                                         _('The IPv6 address ss-server will initiate IPv6 connections from'));
100                                                 o.datatype = 'ip6addr';
101                                                 ss.values_ip6addr(o, res[1]);
102                                         } else {
103                                                 ss.options_client(s, 'general', res[1]);
104                                                 if (stype === 'ss_tunnel') {
105                                                         o = s.taboption('general', form.Value, 'tunnel_address',
106                                                                 _('Tunnel address'),
107                                                                 _('The address ss-tunnel will forward traffic to'));
108                                                         o.datatype = 'hostport';
109                                                 }
110                                         }
111                                         if (stype === 'ss_local' || stype === 'ss_server') {
112                                                 o = s.taboption('advanced', form.FileUpload, 'acl',
113                                                         _('ACL file'),
114                                                         _('File containing Access Control List'));
115                                                 o.root_directory = '/etc/shadowsocks-libev';
116                                         }
117                                 }, this));
118                         }
119                 };
120
121                 o = s.option(form.DummyValue, 'overview', _('Overview'));
122                 o.modalonly = false;
123                 o.editable = true;
124                 o.rawhtml = true;
125                 o.renderWidget = function(section_id, option_index, cfgvalue) {
126                         var sdata = uci.get(conf, section_id);
127                         if (sdata) {
128                                 return form.DummyValue.prototype.renderWidget.call(this, section_id, option_index, ss.cfgvalue_overview(sdata));
129                         }
130                         return null;
131                 };
132
133                 o = s.option(form.DummyValue, 'running', _('Running'));
134                 o.modalonly = false;
135                 o.editable = true;
136                 o.default = '';
137
138                 o = s.option(form.Button, 'disabled', _('Enable/Disable'));
139                 o.modalonly = false;
140                 o.editable = true;
141                 o.inputtitle = function(section_id) {
142                         var s = uci.get(conf, section_id);
143                         if (ss.ucival_to_bool(s['disabled'])) {
144                                 this.inputstyle = 'reset';
145                                 return _('Disabled');
146                         }
147                         this.inputstyle = 'save';
148                         return _('Enabled');
149                 }
150                 o.onclick = function(ev) {
151                         var inputEl = ev.target.parentElement.nextElementSibling;
152                         inputEl.value = ss.ucival_to_bool(inputEl.value) ? '0' : '1';
153                         return this.map.save();
154                 }
155
156                 return m.render().finally(function() {
157                         poll.add(function() {
158                                 return L.resolveDefault(callServiceList(conf), {})
159                                 .then(function(res) {
160                                         var instances = null;
161                                         try {
162                                                 instances = res[conf]['instances'];
163                                         } catch (e) {}
164                                         if (!instances) return;
165                                         uci.sections(conf)
166                                         .filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
167                                         .forEach(function(s) {
168                                                 var el = document.getElementById('cbi-shadowsocks-libev-' + s['.name'] + '-running');
169                                                 if (el) {
170                                                         var name = s['.type'] + '.' + s['.name'],
171                                                                 running = instances.hasOwnProperty(name)? instances[name].running : false;
172                                                         el.innerText = running ? 'yes' : 'no';
173                                                 }
174                                         });
175                                 });
176                         });
177                 });
178         },
179 });
git clone https://git.99rst.org/PROJECT