| // |
| 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 |
| 15 | package legacy |
| 16 |
| 17 | import ( |
| 18 | "strings" |
| 19 |
| 20 | "gopkg.in/ini.v1" |
| 21 |
| 22 | legacyauth "github.com/fatedier/frp/pkg/auth/legacy" |
| 23 | ) |
| 24 |
| 25 | type HTTPPluginOptions struct { |
| 26 | Name string `ini:"name"` |
| 27 | Addr string `ini:"addr"` |
| 28 | Path string `ini:"path"` |
| 29 | Ops []string `ini:"ops"` |
| 30 | TLSVerify bool `ini:"tlsVerify"` |
| 31 | } |
| 32 |
| 33 | // ServerCommonConf contains information for a server service. It is |
| 34 | // recommended to use GetDefaultServerConf instead of creating this object |
| 35 | // directly, so that all unspecified fields have reasonable default values. |
| 36 | type ServerCommonConf struct { |
| 37 | legacyauth.ServerConfig `ini:",extends"` |
| 38 |
| 39 | // BindAddr specifies the address that the server binds to. By default, |
| 40 | // this value is "0.0.0.0". |
| 41 | BindAddr string `ini:"bind_addr" json:"bind_addr"` |
| 42 | // BindPort specifies the port that the server listens on. By default, this |
| 43 | // value is 7000. |
| 44 | BindPort int `ini:"bind_port" json:"bind_port"` |
| 45 | // KCPBindPort specifies the KCP port that the server listens on. If this |
| 46 | // value is 0, the server will not listen for KCP connections. By default, |
| 47 | // this value is 0. |
| 48 | KCPBindPort int `ini:"kcp_bind_port" json:"kcp_bind_port"` |
| 49 | // QUICBindPort specifies the QUIC port that the server listens on. |
| 50 | // Set this value to 0 will disable this feature. |
| 51 | // By default, the value is 0. |
| 52 | QUICBindPort int `ini:"quic_bind_port" json:"quic_bind_port"` |
| 53 | // QUIC protocol options |
| 54 | QUICKeepalivePeriod int `ini:"quic_keepalive_period" json:"quic_keepalive_period"` |
| 55 | QUICMaxIdleTimeout int `ini:"quic_max_idle_timeout" json:"quic_max_idle_timeout"` |
| 56 | QUICMaxIncomingStreams int `ini:"quic_max_incoming_streams" json:"quic_max_incoming_streams"` |
| 57 | // ProxyBindAddr specifies the address that the proxy binds to. This value |
| 58 | // may be the same as BindAddr. |
| 59 | ProxyBindAddr string `ini:"proxy_bind_addr" json:"proxy_bind_addr"` |
| 60 | // VhostHTTPPort specifies the port that the server listens for HTTP Vhost |
| 61 | // requests. If this value is 0, the server will not listen for HTTP |
| 62 | // requests. By default, this value is 0. |
| 63 | VhostHTTPPort int `ini:"vhost_http_port" json:"vhost_http_port"` |
| 64 | // VhostHTTPSPort specifies the port that the server listens for HTTPS |
| 65 | // Vhost requests. If this value is 0, the server will not listen for HTTPS |
| 66 | // requests. By default, this value is 0. |
| 67 | VhostHTTPSPort int `ini:"vhost_https_port" json:"vhost_https_port"` |
| 68 | // TCPMuxHTTPConnectPort specifies the port that the server listens for TCP |
| 69 | // HTTP CONNECT requests. If the value is 0, the server will not multiplex TCP |
| 70 | // requests on one single port. If it's not - it will listen on this value for |
| 71 | // HTTP CONNECT requests. By default, this value is 0. |
| 72 | TCPMuxHTTPConnectPort int `ini:"tcpmux_httpconnect_port" json:"tcpmux_httpconnect_port"` |
| 73 | // If TCPMuxPassthrough is true, frps won't do any update on traffic. |
| 74 | TCPMuxPassthrough bool `ini:"tcpmux_passthrough" json:"tcpmux_passthrough"` |
| 75 | // VhostHTTPTimeout specifies the response header timeout for the Vhost |
| 76 | // HTTP server, in seconds. By default, this value is 60. |
| 77 | VhostHTTPTimeout int64 `ini:"vhost_http_timeout" json:"vhost_http_timeout"` |
| 78 | // DashboardAddr specifies the address that the dashboard binds to. By |
| 79 | // default, this value is "0.0.0.0". |
| 80 | DashboardAddr string `ini:"dashboard_addr" json:"dashboard_addr"` |
| 81 | // DashboardPort specifies the port that the dashboard listens on. If this |
| 82 | // value is 0, the dashboard will not be started. By default, this value is |
| 83 | // 0. |
| 84 | DashboardPort int `ini:"dashboard_port" json:"dashboard_port"` |
| 85 | // DashboardTLSCertFile specifies the path of the cert file that the server will |
| 86 | // load. If "dashboard_tls_cert_file", "dashboard_tls_key_file" are valid, the server will use this |
| 87 | // supplied tls configuration. |
| 88 | DashboardTLSCertFile string `ini:"dashboard_tls_cert_file" json:"dashboard_tls_cert_file"` |
| 89 | // DashboardTLSKeyFile specifies the path of the secret key that the server will |
| 90 | // load. If "dashboard_tls_cert_file", "dashboard_tls_key_file" are valid, the server will use this |
| 91 | // supplied tls configuration. |
| 92 | DashboardTLSKeyFile string `ini:"dashboard_tls_key_file" json:"dashboard_tls_key_file"` |
| 93 | // DashboardTLSMode specifies the mode of the dashboard between HTTP or HTTPS modes. By |
| 94 | // default, this value is false, which is HTTP mode. |
| 95 | DashboardTLSMode bool `ini:"dashboard_tls_mode" json:"dashboard_tls_mode"` |
| 96 | // DashboardUser specifies the username that the dashboard will use for |
| 97 | // login. |
| 98 | DashboardUser string `ini:"dashboard_user" json:"dashboard_user"` |
| 99 | // DashboardPwd specifies the password that the dashboard will use for |
| 100 | // login. |
| 101 | DashboardPwd string `ini:"dashboard_pwd" json:"dashboard_pwd"` |
| 102 | // EnablePrometheus will export prometheus metrics on {dashboard_addr}:{dashboard_port} |
| 103 | // in /metrics api. |
| 104 | EnablePrometheus bool `ini:"enable_prometheus" json:"enable_prometheus"` |
| 105 | // AssetsDir specifies the local directory that the dashboard will load |
| 106 | // resources from. If this value is "", assets will be loaded from the |
| 107 | // bundled executable using statik. By default, this value is "". |
| 108 | AssetsDir string `ini:"assets_dir" json:"assets_dir"` |
| 109 | // LogFile specifies a file where logs will be written to. This value will |
| 110 | // only be used if LogWay is set appropriately. By default, this value is |
| 111 | // "console". |
| 112 | LogFile string `ini:"log_file" json:"log_file"` |
| 113 | // LogWay specifies the way logging is managed. Valid values are "console" |
| 114 | // or "file". If "console" is used, logs will be printed to stdout. If |
| 115 | // "file" is used, logs will be printed to LogFile. By default, this value |
| 116 | // is "console". |
| 117 | LogWay string `ini:"log_way" json:"log_way"` |
| 118 | // LogLevel specifies the minimum log level. Valid values are "trace", |
| 119 | // "debug", "info", "warn", and "error". By default, this value is "info". |
| 120 | LogLevel string `ini:"log_level" json:"log_level"` |
| 121 | // LogMaxDays specifies the maximum number of days to store log information |
| 122 | // before deletion. This is only used if LogWay == "file". By default, this |
| 123 | // value is 0. |
| 124 | LogMaxDays int64 `ini:"log_max_days" json:"log_max_days"` |
| 125 | // DisableLogColor disables log colors when LogWay == "console" when set to |
| 126 | // true. By default, this value is false. |
| 127 | DisableLogColor bool `ini:"disable_log_color" json:"disable_log_color"` |
| 128 | // DetailedErrorsToClient defines whether to send the specific error (with |
| 129 | // debug info) to frpc. By default, this value is true. |
| 130 | DetailedErrorsToClient bool `ini:"detailed_errors_to_client" json:"detailed_errors_to_client"` |
| 131 |
| 132 | // SubDomainHost specifies the domain that will be attached to sub-domains |
| 133 | // requested by the client when using Vhost proxying. For example, if this |
| 134 | // value is set to "frps.com" and the client requested the subdomain |
| 135 | // "test", the resulting URL would be "test.frps.com". By default, this |
| 136 | // value is "". |
| 137 | SubDomainHost string `ini:"subdomain_host" json:"subdomain_host"` |
| 138 | // TCPMux toggles TCP stream multiplexing. This allows multiple requests |
| 139 | // from a client to share a single TCP connection. By default, this value |
| 140 | // is true. |
| 141 | TCPMux bool `ini:"tcp_mux" json:"tcp_mux"` |
| 142 | // TCPMuxKeepaliveInterval specifies the keep alive interval for TCP stream multiplier. |
| 143 | // If TCPMux is true, heartbeat of application layer is unnecessary because it can only rely on heartbeat in TCPMux. |
| 144 | TCPMuxKeepaliveInterval int64 `ini:"tcp_mux_keepalive_interval" json:"tcp_mux_keepalive_interval"` |
| 145 | // TCPKeepAlive specifies the interval between keep-alive probes for an active network connection between frpc and frps. |
| 146 | // If negative, keep-alive probes are disabled. |
| 147 | TCPKeepAlive int64 `ini:"tcp_keepalive" json:"tcp_keepalive"` |
| 148 | // Custom404Page specifies a path to a custom 404 page to display. If this |
| 149 | // value is "", a default page will be displayed. By default, this value is |
| 150 | // "". |
| 151 | Custom404Page string `ini:"custom_404_page" json:"custom_404_page"` |
| 152 |
| 153 | // AllowPorts specifies a set of ports that clients are able to proxy to. |
| 154 | // If the length of this value is 0, all ports are allowed. By default, |
| 155 | // this value is an empty set. |
| 156 | AllowPorts map[int]struct{} `ini:"-" json:"-"` |
| 157 | // Original string. |
| 158 | AllowPortsStr string `ini:"-" json:"-"` |
| 159 | // MaxPoolCount specifies the maximum pool size for each proxy. By default, |
| 160 | // this value is 5. |
| 161 | MaxPoolCount int64 `ini:"max_pool_count" json:"max_pool_count"` |
| 162 | // MaxPortsPerClient specifies the maximum number of ports a single client |
| 163 | // may proxy to. If this value is 0, no limit will be applied. By default, |
| 164 | // this value is 0. |
| 165 | MaxPortsPerClient int64 `ini:"max_ports_per_client" json:"max_ports_per_client"` |
| 166 | // TLSOnly specifies whether to only accept TLS-encrypted connections. |
| 167 | // By default, the value is false. |
| 168 | TLSOnly bool `ini:"tls_only" json:"tls_only"` |
| 169 | // TLSCertFile specifies the path of the cert file that the server will |
| 170 | // load. If "tls_cert_file", "tls_key_file" are valid, the server will use this |
| 171 | // supplied tls configuration. Otherwise, the server will use the tls |
| 172 | // configuration generated by itself. |
| 173 | TLSCertFile string `ini:"tls_cert_file" json:"tls_cert_file"` |
| 174 | // TLSKeyFile specifies the path of the secret key that the server will |
| 175 | // load. If "tls_cert_file", "tls_key_file" are valid, the server will use this |
| 176 | // supplied tls configuration. Otherwise, the server will use the tls |
| 177 | // configuration generated by itself. |
| 178 | TLSKeyFile string `ini:"tls_key_file" json:"tls_key_file"` |
| 179 | // TLSTrustedCaFile specifies the paths of the client cert files that the |
| 180 | // server will load. It only works when "tls_only" is true. If |
| 181 | // "tls_trusted_ca_file" is valid, the server will verify each client's |
| 182 | // certificate. |
| 183 | TLSTrustedCaFile string `ini:"tls_trusted_ca_file" json:"tls_trusted_ca_file"` |
| 184 | // HeartBeatTimeout specifies the maximum time to wait for a heartbeat |
| 185 | // before terminating the connection. It is not recommended to change this |
| 186 | // value. By default, this value is 90. Set negative value to disable it. |
| 187 | HeartbeatTimeout int64 `ini:"heartbeat_timeout" json:"heartbeat_timeout"` |
| 188 | // UserConnTimeout specifies the maximum time to wait for a work |
| 189 | // connection. By default, this value is 10. |
| 190 | UserConnTimeout int64 `ini:"user_conn_timeout" json:"user_conn_timeout"` |
| 191 | // HTTPPlugins specify the server plugins support HTTP protocol. |
| 192 | HTTPPlugins map[string]HTTPPluginOptions `ini:"-" json:"http_plugins"` |
| 193 | // UDPPacketSize specifies the UDP packet size |
| 194 | // By default, this value is 1500 |
| 195 | UDPPacketSize int64 `ini:"udp_packet_size" json:"udp_packet_size"` |
| 196 | // Enable golang pprof handlers in dashboard listener. |
| 197 | // Dashboard port must be set first. |
| 198 | PprofEnable bool `ini:"pprof_enable" json:"pprof_enable"` |
| 199 | // NatHoleAnalysisDataReserveHours specifies the hours to reserve nat hole analysis data. |
| 200 | NatHoleAnalysisDataReserveHours int64 `ini:"nat_hole_analysis_data_reserve_hours" json:"nat_hole_analysis_data_reserve_hours"` |
| 201 | } |
| 202 |
| 203 | // GetDefaultServerConf returns a server configuration with reasonable defaults. |
| 204 | // Note: Some default values here will be set to empty and will be converted to them |
| 205 | // new configuration through the 'Complete' function to set them as the default |
| 206 | // values of the new configuration. |
| 207 | func GetDefaultServerConf() ServerCommonConf { |
| 208 | return ServerCommonConf{ |
| 209 | ServerConfig: legacyauth.GetDefaultServerConf(), |
| 210 | DashboardAddr: "0.0.0.0", |
| 211 | LogFile: "console", |
| 212 | LogWay: "console", |
| 213 | DetailedErrorsToClient: true, |
| 214 | TCPMux: true, |
| 215 | AllowPorts: make(map[int]struct{}), |
| 216 | HTTPPlugins: make(map[string]HTTPPluginOptions), |
| 217 | } |
| 218 | } |
| 219 |
| 220 | func UnmarshalServerConfFromIni(source any) (ServerCommonConf, error) { |
| 221 | f, err := ini.LoadSources(ini.LoadOptions{ |
| 222 | Insensitive: false, |
| 223 | InsensitiveSections: false, |
| 224 | InsensitiveKeys: false, |
| 225 | IgnoreInlineComment: true, |
| 226 | AllowBooleanKeys: true, |
| 227 | }, source) |
| 228 | if err != nil { |
| 229 | return ServerCommonConf{}, err |
| 230 | } |
| 231 |
| 232 | s, err := f.GetSection("common") |
| 233 | if err != nil { |
| 234 | return ServerCommonConf{}, err |
| 235 | } |
| 236 |
| 237 | common := GetDefaultServerConf() |
| 238 | err = s.MapTo(&common) |
| 239 | if err != nil { |
| 240 | return ServerCommonConf{}, err |
| 241 | } |
| 242 |
| 243 | // allow_ports |
| 244 | allowPortStr := s.Key("allow_ports").String() |
| 245 | if allowPortStr != "" { |
| 246 | common.AllowPortsStr = allowPortStr |
| 247 | } |
| 248 |
| 249 | // plugin.xxx |
| 250 | pluginOpts := make(map[string]HTTPPluginOptions) |
| 251 | for _, section := range f.Sections() { |
| 252 | name := section.Name() |
| 253 | if !strings.HasPrefix(name, "plugin.") { |
| 254 | continue |
| 255 | } |
| 256 |
| 257 | opt, err := loadHTTPPluginOpt(section) |
| 258 | if err != nil { |
| 259 | return ServerCommonConf{}, err |
| 260 | } |
| 261 |
| 262 | pluginOpts[opt.Name] = *opt |
| 263 | } |
| 264 | common.HTTPPlugins = pluginOpts |
| 265 |
| 266 | return common, nil |
| 267 | } |
| 268 |
| 269 | func loadHTTPPluginOpt(section *ini.Section) (*HTTPPluginOptions, error) { |
| 270 | name := strings.TrimSpace(strings.TrimPrefix(section.Name(), "plugin.")) |
| 271 |
| 272 | opt := &HTTPPluginOptions{} |
| 273 | err := section.MapTo(opt) |
| 274 | if err != nil { |
| 275 | return nil, err |
| 276 | } |
| 277 |
| 278 | opt.Name = name |
| 279 |
| 280 | return opt, nil |
| 281 | } |