OSDN Git Service

luci-app-v2raya: fix bind arguments
[v2raya/v2raya-openwrt.git] / luci-app-v2raya / htdocs / luci-static / resources / view / v2raya / config.js
1 /* SPDX-License-Identifier: GPL-3.0-only
2  *
3  * Copyright (C) 2022 ImmortalWrt.org
4  */
5
6 'use strict';
7 'require form';
8 'require fs';
9 'require poll';
10 'require rpc';
11 'require uci';
12 'require ui';
13 'require validation';
14 'require view';
15
16 var callServiceList = rpc.declare({
17         object: 'service',
18         method: 'list',
19         params: ['name'],
20         expect: { '': {} }
21 });
22
23 function getServiceStatus() {
24         return L.resolveDefault(callServiceList('v2raya'), {}).then(function (res) {
25                 var isRunning = false;
26                 try {
27                         isRunning = res['v2raya']['instances']['v2raya']['running'];
28                 } catch (e) { }
29                 return isRunning;
30         });
31 }
32
33 function renderStatus(isRunning, port) {
34         var spanTemp = '<span style="color:%s"><strong>%s %s</strong></span>';
35         var renderHTML;
36         if (isRunning) {
37                 var button = String.format('&#160;<a class="btn cbi-button" href="%s:%s" target="_blank" rel="noreferrer noopener">%s</a>',
38                         window.location.origin, port, _('Open Web Interface'));
39                 renderHTML = spanTemp.format('green', _('v2rayA'), _('RUNNING')) + button;
40         } else {
41                 renderHTML = spanTemp.format('red', _('v2rayA'), _('NOT RUNNING'));
42         }
43
44         return renderHTML;
45 }
46
47 function uploadCertificate(type, filename, ev) {
48         L.resolveDefault(fs.exec('/bin/mkdir', [ '-p', '/etc/v2raya/' ]));
49
50         return ui.uploadFile('/etc/v2raya/' + filename, ev.target)
51         .then(L.bind(function(btn, res) {
52                 btn.firstChild.data = _('Checking %s...').format(type);
53
54                 if (res.size <= 0) {
55                         ui.addNotification(null, E('p', _('The uploaded %s is empty.').format(type)));
56                         return fs.remove('/etc/v2raya/' + filename);
57                 }
58
59                 ui.addNotification(null, E('p', _('Your %s was successfully uploaded. Size: %sB.').format(type, res.size)));
60         }, this, ev.target))
61         .catch(function(e) { ui.addNotification(null, E('p', e.message)) })
62         .finally(L.bind(function(btn, input) {
63                 btn.firstChild.data = _('Upload...');
64         }, this, ev.target));
65 }
66
67 return view.extend({
68         load: function() {
69                 return Promise.all([
70                         uci.load('v2raya')
71                 ]);
72         },
73
74         render: function(data) {
75                 var m, s, o;
76                 var webport = (uci.get(data[0], 'config', 'address') || '0.0.0.0:2017').split(':').slice(-1)[0];
77
78                 m = new form.Map('v2raya', _('v2rayA'),
79                         _('v2rayA is a V2Ray Linux client supporting global transparent proxy, compatible with SS, SSR, Trojan(trojan-go), PingTunnel protocols.'));
80
81                 s = m.section(form.TypedSection);
82                 s.anonymous = true;
83                 s.render = function () {
84                         poll.add(function () {
85                                 return L.resolveDefault(getServiceStatus()).then(function (res) {
86                                         var view = document.getElementById('service_status');
87                                         view.innerHTML = renderStatus(res, webport);
88                                 });
89                         });
90
91                         return E('div', { class: 'cbi-section', id: 'status_bar' }, [
92                                         E('p', { id: 'service_status' }, _('Collecting data...'))
93                         ]);
94                 }
95
96                 s = m.section(form.NamedSection, 'config', 'v2raya');
97
98                 o = s.option(form.Flag, 'enabled', _('Enable'));
99                 o.default = o.disabled;
100                 o.rmempty = false;
101
102                 o = s.option(form.Value, 'address', _('Listening address'));
103                 o.datatype = 'ipaddrport(1)';
104                 o.default = '0.0.0.0:2017';
105                 o.rmempty = false;
106
107                 o = s.option(form.Value, 'config', _('Configuration directory'));
108                 o.datatype = 'path';
109                 o.default = '/etc/v2raya';
110                 o.rmempty = false;
111
112                 o = s.option(form.ListValue, 'ipv6_support', _('IPv6 support'),
113                         _('Make sure your IPv6 network works fine before you turn it on.'));
114                 o.value('auto', _('Auto'));
115                 o.value('on', _('On'));
116                 o.value('off', _('Off'));
117                 o.default = 'auto';
118                 o.rmempty = false;
119
120                 o = s.option(form.ListValue, 'log_level', _('Log level'));
121                 o.value('trace', _('Trace'));
122                 o.value('debug', _('Debug'));
123                 o.value('info', _('Info'));
124                 o.value('warn', _('Warn'));
125                 o.value('error', _('Error'));
126                 o.default = 'info';
127                 o.rmempty = false;
128
129                 o = s.option(form.Value, 'log_file', _('Log file path'));
130                 o.datatype = 'path';
131                 o.default = '/var/log/v2raya/v2raya.log';
132                 o.rmempty = false;
133                 /* Due to ACL rule, this value must retain default otherwise log page will be broken */
134                 o.readonly = true;
135
136                 o = s.option(form.Value, 'log_max_days', _('Max log retention period'),
137                         _('Maximum number of days to keep log files.'));
138                 o.datatype = 'uinteger';
139                 o.default = '3';
140                 o.rmempty = false;
141
142                 o = s.option(form.Flag, 'log_disable_color', _('Disable log color output'));
143                 o.default = o.enabled;
144                 o.rmempty = false;
145
146                 o = s.option(form.Flag, 'log_disable_timestamp', _('Disable log timestamp'));
147                 o.default = o.disabled;
148                 o.rmempty = false;
149
150                 o = s.option(form.Value, 'v2ray_bin', _('v2ray binary path'),
151                         _('Executable v2ray binary path. Auto-detect if put it empty (recommended).'));
152                 o.datatype = 'path';
153
154                 o = s.option(form.Value, 'v2ray_confdir', _('Extra config directory'),
155                         _('Additional v2ray config directory, files in it will be combined with config generated by v2rayA.'));
156                 o.datatype = 'path';
157
158                 o = s.option(form.Value, 'vless_grpc_inbound_cert_key', _('Certpath for gRPC inbound'),
159                         _('Specify the certification path instead of automatically generating a self-signed certificate.'));
160                 o.value('', _('Automatically generate'));
161                 o.value('/etc/v2raya/grpc_certificate.crt,/etc/v2raya/grpc_private.key');
162
163                 o = s.option(form.Button, '_upload_cert', _('Upload certificate'));
164                 o.inputstyle = 'action';
165                 o.inputtitle = _('Upload...');
166                 o.onclick = L.bind(uploadCertificate, this, _('certificate'), 'grpc_certificate.crt');
167                 o.depends('vless_grpc_inbound_cert_key', '/etc/v2raya/grpc_certificate.crt,/etc/v2raya/grpc_private.key');
168
169                 o = s.option(form.Button, '_upload_key', _('Upload privateKey'));
170                 o.inputstyle = 'action';
171                 o.inputtitle = _('Upload...');
172                 o.onclick = L.bind(uploadCertificate, this, _('private key'), 'grpc_private.key');
173                 o.depends('vless_grpc_inbound_cert_key', '/etc/v2raya/grpc_certificate.crt,/etc/v2raya/grpc_private.key');
174
175                 return m.render();
176         }
177 });