common.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2023 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v1
  15. import (
  16. "sync"
  17. "github.com/fatedier/frp/pkg/util/util"
  18. )
  19. // TODO(fatedier): Due to the current implementation issue of the go json library, the UnmarshalJSON method
  20. // of a custom struct cannot access the DisallowUnknownFields parameter of the parent decoder.
  21. // Here, a global variable is temporarily used to control whether unknown fields are allowed.
  22. // Once the v2 version is implemented by the community, we can switch to a standardized approach.
  23. //
  24. // https://github.com/golang/go/issues/41144
  25. // https://github.com/golang/go/discussions/63397
  26. var (
  27. DisallowUnknownFields = false
  28. DisallowUnknownFieldsMu sync.Mutex
  29. )
  30. type AuthScope string
  31. const (
  32. AuthScopeHeartBeats AuthScope = "HeartBeats"
  33. AuthScopeNewWorkConns AuthScope = "NewWorkConns"
  34. )
  35. type AuthMethod string
  36. const (
  37. AuthMethodToken AuthMethod = "token"
  38. AuthMethodOIDC AuthMethod = "oidc"
  39. )
  40. // QUIC protocol options
  41. type QUICOptions struct {
  42. KeepalivePeriod int `json:"keepalivePeriod,omitempty"`
  43. MaxIdleTimeout int `json:"maxIdleTimeout,omitempty"`
  44. MaxIncomingStreams int `json:"maxIncomingStreams,omitempty"`
  45. }
  46. func (c *QUICOptions) Complete() {
  47. c.KeepalivePeriod = util.EmptyOr(c.KeepalivePeriod, 10)
  48. c.MaxIdleTimeout = util.EmptyOr(c.MaxIdleTimeout, 30)
  49. c.MaxIncomingStreams = util.EmptyOr(c.MaxIncomingStreams, 100000)
  50. }
  51. type WebServerConfig struct {
  52. // This is the network address to bind on for serving the web interface and API.
  53. // By default, this value is "127.0.0.1".
  54. Addr string `json:"addr,omitempty"`
  55. // Port specifies the port for the web server to listen on. If this
  56. // value is 0, the admin server will not be started.
  57. Port int `json:"port,omitempty"`
  58. // User specifies the username that the web server will use for login.
  59. User string `json:"user,omitempty"`
  60. // Password specifies the password that the admin server will use for login.
  61. Password string `json:"password,omitempty"`
  62. // AssetsDir specifies the local directory that the admin server will load
  63. // resources from. If this value is "", assets will be loaded from the
  64. // bundled executable using embed package.
  65. AssetsDir string `json:"assetsDir,omitempty"`
  66. // Enable golang pprof handlers.
  67. PprofEnable bool `json:"pprofEnable,omitempty"`
  68. // Enable TLS if TLSConfig is not nil.
  69. TLS *TLSConfig `json:"tls,omitempty"`
  70. }
  71. func (c *WebServerConfig) Complete() {
  72. c.Addr = util.EmptyOr(c.Addr, "127.0.0.1")
  73. }
  74. type TLSConfig struct {
  75. // CertPath specifies the path of the cert file that client will load.
  76. CertFile string `json:"certFile,omitempty"`
  77. // KeyPath specifies the path of the secret key file that client will load.
  78. KeyFile string `json:"keyFile,omitempty"`
  79. // TrustedCaFile specifies the path of the trusted ca file that will load.
  80. TrustedCaFile string `json:"trustedCaFile,omitempty"`
  81. // ServerName specifies the custom server name of tls certificate. By
  82. // default, server name if same to ServerAddr.
  83. ServerName string `json:"serverName,omitempty"`
  84. }
  85. type LogConfig struct {
  86. // This is destination where frp should write the logs.
  87. // If "console" is used, logs will be printed to stdout, otherwise,
  88. // logs will be written to the specified file.
  89. // By default, this value is "console".
  90. To string `json:"to,omitempty"`
  91. // Level specifies the minimum log level. Valid values are "trace",
  92. // "debug", "info", "warn", and "error". By default, this value is "info".
  93. Level string `json:"level,omitempty"`
  94. // MaxDays specifies the maximum number of days to store log information
  95. // before deletion.
  96. MaxDays int64 `json:"maxDays"`
  97. // DisablePrintColor disables log colors when log.to is "console".
  98. DisablePrintColor bool `json:"disablePrintColor,omitempty"`
  99. }
  100. func (c *LogConfig) Complete() {
  101. c.To = util.EmptyOr(c.To, "console")
  102. c.Level = util.EmptyOr(c.Level, "info")
  103. c.MaxDays = util.EmptyOr(c.MaxDays, 3)
  104. }
  105. type HTTPPluginOptions struct {
  106. Name string `json:"name"`
  107. Addr string `json:"addr"`
  108. Path string `json:"path"`
  109. Ops []string `json:"ops"`
  110. TLSVerify bool `json:"tlsVerify,omitempty"`
  111. }
  112. type HeaderOperations struct {
  113. Set map[string]string `json:"set,omitempty"`
  114. }
  115. type HTTPHeader struct {
  116. Name string `json:"name"`
  117. Value string `json:"value"`
  118. }