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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
|
'use strict';
'require view';
'require form';
'require rpc';
'require tools.widgets as widgets';
/*
* Important:
*
* This LuCI page writes UCI options into /etc/config/frps.
* UCI option names must not contain ".".
*
* Therefore we keep UCI-safe option names here, then let init/generator script
* convert them to frp TOML keys, for example:
*
* bind_addr -> bindAddr
* bind_port -> bindPort
* kcp_bind_port -> kcpBindPort
* quic_bind_port -> quicBindPort
* admin_port -> webServer.port
* admin_tls_cert_file -> webServer.tls.certFile
* tls_force -> transport.tls.force
* max_pool_count -> transport.maxPoolCount
* authentication_method -> auth.method
* token_source_type -> auth.tokenSource.type
*
* Do NOT use option names like "webServer.port" in LuCI form options.
*/
/*
* Debug-only TOML mapping. Keep these TOML: hints in the JS source for
* troubleshooting generated configs, but do not show them in LuCI form text
* and do not wrap them in _().
*
* common.bind_addr -> TOML: bindAddr
* common.bind_port -> TOML: bindPort
* common.kcp_bind_port -> TOML: kcpBindPort
* common.quic_bind_port -> TOML: quicBindPort
* common.proxy_bind_addr -> TOML: proxyBindAddr
* common.vhost_http_port -> TOML: vhostHTTPPort
* common.vhost_https_port -> TOML: vhostHTTPSPort
* common.vhost_http_timeout -> TOML: vhostHTTPTimeout
* common.tcpmux_httpconnect_port -> TOML: tcpmuxHTTPConnectPort
* common.tcpmux_passthrough -> TOML: tcpmuxPassthrough
* common.subdomain_host -> TOML: subDomainHost
* common.custom_404_page -> TOML: custom404Page
* common.udp_packet_size -> TOML: udpPacketSize
* common.detailed_errors_to_client -> TOML: detailedErrorsToClient
* common.user_conn_timeout -> TOML: userConnTimeout
* common.nathole_analysis_data_reserve_hours -> TOML: natholeAnalysisDataReserveHours
* common.authentication_method -> TOML: auth.method
* common.token -> TOML: auth.token
* common.auth_additional_scopes -> TOML: auth.additionalScopes
* common.token_source_type -> TOML: auth.tokenSource.type
* common.token_source_file_path -> TOML: auth.tokenSource.file.path
* common.token_source_exec_command -> TOML: auth.tokenSource.exec.command
* common.token_source_exec_args -> TOML: auth.tokenSource.exec.args
* common.token_source_exec_env -> TOML: auth.tokenSource.exec.env
* common.oidc_issuer -> TOML: auth.oidc.issuer
* common.oidc_audience -> TOML: auth.oidc.audience
* common.oidc_skip_expiry_check -> TOML: auth.oidc.skipExpiryCheck
* common.oidc_skip_issuer_check -> TOML: auth.oidc.skipIssuerCheck
* common.max_pool_count -> TOML: transport.maxPoolCount
* common.tcp_mux -> TOML: transport.tcpMux
* common.tcp_mux_keepalive_interval -> TOML: transport.tcpMuxKeepaliveInterval
* common.tcp_keepalive -> TOML: transport.tcpKeepalive
* common.heartbeat_timeout -> TOML: transport.heartbeatTimeout
* common.tls_force -> TOML: transport.tls.force
* common.tls_cert_file -> TOML: transport.tls.certFile
* common.tls_key_file -> TOML: transport.tls.keyFile
* common.tls_trusted_ca_file -> TOML: transport.tls.trustedCaFile
* common.quic_keepalive_period -> TOML: transport.quic.keepalivePeriod
* common.quic_max_idle_timeout -> TOML: transport.quic.maxIdleTimeout
* common.quic_max_incoming_streams -> TOML: transport.quic.maxIncomingStreams
* common.admin_addr -> TOML: webServer.addr
* common.admin_port -> TOML: webServer.port
* common.admin_user -> TOML: webServer.user
* common.admin_pwd -> TOML: webServer.password
* common.admin_tls_cert_file -> TOML: webServer.tls.certFile
* common.admin_tls_key_file -> TOML: webServer.tls.keyFile
* common.assets_dir -> TOML: webServer.assetsDir
* common.pprof_enable -> TOML: webServer.pprofEnable
* common.enable_prometheus -> TOML: enablePrometheus
* common.allow_ports -> TOML: allowPorts
* common.max_ports_per_client -> TOML: maxPortsPerClient
* common.ssh_tunnel_bind_port -> TOML: sshTunnelGateway.bindPort
* common.ssh_tunnel_private_key_file -> TOML: sshTunnelGateway.privateKeyFile
* common.ssh_tunnel_auto_gen_private_key_path -> TOML: sshTunnelGateway.autoGenPrivateKeyPath
* common.ssh_tunnel_authorized_keys_file -> TOML: sshTunnelGateway.authorizedKeysFile
* common.log_file -> TOML: log.to
* common.log_level -> TOML: log.level
* common.log_max_days -> TOML: log.maxDays
* common.disable_log_color -> TOML: log.disablePrintColor
* http_plugin.name -> TOML: httpPlugins[].name
* http_plugin.addr -> TOML: httpPlugins[].addr
* http_plugin.path -> TOML: httpPlugins[].path
* http_plugin.ops -> TOML: httpPlugins[].ops
* http_plugin.tls_verify -> TOML: httpPlugins[].tlsVerify
*/
const startupConf = [
[form.Flag, 'stdout', _('Log stdout'), null,
{
enabled: '1',
disabled: '0',
default: '1',
rmempty: false,
retain: true,
remove: writeFlagDisabled
}],
[form.Flag, 'stderr', _('Log stderr'), null,
{
enabled: '1',
disabled: '0',
default: '1',
rmempty: false,
retain: true,
remove: writeFlagDisabled
}],
[widgets.UserSelect, 'user', _('Run daemon as user')],
[widgets.GroupSelect, 'group', _('Run daemon as group')],
[form.Flag, 'respawn', _('Respawn when crashed'), null,
{
enabled: '1',
disabled: '0',
default: '1',
rmempty: false,
retain: true,
remove: writeFlagDisabled
}],
[form.DynamicList, 'env', _('Environment variable'),
_('OS environments passed to frp for config file template, see %s.').format('<a href="https://github.com/fatedier/frp#configuration-file-template">frp README</a>'),
{
placeholder: 'ENV_NAME=value',
validate: function (section_id, value) {
return validateEnv(value);
}
}],
[form.DynamicList, 'conf_inc', _('Additional configs'),
_('Extra root-level configuration fragments included before generated HTTP plugin sections. Use raw settings for table-specific extra options.'),
{
datatype: 'file',
placeholder: '/etc/frp/frps.d/frps_extra.toml'
}]
];
const commonBaseConf = [
[form.Value, 'bind_addr', _('Bind address'),
_('Address that frps binds to.'),
{ datatype: 'host', placeholder: '0.0.0.0' }],
[form.Value, 'bind_port', _('Bind port'),
_('Port that frps listens on.'),
{ datatype: 'port', rmempty: false, placeholder: '7000' }],
[form.Value, 'kcp_bind_port', _('KCP bind port'),
_('UDP port used for KCP. Empty disables KCP. Do not reuse the same UDP port as QUIC.'),
{
placeholder: '7000',
validate: function (section_id, value) {
const portValid = validatePortOrZero(section_id, value);
if (portValid !== true)
return portValid;
const quic = this.section.getOption('quic_bind_port');
const quicValue = quic ? quic.formvalue(section_id) : null;
if (value && value !== '0' && quicValue && quicValue !== '0' && value === quicValue)
return _('KCP bind port and QUIC bind port must be different.');
return true;
}
}],
[form.Value, 'quic_bind_port', _('QUIC bind port'),
_('UDP port used for QUIC. Empty disables QUIC. Do not reuse the same UDP port as KCP.'),
{
placeholder: '7002',
validate: function (section_id, value) {
const portValid = validatePortOrZero(section_id, value);
if (portValid !== true)
return portValid;
const kcp = this.section.getOption('kcp_bind_port');
const kcpValue = kcp ? kcp.formvalue(section_id) : null;
if (value && value !== '0' && kcpValue && kcpValue !== '0' && value === kcpValue)
return _('KCP bind port and QUIC bind port must be different.');
return true;
}
}],
[form.Value, 'proxy_bind_addr', _('Proxy bind address'),
_('Address that proxy listeners bind to.'),
{ datatype: 'host', placeholder: '0.0.0.0' }],
[form.Value, 'vhost_http_port', _('Vhost HTTP port'),
_('Port for HTTP virtual host requests. Empty or 0 disables it.'),
{ validate: validatePortOrZero, placeholder: '80' }],
[form.Value, 'vhost_https_port', _('Vhost HTTPS port'),
_('Port for HTTPS virtual host requests. Empty or 0 disables it.'),
{ validate: validatePortOrZero, placeholder: '443' }],
[form.Value, 'vhost_http_timeout', _('Vhost HTTP timeout'),
_('Response header timeout for vhost HTTP server, in seconds.'),
{ datatype: 'uinteger', placeholder: '60' }],
[form.Value, 'tcpmux_httpconnect_port', _('TCPMUX HTTP CONNECT port'),
_('Port for TCPMUX HTTP CONNECT requests. Empty or 0 disables it.'),
{ validate: validatePortOrZero, placeholder: '0' }],
[form.Flag, 'tcpmux_passthrough', _('TCPMUX passthrough'),
_('Do not update traffic when TCPMUX passthrough is enabled.'),
{ datatype: 'bool', default: 'false' }],
[form.Value, 'subdomain_host', _('Subdomain host'),
_('Domain suffix for subdomain-based HTTP/HTTPS proxies.'),
{ placeholder: 'frps.com' }],
[form.Value, 'custom_404_page', _('Custom 404 page'),
_('Path to custom 404 page for HTTP requests.'),
{ datatype: 'file' }],
[form.Value, 'udp_packet_size', _('UDP packet size'),
_('UDP packet size in bytes. Should match frpc.'),
{ datatype: 'uinteger', placeholder: '1500' }]
];
const commonAuthConf = [
[form.ListValue, 'authentication_method', _('Authentication method'),
_('Authentication method used to authenticate frpc.'),
{ values: [['token', 'token'], ['oidc', 'oidc']], default: 'token' }],
[form.Value, 'token', _('Token'),
_('Token used for authentication.'),
{
depends: { authentication_method: 'token' },
password: true,
validate: function (section_id, value) {
const tokenSourceType = this.section.getOption('token_source_type');
if (value && tokenSourceType && tokenSourceType.formvalue(section_id))
return _('Token and token source are mutually exclusive.');
return true;
}
}],
[form.MultiValue, 'auth_additional_scopes', _('Additional auth scopes'),
_('Additional scopes to include authentication information.'),
{ values: ['HeartBeats', 'NewWorkConns'] }],
[form.ListValue, 'token_source_type', _('Token source type'),
_('Load token from an external source. File reads the token from a local file; exec runs a command and requires frp unsafe permission. Mutually exclusive with auth.token.'),
{
depends: { authentication_method: 'token' },
values: [['', _('Disabled')], ['file', 'file'], ['exec', 'exec']],
rmempty: true,
validate: function (section_id, value) {
const token = this.section.getOption('token');
if (value && token && token.formvalue(section_id))
return _('Token and token source are mutually exclusive.');
return true;
}
}],
[form.Value, 'token_source_file_path', _('Token source file'),
_('Path of token file.'),
{
depends: {
authentication_method: 'token',
token_source_type: 'file'
},
datatype: 'file',
rmempty: false,
validate: function (section_id, value) {
const tokenSourceType = this.section.getOption('token_source_type');
if (
tokenSourceType &&
tokenSourceType.formvalue(section_id) === 'file' &&
!String(value || '').trim()
)
return _('Token source file path is required when token source type is file.');
return true;
}
}],
[form.Value, 'token_source_exec_command', _('Token source command'),
_('Command used to obtain the token. The init script adds --allow-unsafe=TokenSourceExec when this source type is used.'),
{
depends: {
authentication_method: 'token',
token_source_type: 'exec'
},
datatype: 'file',
rmempty: false,
validate: function (section_id, value) {
const tokenSourceType = this.section.getOption('token_source_type');
if (
tokenSourceType &&
tokenSourceType.formvalue(section_id) === 'exec' &&
!String(value || '').trim()
)
return _('Token source command is required when token source type is exec.');
return true;
}
}],
[form.DynamicList, 'token_source_exec_args', _('Token source command arguments'),
_('Command arguments passed to token source exec.'),
{
depends: {
authentication_method: 'token',
token_source_type: 'exec'
},
placeholder: '--format'
}],
[form.DynamicList, 'token_source_exec_env', _('Token source environment'),
_('Environment variables passed to token source exec. Use KEY=value.'),
{
depends: {
authentication_method: 'token',
token_source_type: 'exec'
},
placeholder: 'TOKEN_SERVICE=production',
validate: function (section_id, value) {
return validateEnv(value);
}
}],
[form.Value, 'oidc_issuer', _('OIDC issuer'),
_('OIDC issuer used to verify tokens.'),
{ depends: { authentication_method: 'oidc' } }],
[form.Value, 'oidc_audience', _('OIDC audience'),
_('Audience OIDC tokens should contain.'),
{ depends: { authentication_method: 'oidc' } }],
[form.Flag, 'oidc_skip_expiry_check', _('OIDC skip expiry check'),
_('Skip checking whether the OIDC token is expired.'),
{ depends: { authentication_method: 'oidc' }, datatype: 'bool', default: 'false' }],
[form.Flag, 'oidc_skip_issuer_check', _('OIDC skip issuer check'),
_('Skip checking whether OIDC token issuer matches configured issuer.'),
{ depends: { authentication_method: 'oidc' }, datatype: 'bool', default: 'false' }]
];
const commonLogConf = [
[form.Value, 'log_file', _('Log output'),
_('Log output path or console.'),
{ placeholder: 'console' }],
[form.ListValue, 'log_level', _('Log level'),
_('Configuration key: log.level.'),
{ values: ['trace', 'debug', 'info', 'warn', 'error'], default: 'info' }],
[form.Value, 'log_max_days', _('Log max days'),
_('Maximum number of days to keep logs.'),
{ datatype: 'uinteger', placeholder: '3' }],
[form.Flag, 'disable_log_color', _('Disable log color'),
_('Disable log colors when log output is console.'),
{ datatype: 'bool', default: 'false' }]
];
const commonWebConf = [
[form.Value, 'admin_addr', _('Web server address'),
_('Web server bind address.'),
{ datatype: 'host', placeholder: '127.0.0.1' }],
[form.Value, 'admin_port', _('Web server port'),
_('Web server port. Leave empty or set to 0 to disable the web server.'),
{ validate: validatePortOrZero, placeholder: '7500' }],
[form.Value, 'admin_user', _('Web server user'),
_('Web server username.'),
{
placeholder: 'admin'
}],
[form.Value, 'admin_pwd', _('Web server password'),
_('Web server password.'),
{
password: true,
placeholder: 'change_me_to_a_strong_password'
}],
[form.Flag, 'admin_tls_enable', _('Enable web HTTPS'),
_('Enable HTTPS for the frps web server. This switch controls whether webServer.tls.certFile and webServer.tls.keyFile are emitted.'),
{
enabled: 'true',
disabled: 'false',
default: 'false',
rmempty: false,
retain: true,
remove: writeFlagDisabled
}],
[form.Value, 'admin_tls_cert_file', _('TLS certificate path'),
_('Path to the certificate file used by the HTTPS web server.'),
{
datatype: 'file',
retain: true,
placeholder: '/etc/ssl/acme/example.com.fullchain.crt',
validate: function (section_id, value) {
if (
adminWebEnabled(this.section, section_id) &&
adminHttpsEnabled(this.section, section_id) &&
!String(value || '').trim()
)
return _('TLS certificate path is required when web HTTPS is enabled.');
return true;
}
}],
[form.Value, 'admin_tls_key_file', _('TLS private key path'),
_('Path to the private key file used by the HTTPS web server.'),
{
datatype: 'file',
retain: true,
placeholder: '/etc/ssl/acme/example.com.key',
validate: function (section_id, value) {
if (
adminWebEnabled(this.section, section_id) &&
adminHttpsEnabled(this.section, section_id) &&
!String(value || '').trim()
)
return _('TLS private key path is required when web HTTPS is enabled.');
return true;
}
}],
[form.Value, 'assets_dir', _('Assets dir'),
_('Web server assets directory.'),
{ datatype: 'directory' }],
[form.Flag, 'pprof_enable', _('Enable pprof'),
_('Enable Go pprof debug/profiling endpoints on the web server. It only takes effect when web server port is set.'),
{
datatype: 'bool',
default: 'false',
rmempty: false,
retain: true,
remove: writeFlagDisabled
}],
[form.Flag, 'enable_prometheus', _('Enable Prometheus'),
_('Export Prometheus metrics on webServer /metrics API. It only takes effect when web server port is set.'),
{
datatype: 'bool',
default: 'false',
rmempty: false,
retain: true,
remove: writeFlagDisabled
}]
];
const commonTransportConf = [
[form.Value, 'max_pool_count', _('Max pool count'),
_('Maximum pool count for each proxy.'),
{ datatype: 'uinteger', placeholder: '5' }],
[form.Flag, 'tcp_mux', _('TCP mux'),
_('Enable TCP stream multiplexing. This setting must match frpc.'),
{
enabled: 'true',
disabled: 'false',
default: 'true',
optional: false,
rmempty: false,
retain: true,
remove: writeFlagDisabled
}],
[form.Value, 'tcp_mux_keepalive_interval', _('TCP mux keepalive interval'),
_('Keepalive interval for TCP mux.'),
{ datatype: 'uinteger', placeholder: '30' }],
[form.Value, 'tcp_keepalive', _('TCP keepalive'),
_('TCP keepalive interval. Negative value disables keepalive.'),
{ datatype: 'integer', placeholder: '7200' }],
[form.Value, 'heartbeat_timeout', _('Heartbeat timeout'),
_('Heartbeat timeout in seconds.'),
{ datatype: 'integer', placeholder: '90' }],
[form.Value, 'user_conn_timeout', _('User connection timeout'),
_('Maximum time to wait for a work connection.'),
{ datatype: 'uinteger', placeholder: '10' }],
[form.Flag, 'detailed_errors_to_client', _('Detailed errors to client'),
_('Send specific error details to frpc.'),
{
enabled: 'true',
disabled: 'false',
default: 'true',
optional: false,
rmempty: false,
retain: true,
remove: writeFlagDisabled
}]
];
const commonTlsQuicConf = [
[form.Flag, 'tls_force', _('Force TLS'),
_('Only accept TLS-encrypted frpc connections.'),
{ datatype: 'bool', default: 'false' }],
[form.Value, 'tls_cert_file', _('TLS certificate path'),
_('Path to the TLS certificate file.'),
{ datatype: 'file', placeholder: '/etc/ssl/frp/server.crt' }],
[form.Value, 'tls_key_file', _('TLS private key path'),
_('Path to the TLS private key file.'),
{ datatype: 'file', placeholder: '/etc/ssl/frp/server.key' }],
[form.Value, 'tls_trusted_ca_file', _('TLS trusted CA path'),
_('Path to the trusted CA certificate file.'),
{ datatype: 'file', placeholder: '/etc/ssl/frp/ca.crt' }],
[form.Value, 'quic_keepalive_period', _('QUIC keepalive period'),
_('Configuration key: transport.quic.keepalivePeriod.'),
{ datatype: 'uinteger', placeholder: '10' }],
[form.Value, 'quic_max_idle_timeout', _('QUIC max idle timeout'),
_('Configuration key: transport.quic.maxIdleTimeout.'),
{ datatype: 'uinteger', placeholder: '30' }],
[form.Value, 'quic_max_incoming_streams', _('QUIC max incoming streams'),
_('Configuration key: transport.quic.maxIncomingStreams.'),
{ datatype: 'uinteger', placeholder: '100000' }]
];
const commonAccessConf = [
[form.DynamicList, 'allow_ports', _('Allow ports'),
_('Ports clients are allowed to bind. Use single port like 3001 or range like 2000-3000.'),
{
placeholder: '2000-3000',
validate: function (section_id, value) {
return validatePortRange(value);
}
}],
[form.Value, 'max_ports_per_client', _('Max ports per client'),
_('Maximum ports each client can use. 0 means no limit.'),
{ datatype: 'uinteger', placeholder: '0' }],
[form.Value, 'nathole_analysis_data_reserve_hours', _('NAT hole analysis reserve hours'),
_('Retention time for NAT hole punching strategy data.'),
{ datatype: 'uinteger', placeholder: '168' }]
];
const commonSshTunnelConf = [
[form.Value, 'ssh_tunnel_bind_port', _('SSH tunnel bind port'),
_('SSH tunnel gateway bind port. Empty disables this feature.'),
{ validate: validatePortOrZero, placeholder: '2200' }],
[form.Value, 'ssh_tunnel_private_key_file', _('SSH tunnel private key file'),
_('Private key file for SSH tunnel gateway.'),
{ datatype: 'file' }],
[form.Value, 'ssh_tunnel_auto_gen_private_key_path', _('SSH tunnel auto-generated private key path'),
_('Path for auto-generated private key.'),
{ datatype: 'file' }],
[form.Value, 'ssh_tunnel_authorized_keys_file', _('SSH tunnel authorized keys file'),
_('Authorized keys file for SSH tunnel gateway.'),
{ datatype: 'file' }]
];
const httpPluginConf = [
[form.Value, 'name', _('Plugin name'),
_('Unique HTTP plugin name.'),
{ rmempty: false, optional: false }],
[form.Value, 'addr', _('Plugin address'),
_('HTTP plugin backend address.'),
{
placeholder: '127.0.0.1:9000',
rmempty: false,
optional: false,
validate: function (section_id, value) {
return validateHttpPluginAddr(value);
}
}],
[form.Value, 'path', _('Plugin path'),
_('HTTP plugin request path.'),
{
placeholder: '/handler',
rmempty: false,
optional: false,
validate: function (section_id, value) {
return validateHttpPath(value);
}
}],
[form.MultiValue, 'ops', _('Plugin operations'),
_('frps operations handled by this HTTP plugin.'),
{
values: ['Login', 'NewProxy', 'CloseProxy', 'Ping', 'NewWorkConn', 'NewUserConn'],
default: ['Login'],
optional: false,
rmempty: false,
validate: function (section_id, value) {
return validateHttpPluginOp(value);
}
}],
[form.Flag, 'tls_verify', _('Verify plugin TLS'),
_('Verify the HTTP plugin TLS certificate when the plugin address uses HTTPS.'),
{ datatype: 'bool', default: 'false' }]
];
function writeFlagDisabled(section_id) {
return this.write(section_id, this.disabled || 'false');
}
function removeIfPresent(section_id) {
const this_cfg = this.uciconfig || this.section.uciconfig || this.map.config;
const this_sid = this.ucisection || section_id;
const this_opt = this.ucioption || this.option;
for (let i = 0; i < this.section.children.length; i++) {
const sibling = this.section.children[i];
if (sibling === this || sibling.ucioption == null)
continue;
const sibling_cfg = sibling.uciconfig || sibling.section.uciconfig || sibling.map.config;
const sibling_sid = sibling.ucisection || section_id;
const sibling_opt = sibling.ucioption || sibling.option;
if (this_cfg != sibling_cfg || this_sid != sibling_sid || this_opt != sibling_opt)
continue;
if (typeof sibling.isActive === 'function' && sibling.isActive(section_id))
return Promise.resolve();
}
if (this.map.data.get(this_cfg, this_sid, this_opt) == null)
return Promise.resolve();
return this.map.data.unset(this_cfg, this_sid, this_opt);
}
function isUciDeleteNotFoundError(err) {
const message = err && err.message ? err.message : String(err);
return /uci\/delete/.test(message) && /ubus code 4/.test(message);
}
function guardUciDeleteNotFound(data, config) {
data._frpIgnoreMissingDeleteConfigs ??= {};
data._frpIgnoreMissingDeleteConfigs[config] = true;
if (data._frpIgnoreMissingDeleteInstalled)
return;
const callDelete = data.callDelete;
data.callDelete = function(conf, sid, options) {
const guarded = this._frpIgnoreMissingDeleteConfigs && this._frpIgnoreMissingDeleteConfigs[conf];
return callDelete.apply(this, arguments).catch(L.bind(function(err) {
if (!guarded || !isUciDeleteNotFoundError(err))
return Promise.reject(err);
if (!Array.isArray(options) || options.length <= 1)
return null;
return Promise.all(options.map(L.bind(function(opt) {
return callDelete.call(this, conf, sid, [ opt ]).catch(function(e) {
return isUciDeleteNotFoundError(e) ? null : Promise.reject(e);
});
}, this)));
}, this));
};
data._frpIgnoreMissingDeleteInstalled = true;
}
function adminWebEnabled(section, section_id) {
const port = section.getOption('admin_port');
const value = port ? port.formvalue(section_id) : null;
return !!(value && value !== '0');
}
function adminHttpsEnabled(section, section_id) {
const enable = section.getOption('admin_tls_enable');
return !!(enable && enable.formvalue(section_id) === 'true');
}
function validatePortOrZero(section_id, value) {
if (!value)
return true;
value = String(value).trim();
if (!/^\d+$/.test(value))
return _('Port must be a number between 0 and 65535.');
const port = Number(value);
if (port < 0 || port > 65535)
return _('Port must be between 0 and 65535.');
return true;
}
function validatePortRange(value) {
if (!value)
return true;
value = String(value).trim();
const m = value.match(/^(\d+)(?:-(\d+))?$/);
if (!m)
return _('Use a single port like 3001 or a range like 2000-3000.');
const start = Number(m[1]);
const end = Number(m[2] || m[1]);
if (start < 1 || start > 65535 || end < 1 || end > 65535)
return _('Port must be between 1 and 65535.');
if (start > end)
return _('Port range start must not be greater than end.');
return true;
}
function validateEnv(value) {
if (!value)
return true;
if (/\r|\n/.test(String(value)) || !/^[A-Za-z_][A-Za-z0-9_]*=.*$/.test(String(value)))
return _('Environment variable must use KEY=value format.');
return true;
}
function validateHttpPath(value) {
if (!value)
return true;
if (/\r|\n/.test(String(value)) || String(value).charAt(0) !== '/')
return _('HTTP plugin path must start with /.');
return true;
}
function validateHttpPluginAddr(value) {
if (!value)
return true;
value = String(value).trim();
if (/\r|\n/.test(value))
return _('HTTP plugin address must be a single address.');
if (/^https?:\/\//.test(value)) {
if (!/^https?:\/\/[^\s/:]+(?::\d+)?(?:\/.*)?$/.test(value) && !/^https?:\/\/\[[0-9A-Fa-f:.]+\](?::\d+)?(?:\/.*)?$/.test(value))
return _('HTTP plugin URL must include a host.');
return true;
}
if (/^[^\s/:]+:\d+$/.test(value) || /^\[[0-9A-Fa-f:.]+\]:\d+$/.test(value))
return true;
return _('HTTP plugin address must be host:port or an http:// or https:// URL.');
}
function validateHttpPluginOp(value) {
if (!value)
return true;
if (!/^[A-Za-z][A-Za-z0-9]*$/.test(String(value)))
return _('HTTP plugin operation must be a frps operation name, for example Login or NewProxy.');
return true;
}
function normalizeDepends(depends) {
if (depends == null)
return [];
return Array.isArray(depends) ? depends : [ depends ];
}
function mergeDepends(existing, next) {
const current = normalizeDepends(existing);
const incoming = normalizeDepends(next);
if (current.length === 0)
return incoming;
if (incoming.length === 0)
return current;
const merged = [];
for (let oldDep of current) {
for (let newDep of incoming) {
const dep = {};
let conflict = false;
for (let key in oldDep)
dep[key] = oldDep[key];
for (let key in newDep) {
if (Object.prototype.hasOwnProperty.call(dep, key) && dep[key] !== newDep[key]) {
conflict = true;
break;
}
dep[key] = newDep[key];
}
if (!conflict)
merged.push(dep);
}
}
return merged;
}
function setParams(o, params) {
if (!params)
return;
for (let key in params) {
let val = params[key];
if (key === 'values') {
for (let v of val) {
let args = v;
if (!Array.isArray(args))
args = [args];
o.value.apply(o, args);
}
} else if (key === 'depends') {
o.deps = mergeDepends(o.deps, val);
} else {
o[key] = params[key];
}
}
if (params['datatype'] === 'bool') {
o.enabled = 'true';
o.disabled = 'false';
}
}
function defTabOpts(s, t, opts, params) {
for (let opt of opts) {
const o = s.taboption(t, opt[0], opt[1], opt[2], opt[3]);
setParams(o, opt[4]);
setParams(o, params);
/*
* Per-option optional must win over tab-wide optional.
* This is important for form.Flag with default='true',
* otherwise LuCI may treat checked state as default and call remove().
*/
if (opt[4] && Object.prototype.hasOwnProperty.call(opt[4], 'optional'))
o.optional = opt[4].optional;
if (
!(opt[4] && Object.prototype.hasOwnProperty.call(opt[4], 'remove')) &&
!(params && Object.prototype.hasOwnProperty.call(params, 'remove'))
)
o.remove = removeIfPresent;
}
}
function defOpts(s, opts, params) {
for (let opt of opts) {
const o = s.option(opt[0], opt[1], opt[2], opt[3]);
setParams(o, opt[4]);
setParams(o, params);
if (opt[4] && Object.prototype.hasOwnProperty.call(opt[4], 'optional'))
o.optional = opt[4].optional;
if (
!(opt[4] && Object.prototype.hasOwnProperty.call(opt[4], 'remove')) &&
!(params && Object.prototype.hasOwnProperty.call(params, 'remove'))
)
o.remove = removeIfPresent;
}
}
const callServiceList = rpc.declare({
object: 'service',
method: 'list',
params: ['name'],
expect: { '': {} }
});
function getServiceStatus() {
return L.resolveDefault(callServiceList('frps'), {}).then(function (res) {
const instances = res.frps && res.frps.instances;
if (!instances)
return false;
for (let name in instances) {
if (instances[name] && instances[name].running)
return true;
}
return false;
});
}
function renderStatus(isRunning) {
const color = isRunning ? 'green' : 'red';
const status = isRunning ? _('RUNNING') : _('NOT RUNNING');
return String.format('<em><span style="color:%s"><strong>%s %s</strong></span></em>',
color, _('frp Server'), status);
}
return view.extend({
render() {
let m, s, o;
m = new form.Map('frps', _('frp Server'));
guardUciDeleteNotFound(m.data, 'frps');
s = m.section(form.NamedSection, '_status');
s.anonymous = true;
s.render = function (section_id) {
L.Poll.add(function () {
return L.resolveDefault(getServiceStatus()).then(function (res) {
const view = document.getElementById('service_status');
if (view)
view.innerHTML = renderStatus(res);
});
});
return E('div', {},
E('fieldset', { class: 'cbi-section' }, [
E('p', { id: 'service_status' }, _('Collecting data ...'))
])
);
};
s = m.section(form.NamedSection, 'common', 'conf');
s.dynamic = true;
s.tab('common', _('Common Settings'));
s.tab('auth', _('Authentication'));
s.tab('transport', _('Transport Settings'));
s.tab('tls_quic', _('TLS / QUIC'));
s.tab('web', _('Web Server'));
s.tab('access', _('Access Control'));
s.tab('ssh_tunnel', _('SSH Tunnel'));
s.tab('log', _('Log Settings'));
s.tab('init', _('Startup Settings'));
defTabOpts(s, 'common', commonBaseConf, { optional: true });
defTabOpts(s, 'auth', commonAuthConf, { optional: true });
defTabOpts(s, 'transport', commonTransportConf, { optional: true });
defTabOpts(s, 'tls_quic', commonTlsQuicConf, { optional: true });
defTabOpts(s, 'web', commonWebConf, { optional: true });
defTabOpts(s, 'access', commonAccessConf, { optional: true });
defTabOpts(s, 'ssh_tunnel', commonSshTunnelConf, { optional: true });
defTabOpts(s, 'log', commonLogConf, { optional: true });
o = s.taboption('init', form.SectionValue, 'init', form.TypedSection, 'init', _('Startup Settings'));
s = o.subsection;
s.anonymous = true;
s.dynamic = true;
defOpts(s, startupConf);
s = m.section(form.GridSection, 'http_plugin', _('HTTP Plugin Settings'));
s.anonymous = true;
s.addremove = true;
s.sortable = true;
s.nodescriptions = true;
s.addbtntitle = _('Add new HTTP plugin...');
defOpts(s, httpPluginConf, { optional: true });
return m.render();
}
});
|