luci-app-banip: handle load errors of the countries file
[project/luci.git] / applications / luci-app-frpc / htdocs / luci-static / resources / view / frpc.js
1 'use strict';
2 'require view';
3 'require ui';
4 'require form';
5 'require rpc';
6 'require tools.widgets as widgets';
7
8 // [Widget, Option, Title, Description, {Param: 'Value'}],
9 var startupConf = [
10 [form.Flag, 'stdout', _('Log stdout')],
11 [form.Flag, 'stderr', _('Log stderr')],
12 [widgets.UserSelect, 'user', _('Run daemon as user')],
13 [widgets.GroupSelect, 'group', _('Run daemon as group')],
14 [form.Flag, 'respawn', _('Respawn when crashed')],
15 [form.DynamicList, 'env', _('Environment variable'), _('OS environments pass to frp for config file template, see <a href="https://github.com/fatedier/frp#configuration-file-template">frp README</a>'), {placeholder: 'ENV_NAME=value'}],
16 [form.DynamicList, 'conf_inc', _('Additional configs'), _('Config files include in temporary config file'), {placeholder: '/etc/frp/frpc.d/frpc_full.ini'}]
17 ];
18
19 var commonConf = [
20 [form.Value, 'server_addr', _('Server address'), _('ServerAddr specifies the address of the server to connect to.<br />By default, this value is "0.0.0.0".'), {datatype: 'host'}],
21 [form.Value, 'server_port', _('Server port'), _('ServerPort specifies the port to connect to the server on.<br />By default, this value is 7000.'), {datatype: 'port'}],
22 [form.Value, 'http_proxy', _('HTTP proxy'), _('HttpProxy specifies a proxy address to connect to the server through. If this value is "", the server will be connected to directly.<br />By default, this value is read from the "http_proxy" environment variable.')],
23 [form.ListValue, 'log_level', _('Log level'), _('LogLevel specifies the minimum log level. Valid values are "trace", "debug", "info", "warn", and "error".<br />By default, this value is "info".'), {values: ['trace', 'debug', 'info', 'warn', 'error']}],
24 [form.Flag, 'disable_log_color', _('Disable log color'), _('DisableLogColor disables log colors when LogWay == "console" when set to true.'), {datatype: 'bool', default: 'false'}],
25 [form.Value, 'token', _('Token'), _('Token specifies the authorization token used to create keys to be sent to the server. The server must have a matching token for authorization to succeed. <br />By default, this value is "".')],
26 [form.Value, 'admin_addr', _('Admin address'), _('AdminAddr specifies the address that the admin server binds to.<br />By default, this value is "127.0.0.1".'), {datatype: 'ipaddr'}],
27 [form.Value, 'admin_port', _('Admin port'), _('AdminPort specifies the port for the admin server to listen on. If this value is 0, the admin server will not be started.<br />By default, this value is 0.'), {datatype: 'port'}],
28 [form.Value, 'admin_user', _('Admin user'), _('AdminUser specifies the username that the admin server will use for login.<br />By default, this value is "admin".')],
29 [form.Value, 'admin_pwd', _('Admin password'), _('AdminPwd specifies the password that the admin server will use for login.<br />By default, this value is "admin".'), {password: true}],
30 [form.Value, 'assets_dir', _('Assets dir'), _('AssetsDir specifies the local directory that the admin server will load resources from. If this value is "", assets will be loaded from the bundled executable using statik.<br />By default, this value is "".')],
31 [form.Flag, 'tcp_mux', _('TCP mux'), _('TcpMux toggles TCP stream multiplexing. This allows multiple requests from a client to share a single TCP connection. If this value is true, the server must have TCP multiplexing enabled as well.<br />By default, this value is true.'), {datatype: 'bool', default: 'true'}],
32 [form.Value, 'user', _('User'), _('User specifies a prefix for proxy names to distinguish them from other clients. If this value is not "", proxy names will automatically be changed to "{user}.{proxy_name}".<br />By default, this value is "".')],
33 [form.Flag, 'login_fail_exit', _('Exit when login fail'), _('LoginFailExit controls whether or not the client should exit after a failed login attempt. If false, the client will retry until a login attempt succeeds.<br />By default, this value is true.'), {datatype: 'bool', default: 'true'}],
34 [form.ListValue, 'protocol', _('Protocol'), _('Protocol specifies the protocol to use when interacting with the server. Valid values are "tcp", "kcp", and "websocket".<br />By default, this value is "tcp".'), {values: ['tcp', 'kcp', 'websocket']}],
35 [form.Flag, 'tls_enable', _('TLS'), _('TLSEnable specifies whether or not TLS should be used when communicating with the server.'), {datatype: 'bool'}],
36 [form.Value, 'heartbeat_interval', _('Heartbeat interval'), _('HeartBeatInterval specifies at what interval heartbeats are sent to the server, in seconds. It is not recommended to change this value.<br />By default, this value is 30.'), {datatype: 'uinteger'}],
37 [form.Value, 'heartbeat_timeout', _('Heartbeat timeout'), _('HeartBeatTimeout specifies the maximum allowed heartbeat response delay before the connection is terminated, in seconds. It is not recommended to change this value.<br />By default, this value is 90.'), {datatype: 'uinteger'}],
38 [form.DynamicList, '_', _('Additional settings'), _('This list can be used to specify some additional parameters which have not been included in this LuCI.'), {placeholder: 'Key-A=Value-A'}]
39 ];
40
41 var baseProxyConf = [
42 [form.ListValue, 'type', _('Proxy type'), _('ProxyType specifies the type of this proxy. Valid values include "tcp", "udp", "http", "https", "stcp", and "xtcp".<br />By default, this value is "tcp".'), {values: ['tcp', 'udp', 'http', 'https', 'stcp', 'xtcp']}],
43 [form.Flag, 'use_encryption', _('Encryption'), _('UseEncryption controls whether or not communication with the server will be encrypted. Encryption is done using the tokens supplied in the server and client configuration.<br />By default, this value is false.'), {datatype: 'bool'}],
44 [form.Flag, 'use_compression', _('Compression'), _('UseCompression controls whether or not communication with the server will be compressed.<br />By default, this value is false.'), {datatype: 'bool'}],
45 [form.Value, 'local_ip', _('Local IP'), _('LocalIp specifies the IP address or host name to proxy to.'), {datatype: 'ipaddr'}],
46 [form.Value, 'local_port', _('Local port'), _('LocalPort specifies the port to proxy to.'), {datatype: 'port'}],
47 ];
48
49 var bindInfoConf = [
50 [form.Value, 'remote_port', _('Remote port'), _('If remote_port is 0, frps will assign a random port for you'), {datatype: 'port'}]
51 ];
52
53 var domainConf = [
54 [form.Value, 'custom_domains', _('Custom domains')],
55 [form.Value, 'subdomain', _('Subdomain')],
56 ];
57
58 var httpProxyConf = [
59 [form.Value, 'locations', _('Locations')],
60 [form.Value, 'http_user', _('HTTP user')],
61 [form.Value, 'http_pwd', _('HTTP password')],
62 [form.Value, 'host_header_rewrite', _('Host header rewrite')],
63 // [form.Value, 'headers', _('Headers')], // FIXME
64 ];
65
66 var stcpProxyConf = [
67 [form.ListValue, 'role', _('Role'), undefined, {values: ['server', 'visitor']}],
68 [form.Value, 'sk', _('Sk')],
69 ];
70
71 function setParams(o, params) {
72 if (!params) return;
73 for (var key in params) {
74 var val = params[key];
75 if (key === 'values') {
76 for (var j = 0; j < val.length; j++) {
77 var args = val[j];
78 if (!Array.isArray(args))
79 args = [args];
80 o.value.apply(o, args);
81 }
82 } else if (key === 'depends') {
83 if (!Array.isArray(val))
84 val = [val];
85 for (var j = 0; j < val.length; j++) {
86 var args = val[j];
87 if (!Array.isArray(args))
88 args = [args];
89 o.depends.apply(o, args);
90 }
91 } else {
92 o[key] = params[key];
93 }
94 }
95 if (params['datatype'] === 'bool') {
96 o.enabled = 'true';
97 o.disabled = 'false';
98 }
99 }
100
101 function defTabOpts(s, t, opts, params) {
102 for (var i = 0; i < opts.length; i++) {
103 var opt = opts[i];
104 var o = s.taboption(t, opt[0], opt[1], opt[2], opt[3]);
105 setParams(o, opt[4]);
106 setParams(o, params);
107 }
108 }
109
110 function defOpts(s, opts, params) {
111 for (var i = 0; i < opts.length; i++) {
112 var opt = opts[i];
113 var o = s.option(opt[0], opt[1], opt[2], opt[3]);
114 setParams(o, opt[4]);
115 setParams(o, params);
116 }
117 }
118
119 var callServiceList = rpc.declare({
120 object: 'service',
121 method: 'list',
122 params: ['name'],
123 expect: { '': {} }
124 });
125
126 function getServiceStatus() {
127 return L.resolveDefault(callServiceList('frpc'), {}).then(function (res) {
128 var isRunning = false;
129 try {
130 isRunning = res['frpc']['instances']['instance1']['running'];
131 } catch (e) { }
132 return isRunning;
133 });
134 }
135
136 function renderStatus(isRunning) {
137 var renderHTML = "";
138 var spanTemp = '<em><span style="color:%s"><strong>%s %s</strong></span></em>';
139
140 if (isRunning) {
141 renderHTML += String.format(spanTemp, 'green', _("frp Client"), _("RUNNING"));
142 } else {
143 renderHTML += String.format(spanTemp, 'red', _("frp Client"), _("NOT RUNNING"));
144 }
145
146 return renderHTML;
147 }
148
149 return view.extend({
150 render: function() {
151 var m, s, o;
152
153 m = new form.Map('frpc', _('frp Client'));
154
155 s = m.section(form.NamedSection, '_status');
156 s.anonymous = true;
157 s.render = function (section_id) {
158 L.Poll.add(function () {
159 return L.resolveDefault(getServiceStatus()).then(function(res) {
160 var view = document.getElementById("service_status");
161 view.innerHTML = renderStatus(res);
162 });
163 });
164
165 return E('div', { class: 'cbi-map' },
166 E('fieldset', { class: 'cbi-section'}, [
167 E('p', { id: 'service_status' },
168 _('Collecting data ...'))
169 ])
170 );
171 }
172
173 s = m.section(form.NamedSection, 'common', 'conf');
174 s.dynamic = true;
175
176 s.tab('common', _('Common Settings'));
177 s.tab('init', _('Startup Settings'));
178
179 defTabOpts(s, 'common', commonConf, {optional: true});
180
181 o = s.taboption('init', form.SectionValue, 'init', form.TypedSection, 'init', _('Startup Settings'));
182 s = o.subsection;
183 s.anonymous = true;
184 s.dynamic = true;
185
186 defOpts(s, startupConf);
187
188 s = m.section(form.GridSection, 'conf', _('Proxy Settings'));
189 s.addremove = true;
190 s.filter = function(s) { return s !== 'common'; };
191 s.renderSectionAdd = function(extra_class) {
192 var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
193 nameEl = el.querySelector('.cbi-section-create-name');
194 ui.addValidator(nameEl, 'uciname', true, function(v) {
195 if (v === 'common') return _('Name can not be "common"');
196 return true;
197 }, 'blur', 'keyup');
198 return el;
199 }
200
201 s.tab('general', _('General Settings'));
202 s.tab('http', _('HTTP Settings'));
203
204 s.option(form.Value, 'type', _('Proxy type')).modalonly = false;
205 s.option(form.Value, 'local_ip', _('Local IP')).modalonly = false;
206 s.option(form.Value, 'local_port', _('Local port')).modalonly = false;
207
208 defTabOpts(s, 'general', baseProxyConf, {modalonly: true});
209
210 // TCP and UDP
211 defTabOpts(s, 'general', bindInfoConf, {optional: true, modalonly: true, depends: [{type: 'tcp'}, {type: 'udp'}]});
212
213 // HTTP and HTTPS
214 defTabOpts(s, 'http', domainConf, {optional: true, modalonly: true, depends: [{type: 'http'}, {type: 'https'}]});
215
216 // HTTP
217 defTabOpts(s, 'http', httpProxyConf, {optional: true, modalonly: true, depends: {type: 'http'}});
218
219 // STCP and XTCP
220 defTabOpts(s, 'general', stcpProxyConf, {modalonly: true, depends: [{type: 'stcp'}, {type: 'xtcp'}]});
221
222 return m.render();
223 }
224 });