9a591d596245db3a060a30c15205c69ddb18c8fc
[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('general', form.Value, 'bind_address',
88                                                         _('Bind address'),
89                                                         _('The address ss-server will initiate connection from'));
90                                                 o.datatype = 'ipaddr';
91                                                 o.placeholder = '0.0.0.0';
92                                                 ss.values_ipaddr(o, res[1]);
93                                         } else {
94                                                 ss.options_client(s, 'general', res[1]);
95                                                 if (stype === 'ss_tunnel') {
96                                                         o = s.taboption('general', form.Value, 'tunnel_address',
97                                                                 _('Tunnel address'),
98                                                                 _('The address ss-tunnel will forward traffic to'));
99                                                         o.datatype = 'hostport';
100                                                 }
101                                         }
102                                 }, this));
103                         }
104                 };
105
106                 o = s.option(form.DummyValue, 'overview', _('Overview'));
107                 o.modalonly = false;
108                 o.editable = true;
109                 o.rawhtml = true;
110                 o.renderWidget = function(section_id, option_index, cfgvalue) {
111                         var sdata = uci.get(conf, section_id);
112                         if (sdata) {
113                                 return form.DummyValue.prototype.renderWidget.call(this, section_id, option_index, ss.cfgvalue_overview(sdata));
114                         }
115                         return null;
116                 };
117
118                 o = s.option(form.DummyValue, 'running', _('Running'));
119                 o.modalonly = false;
120                 o.editable = true;
121                 o.default = '';
122
123                 o = s.option(form.Button, 'disabled', _('Enable/Disable'));
124                 o.modalonly = false;
125                 o.editable = true;
126                 o.inputtitle = function(section_id) {
127                         var s = uci.get(conf, section_id);
128                         if (ss.ucival_to_bool(s['disabled'])) {
129                                 this.inputstyle = 'reset';
130                                 return _('Disabled');
131                         }
132                         this.inputstyle = 'save';
133                         return _('Enabled');
134                 }
135                 o.onclick = function(ev) {
136                         var inputEl = ev.target.parentElement.nextElementSibling;
137                         inputEl.value = ss.ucival_to_bool(inputEl.value) ? '0' : '1';
138                         return this.map.save();
139                 }
140
141                 return m.render().finally(function() {
142                         poll.add(function() {
143                                 return L.resolveDefault(callServiceList(conf), {})
144                                 .then(function(res) {
145                                         var instances = null;
146                                         try {
147                                                 instances = res[conf]['instances'];
148                                         } catch (e) {}
149                                         if (!instances) return;
150                                         uci.sections(conf)
151                                         .filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
152                                         .forEach(function(s) {
153                                                 var el = document.getElementById('cbi-shadowsocks-libev-' + s['.name'] + '-running');
154                                                 if (el) {
155                                                         var name = s['.type'] + '.' + s['.name'],
156                                                                 running = instances.hasOwnProperty(name)? instances[name].running : false;
157                                                         el.innerText = running ? 'yes' : 'no';
158                                                 }
159                                         });
160                                 });
161                         });
162                 });
163         },
164 });
git clone https://git.99rst.org/PROJECT