frp/models/config/server_common.go

478 lines
16 KiB
Go
Raw Normal View History

2017-03-08 19:03:47 +01:00
// Copyright 2016 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"fmt"
"strconv"
"strings"
ini "github.com/vaughan0/go-ini"
"github.com/fatedier/frp/models/auth"
2019-12-20 13:28:28 +01:00
plugin "github.com/fatedier/frp/models/plugin/server"
"github.com/fatedier/frp/utils/util"
2017-03-08 19:03:47 +01:00
)
// ServerCommonConf contains information for a server service. It is
// recommended to use GetDefaultServerConf instead of creating this object
// directly, so that all unspecified fields have reasonable default values.
2017-03-08 19:03:47 +01:00
type ServerCommonConf struct {
2020-05-24 11:48:37 +02:00
auth.ServerConfig
// BindAddr specifies the address that the server binds to. By default,
// this value is "0.0.0.0".
BindAddr string `json:"bind_addr"`
// BindPort specifies the port that the server listens on. By default, this
// value is 7000.
BindPort int `json:"bind_port"`
2020-05-24 11:48:37 +02:00
// BindUDPPort specifies the UDP port that the server listens on. If this
// value is 0, the server will not listen for UDP connections. By default,
// this value is 0
2020-05-24 11:48:37 +02:00
BindUDPPort int `json:"bind_udp_port"`
// KCPBindPort specifies the KCP port that the server listens on. If this
// value is 0, the server will not listen for KCP connections. By default,
// this value is 0.
2020-05-24 11:48:37 +02:00
KCPBindPort int `json:"kcp_bind_port"`
// ProxyBindAddr specifies the address that the proxy binds to. This value
// may be the same as BindAddr. By default, this value is "0.0.0.0".
2018-04-10 11:46:49 +02:00
ProxyBindAddr string `json:"proxy_bind_addr"`
2020-05-24 11:48:37 +02:00
// 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. By default, this value is 0.
2020-05-24 11:48:37 +02:00
VhostHTTPPort int `json:"vhost_http_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. By default, this value is 0.
2020-05-24 11:48:37 +02:00
VhostHTTPSPort int `json:"vhost_https_port"`
// TCPMuxHTTPConnectPort specifies the port that the server listens for TCP
// HTTP CONNECT requests. If the value is 0, the server will not multiplex TCP
// requests on one single port. If it's not - it will listen on this value for
// HTTP CONNECT requests. By default, this value is 0.
2020-05-24 11:48:37 +02:00
TCPMuxHTTPConnectPort int `json:"tcpmux_httpconnect_port"`
// VhostHTTPTimeout specifies the response header timeout for the Vhost
// HTTP server, in seconds. By default, this value is 60.
2020-05-24 11:48:37 +02:00
VhostHTTPTimeout int64 `json:"vhost_http_timeout"`
// DashboardAddr specifies the address that the dashboard binds to. By
// default, this value is "0.0.0.0".
2018-08-08 05:18:38 +02:00
DashboardAddr string `json:"dashboard_addr"`
// DashboardPort specifies the port that the dashboard listens on. If this
// value is 0, the dashboard will not be started. By default, this value is
// 0.
DashboardPort int `json:"dashboard_port"`
// DashboardUser specifies the username that the dashboard will use for
// login. By default, this value is "admin".
DashboardUser string `json:"dashboard_user"`
// DashboardUser specifies the password that the dashboard will use for
// login. By default, this value is "admin".
DashboardPwd string `json:"dashboard_pwd"`
// EnablePrometheus will export prometheus metrics on {dashboard_addr}:{dashboard_port}
// in /metrics api.
EnablePrometheus bool `json:"enable_prometheus"`
// 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. By default, this value is "".
AssetsDir string `json:"asserts_dir"`
// LogFile specifies a file where logs will be written to. This value will
// only be used if LogWay is set appropriately. By default, this value is
// "console".
LogFile string `json:"log_file"`
// LogWay specifies the way logging is managed. Valid values are "console"
// or "file". If "console" is used, logs will be printed to stdout. If
// "file" is used, logs will be printed to LogFile. By default, this value
// is "console".
LogWay string `json:"log_way"`
// LogLevel specifies the minimum log level. Valid values are "trace",
// "debug", "info", "warn", and "error". By default, this value is "info".
LogLevel string `json:"log_level"`
// LogMaxDays specifies the maximum number of days to store log information
// before deletion. This is only used if LogWay == "file". By default, this
// value is 0.
LogMaxDays int64 `json:"log_max_days"`
// DisableLogColor disables log colors when LogWay == "console" when set to
// true. By default, this value is false.
DisableLogColor bool `json:"disable_log_color"`
// DetailedErrorsToClient defines whether to send the specific error (with
// debug info) to frpc. By default, this value is true.
DetailedErrorsToClient bool `json:"detailed_errors_to_client"`
// 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". By default, this
// value is "".
SubDomainHost string `json:"subdomain_host"`
2020-05-24 11:48:37 +02:00
// TCPMux toggles TCP stream multiplexing. This allows multiple requests
// from a client to share a single TCP connection. By default, this value
// is true.
2020-05-24 11:48:37 +02:00
TCPMux bool `json:"tcp_mux"`
// Custom404Page specifies a path to a custom 404 page to display. If this
// value is "", a default page will be displayed. By default, this value is
// "".
Custom404Page string `json:"custom_404_page"`
// 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. By default,
// this value is an empty set.
AllowPorts map[int]struct{}
// MaxPoolCount specifies the maximum pool size for each proxy. By default,
// this value is 5.
MaxPoolCount int64 `json:"max_pool_count"`
// MaxPortsPerClient specifies the maximum number of ports a single client
// may proxy to. If this value is 0, no limit will be applied. By default,
// this value is 0.
2018-04-23 19:26:05 +02:00
MaxPortsPerClient int64 `json:"max_ports_per_client"`
// TLSOnly specifies whether to only accept TLS-encrypted connections.
// By default, the value is false.
2020-05-24 11:48:37 +02:00
TLSOnly bool `json:"tls_only"`
// TLSCertFile specifies the path of the cert file that the server will
// load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
// supplied tls configuration. Otherwise, the server will use the tls
// configuration generated by itself.
TLSCertFile string `json:"tls_cert_file"`
// TLSKeyFile specifies the path of the secret key that the server will
// load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
// supplied tls configuration. Otherwise, the server will use the tls
// configuration generated by itself.
TLSKeyFile string `json:"tls_key_file"`
// TLSTrustedCaFile specifies the paths of the client cert files that the
// server will load. It only works when "tls_only" is true. If
// "tls_trusted_ca_file" is valid, the server will verify each client's
// certificate.
TLSTrustedCaFile string `json:"tls_trusted_ca_file"`
// HeartBeatTimeout specifies the maximum time to wait for a heartbeat
// before terminating the connection. It is not recommended to change this
// value. By default, this value is 90.
HeartBeatTimeout int64 `json:"heart_beat_timeout"`
// UserConnTimeout specifies the maximum time to wait for a work
// connection. By default, this value is 10.
UserConnTimeout int64 `json:"user_conn_timeout"`
2019-12-20 13:28:28 +01:00
// HTTPPlugins specify the server plugins support HTTP protocol.
HTTPPlugins map[string]plugin.HTTPPluginOptions `json:"http_plugins"`
2020-05-24 11:48:37 +02:00
// UDPPacketSize specifies the UDP packet size
// By default, this value is 1500
2020-05-24 11:48:37 +02:00
UDPPacketSize int64 `json:"udp_packet_size"`
2017-03-08 19:03:47 +01:00
}
// GetDefaultServerConf returns a server configuration with reasonable
// defaults.
func GetDefaultServerConf() ServerCommonConf {
return ServerCommonConf{
BindAddr: "0.0.0.0",
BindPort: 7000,
2020-05-24 11:48:37 +02:00
BindUDPPort: 0,
KCPBindPort: 0,
ProxyBindAddr: "0.0.0.0",
2020-05-24 11:48:37 +02:00
VhostHTTPPort: 0,
VhostHTTPSPort: 0,
TCPMuxHTTPConnectPort: 0,
VhostHTTPTimeout: 60,
DashboardAddr: "0.0.0.0",
DashboardPort: 0,
DashboardUser: "admin",
DashboardPwd: "admin",
EnablePrometheus: false,
AssetsDir: "",
LogFile: "console",
LogWay: "console",
LogLevel: "info",
LogMaxDays: 3,
DisableLogColor: false,
DetailedErrorsToClient: true,
SubDomainHost: "",
2020-05-24 11:48:37 +02:00
TCPMux: true,
AllowPorts: make(map[int]struct{}),
MaxPoolCount: 5,
MaxPortsPerClient: 0,
2020-05-24 11:48:37 +02:00
TLSOnly: false,
TLSCertFile: "",
TLSKeyFile: "",
TLSTrustedCaFile: "",
HeartBeatTimeout: 90,
UserConnTimeout: 10,
Custom404Page: "",
HTTPPlugins: make(map[string]plugin.HTTPPluginOptions),
2020-05-24 11:48:37 +02:00
UDPPacketSize: 1500,
2017-03-08 19:03:47 +01:00
}
}
// UnmarshalServerConfFromIni parses the contents of a server configuration ini
// file and returns the resulting server configuration.
func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error) {
cfg = GetDefaultServerConf()
2018-04-10 11:46:49 +02:00
conf, err := ini.Load(strings.NewReader(content))
if err != nil {
err = fmt.Errorf("parse ini conf file error: %v", err)
return ServerCommonConf{}, err
2018-04-10 11:46:49 +02:00
}
2019-12-20 13:28:28 +01:00
UnmarshalPluginsFromIni(conf, &cfg)
2020-05-24 11:48:37 +02:00
cfg.ServerConfig = auth.UnmarshalServerConfFromIni(conf)
2017-03-08 19:03:47 +01:00
var (
tmpStr string
ok bool
v int64
)
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "bind_addr"); ok {
2017-03-08 19:03:47 +01:00
cfg.BindAddr = tmpStr
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "bind_port"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid bind_port")
return
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
cfg.BindPort = int(v)
2017-03-08 19:03:47 +01:00
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "bind_udp_port"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
return
2017-10-24 12:20:07 +02:00
}
2020-05-24 11:48:37 +02:00
cfg.BindUDPPort = int(v)
2017-10-24 12:20:07 +02:00
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "kcp_bind_port"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
return
2017-06-13 17:36:10 +02:00
}
2020-05-24 11:48:37 +02:00
cfg.KCPBindPort = int(v)
2017-06-13 17:36:10 +02:00
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "proxy_bind_addr"); ok {
2017-07-04 19:40:01 +02:00
cfg.ProxyBindAddr = tmpStr
} else {
cfg.ProxyBindAddr = cfg.BindAddr
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "vhost_http_port"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
2017-03-08 19:03:47 +01:00
return
}
2020-05-24 11:48:37 +02:00
cfg.VhostHTTPPort = int(v)
2017-03-08 19:03:47 +01:00
} else {
2020-05-24 11:48:37 +02:00
cfg.VhostHTTPPort = 0
2017-03-08 19:03:47 +01:00
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "vhost_https_port"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
2017-03-08 19:03:47 +01:00
return
}
2020-05-24 11:48:37 +02:00
cfg.VhostHTTPSPort = int(v)
2017-03-08 19:03:47 +01:00
} else {
2020-05-24 11:48:37 +02:00
cfg.VhostHTTPSPort = 0
2017-03-08 19:03:47 +01:00
}
if tmpStr, ok = conf.Get("common", "tcpmux_httpconnect_port"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid tcpmux_httpconnect_port")
return
}
2020-05-24 11:48:37 +02:00
cfg.TCPMuxHTTPConnectPort = int(v)
} else {
2020-05-24 11:48:37 +02:00
cfg.TCPMuxHTTPConnectPort = 0
}
2018-08-08 05:18:38 +02:00
if tmpStr, ok = conf.Get("common", "vhost_http_timeout"); ok {
v, errRet := strconv.ParseInt(tmpStr, 10, 64)
if errRet != nil || v < 0 {
err = fmt.Errorf("Parse conf error: invalid vhost_http_timeout")
return
}
2020-05-24 11:48:37 +02:00
cfg.VhostHTTPTimeout = v
2018-08-08 05:18:38 +02:00
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok {
2017-11-28 08:56:34 +01:00
cfg.DashboardAddr = tmpStr
} else {
cfg.DashboardAddr = cfg.BindAddr
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "dashboard_port"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid dashboard_port")
2017-03-08 19:03:47 +01:00
return
}
2020-05-24 11:48:37 +02:00
cfg.DashboardPort = int(v)
2017-03-08 19:03:47 +01:00
} else {
cfg.DashboardPort = 0
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
2017-03-08 19:03:47 +01:00
cfg.DashboardUser = tmpStr
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
2017-03-08 19:03:47 +01:00
cfg.DashboardPwd = tmpStr
}
if tmpStr, ok = conf.Get("common", "enable_prometheus"); ok && tmpStr == "true" {
cfg.EnablePrometheus = true
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
2017-03-08 19:03:47 +01:00
cfg.AssetsDir = tmpStr
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "log_file"); ok {
2017-03-08 19:03:47 +01:00
cfg.LogFile = tmpStr
if cfg.LogFile == "console" {
cfg.LogWay = "console"
} else {
cfg.LogWay = "file"
}
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "log_level"); ok {
2017-03-08 19:03:47 +01:00
cfg.LogLevel = tmpStr
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
2017-03-08 19:03:47 +01:00
v, err = strconv.ParseInt(tmpStr, 10, 64)
if err == nil {
cfg.LogMaxDays = v
}
}
if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
cfg.DisableLogColor = true
}
if tmpStr, ok = conf.Get("common", "detailed_errors_to_client"); ok && tmpStr == "false" {
cfg.DetailedErrorsToClient = false
} else {
cfg.DetailedErrorsToClient = true
}
2018-04-23 19:26:05 +02:00
if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
2018-04-10 11:46:49 +02:00
// e.g. 1000-2000,2001,2002,3000-4000
ports, errRet := util.ParseRangeNumbers(allowPortsStr)
if errRet != nil {
2018-04-23 19:26:05 +02:00
err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
2018-04-10 11:46:49 +02:00
return
}
2018-04-10 11:46:49 +02:00
for _, port := range ports {
2018-04-23 19:26:05 +02:00
cfg.AllowPorts[int(port)] = struct{}{}
2017-03-08 19:03:47 +01:00
}
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
2018-01-26 07:56:55 +01:00
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid max_pool_count")
return
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
if v < 0 {
err = fmt.Errorf("Parse conf error: invalid max_pool_count")
return
}
cfg.MaxPoolCount = v
2017-03-08 19:03:47 +01:00
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
2018-01-26 07:56:55 +01:00
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
return
}
2020-05-24 11:48:37 +02:00
if v < 0 {
err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
return
}
cfg.MaxPortsPerClient = v
2018-01-26 07:56:55 +01:00
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "subdomain_host"); ok {
2017-03-08 19:03:47 +01:00
cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
2020-05-24 11:48:37 +02:00
cfg.TCPMux = false
} else {
2020-05-24 11:48:37 +02:00
cfg.TCPMux = true
}
2019-04-25 06:29:34 +02:00
if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
cfg.Custom404Page = tmpStr
}
2018-04-10 11:46:49 +02:00
if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
2017-03-08 19:03:47 +01:00
v, errRet := strconv.ParseInt(tmpStr, 10, 64)
if errRet != nil {
err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
return
}
2020-05-24 11:48:37 +02:00
cfg.HeartBeatTimeout = v
2017-03-08 19:03:47 +01:00
}
if tmpStr, ok = conf.Get("common", "tls_only"); ok && tmpStr == "true" {
2020-05-24 11:48:37 +02:00
cfg.TLSOnly = true
} else {
2020-05-24 11:48:37 +02:00
cfg.TLSOnly = false
}
if tmpStr, ok = conf.Get("common", "udp_packet_size"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid udp_packet_size")
return
}
2020-05-24 11:48:37 +02:00
cfg.UDPPacketSize = v
}
if tmpStr, ok := conf.Get("common", "tls_cert_file"); ok {
cfg.TLSCertFile = tmpStr
}
if tmpStr, ok := conf.Get("common", "tls_key_file"); ok {
cfg.TLSKeyFile = tmpStr
}
if tmpStr, ok := conf.Get("common", "tls_trusted_ca_file"); ok {
cfg.TLSTrustedCaFile = tmpStr
2020-09-18 14:06:09 +02:00
cfg.TLSOnly = true
}
2017-03-08 19:03:47 +01:00
return
}
2018-04-10 11:46:49 +02:00
2019-12-20 13:28:28 +01:00
func UnmarshalPluginsFromIni(sections ini.File, cfg *ServerCommonConf) {
for name, section := range sections {
if strings.HasPrefix(name, "plugin.") {
name = strings.TrimSpace(strings.TrimPrefix(name, "plugin."))
options := plugin.HTTPPluginOptions{
Name: name,
Addr: section["addr"],
Path: section["path"],
Ops: strings.Split(section["ops"], ","),
}
2020-05-24 11:48:37 +02:00
for i := range options.Ops {
2019-12-20 13:28:28 +01:00
options.Ops[i] = strings.TrimSpace(options.Ops[i])
}
cfg.HTTPPlugins[name] = options
}
}
}
2020-09-18 14:06:09 +02:00
func (cfg *ServerCommonConf) Check() error {
return nil
2018-04-10 11:46:49 +02:00
}