frps.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict';
  2. 'require view';
  3. 'require form';
  4. 'require rpc';
  5. 'require tools.widgets as widgets';
  6. // [Widget, Option, Title, Description, {Param: 'Value'}],
  7. var startupConf = [
  8. [form.Flag, 'stdout', _('Log stdout')],
  9. [form.Flag, 'stderr', _('Log stderr')],
  10. [widgets.UserSelect, 'user', _('Run daemon as user')],
  11. [widgets.GroupSelect, 'group', _('Run daemon as group')],
  12. [form.Flag, 'respawn', _('Respawn when crashed')],
  13. [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'}],
  14. [form.DynamicList, 'conf_inc', _('Additional configs'), _('Config files include in temporary config file'), {placeholder: '/etc/frp/frps.d/frps_full.ini'}]
  15. ];
  16. var commonConf = [
  17. [form.Value, 'bind_addr', _('Bind address'), _('BindAddr specifies the address that the server binds to.<br />By default, this value is "0.0.0.0".'), {datatype: 'ipaddr'}],
  18. [form.Value, 'bind_port', _('Bind port'), _('BindPort specifies the port that the server listens on.<br />By default, this value is 7000.'), {datatype: 'port'}],
  19. [form.Value, 'bind_udp_port', _('UDP bind port'), _('BindUdpPort specifies the UDP port that the server listens on. If this value is 0, the server will not listen for UDP connections.<br />By default, this value is 0'), {datatype: 'port'}],
  20. [form.Value, 'kcp_bind_port', _('KCP bind port'), _('BindKcpPort specifies the KCP port that the server listens on. If this value is 0, the server will not listen for KCP connections.<br />By default, this value is 0.'), {datatype: 'port'}],
  21. [form.Value, 'proxy_bind_addr', _('Proxy bind address'), _('ProxyBindAddr specifies the address that the proxy binds to. This value may be the same as BindAddr.<br />By default, this value is "0.0.0.0".'), {datatype: 'ipaddr'}],
  22. [form.Value, 'vhost_http_port', _('Vhost HTTP port'), _('VhostHttpPort specifies the port that the server listens for HTTP Vhost requests. If this value is 0, the server will not listen for HTTP requests.<br />By default, this value is 0.'), {datatype: 'port'}],
  23. [form.Value, 'vhost_https_port', _('Vhost HTTPS port'), _('VhostHttpsPort specifies the port that the server listens for HTTPS Vhost requests. If this value is 0, the server will not listen for HTTPS requests.<br />By default, this value is 0.'), {datatype: 'port'}],
  24. [form.Value, 'vhost_http_timeout', _('Vhost HTTP timeout'), _('VhostHttpTimeout specifies the response header timeout for the Vhost HTTP server, in seconds.<br />By default, this value is 60.'), {datatype: 'uinteger'}],
  25. [form.Value, 'dashboard_addr', _('Dashboard address'), _('DashboardAddr specifies the address that the dashboard binds to.<br />By default, this value is "0.0.0.0".'), {datatype: 'ipaddr'}],
  26. [form.Value, 'dashboard_port', _('Dashboard port'), _('DashboardPort specifies the port that the dashboard listens on. If this value is 0, the dashboard will not be started.<br />By default, this value is 0.'), {datatype: 'port'}],
  27. [form.Value, 'dashboard_user', _('Dashboard user'), _('DashboardUser specifies the username that the dashboard will use for login.<br />By default, this value is "admin".')],
  28. [form.Value, 'dashboard_pwd', _('Dashboard password'), _('DashboardPwd specifies the password that the dashboard will use for login.<br />By default, this value is "admin".'), {password: true}],
  29. [form.Value, 'assets_dir', _('Assets dir'), _('AssetsDir specifies the local directory that the dashboard will load resources from. If this value is "", assets will be loaded from the bundled executable using statik.<br />By default, this value is "".')],
  30. [form.Value, 'log_file', _('Log file'), _('LogFile specifies a file where logs will be written to. This value will only be used if LogWay is set appropriately.<br />By default, this value is "console".')],
  31. [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']}],
  32. [form.Value, 'log_max_days', _('Log max days'), _('LogMaxDays specifies the maximum number of days to store log information before deletion. This is only used if LogWay == "file".<br />By default, this value is 0.'), {datatype: 'uinteger'}],
  33. [form.Flag, 'disable_log_color', _('Disable log color'), _('DisableLogColor disables log colors when LogWay == "console" when set to true.<br />By default, this value is false.'), {datatype: 'bool', default: 'true'}],
  34. [form.Value, 'token', _('Token'), _('Token specifies the authorization token used to authenticate keys received from clients. Clients must have a matching token to be authorized to use the server.<br />By default, this value is "".')],
  35. [form.Value, 'subdomain_host', _('Subdomain host'), _('SubDomainHost specifies the domain that will be attached to sub-domains requested by the client when using Vhost proxying. For example, if this value is set to "frps.com" and the client requested the subdomain "test", the resulting URL would be "test.frps.com".<br />By default, this value is "".')],
  36. [form.Flag, 'tcp_mux', _('TCP mux'), _('TcpMux toggles TCP stream multiplexing. This allows multiple requests from a client to share a single TCP connection.<br />By default, this value is true.'), {datatype: 'bool', default: 'true'}],
  37. [form.Value, 'custom_404_page', _('Custom 404 page'), _('Custom404Page specifies a path to a custom 404 page to display. If this value is "", a default page will be displayed.<br />By default, this value is "".')],
  38. [form.Value, 'allow_ports', _('Allow ports'), _('AllowPorts specifies a set of ports that clients are able to proxy to. If the length of this value is 0, all ports are allowed.<br />By default, this value is an empty set.')],
  39. [form.Value, 'max_ports_per_client', _('Max ports per client'), _('MaxPortsPerClient specifies the maximum number of ports a single client may proxy to. If this value is 0, no limit will be applied.<br />By default, this value is 0.'), {datatype: 'uinteger'}],
  40. [form.Value, 'heartbeat_timeout', _('Heartbeat timeout'), _('HeartBeatTimeout specifies the maximum time to wait for a heartbeat before terminating the connection. It is not recommended to change this value.<br />By default, this value is 90.'), {datatype: 'uinteger'}],
  41. [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'}]
  42. ];
  43. function setParams(o, params) {
  44. if (!params) return;
  45. for (var key in params) {
  46. var val = params[key];
  47. if (key === 'values') {
  48. for (var j = 0; j < val.length; j++) {
  49. var args = val[j];
  50. if (!Array.isArray(args))
  51. args = [args];
  52. o.value.apply(o, args);
  53. }
  54. } else if (key === 'depends') {
  55. if (!Array.isArray(val))
  56. val = [val];
  57. for (var j = 0; j < val.length; j++) {
  58. var args = val[j];
  59. if (!Array.isArray(args))
  60. args = [args];
  61. o.depends.apply(o, args);
  62. }
  63. } else {
  64. o[key] = params[key];
  65. }
  66. }
  67. if (params['datatype'] === 'bool') {
  68. o.enabled = 'true';
  69. o.disabled = 'false';
  70. }
  71. }
  72. function defTabOpts(s, t, opts, params) {
  73. for (var i = 0; i < opts.length; i++) {
  74. var opt = opts[i];
  75. var o = s.taboption(t, opt[0], opt[1], opt[2], opt[3]);
  76. setParams(o, opt[4]);
  77. setParams(o, params);
  78. }
  79. }
  80. function defOpts(s, opts, params) {
  81. for (var i = 0; i < opts.length; i++) {
  82. var opt = opts[i];
  83. var o = s.option(opt[0], opt[1], opt[2], opt[3]);
  84. setParams(o, opt[4]);
  85. setParams(o, params);
  86. }
  87. }
  88. const callServiceList = rpc.declare({
  89. object: 'service',
  90. method: 'list',
  91. params: ['name'],
  92. expect: { '': {} }
  93. });
  94. function getServiceStatus() {
  95. return L.resolveDefault(callServiceList('frps'), {}).then(function (res) {
  96. var isRunning = false;
  97. try {
  98. isRunning = res['frps']['instances']['instance1']['running'];
  99. } catch (e) { }
  100. return isRunning;
  101. });
  102. }
  103. function renderStatus(isRunning) {
  104. var renderHTML = "";
  105. var spanTemp = '<em><span style="color:%s"><strong>%s %s</strong></span></em>';
  106. if (isRunning) {
  107. renderHTML += String.format(spanTemp, 'green', _("frp Server"), _("RUNNING"));
  108. } else {
  109. renderHTML += String.format(spanTemp, 'red', _("frp Server"), _("NOT RUNNING"));
  110. }
  111. return renderHTML;
  112. }
  113. return view.extend({
  114. render: function() {
  115. let m, s, o;
  116. m = new form.Map('frps', _('frp Server'));
  117. s = m.section(form.NamedSection, '_status');
  118. s.anonymous = true;
  119. s.render = function (section_id) {
  120. L.Poll.add(function () {
  121. return L.resolveDefault(getServiceStatus()).then(function(res) {
  122. var view = document.getElementById("service_status");
  123. view.innerHTML = renderStatus(res);
  124. });
  125. });
  126. return E('div', { class: 'cbi-map' },
  127. E('fieldset', { class: 'cbi-section'}, [
  128. E('p', { id: 'service_status' },
  129. _('Collecting data ...'))
  130. ])
  131. );
  132. }
  133. s = m.section(form.NamedSection, 'common', 'conf');
  134. s.dynamic = true;
  135. s.tab('common', _('Common settings'));
  136. s.tab('init', _('Startup settings'));
  137. defTabOpts(s, 'common', commonConf, {optional: true});
  138. o = s.taboption('init', form.SectionValue, 'init', form.TypedSection, 'init', _('Startup settings'));
  139. s = o.subsection;
  140. s.anonymous = true;
  141. s.dynamic = true;
  142. defOpts(s, startupConf);
  143. return m.render();
  144. }
  145. });