frp/models/config/server_common.go

381 lines
12 KiB
Go
Raw Normal View History

2017-03-09 02:03:47 +08: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/utils/util"
2017-03-09 02:03:47 +08: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-09 02:03:47 +08:00
type ServerCommonConf struct {
// 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"`
// 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
BindUdpPort int `json:"bind_udp_port"`
// BindKcpPort 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.
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 17:46:49 +08:00
ProxyBindAddr string `json:"proxy_bind_addr"`
2017-03-09 02:03:47 +08: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.
2018-04-10 17:46:49 +08:00
VhostHttpPort int `json:"vhost_http_port"`
2017-03-09 02:03:47 +08:00
// 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.
2019-01-31 16:49:23 +08:00
VhostHttpsPort int `json:"vhost_https_port"`
2018-08-08 11:18:38 +08:00
// VhostHttpTimeout specifies the response header timeout for the Vhost
// HTTP server, in seconds. By default, this value is 60.
2018-08-08 11:18:38 +08: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 11:18:38 +08:00
DashboardAddr string `json:"dashboard_addr"`
2017-03-09 02:03:47 +08:00
// 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"`
// 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"`
// 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. By default, this value is "".
Token string `json:"token"`
// 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"`
// TcpMux toggles TCP stream multiplexing. This allows multiple requests
// from a client to share a single TCP connection. By default, this value
// is true.
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-24 01:26:05 +08:00
MaxPortsPerClient int64 `json:"max_ports_per_client"`
// 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"`
2017-03-09 02:03:47 +08:00
}
// GetDefaultServerConf returns a server configuration with reasonable
// defaults.
func GetDefaultServerConf() ServerCommonConf {
return ServerCommonConf{
2018-04-24 01:26:05 +08:00
BindAddr: "0.0.0.0",
BindPort: 7000,
BindUdpPort: 0,
KcpBindPort: 0,
ProxyBindAddr: "0.0.0.0",
VhostHttpPort: 0,
VhostHttpsPort: 0,
2018-08-08 11:18:38 +08:00
VhostHttpTimeout: 60,
2018-04-24 01:26:05 +08:00
DashboardAddr: "0.0.0.0",
DashboardPort: 0,
DashboardUser: "admin",
DashboardPwd: "admin",
AssetsDir: "",
LogFile: "console",
LogWay: "console",
LogLevel: "info",
LogMaxDays: 3,
DisableLogColor: false,
2018-04-24 01:26:05 +08:00
Token: "",
SubDomainHost: "",
TcpMux: true,
AllowPorts: make(map[int]struct{}),
MaxPoolCount: 5,
MaxPortsPerClient: 0,
HeartBeatTimeout: 90,
UserConnTimeout: 10,
2019-04-25 12:29:34 +08:00
Custom404Page: "",
2017-03-09 02:03:47 +08: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 17:46:49 +08: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 17:46:49 +08:00
}
2017-03-09 02:03:47 +08:00
var (
tmpStr string
ok bool
v int64
)
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "bind_addr"); ok {
2017-03-09 02:03:47 +08:00
cfg.BindAddr = tmpStr
}
2018-04-10 17:46:49 +08: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
} else {
cfg.BindPort = int(v)
2017-03-09 02:03:47 +08:00
}
}
2018-04-10 17:46:49 +08: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
} else {
cfg.BindUdpPort = int(v)
2017-10-24 18:20:07 +08:00
}
}
2018-04-10 17:46:49 +08: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
} else {
cfg.KcpBindPort = int(v)
2017-06-13 23:36:10 +08:00
}
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "proxy_bind_addr"); ok {
2017-07-05 01:40:01 +08:00
cfg.ProxyBindAddr = tmpStr
} else {
cfg.ProxyBindAddr = cfg.BindAddr
}
2018-04-10 17:46:49 +08: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-09 02:03:47 +08:00
return
} else {
cfg.VhostHttpPort = int(v)
2017-03-09 02:03:47 +08:00
}
} else {
cfg.VhostHttpPort = 0
}
2018-04-10 17:46:49 +08: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-09 02:03:47 +08:00
return
} else {
cfg.VhostHttpsPort = int(v)
2017-03-09 02:03:47 +08:00
}
} else {
cfg.VhostHttpsPort = 0
}
2018-08-08 11:18:38 +08: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
} else {
cfg.VhostHttpTimeout = v
}
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok {
2017-11-28 15:56:34 +08:00
cfg.DashboardAddr = tmpStr
} else {
cfg.DashboardAddr = cfg.BindAddr
}
2018-04-10 17:46:49 +08: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-09 02:03:47 +08:00
return
} else {
cfg.DashboardPort = int(v)
2017-03-09 02:03:47 +08:00
}
} else {
cfg.DashboardPort = 0
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
2017-03-09 02:03:47 +08:00
cfg.DashboardUser = tmpStr
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
2017-03-09 02:03:47 +08:00
cfg.DashboardPwd = tmpStr
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
2017-03-09 02:03:47 +08:00
cfg.AssetsDir = tmpStr
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "log_file"); ok {
2017-03-09 02:03:47 +08:00
cfg.LogFile = tmpStr
if cfg.LogFile == "console" {
cfg.LogWay = "console"
} else {
cfg.LogWay = "file"
}
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "log_level"); ok {
2017-03-09 02:03:47 +08:00
cfg.LogLevel = tmpStr
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
2017-03-09 02:03:47 +08: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
}
2018-04-10 17:46:49 +08:00
cfg.Token, _ = conf.Get("common", "token")
2017-03-09 02:03:47 +08:00
2018-04-24 01:26:05 +08:00
if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
2018-04-10 17:46:49 +08:00
// e.g. 1000-2000,2001,2002,3000-4000
ports, errRet := util.ParseRangeNumbers(allowPortsStr)
if errRet != nil {
2018-04-24 01:26:05 +08:00
err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
2018-04-10 17:46:49 +08:00
return
}
2018-04-10 17:46:49 +08:00
for _, port := range ports {
2018-04-24 01:26:05 +08:00
cfg.AllowPorts[int(port)] = struct{}{}
2017-03-09 02:03:47 +08:00
}
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
2018-01-26 14:56:55 +08:00
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid max_pool_count")
return
} else {
if v < 0 {
err = fmt.Errorf("Parse conf error: invalid max_pool_count")
return
}
2017-03-09 02:03:47 +08:00
cfg.MaxPoolCount = v
}
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
2018-01-26 14:56:55 +08:00
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
return
} else {
if v < 0 {
err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
return
}
cfg.MaxPortsPerClient = v
}
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "subdomain_host"); ok {
2017-03-09 02:03:47 +08:00
cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
cfg.TcpMux = false
} else {
cfg.TcpMux = true
}
2019-04-25 12:29:34 +08:00
if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
cfg.Custom404Page = tmpStr
}
2018-04-10 17:46:49 +08:00
if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
2017-03-09 02:03:47 +08:00
v, errRet := strconv.ParseInt(tmpStr, 10, 64)
if errRet != nil {
err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
return
} else {
cfg.HeartBeatTimeout = v
}
}
return
}
2018-04-10 17:46:49 +08:00
func (cfg *ServerCommonConf) Check() (err error) {
return
}