frp/pkg/config/proxy.go

1188 lines
32 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"
2017-03-09 19:01:17 +01:00
"reflect"
2017-03-08 19:03:47 +01:00
"strconv"
"strings"
2020-09-23 07:49:14 +02:00
"github.com/fatedier/frp/pkg/consts"
"github.com/fatedier/frp/pkg/msg"
"github.com/fatedier/frp/pkg/util/util"
2017-03-08 19:03:47 +01:00
ini "github.com/vaughan0/go-ini"
)
2018-04-10 11:46:49 +02:00
var (
proxyConfTypeMap map[string]reflect.Type
)
2017-03-09 19:01:17 +01:00
func init() {
proxyConfTypeMap = make(map[string]reflect.Type)
2020-05-24 11:48:37 +02:00
proxyConfTypeMap[consts.TCPProxy] = reflect.TypeOf(TCPProxyConf{})
proxyConfTypeMap[consts.TCPMuxProxy] = reflect.TypeOf(TCPMuxProxyConf{})
proxyConfTypeMap[consts.UDPProxy] = reflect.TypeOf(UDPProxyConf{})
proxyConfTypeMap[consts.HTTPProxy] = reflect.TypeOf(HTTPProxyConf{})
proxyConfTypeMap[consts.HTTPSProxy] = reflect.TypeOf(HTTPSProxyConf{})
proxyConfTypeMap[consts.STCPProxy] = reflect.TypeOf(STCPProxyConf{})
proxyConfTypeMap[consts.XTCPProxy] = reflect.TypeOf(XTCPProxyConf{})
proxyConfTypeMap[consts.SUDPProxy] = reflect.TypeOf(SUDPProxyConf{})
2017-03-09 19:01:17 +01:00
}
// NewConfByType creates a empty ProxyConf object by proxyType.
// If proxyType isn't exist, return nil.
func NewConfByType(proxyType string) ProxyConf {
v, ok := proxyConfTypeMap[proxyType]
if !ok {
return nil
}
cfg := reflect.New(v).Interface().(ProxyConf)
return cfg
}
2017-03-08 19:03:47 +01:00
type ProxyConf interface {
GetBaseInfo() *BaseProxyConf
2018-04-10 11:46:49 +02:00
UnmarshalFromMsg(pMsg *msg.NewProxy)
UnmarshalFromIni(prefix string, name string, conf ini.Section) error
MarshalToMsg(pMsg *msg.NewProxy)
CheckForCli() error
CheckForSvr(serverCfg ServerCommonConf) error
Compare(conf ProxyConf) bool
2017-03-08 19:03:47 +01:00
}
func NewProxyConfFromMsg(pMsg *msg.NewProxy, serverCfg ServerCommonConf) (cfg ProxyConf, err error) {
2017-03-08 19:03:47 +01:00
if pMsg.ProxyType == "" {
2020-05-24 11:48:37 +02:00
pMsg.ProxyType = consts.TCPProxy
2017-03-08 19:03:47 +01:00
}
2017-03-09 19:01:17 +01:00
cfg = NewConfByType(pMsg.ProxyType)
if cfg == nil {
2017-03-08 19:03:47 +01:00
err = fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
return
}
2018-04-10 11:46:49 +02:00
cfg.UnmarshalFromMsg(pMsg)
err = cfg.CheckForSvr(serverCfg)
2017-03-08 19:03:47 +01:00
return
}
2018-04-10 11:46:49 +02:00
func NewProxyConfFromIni(prefix string, name string, section ini.Section) (cfg ProxyConf, err error) {
2017-03-08 19:03:47 +01:00
proxyType := section["type"]
if proxyType == "" {
2020-05-24 11:48:37 +02:00
proxyType = consts.TCPProxy
section["type"] = consts.TCPProxy
2017-03-08 19:03:47 +01:00
}
2017-03-09 19:01:17 +01:00
cfg = NewConfByType(proxyType)
if cfg == nil {
2017-03-08 19:03:47 +01:00
err = fmt.Errorf("proxy [%s] type [%s] error", name, proxyType)
return
}
2018-04-10 11:46:49 +02:00
if err = cfg.UnmarshalFromIni(prefix, name, section); err != nil {
return
}
if err = cfg.CheckForCli(); err != nil {
return
}
2017-03-08 19:03:47 +01:00
return
}
// BaseProxyConf provides configuration info that is common to all proxy types.
2017-03-08 19:03:47 +01:00
type BaseProxyConf struct {
// ProxyName is the name of this proxy.
2017-03-22 19:01:25 +01:00
ProxyName string `json:"proxy_name"`
// ProxyType specifies the type of this proxy. Valid values include "tcp",
// "udp", "http", "https", "stcp", and "xtcp". By default, this value is
// "tcp".
2017-03-22 19:01:25 +01:00
ProxyType string `json:"proxy_type"`
2017-03-08 19:03:47 +01:00
// UseEncryption controls whether or not communication with the server will
// be encrypted. Encryption is done using the tokens supplied in the server
// and client configuration. By default, this value is false.
UseEncryption bool `json:"use_encryption"`
// UseCompression controls whether or not communication with the server
// will be compressed. By default, this value is false.
UseCompression bool `json:"use_compression"`
// Group specifies which group the proxy is a part of. The server will use
// this information to load balance proxies in the same group. If the value
// is "", this proxy will not be in a group. By default, this value is "".
Group string `json:"group"`
// GroupKey specifies a group key, which should be the same among proxies
// of the same group. By default, this value is "".
GroupKey string `json:"group_key"`
// ProxyProtocolVersion specifies which protocol version to use. Valid
// values include "v1", "v2", and "". If the value is "", a protocol
// version will be automatically selected. By default, this value is "".
2019-03-29 12:01:18 +01:00
ProxyProtocolVersion string `json:"proxy_protocol_version"`
2019-11-02 18:20:49 +01:00
2019-11-08 18:13:30 +01:00
// BandwidthLimit limit the proxy bandwidth
2019-11-02 18:20:49 +01:00
// 0 means no limit
2019-11-08 18:13:30 +01:00
BandwidthLimit BandwidthQuantity `json:"bandwidth_limit"`
2019-11-02 18:20:49 +01:00
2019-12-08 14:01:58 +01:00
// meta info for each proxy
Metas map[string]string `json:"metas"`
LocalSvrConf
2019-03-29 12:01:18 +01:00
HealthCheckConf
2017-03-08 19:03:47 +01:00
}
func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
return cfg
}
func (cfg *BaseProxyConf) compare(cmp *BaseProxyConf) bool {
if cfg.ProxyName != cmp.ProxyName ||
cfg.ProxyType != cmp.ProxyType ||
cfg.UseEncryption != cmp.UseEncryption ||
cfg.UseCompression != cmp.UseCompression ||
cfg.Group != cmp.Group ||
2019-03-29 12:01:18 +01:00
cfg.GroupKey != cmp.GroupKey ||
2019-11-02 18:20:49 +01:00
cfg.ProxyProtocolVersion != cmp.ProxyProtocolVersion ||
!cfg.BandwidthLimit.Equal(&cmp.BandwidthLimit) ||
2019-12-08 14:01:58 +01:00
!reflect.DeepEqual(cfg.Metas, cmp.Metas) {
return false
}
if !cfg.LocalSvrConf.compare(&cmp.LocalSvrConf) {
return false
}
if !cfg.HealthCheckConf.compare(&cmp.HealthCheckConf) {
return false
}
return true
}
2018-04-10 11:46:49 +02:00
func (cfg *BaseProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2017-03-08 19:03:47 +01:00
cfg.ProxyName = pMsg.ProxyName
cfg.ProxyType = pMsg.ProxyType
cfg.UseEncryption = pMsg.UseEncryption
cfg.UseCompression = pMsg.UseCompression
cfg.Group = pMsg.Group
cfg.GroupKey = pMsg.GroupKey
2019-12-08 14:01:58 +01:00
cfg.Metas = pMsg.Metas
2017-03-08 19:03:47 +01:00
}
2018-04-10 11:46:49 +02:00
func (cfg *BaseProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) error {
2017-03-08 19:03:47 +01:00
var (
tmpStr string
ok bool
2019-11-02 18:20:49 +01:00
err error
2017-03-08 19:03:47 +01:00
)
2018-04-10 11:46:49 +02:00
cfg.ProxyName = prefix + name
2017-03-08 19:03:47 +01:00
cfg.ProxyType = section["type"]
tmpStr, ok = section["use_encryption"]
if ok && tmpStr == "true" {
cfg.UseEncryption = true
}
tmpStr, ok = section["use_compression"]
if ok && tmpStr == "true" {
cfg.UseCompression = true
}
cfg.Group = section["group"]
cfg.GroupKey = section["group_key"]
2019-03-29 12:01:18 +01:00
cfg.ProxyProtocolVersion = section["proxy_protocol_version"]
2019-11-08 18:13:30 +01:00
if cfg.BandwidthLimit, err = NewBandwidthQuantity(section["bandwidth_limit"]); err != nil {
2019-11-02 18:20:49 +01:00
return err
}
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
return err
}
2019-11-02 18:20:49 +01:00
if err = cfg.HealthCheckConf.UnmarshalFromIni(prefix, name, section); err != nil {
return err
}
2018-12-07 10:05:36 +01:00
if cfg.HealthCheckType == "tcp" && cfg.Plugin == "" {
2020-05-24 11:48:37 +02:00
cfg.HealthCheckAddr = cfg.LocalIP + fmt.Sprintf(":%d", cfg.LocalPort)
2018-12-07 10:05:36 +01:00
}
2020-05-24 11:48:37 +02:00
if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckURL != "" {
s := fmt.Sprintf("http://%s:%d", cfg.LocalIP, cfg.LocalPort)
if !strings.HasPrefix(cfg.HealthCheckURL, "/") {
2018-12-09 14:56:46 +01:00
s += "/"
}
2020-05-24 11:48:37 +02:00
cfg.HealthCheckURL = s + cfg.HealthCheckURL
2018-12-09 14:56:46 +01:00
}
2019-12-08 14:01:58 +01:00
cfg.Metas = make(map[string]string)
2019-12-08 14:01:58 +01:00
for k, v := range section {
if strings.HasPrefix(k, "meta_") {
cfg.Metas[strings.TrimPrefix(k, "meta_")] = v
}
}
2017-03-08 19:03:47 +01:00
return nil
}
2018-04-10 11:46:49 +02:00
func (cfg *BaseProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
2017-03-08 19:03:47 +01:00
pMsg.ProxyName = cfg.ProxyName
pMsg.ProxyType = cfg.ProxyType
pMsg.UseEncryption = cfg.UseEncryption
pMsg.UseCompression = cfg.UseCompression
pMsg.Group = cfg.Group
pMsg.GroupKey = cfg.GroupKey
2019-12-08 14:01:58 +01:00
pMsg.Metas = cfg.Metas
2017-03-08 19:03:47 +01:00
}
func (cfg *BaseProxyConf) checkForCli() (err error) {
2019-03-29 12:01:18 +01:00
if cfg.ProxyProtocolVersion != "" {
if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" {
return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion)
}
}
if err = cfg.LocalSvrConf.checkForCli(); err != nil {
return
}
if err = cfg.HealthCheckConf.checkForCli(); err != nil {
return
}
return nil
}
2017-03-08 19:03:47 +01:00
// Bind info
type BindInfoConf struct {
2018-04-10 11:46:49 +02:00
RemotePort int `json:"remote_port"`
2017-03-08 19:03:47 +01:00
}
func (cfg *BindInfoConf) compare(cmp *BindInfoConf) bool {
2018-04-10 11:46:49 +02:00
if cfg.RemotePort != cmp.RemotePort {
return false
}
return true
}
2018-04-10 11:46:49 +02:00
func (cfg *BindInfoConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2017-03-08 19:03:47 +01:00
cfg.RemotePort = pMsg.RemotePort
}
2018-04-10 11:46:49 +02:00
func (cfg *BindInfoConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2017-03-08 19:03:47 +01:00
var (
tmpStr string
ok bool
v int64
2017-03-08 19:03:47 +01:00
)
if tmpStr, ok = section["remote_port"]; ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
2017-03-08 19:03:47 +01:00
return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
}
2020-05-24 11:48:37 +02:00
cfg.RemotePort = int(v)
2017-03-08 19:03:47 +01:00
} else {
return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
}
return nil
}
2018-04-10 11:46:49 +02:00
func (cfg *BindInfoConf) MarshalToMsg(pMsg *msg.NewProxy) {
2017-03-08 19:03:47 +01:00
pMsg.RemotePort = cfg.RemotePort
}
// Domain info
type DomainConf struct {
2017-03-22 19:01:25 +01:00
CustomDomains []string `json:"custom_domains"`
SubDomain string `json:"sub_domain"`
2017-03-08 19:03:47 +01:00
}
func (cfg *DomainConf) compare(cmp *DomainConf) bool {
if strings.Join(cfg.CustomDomains, " ") != strings.Join(cmp.CustomDomains, " ") ||
cfg.SubDomain != cmp.SubDomain {
return false
}
return true
}
2018-04-10 11:46:49 +02:00
func (cfg *DomainConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2017-03-08 19:03:47 +01:00
cfg.CustomDomains = pMsg.CustomDomains
cfg.SubDomain = pMsg.SubDomain
}
2018-04-10 11:46:49 +02:00
func (cfg *DomainConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2017-03-08 19:03:47 +01:00
var (
tmpStr string
ok bool
)
if tmpStr, ok = section["custom_domains"]; ok {
cfg.CustomDomains = strings.Split(tmpStr, ",")
for i, domain := range cfg.CustomDomains {
cfg.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
}
}
if tmpStr, ok = section["subdomain"]; ok {
cfg.SubDomain = tmpStr
}
return
}
2018-04-10 11:46:49 +02:00
func (cfg *DomainConf) MarshalToMsg(pMsg *msg.NewProxy) {
2017-03-08 19:03:47 +01:00
pMsg.CustomDomains = cfg.CustomDomains
pMsg.SubDomain = cfg.SubDomain
}
func (cfg *DomainConf) check() (err error) {
2018-04-10 11:46:49 +02:00
if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
err = fmt.Errorf("custom_domains and subdomain should set at least one of them")
return
}
return
}
func (cfg *DomainConf) checkForCli() (err error) {
if err = cfg.check(); err != nil {
return
}
return
}
func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) {
2018-04-10 11:46:49 +02:00
if err = cfg.check(); err != nil {
return
}
2017-03-08 19:03:47 +01:00
for _, domain := range cfg.CustomDomains {
if serverCfg.SubDomainHost != "" && len(strings.Split(serverCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
if strings.Contains(domain, serverCfg.SubDomainHost) {
return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, serverCfg.SubDomainHost)
2017-03-08 19:03:47 +01:00
}
}
}
if cfg.SubDomain != "" {
if serverCfg.SubDomainHost == "" {
2018-05-18 04:58:08 +02:00
return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
2017-03-08 19:03:47 +01:00
}
if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
return fmt.Errorf("'.' and '*' is not supported in subdomain")
}
}
2018-04-10 11:46:49 +02:00
return
2017-03-08 19:03:47 +01:00
}
// LocalSvrConf configures what location the client will proxy to, or what
// plugin will be used.
2017-03-08 19:03:47 +01:00
type LocalSvrConf struct {
2020-05-24 11:48:37 +02:00
// LocalIP specifies the IP address or host name to proxy to.
LocalIP string `json:"local_ip"`
// LocalPort specifies the port to proxy to.
LocalPort int `json:"local_port"`
// Plugin specifies what plugin should be used for proxying. If this value
// is set, the LocalIp and LocalPort values will be ignored. By default,
// this value is "".
Plugin string `json:"plugin"`
// PluginParams specify parameters to be passed to the plugin, if one is
// being used. By default, this value is an empty map.
2018-04-10 11:46:49 +02:00
PluginParams map[string]string `json:"plugin_params"`
2017-03-08 19:03:47 +01:00
}
func (cfg *LocalSvrConf) compare(cmp *LocalSvrConf) bool {
2020-05-24 11:48:37 +02:00
if cfg.LocalIP != cmp.LocalIP ||
cfg.LocalPort != cmp.LocalPort {
return false
}
if cfg.Plugin != cmp.Plugin ||
len(cfg.PluginParams) != len(cmp.PluginParams) {
return false
}
for k, v := range cfg.PluginParams {
value, ok := cmp.PluginParams[k]
if !ok || v != value {
return false
}
}
return true
}
2018-04-10 11:46:49 +02:00
func (cfg *LocalSvrConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
cfg.Plugin = section["plugin"]
cfg.PluginParams = make(map[string]string)
if cfg.Plugin != "" {
// get params begin with "plugin_"
for k, v := range section {
if strings.HasPrefix(k, "plugin_") {
cfg.PluginParams[k] = v
}
}
} else {
2020-05-24 11:48:37 +02:00
if cfg.LocalIP = section["local_ip"]; cfg.LocalIP == "" {
cfg.LocalIP = "127.0.0.1"
2018-04-10 11:46:49 +02:00
}
if tmpStr, ok := section["local_port"]; ok {
if cfg.LocalPort, err = strconv.Atoi(tmpStr); err != nil {
return fmt.Errorf("Parse conf error: proxy [%s] local_port error", name)
}
} else {
return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", name)
}
}
return
}
func (cfg *LocalSvrConf) checkForCli() (err error) {
if cfg.Plugin == "" {
2020-05-24 11:48:37 +02:00
if cfg.LocalIP == "" {
err = fmt.Errorf("local ip or plugin is required")
return
}
if cfg.LocalPort <= 0 {
err = fmt.Errorf("error local_port")
return
}
}
return
}
// HealthCheckConf configures health checking. This can be useful for load
// balancing purposes to detect and remove proxies to failing services.
type HealthCheckConf struct {
// HealthCheckType specifies what protocol to use for health checking.
// Valid values include "tcp", "http", and "". If this value is "", health
// checking will not be performed. By default, this value is "".
//
// If the type is "tcp", a connection will be attempted to the target
// server. If a connection cannot be established, the health check fails.
//
// If the type is "http", a GET request will be made to the endpoint
2020-05-24 11:48:37 +02:00
// specified by HealthCheckURL. If the response is not a 200, the health
// check fails.
HealthCheckType string `json:"health_check_type"` // tcp | http
// HealthCheckTimeoutS specifies the number of seconds to wait for a health
// check attempt to connect. If the timeout is reached, this counts as a
// health check failure. By default, this value is 3.
HealthCheckTimeoutS int `json:"health_check_timeout_s"`
// HealthCheckMaxFailed specifies the number of allowed failures before the
// proxy is stopped. By default, this value is 1.
HealthCheckMaxFailed int `json:"health_check_max_failed"`
// HealthCheckIntervalS specifies the time in seconds between health
// checks. By default, this value is 10.
HealthCheckIntervalS int `json:"health_check_interval_s"`
2020-05-24 11:48:37 +02:00
// HealthCheckURL specifies the address to send health checks to if the
// health check type is "http".
2020-05-24 11:48:37 +02:00
HealthCheckURL string `json:"health_check_url"`
// HealthCheckAddr specifies the address to connect to if the health check
// type is "tcp".
HealthCheckAddr string `json:"-"`
}
func (cfg *HealthCheckConf) compare(cmp *HealthCheckConf) bool {
if cfg.HealthCheckType != cmp.HealthCheckType ||
2018-12-07 10:05:36 +01:00
cfg.HealthCheckTimeoutS != cmp.HealthCheckTimeoutS ||
cfg.HealthCheckMaxFailed != cmp.HealthCheckMaxFailed ||
cfg.HealthCheckIntervalS != cmp.HealthCheckIntervalS ||
2020-05-24 11:48:37 +02:00
cfg.HealthCheckURL != cmp.HealthCheckURL {
return false
}
return true
}
func (cfg *HealthCheckConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
cfg.HealthCheckType = section["health_check_type"]
2020-05-24 11:48:37 +02:00
cfg.HealthCheckURL = section["health_check_url"]
2018-12-07 10:05:36 +01:00
if tmpStr, ok := section["health_check_timeout_s"]; ok {
if cfg.HealthCheckTimeoutS, err = strconv.Atoi(tmpStr); err != nil {
return fmt.Errorf("Parse conf error: proxy [%s] health_check_timeout_s error", name)
}
}
if tmpStr, ok := section["health_check_max_failed"]; ok {
if cfg.HealthCheckMaxFailed, err = strconv.Atoi(tmpStr); err != nil {
return fmt.Errorf("Parse conf error: proxy [%s] health_check_max_failed error", name)
}
}
if tmpStr, ok := section["health_check_interval_s"]; ok {
if cfg.HealthCheckIntervalS, err = strconv.Atoi(tmpStr); err != nil {
return fmt.Errorf("Parse conf error: proxy [%s] health_check_interval_s error", name)
}
}
return
}
func (cfg *HealthCheckConf) checkForCli() error {
if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
return fmt.Errorf("unsupport health check type")
}
if cfg.HealthCheckType != "" {
2020-05-24 11:48:37 +02:00
if cfg.HealthCheckType == "http" && cfg.HealthCheckURL == "" {
return fmt.Errorf("health_check_url is required for health check type 'http'")
}
}
return nil
}
2017-03-08 19:03:47 +01:00
// TCP
2020-05-24 11:48:37 +02:00
type TCPProxyConf struct {
2017-03-08 19:03:47 +01:00
BaseProxyConf
BindInfoConf
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*TCPProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
!cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
return false
}
return true
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2018-04-10 11:46:49 +02:00
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-03-08 19:03:47 +01:00
return
}
2018-04-10 11:46:49 +02:00
if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-03-08 19:03:47 +01:00
return
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.MarshalToMsg(pMsg)
cfg.BindInfoConf.MarshalToMsg(pMsg)
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPProxyConf) CheckForCli() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
return err
}
return
}
2018-04-10 11:46:49 +02:00
2020-05-24 11:48:37 +02:00
func (cfg *TCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }
2017-03-08 19:03:47 +01:00
// TCP Multiplexer
2020-05-24 11:48:37 +02:00
type TCPMuxProxyConf struct {
BaseProxyConf
DomainConf
Multiplexer string `json:"multiplexer"`
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*TCPMuxProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
!cfg.DomainConf.compare(&cmpConf.DomainConf) ||
cfg.Multiplexer != cmpConf.Multiplexer {
return false
}
return true
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPMuxProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
cfg.DomainConf.UnmarshalFromMsg(pMsg)
cfg.Multiplexer = pMsg.Multiplexer
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
return
}
if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
return
}
cfg.Multiplexer = section["multiplexer"]
2020-05-24 11:48:37 +02:00
if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
return fmt.Errorf("parse conf error: proxy [%s] incorrect multiplexer [%s]", name, cfg.Multiplexer)
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPMuxProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
cfg.BaseProxyConf.MarshalToMsg(pMsg)
cfg.DomainConf.MarshalToMsg(pMsg)
pMsg.Multiplexer = cfg.Multiplexer
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPMuxProxyConf) CheckForCli() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
return err
}
if err = cfg.DomainConf.checkForCli(); err != nil {
return err
}
2020-05-24 11:48:37 +02:00
if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
return fmt.Errorf("parse conf error: incorrect multiplexer [%s]", cfg.Multiplexer)
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *TCPMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
return fmt.Errorf("proxy [%s] incorrect multiplexer [%s]", cfg.ProxyName, cfg.Multiplexer)
}
2020-05-24 11:48:37 +02:00
if cfg.Multiplexer == consts.HTTPConnectTCPMultiplexer && serverCfg.TCPMuxHTTPConnectPort == 0 {
return fmt.Errorf("proxy [%s] type [tcpmux] with multiplexer [httpconnect] requires tcpmux_httpconnect_port configuration", cfg.ProxyName)
}
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
return
}
return
}
2017-03-08 19:03:47 +01:00
// UDP
2020-05-24 11:48:37 +02:00
type UDPProxyConf struct {
2017-03-08 19:03:47 +01:00
BaseProxyConf
BindInfoConf
}
2020-05-24 11:48:37 +02:00
func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*UDPProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
!cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
return false
}
return true
}
2020-05-24 11:48:37 +02:00
func (cfg *UDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2018-04-10 11:46:49 +02:00
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-03-08 19:03:47 +01:00
return
}
2018-04-10 11:46:49 +02:00
if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-03-08 19:03:47 +01:00
return
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *UDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.MarshalToMsg(pMsg)
cfg.BindInfoConf.MarshalToMsg(pMsg)
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *UDPProxyConf) CheckForCli() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
return
}
return
}
2018-04-10 11:46:49 +02:00
2020-05-24 11:48:37 +02:00
func (cfg *UDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }
2017-03-08 19:03:47 +01:00
// HTTP
2020-05-24 11:48:37 +02:00
type HTTPProxyConf struct {
2017-03-08 19:03:47 +01:00
BaseProxyConf
DomainConf
2018-05-20 17:22:07 +02:00
Locations []string `json:"locations"`
2020-05-24 11:48:37 +02:00
HTTPUser string `json:"http_user"`
HTTPPwd string `json:"http_pwd"`
2018-05-20 17:22:07 +02:00
HostHeaderRewrite string `json:"host_header_rewrite"`
Headers map[string]string `json:"headers"`
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*HTTPProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
!cfg.DomainConf.compare(&cmpConf.DomainConf) ||
strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
2020-05-24 11:48:37 +02:00
cfg.HTTPUser != cmpConf.HTTPUser ||
cfg.HTTPPwd != cmpConf.HTTPPwd ||
2018-05-20 17:22:07 +02:00
len(cfg.Headers) != len(cmpConf.Headers) {
return false
}
2018-05-20 17:22:07 +02:00
for k, v := range cfg.Headers {
2020-05-24 11:48:37 +02:00
v2, ok := cmpConf.Headers[k]
if !ok {
return false
}
if v != v2 {
2018-05-20 17:22:07 +02:00
return false
}
}
return true
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
cfg.DomainConf.UnmarshalFromMsg(pMsg)
2017-03-08 19:03:47 +01:00
cfg.Locations = pMsg.Locations
cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
2020-05-24 11:48:37 +02:00
cfg.HTTPUser = pMsg.HTTPUser
cfg.HTTPPwd = pMsg.HTTPPwd
2018-05-20 17:22:07 +02:00
cfg.Headers = pMsg.Headers
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2018-04-10 11:46:49 +02:00
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-03-08 19:03:47 +01:00
return
}
2018-04-10 11:46:49 +02:00
if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-03-08 19:03:47 +01:00
return
}
var (
tmpStr string
ok bool
)
if tmpStr, ok = section["locations"]; ok {
cfg.Locations = strings.Split(tmpStr, ",")
} else {
cfg.Locations = []string{""}
}
cfg.HostHeaderRewrite = section["host_header_rewrite"]
2020-05-24 11:48:37 +02:00
cfg.HTTPUser = section["http_user"]
cfg.HTTPPwd = section["http_pwd"]
2018-05-20 17:22:07 +02:00
cfg.Headers = make(map[string]string)
for k, v := range section {
if strings.HasPrefix(k, "header_") {
cfg.Headers[strings.TrimPrefix(k, "header_")] = v
}
}
2017-03-08 19:03:47 +01:00
return
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.MarshalToMsg(pMsg)
cfg.DomainConf.MarshalToMsg(pMsg)
2017-03-08 19:03:47 +01:00
pMsg.Locations = cfg.Locations
pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
2020-05-24 11:48:37 +02:00
pMsg.HTTPUser = cfg.HTTPUser
pMsg.HTTPPwd = cfg.HTTPPwd
2018-05-20 17:22:07 +02:00
pMsg.Headers = cfg.Headers
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPProxyConf) CheckForCli() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
return
}
2018-04-10 11:46:49 +02:00
if err = cfg.DomainConf.checkForCli(); err != nil {
return
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
if serverCfg.VhostHTTPPort == 0 {
return fmt.Errorf("type [http] not support when vhost_http_port is not set")
2018-04-10 11:46:49 +02:00
}
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
2018-05-11 04:42:57 +02:00
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
2018-04-10 11:46:49 +02:00
return
2017-03-08 19:03:47 +01:00
}
return
}
// HTTPS
2020-05-24 11:48:37 +02:00
type HTTPSProxyConf struct {
2017-03-08 19:03:47 +01:00
BaseProxyConf
DomainConf
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*HTTPSProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
!cfg.DomainConf.compare(&cmpConf.DomainConf) {
return false
}
return true
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPSProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
cfg.DomainConf.UnmarshalFromMsg(pMsg)
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2018-04-10 11:46:49 +02:00
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-03-08 19:03:47 +01:00
return
}
2018-04-10 11:46:49 +02:00
if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-03-08 19:03:47 +01:00
return
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPSProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.MarshalToMsg(pMsg)
cfg.DomainConf.MarshalToMsg(pMsg)
2017-03-08 19:03:47 +01:00
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPSProxyConf) CheckForCli() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
return
}
2018-04-10 11:46:49 +02:00
if err = cfg.DomainConf.checkForCli(); err != nil {
return
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *HTTPSProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
if serverCfg.VhostHTTPSPort == 0 {
2017-03-08 19:03:47 +01:00
return fmt.Errorf("type [https] not support when vhost_https_port is not set")
}
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
2018-05-11 04:42:57 +02:00
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
2018-04-10 11:46:49 +02:00
return
}
2017-03-08 19:03:47 +01:00
return
}
2020-04-22 15:37:45 +02:00
// SUDP
2020-05-24 11:48:37 +02:00
type SUDPProxyConf struct {
2020-04-22 15:37:45 +02:00
BaseProxyConf
Role string `json:"role"`
Sk string `json:"sk"`
}
2020-05-24 11:48:37 +02:00
func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*SUDPProxyConf)
2020-04-22 15:37:45 +02:00
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
return false
}
return true
}
2020-05-24 11:48:37 +02:00
func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2020-04-22 15:37:45 +02:00
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
return
}
cfg.Role = section["role"]
if cfg.Role != "server" {
return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
}
cfg.Sk = section["sk"]
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
return
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *SUDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
2020-04-22 15:37:45 +02:00
cfg.BaseProxyConf.MarshalToMsg(pMsg)
pMsg.Sk = cfg.Sk
}
2020-05-24 11:48:37 +02:00
func (cfg *SUDPProxyConf) CheckForCli() (err error) {
2020-04-22 15:37:45 +02:00
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
return
}
if cfg.Role != "server" {
err = fmt.Errorf("role should be 'server'")
return
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *SUDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
2020-04-22 15:37:45 +02:00
return
}
// Only for role server.
2020-05-24 11:48:37 +02:00
func (cfg *SUDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2020-04-22 15:37:45 +02:00
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
cfg.Sk = pMsg.Sk
}
2017-06-25 21:02:33 +02:00
// STCP
2020-05-24 11:48:37 +02:00
type STCPProxyConf struct {
2017-06-25 21:02:33 +02:00
BaseProxyConf
Role string `json:"role"`
Sk string `json:"sk"`
}
2020-05-24 11:48:37 +02:00
func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*STCPProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
return false
}
return true
}
2017-06-25 21:02:33 +02:00
// Only for role server.
2020-05-24 11:48:37 +02:00
func (cfg *STCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
2017-06-25 21:02:33 +02:00
cfg.Sk = pMsg.Sk
}
2020-05-24 11:48:37 +02:00
func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2018-04-10 11:46:49 +02:00
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-06-25 21:02:33 +02:00
return
}
cfg.Role = section["role"]
if cfg.Role != "server" {
return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
2017-06-25 21:02:33 +02:00
}
cfg.Sk = section["sk"]
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
return
2017-06-25 21:02:33 +02:00
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *STCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.MarshalToMsg(pMsg)
2017-06-25 21:02:33 +02:00
pMsg.Sk = cfg.Sk
}
2020-05-24 11:48:37 +02:00
func (cfg *STCPProxyConf) CheckForCli() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
2018-04-10 11:46:49 +02:00
return
}
if cfg.Role != "server" {
err = fmt.Errorf("role should be 'server'")
return
2018-04-10 11:46:49 +02:00
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *STCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
2017-06-25 21:02:33 +02:00
return
}
2017-10-24 12:20:07 +02:00
// XTCP
2020-05-24 11:48:37 +02:00
type XTCPProxyConf struct {
2017-10-24 12:20:07 +02:00
BaseProxyConf
Role string `json:"role"`
Sk string `json:"sk"`
}
2020-05-24 11:48:37 +02:00
func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*XTCPProxyConf)
2017-10-24 12:20:07 +02:00
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
!cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
2017-10-24 12:20:07 +02:00
return false
}
return true
}
// Only for role server.
2020-05-24 11:48:37 +02:00
func (cfg *XTCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
2017-10-24 12:20:07 +02:00
cfg.Sk = pMsg.Sk
}
2020-05-24 11:48:37 +02:00
func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
2018-04-10 11:46:49 +02:00
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
2017-10-24 12:20:07 +02:00
return
}
cfg.Role = section["role"]
if cfg.Role != "server" {
return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
2017-10-24 12:20:07 +02:00
}
cfg.Sk = section["sk"]
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
return
2017-10-24 12:20:07 +02:00
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *XTCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
2018-04-10 11:46:49 +02:00
cfg.BaseProxyConf.MarshalToMsg(pMsg)
2017-10-24 12:20:07 +02:00
pMsg.Sk = cfg.Sk
}
2020-05-24 11:48:37 +02:00
func (cfg *XTCPProxyConf) CheckForCli() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
2018-04-10 11:46:49 +02:00
return
}
if cfg.Role != "server" {
err = fmt.Errorf("role should be 'server'")
return
2018-04-10 11:46:49 +02:00
}
return
}
2020-05-24 11:48:37 +02:00
func (cfg *XTCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
2017-10-24 12:20:07 +02:00
return
}
func ParseRangeSection(name string, section ini.Section) (sections map[string]ini.Section, err error) {
localPorts, errRet := util.ParseRangeNumbers(section["local_port"])
if errRet != nil {
err = fmt.Errorf("Parse conf error: range section [%s] local_port invalid, %v", name, errRet)
return
}
remotePorts, errRet := util.ParseRangeNumbers(section["remote_port"])
if errRet != nil {
err = fmt.Errorf("Parse conf error: range section [%s] remote_port invalid, %v", name, errRet)
return
}
if len(localPorts) != len(remotePorts) {
err = fmt.Errorf("Parse conf error: range section [%s] local ports number should be same with remote ports number", name)
return
}
if len(localPorts) == 0 {
2018-03-21 04:54:51 +01:00
err = fmt.Errorf("Parse conf error: range section [%s] local_port and remote_port is necessary", name)
return
}
sections = make(map[string]ini.Section)
for i, port := range localPorts {
subName := fmt.Sprintf("%s_%d", name, i)
subSection := copySection(section)
subSection["local_port"] = fmt.Sprintf("%d", port)
subSection["remote_port"] = fmt.Sprintf("%d", remotePorts[i])
sections[subName] = subSection
}
return
}
// if len(startProxy) is 0, start all
// otherwise just start proxies in startProxy map
func LoadAllConfFromIni(prefix string, content string, startProxy map[string]struct{}) (
proxyConfs map[string]ProxyConf, visitorConfs map[string]VisitorConf, err error) {
2017-06-25 21:02:33 +02:00
conf, errRet := ini.Load(strings.NewReader(content))
if errRet != nil {
err = errRet
return
}
if prefix != "" {
prefix += "."
}
startAll := true
if len(startProxy) > 0 {
startAll = false
2017-03-08 19:03:47 +01:00
}
proxyConfs = make(map[string]ProxyConf)
visitorConfs = make(map[string]VisitorConf)
2017-03-08 19:03:47 +01:00
for name, section := range conf {
if name == "common" {
continue
}
_, shouldStart := startProxy[name]
if !startAll && !shouldStart {
continue
}
subSections := make(map[string]ini.Section)
if strings.HasPrefix(name, "range:") {
// range section
rangePrefix := strings.TrimSpace(strings.TrimPrefix(name, "range:"))
subSections, err = ParseRangeSection(rangePrefix, section)
if err != nil {
return
}
} else {
subSections[name] = section
}
for subName, subSection := range subSections {
if subSection["role"] == "" {
subSection["role"] = "server"
2017-06-25 21:02:33 +02:00
}
role := subSection["role"]
if role == "server" {
cfg, errRet := NewProxyConfFromIni(prefix, subName, subSection)
if errRet != nil {
err = errRet
return
}
proxyConfs[prefix+subName] = cfg
} else if role == "visitor" {
cfg, errRet := NewVisitorConfFromIni(prefix, subName, subSection)
if errRet != nil {
err = errRet
return
}
visitorConfs[prefix+subName] = cfg
2017-06-25 21:02:33 +02:00
} else {
err = fmt.Errorf("role should be 'server' or 'visitor'")
return
2017-03-08 19:03:47 +01:00
}
}
}
return
}
func copySection(section ini.Section) (out ini.Section) {
out = make(ini.Section)
for k, v := range section {
out[k] = v
}
return
}