summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-shadowsocks-libev/htdocs/luci-static/resources/view/shadowsocks-libev/instances.js
blob: 9a591d596245db3a060a30c15205c69ddb18c8fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
'require view';
'require poll';
'require form';
'require uci';
'require fs';
'require network';
'require rpc';
'require shadowsocks-libev as ss';

var conf = 'shadowsocks-libev';
var cfgtypes = ['ss_local', 'ss_redir', 'ss_server', 'ss_tunnel'];

var callServiceList = rpc.declare({
	object: 'service',
	method: 'list',
	params: [ 'name' ],
	expect: { '': {} }
});

return view.extend({
	render: function(stats) {
		var m, s, o;

		m = new form.Map(conf,
			_('Local Instances'),
			_('Instances of shadowsocks-libev components, e.g. ss-local, \
			   ss-redir, ss-tunnel, ss-server, etc.  To enable an instance it \
			   is required to enable both the instance itself and the remote \
			   server it refers to.'));

		s = m.section(form.GridSection);
		s.addremove = true;
		s.cfgsections = function() {
			return this.map.data.sections(this.map.config)
				.filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
				.map(function(s) { return s['.name']; });
		};
		s.sectiontitle = function(section_id) {
			var s = uci.get(conf, section_id);
			return (s ? s['.type'] + '.' : '') + section_id;
		};
		s.renderSectionAdd = function(extra_class) {
			var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
				optionEl = [E('option', { value: '_dummy' }, [_('-- instance type --')])];
			cfgtypes.forEach(function(t) {
				optionEl.push(E('option', { value: t }, [t.replace('_', '-')]));
			});
			var selectEl = E('select', {
				class: 'cbi-input-select',
				change: function(ev) {
					ev.target.parentElement.nextElementSibling.nextElementSibling
						.toggleAttribute('disabled', ev.target.value === '_dummy');
				}
			}, optionEl);
			el.lastElementChild.setAttribute('disabled', '');
			el.prepend(E('div', {}, selectEl));
			return el;
		};
		s.handleAdd = function(ev, name) {
			var selectEl = ev.target.parentElement.firstElementChild.firstElementChild,
				type = selectEl.value;
			this.sectiontype = type;
			var promise = form.GridSection.prototype.handleAdd.apply(this, arguments);
			this.sectiontype = undefined;
			return promise;
		};
		s.addModalOptions = function(s, section_id, ev) {
			var sdata = uci.get(conf, section_id),
				stype = sdata ? sdata['.type'] : null;
			if (stype) {
				s.sectiontype = stype;
				return Promise.all([
					L.resolveDefault(fs.stat('/usr/bin/' + stype.replace('_', '-')), null),
					network.getDevices()
				]).then(L.bind(function(res) {
					s.tab('general', _('General Settings'));
					s.tab('advanced', _('Advanced Settings'));
					s.taboption('general', form.Flag, 'disabled', _('Disable'));
					if (!res[0]) {
						ss.option_install_package(s, 'general');
					}
					ss.options_common(s, 'advanced');

					if (stype === 'ss_server') {
						ss.options_server(s, { tab: 'general' });
						o = s.taboption('general', form.Value, 'bind_address',
							_('Bind address'),
							_('The address ss-server will initiate connection from'));
						o.datatype = 'ipaddr';
						o.placeholder = '0.0.0.0';
						ss.values_ipaddr(o, res[1]);
					} else {
						ss.options_client(s, 'general', res[1]);
						if (stype === 'ss_tunnel') {
							o = s.taboption('general', form.Value, 'tunnel_address',
								_('Tunnel address'),
								_('The address ss-tunnel will forward traffic to'));
							o.datatype = 'hostport';
						}
					}
				}, this));
			}
		};

		o = s.option(form.DummyValue, 'overview', _('Overview'));
		o.modalonly = false;
		o.editable = true;
		o.rawhtml = true;
		o.renderWidget = function(section_id, option_index, cfgvalue) {
			var sdata = uci.get(conf, section_id);
			if (sdata) {
				return form.DummyValue.prototype.renderWidget.call(this, section_id, option_index, ss.cfgvalue_overview(sdata));
			}
			return null;
		};

		o = s.option(form.DummyValue, 'running', _('Running'));
		o.modalonly = false;
		o.editable = true;
		o.default = '';

		o = s.option(form.Button, 'disabled', _('Enable/Disable'));
		o.modalonly = false;
		o.editable = true;
		o.inputtitle = function(section_id) {
			var s = uci.get(conf, section_id);
			if (ss.ucival_to_bool(s['disabled'])) {
				this.inputstyle = 'reset';
				return _('Disabled');
			}
			this.inputstyle = 'save';
			return _('Enabled');
		}
		o.onclick = function(ev) {
			var inputEl = ev.target.parentElement.nextElementSibling;
			inputEl.value = ss.ucival_to_bool(inputEl.value) ? '0' : '1';
			return this.map.save();
		}

		return m.render().finally(function() {
			poll.add(function() {
				return L.resolveDefault(callServiceList(conf), {})
				.then(function(res) {
					var instances = null;
					try {
						instances = res[conf]['instances'];
					} catch (e) {}
					if (!instances) return;
					uci.sections(conf)
					.filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
					.forEach(function(s) {
						var el = document.getElementById('cbi-shadowsocks-libev-' + s['.name'] + '-running');
						if (el) {
							var name = s['.type'] + '.' + s['.name'],
								running = instances.hasOwnProperty(name)? instances[name].running : false;
							el.innerText = running ? 'yes' : 'no';
						}
					});
				});
			});
		});
	},
});