2021-01-26 11:31:08 +08:00
|
|
|
// Copyright 2020 The frp Authors
|
2017-03-09 02:03:47 +08:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
"os"
|
2021-06-02 00:39:05 +08:00
|
|
|
"path/filepath"
|
2017-05-25 01:45:38 +08:00
|
|
|
"strings"
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/auth"
|
2021-01-26 11:31:08 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
2020-03-01 10:57:01 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
"gopkg.in/ini.v1"
|
2017-03-09 02:03:47 +08:00
|
|
|
)
|
|
|
|
|
2019-08-25 06:20:34 +08:00
|
|
|
// ClientCommonConf contains information for a client service. It is
|
|
|
|
// recommended to use GetDefaultClientConf instead of creating this object
|
|
|
|
// directly, so that all unspecified fields have reasonable default values.
|
2017-03-09 02:03:47 +08:00
|
|
|
type ClientCommonConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
auth.ClientConfig `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2019-08-25 06:20:34 +08:00
|
|
|
// ServerAddr specifies the address of the server to connect to. By
|
|
|
|
// default, this value is "0.0.0.0".
|
2021-01-26 11:31:08 +08:00
|
|
|
ServerAddr string `ini:"server_addr" josn:"server_addr"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// ServerPort specifies the port to connect to the server on. By default,
|
|
|
|
// this value is 7000.
|
2021-01-26 11:31:08 +08:00
|
|
|
ServerPort int `ini:"server_port" json:"server_port"`
|
2020-05-24 17:48:37 +08:00
|
|
|
// HTTPProxy specifies a proxy address to connect to the server through. If
|
2019-08-25 06:20:34 +08:00
|
|
|
// this value is "", the server will be connected to directly. By default,
|
|
|
|
// this value is read from the "http_proxy" environment variable.
|
2021-01-26 11:31:08 +08:00
|
|
|
HTTPProxy string `ini:"http_proxy" json:"http_proxy"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// 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".
|
2021-01-26 11:31:08 +08:00
|
|
|
LogFile string `ini:"log_file" json:"log_file"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// 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".
|
2021-01-26 11:31:08 +08:00
|
|
|
LogWay string `ini:"log_way" json:"log_way"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// LogLevel specifies the minimum log level. Valid values are "trace",
|
|
|
|
// "debug", "info", "warn", and "error". By default, this value is "info".
|
2021-01-26 11:31:08 +08:00
|
|
|
LogLevel string `ini:"log_level" json:"log_level"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// 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.
|
2021-01-26 11:31:08 +08:00
|
|
|
LogMaxDays int64 `ini:"log_max_days" json:"log_max_days"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// DisableLogColor disables log colors when LogWay == "console" when set to
|
|
|
|
// true. By default, this value is false.
|
2021-01-26 11:31:08 +08:00
|
|
|
DisableLogColor bool `ini:"disable_log_color" json:"disable_log_color"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// AdminAddr specifies the address that the admin server binds to. By
|
|
|
|
// default, this value is "127.0.0.1".
|
2021-01-26 11:31:08 +08:00
|
|
|
AdminAddr string `ini:"admin_addr" json:"admin_addr"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// AdminPort specifies the port for the admin server to listen on. If this
|
|
|
|
// value is 0, the admin server will not be started. By default, this value
|
|
|
|
// is 0.
|
2021-01-26 11:31:08 +08:00
|
|
|
AdminPort int `ini:"admin_port" json:"admin_port"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// AdminUser specifies the username that the admin server will use for
|
2021-05-10 23:04:26 +08:00
|
|
|
// login.
|
2021-01-26 11:31:08 +08:00
|
|
|
AdminUser string `ini:"admin_user" json:"admin_user"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// AdminPwd specifies the password that the admin server will use for
|
2021-05-10 23:04:26 +08:00
|
|
|
// login.
|
2021-01-26 11:31:08 +08:00
|
|
|
AdminPwd string `ini:"admin_pwd" json:"admin_pwd"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// AssetsDir specifies the local directory that the admin server will load
|
|
|
|
// resources from. If this value is "", assets will be loaded from the
|
|
|
|
// bundled executable using statik. By default, this value is "".
|
2021-01-26 11:31:08 +08:00
|
|
|
AssetsDir string `ini:"assets_dir" json:"assets_dir"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// PoolCount specifies the number of connections the client will make to
|
|
|
|
// the server in advance. By default, this value is 0.
|
2021-01-26 11:31:08 +08:00
|
|
|
PoolCount int `ini:"pool_count" json:"pool_count"`
|
2020-05-24 17:48:37 +08:00
|
|
|
// TCPMux toggles TCP stream multiplexing. This allows multiple requests
|
2019-08-25 06:20:34 +08:00
|
|
|
// from a client to share a single TCP connection. If this value is true,
|
|
|
|
// the server must have TCP multiplexing enabled as well. By default, this
|
|
|
|
// value is true.
|
2021-01-26 11:31:08 +08:00
|
|
|
TCPMux bool `ini:"tcp_mux" json:"tcp_mux"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// User specifies a prefix for proxy names to distinguish them from other
|
|
|
|
// clients. If this value is not "", proxy names will automatically be
|
|
|
|
// changed to "{user}.{proxy_name}". By default, this value is "".
|
2021-01-26 11:31:08 +08:00
|
|
|
User string `ini:"user" json:"user"`
|
2020-05-24 17:48:37 +08:00
|
|
|
// DNSServer specifies a DNS server address for FRPC to use. If this value
|
2019-08-25 06:20:34 +08:00
|
|
|
// is "", the default DNS will be used. By default, this value is "".
|
2021-01-26 11:31:08 +08:00
|
|
|
DNSServer string `ini:"dns_server" json:"dns_server"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// LoginFailExit controls whether or not the client should exit after a
|
|
|
|
// failed login attempt. If false, the client will retry until a login
|
|
|
|
// attempt succeeds. By default, this value is true.
|
2021-01-26 11:31:08 +08:00
|
|
|
LoginFailExit bool `ini:"login_fail_exit" json:"login_fail_exit"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// Start specifies a set of enabled proxies by name. If this set is empty,
|
|
|
|
// all supplied proxies are enabled. By default, this value is an empty
|
|
|
|
// set.
|
2021-01-26 11:31:08 +08:00
|
|
|
Start []string `ini:"start" json:"start"`
|
|
|
|
//Start map[string]struct{} `json:"start"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// Protocol specifies the protocol to use when interacting with the server.
|
2020-09-23 12:05:05 +08:00
|
|
|
// Valid values are "tcp", "kcp" and "websocket". By default, this value
|
2019-08-25 06:20:34 +08:00
|
|
|
// is "tcp".
|
2021-01-26 11:31:08 +08:00
|
|
|
Protocol string `ini:"protocol" json:"protocol"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// TLSEnable specifies whether or not TLS should be used when communicating
|
2020-09-18 19:58:58 +08:00
|
|
|
// with the server. If "tls_cert_file" and "tls_key_file" are valid,
|
|
|
|
// client will load the supplied tls configuration.
|
2021-01-26 11:31:08 +08:00
|
|
|
TLSEnable bool `ini:"tls_enable" json:"tls_enable"`
|
2021-03-07 14:57:23 +08:00
|
|
|
// TLSCertPath specifies the path of the cert file that client will
|
2020-09-18 19:58:58 +08:00
|
|
|
// load. It only works when "tls_enable" is true and "tls_key_file" is valid.
|
2021-01-26 11:31:08 +08:00
|
|
|
TLSCertFile string `ini:"tls_cert_file" json:"tls_cert_file"`
|
2021-03-07 14:57:23 +08:00
|
|
|
// TLSKeyPath specifies the path of the secret key file that client
|
2020-09-18 19:58:58 +08:00
|
|
|
// will load. It only works when "tls_enable" is true and "tls_cert_file"
|
|
|
|
// are valid.
|
2021-01-26 11:31:08 +08:00
|
|
|
TLSKeyFile string `ini:"tls_key_file" json:"tls_key_file"`
|
2021-03-07 14:57:23 +08:00
|
|
|
// TLSTrustedCaFile specifies the path of the trusted ca file that will load.
|
2020-09-18 19:58:58 +08:00
|
|
|
// It only works when "tls_enable" is valid and tls configuration of server
|
|
|
|
// has been specified.
|
2021-01-26 11:31:08 +08:00
|
|
|
TLSTrustedCaFile string `ini:"tls_trusted_ca_file" json:"tls_trusted_ca_file"`
|
2021-03-07 14:57:23 +08:00
|
|
|
// TLSServerName specifices the custom server name of tls certificate. By
|
|
|
|
// default, server name if same to ServerAddr.
|
|
|
|
TLSServerName string `ini:"tls_server_name" json:"tls_server_name"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// HeartBeatInterval specifies at what interval heartbeats are sent to the
|
|
|
|
// server, in seconds. It is not recommended to change this value. By
|
|
|
|
// default, this value is 30.
|
2021-01-26 11:31:08 +08:00
|
|
|
HeartbeatInterval int64 `ini:"heartbeat_interval" json:"heartbeat_interval"`
|
2019-08-25 06:20:34 +08:00
|
|
|
// HeartBeatTimeout specifies the maximum allowed heartbeat response delay
|
|
|
|
// before the connection is terminated, in seconds. It is not recommended
|
|
|
|
// to change this value. By default, this value is 90.
|
2021-01-26 11:31:08 +08:00
|
|
|
HeartbeatTimeout int64 `ini:"heartbeat_timeout" json:"heartbeat_timeout"`
|
2019-12-08 21:01:58 +08:00
|
|
|
// Client meta info
|
2021-01-26 11:31:08 +08:00
|
|
|
Metas map[string]string `ini:"-" json:"metas"`
|
2020-05-24 17:48:37 +08:00
|
|
|
// UDPPacketSize specifies the udp packet size
|
2020-05-07 17:47:36 +08:00
|
|
|
// By default, this value is 1500
|
2021-01-26 11:31:08 +08:00
|
|
|
UDPPacketSize int64 `ini:"udp_packet_size" json:"udp_packet_size"`
|
2021-06-02 00:39:05 +08:00
|
|
|
// Include other config files for proxies.
|
|
|
|
IncludeConfigFiles []string `ini:"includes" json:"includes"`
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2019-08-25 06:20:34 +08:00
|
|
|
// GetDefaultClientConf returns a client configuration with default values.
|
2019-08-21 04:53:27 +08:00
|
|
|
func GetDefaultClientConf() ClientCommonConf {
|
|
|
|
return ClientCommonConf{
|
2021-06-02 00:39:05 +08:00
|
|
|
ClientConfig: auth.GetDefaultClientConf(),
|
|
|
|
ServerAddr: "0.0.0.0",
|
|
|
|
ServerPort: 7000,
|
|
|
|
HTTPProxy: os.Getenv("http_proxy"),
|
|
|
|
LogFile: "console",
|
|
|
|
LogWay: "console",
|
|
|
|
LogLevel: "info",
|
|
|
|
LogMaxDays: 3,
|
|
|
|
DisableLogColor: false,
|
|
|
|
AdminAddr: "127.0.0.1",
|
|
|
|
AdminPort: 0,
|
|
|
|
AdminUser: "",
|
|
|
|
AdminPwd: "",
|
|
|
|
AssetsDir: "",
|
|
|
|
PoolCount: 1,
|
|
|
|
TCPMux: true,
|
|
|
|
User: "",
|
|
|
|
DNSServer: "",
|
|
|
|
LoginFailExit: true,
|
|
|
|
Start: make([]string, 0),
|
|
|
|
Protocol: "tcp",
|
|
|
|
TLSEnable: false,
|
|
|
|
TLSCertFile: "",
|
|
|
|
TLSKeyFile: "",
|
|
|
|
TLSTrustedCaFile: "",
|
|
|
|
HeartbeatInterval: 30,
|
|
|
|
HeartbeatTimeout: 90,
|
|
|
|
Metas: make(map[string]string),
|
|
|
|
UDPPacketSize: 1500,
|
|
|
|
IncludeConfigFiles: make([]string, 0),
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 14:53:30 +08:00
|
|
|
func (cfg *ClientCommonConf) Complete() {
|
|
|
|
if cfg.LogFile == "console" {
|
|
|
|
cfg.LogWay = "console"
|
|
|
|
} else {
|
|
|
|
cfg.LogWay = "file"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *ClientCommonConf) Validate() error {
|
2021-01-26 11:31:08 +08:00
|
|
|
if cfg.HeartbeatInterval <= 0 {
|
2021-03-22 14:53:30 +08:00
|
|
|
return fmt.Errorf("invalid heartbeat_interval")
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if cfg.HeartbeatTimeout < cfg.HeartbeatInterval {
|
2021-03-22 14:53:30 +08:00
|
|
|
return fmt.Errorf("invalid heartbeat_timeout, heartbeat_timeout is less than heartbeat_interval")
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if cfg.TLSEnable == false {
|
|
|
|
if cfg.TLSCertFile != "" {
|
|
|
|
fmt.Println("WARNING! tls_cert_file is invalid when tls_enable is false")
|
|
|
|
}
|
2020-03-01 10:57:01 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if cfg.TLSKeyFile != "" {
|
|
|
|
fmt.Println("WARNING! tls_key_file is invalid when tls_enable is false")
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if cfg.TLSTrustedCaFile != "" {
|
|
|
|
fmt.Println("WARNING! tls_trusted_ca_file is invalid when tls_enable is false")
|
2018-01-17 21:49:37 +08:00
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-03-22 14:53:30 +08:00
|
|
|
if cfg.Protocol != "tcp" && cfg.Protocol != "kcp" && cfg.Protocol != "websocket" {
|
|
|
|
return fmt.Errorf("invalid protocol")
|
|
|
|
}
|
|
|
|
|
2021-06-02 00:39:05 +08:00
|
|
|
for _, f := range cfg.IncludeConfigFiles {
|
|
|
|
absDir, err := filepath.Abs(filepath.Dir(f))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("include: parse directory of %s failed: %v", f, absDir)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(absDir); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("include: directory of %s not exist", f)
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-12 00:47:35 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Supported sources including: string(file path), []byte, Reader interface.
|
|
|
|
func UnmarshalClientConfFromIni(source interface{}) (ClientCommonConf, error) {
|
|
|
|
f, err := ini.LoadSources(ini.LoadOptions{
|
|
|
|
Insensitive: false,
|
|
|
|
InsensitiveSections: false,
|
|
|
|
InsensitiveKeys: false,
|
|
|
|
IgnoreInlineComment: true,
|
|
|
|
AllowBooleanKeys: true,
|
|
|
|
}, source)
|
|
|
|
if err != nil {
|
|
|
|
return ClientCommonConf{}, err
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
s, err := f.GetSection("common")
|
|
|
|
if err != nil {
|
|
|
|
return ClientCommonConf{}, fmt.Errorf("invalid configuration file, not found [common] section")
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
common := GetDefaultClientConf()
|
|
|
|
err = s.MapTo(&common)
|
|
|
|
if err != nil {
|
|
|
|
return ClientCommonConf{}, err
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
common.Metas = GetMapWithoutPrefix(s.KeysHash(), "meta_")
|
|
|
|
return common, nil
|
|
|
|
}
|
2017-07-13 02:20:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// if len(startProxy) is 0, start all
|
|
|
|
// otherwise just start proxies in startProxy map
|
|
|
|
func LoadAllProxyConfsFromIni(
|
|
|
|
prefix string,
|
|
|
|
source interface{},
|
|
|
|
start []string,
|
|
|
|
) (map[string]ProxyConf, map[string]VisitorConf, error) {
|
|
|
|
|
|
|
|
f, err := ini.LoadSources(ini.LoadOptions{
|
|
|
|
Insensitive: false,
|
|
|
|
InsensitiveSections: false,
|
|
|
|
InsensitiveKeys: false,
|
|
|
|
IgnoreInlineComment: true,
|
|
|
|
AllowBooleanKeys: true,
|
|
|
|
}, source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2017-07-13 02:20:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
proxyConfs := make(map[string]ProxyConf)
|
|
|
|
visitorConfs := make(map[string]VisitorConf)
|
|
|
|
|
|
|
|
if prefix != "" {
|
|
|
|
prefix += "."
|
2017-07-13 02:20:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
startProxy := make(map[string]struct{})
|
|
|
|
for _, s := range start {
|
|
|
|
startProxy[s] = struct{}{}
|
2017-07-13 02:20:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
startAll := true
|
|
|
|
if len(startProxy) > 0 {
|
|
|
|
startAll = false
|
2019-08-20 07:51:03 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Build template sections from range section And append to ini.File.
|
|
|
|
rangeSections := make([]*ini.Section, 0)
|
|
|
|
for _, section := range f.Sections() {
|
|
|
|
|
|
|
|
if !strings.HasPrefix(section.Name(), "range:") {
|
|
|
|
continue
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
rangeSections = append(rangeSections, section)
|
2017-05-17 17:47:20 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
for _, section := range rangeSections {
|
|
|
|
err = renderRangeProxyTemplates(f, section)
|
|
|
|
if err != nil {
|
2021-05-12 12:15:22 +08:00
|
|
|
return nil, nil, fmt.Errorf("failed to render template for proxy %s: %v", section.Name(), err)
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
for _, section := range f.Sections() {
|
|
|
|
name := section.Name()
|
2018-04-23 02:59:40 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if name == ini.DefaultSection || name == "common" || strings.HasPrefix(name, "range:") {
|
|
|
|
continue
|
2017-05-25 01:45:38 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
_, shouldStart := startProxy[name]
|
|
|
|
if !startAll && !shouldStart {
|
|
|
|
continue
|
|
|
|
}
|
2017-05-25 01:10:58 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
roleType := section.Key("role").String()
|
|
|
|
if roleType == "" {
|
|
|
|
roleType = "server"
|
2017-06-04 19:56:21 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
switch roleType {
|
|
|
|
case "server":
|
|
|
|
newConf, newErr := NewProxyConfFromIni(prefix, name, section)
|
|
|
|
if newErr != nil {
|
2021-05-12 12:15:22 +08:00
|
|
|
return nil, nil, fmt.Errorf("failed to parse proxy %s, err: %v", name, newErr)
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
proxyConfs[prefix+name] = newConf
|
|
|
|
case "visitor":
|
|
|
|
newConf, newErr := NewVisitorConfFromIni(prefix, name, section)
|
|
|
|
if newErr != nil {
|
|
|
|
return nil, nil, newErr
|
|
|
|
}
|
|
|
|
visitorConfs[prefix+name] = newConf
|
|
|
|
default:
|
2021-05-12 12:15:22 +08:00
|
|
|
return nil, nil, fmt.Errorf("proxy %s role should be 'server' or 'visitor'", name)
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
2019-03-11 14:14:31 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
return proxyConfs, visitorConfs, nil
|
|
|
|
}
|
2019-03-11 14:14:31 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func renderRangeProxyTemplates(f *ini.File, section *ini.Section) error {
|
2020-09-18 19:58:58 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Validation
|
|
|
|
localPortStr := section.Key("local_port").String()
|
|
|
|
remotePortStr := section.Key("remote_port").String()
|
|
|
|
if localPortStr == "" || remotePortStr == "" {
|
|
|
|
return fmt.Errorf("local_port or remote_port is empty")
|
2020-09-18 19:58:58 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
localPorts, err := util.ParseRangeNumbers(localPortStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-09-18 19:58:58 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
remotePorts, err := util.ParseRangeNumbers(remotePortStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if len(localPorts) != len(remotePorts) {
|
|
|
|
return fmt.Errorf("local ports number should be same with remote ports number")
|
2019-12-08 21:01:58 +08:00
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if len(localPorts) == 0 {
|
|
|
|
return fmt.Errorf("local_port and remote_port is necessary")
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Templates
|
|
|
|
prefix := strings.TrimSpace(strings.TrimPrefix(section.Name(), "range:"))
|
2020-09-18 19:58:58 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
for i := range localPorts {
|
|
|
|
tmpname := fmt.Sprintf("%s_%d", prefix, i)
|
2020-09-18 19:58:58 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
tmpsection, err := f.NewSection(tmpname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-09-18 19:58:58 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
copySection(section, tmpsection)
|
|
|
|
tmpsection.NewKey("local_port", fmt.Sprintf("%d", localPorts[i]))
|
|
|
|
tmpsection.NewKey("remote_port", fmt.Sprintf("%d", remotePorts[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copySection(source, target *ini.Section) {
|
|
|
|
for key, value := range source.KeysHash() {
|
|
|
|
target.NewKey(key, value)
|
2020-09-18 19:58:58 +08:00
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|