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"
|
2022-06-14 14:24:34 +08:00
|
|
|
"net"
|
2017-03-10 02:01:17 +08:00
|
|
|
"reflect"
|
2022-06-14 14:24:34 +08:00
|
|
|
"strconv"
|
2017-03-09 02:03:47 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/consts"
|
|
|
|
"github.com/fatedier/frp/pkg/msg"
|
2017-03-09 02:03:47 +08:00
|
|
|
)
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Proxy
|
2018-04-10 17:46:49 +08:00
|
|
|
var (
|
2021-01-26 11:31:08 +08:00
|
|
|
proxyConfTypeMap = map[string]reflect.Type{
|
|
|
|
consts.TCPProxy: reflect.TypeOf(TCPProxyConf{}),
|
|
|
|
consts.TCPMuxProxy: reflect.TypeOf(TCPMuxProxyConf{}),
|
|
|
|
consts.UDPProxy: reflect.TypeOf(UDPProxyConf{}),
|
|
|
|
consts.HTTPProxy: reflect.TypeOf(HTTPProxyConf{}),
|
|
|
|
consts.HTTPSProxy: reflect.TypeOf(HTTPSProxyConf{}),
|
|
|
|
consts.STCPProxy: reflect.TypeOf(STCPProxyConf{}),
|
|
|
|
consts.XTCPProxy: reflect.TypeOf(XTCPProxyConf{}),
|
|
|
|
consts.SUDPProxy: reflect.TypeOf(SUDPProxyConf{}),
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
)
|
2017-03-10 02:01:17 +08:00
|
|
|
|
|
|
|
func NewConfByType(proxyType string) ProxyConf {
|
|
|
|
v, ok := proxyConfTypeMap[proxyType]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cfg := reflect.New(v).Interface().(ProxyConf)
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
type ProxyConf interface {
|
|
|
|
GetBaseInfo() *BaseProxyConf
|
2021-01-26 11:31:08 +08:00
|
|
|
UnmarshalFromMsg(*msg.NewProxy)
|
|
|
|
UnmarshalFromIni(string, string, *ini.Section) error
|
|
|
|
MarshalToMsg(*msg.NewProxy)
|
2018-04-10 17:46:49 +08:00
|
|
|
CheckForCli() error
|
2021-01-26 11:31:08 +08:00
|
|
|
CheckForSvr(ServerCommonConf) error
|
|
|
|
Compare(ProxyConf) bool
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// LocalSvrConf configures what location the client will to, or what
|
|
|
|
// plugin will be used.
|
|
|
|
type LocalSvrConf struct {
|
|
|
|
// LocalIP specifies the IP address or host name to to.
|
|
|
|
LocalIP string `ini:"local_ip" json:"local_ip"`
|
|
|
|
// LocalPort specifies the port to to.
|
|
|
|
LocalPort int `ini:"local_port" json:"local_port"`
|
2017-03-10 02:01:17 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Plugin specifies what plugin should be used for ng. If this value
|
|
|
|
// is set, the LocalIp and LocalPort values will be ignored. By default,
|
|
|
|
// this value is "".
|
|
|
|
Plugin string `ini:"plugin" json:"plugin"`
|
|
|
|
// PluginParams specify parameters to be passed to the plugin, if one is
|
|
|
|
// being used. By default, this value is an empty map.
|
|
|
|
PluginParams map[string]string `ini:"-"`
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// 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
|
|
|
|
// specified by HealthCheckURL. If the response is not a 200, the health
|
|
|
|
// check fails.
|
|
|
|
HealthCheckType string `ini:"health_check_type" 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 `ini:"health_check_timeout_s" json:"health_check_timeout_s"`
|
|
|
|
// HealthCheckMaxFailed specifies the number of allowed failures before the
|
|
|
|
// is stopped. By default, this value is 1.
|
|
|
|
HealthCheckMaxFailed int `ini:"health_check_max_failed" json:"health_check_max_failed"`
|
|
|
|
// HealthCheckIntervalS specifies the time in seconds between health
|
|
|
|
// checks. By default, this value is 10.
|
|
|
|
HealthCheckIntervalS int `ini:"health_check_interval_s" json:"health_check_interval_s"`
|
|
|
|
// HealthCheckURL specifies the address to send health checks to if the
|
|
|
|
// health check type is "http".
|
|
|
|
HealthCheckURL string `ini:"health_check_url" json:"health_check_url"`
|
|
|
|
// HealthCheckAddr specifies the address to connect to if the health check
|
|
|
|
// type is "tcp".
|
|
|
|
HealthCheckAddr string `ini:"-"`
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// BaseProxyConf provides configuration info that is common to all types.
|
2017-03-09 02:03:47 +08:00
|
|
|
type BaseProxyConf struct {
|
2021-01-26 11:31:08 +08:00
|
|
|
// ProxyName is the name of this
|
|
|
|
ProxyName string `ini:"name" json:"name"`
|
|
|
|
// ProxyType specifies the type of this Valid values include "tcp",
|
2019-08-25 06:25:52 +08:00
|
|
|
// "udp", "http", "https", "stcp", and "xtcp". By default, this value is
|
|
|
|
// "tcp".
|
2021-01-26 11:31:08 +08:00
|
|
|
ProxyType string `ini:"type" json:"type"`
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2019-08-25 06:25:52 +08: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.
|
2021-01-26 11:31:08 +08:00
|
|
|
UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
|
2019-08-25 06:25:52 +08:00
|
|
|
// UseCompression controls whether or not communication with the server
|
|
|
|
// will be compressed. By default, this value is false.
|
2021-01-26 11:31:08 +08:00
|
|
|
UseCompression bool `ini:"use_compression" json:"use_compression"`
|
|
|
|
// Group specifies which group the is a part of. The server will use
|
2019-08-25 06:25:52 +08:00
|
|
|
// this information to load balance proxies in the same group. If the value
|
2021-01-26 11:31:08 +08:00
|
|
|
// is "", this will not be in a group. By default, this value is "".
|
|
|
|
Group string `ini:"group" json:"group"`
|
2019-08-25 06:25:52 +08:00
|
|
|
// GroupKey specifies a group key, which should be the same among proxies
|
|
|
|
// of the same group. By default, this value is "".
|
2021-01-26 11:31:08 +08:00
|
|
|
GroupKey string `ini:"group_key" json:"group_key"`
|
2019-08-25 06:25:52 +08:00
|
|
|
|
|
|
|
// 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 "".
|
2021-01-26 11:31:08 +08:00
|
|
|
ProxyProtocolVersion string `ini:"proxy_protocol_version" json:"proxy_protocol_version"`
|
2019-11-03 01:20:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// BandwidthLimit limit the bandwidth
|
2019-11-03 01:20:49 +08:00
|
|
|
// 0 means no limit
|
2021-01-26 11:31:08 +08:00
|
|
|
BandwidthLimit BandwidthQuantity `ini:"bandwidth_limit" json:"bandwidth_limit"`
|
2019-11-03 01:20:49 +08:00
|
|
|
|
2019-12-08 21:01:58 +08:00
|
|
|
// meta info for each proxy
|
2021-01-26 11:31:08 +08:00
|
|
|
Metas map[string]string `ini:"-" json:"metas"`
|
|
|
|
|
2021-03-19 17:36:39 +08:00
|
|
|
LocalSvrConf `ini:",extends"`
|
|
|
|
HealthCheckConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type DomainConf struct {
|
|
|
|
CustomDomains []string `ini:"custom_domains" json:"custom_domains"`
|
|
|
|
SubDomain string `ini:"subdomain" json:"subdomain"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP
|
|
|
|
type HTTPProxyConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseProxyConf `ini:",extends"`
|
|
|
|
DomainConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
Locations []string `ini:"locations" json:"locations"`
|
|
|
|
HTTPUser string `ini:"http_user" json:"http_user"`
|
|
|
|
HTTPPwd string `ini:"http_pwd" json:"http_pwd"`
|
|
|
|
HostHeaderRewrite string `ini:"host_header_rewrite" json:"host_header_rewrite"`
|
|
|
|
Headers map[string]string `ini:"-" json:"headers"`
|
2022-05-26 23:57:30 +08:00
|
|
|
RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPS
|
|
|
|
type HTTPSProxyConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseProxyConf `ini:",extends"`
|
|
|
|
DomainConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TCP
|
|
|
|
type TCPProxyConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseProxyConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
RemotePort int `ini:"remote_port" json:"remote_port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TCPMux
|
|
|
|
type TCPMuxProxyConf struct {
|
2022-05-26 23:57:30 +08:00
|
|
|
BaseProxyConf `ini:",extends"`
|
|
|
|
DomainConf `ini:",extends"`
|
|
|
|
RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
Multiplexer string `ini:"multiplexer"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// STCP
|
|
|
|
type STCPProxyConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseProxyConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
Role string `ini:"role" json:"role"`
|
|
|
|
Sk string `ini:"sk" json:"sk"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// XTCP
|
|
|
|
type XTCPProxyConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseProxyConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
Role string `ini:"role" json:"role"`
|
|
|
|
Sk string `ini:"sk" json:"sk"`
|
|
|
|
}
|
2019-12-08 21:01:58 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// UDP
|
|
|
|
type UDPProxyConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseProxyConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
RemotePort int `ini:"remote_port" json:"remote_port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SUDP
|
|
|
|
type SUDPProxyConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseProxyConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
Role string `ini:"role" json:"role"`
|
|
|
|
Sk string `ini:"sk" json:"sk"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy Conf Loader
|
|
|
|
// DefaultProxyConf creates a empty ProxyConf object by proxyType.
|
|
|
|
// If proxyType doesn't exist, return nil.
|
|
|
|
func DefaultProxyConf(proxyType string) ProxyConf {
|
|
|
|
var conf ProxyConf
|
|
|
|
switch proxyType {
|
|
|
|
case consts.TCPProxy:
|
|
|
|
conf = &TCPProxyConf{
|
|
|
|
BaseProxyConf: defaultBaseProxyConf(proxyType),
|
|
|
|
}
|
|
|
|
case consts.TCPMuxProxy:
|
|
|
|
conf = &TCPMuxProxyConf{
|
|
|
|
BaseProxyConf: defaultBaseProxyConf(proxyType),
|
|
|
|
}
|
|
|
|
case consts.UDPProxy:
|
|
|
|
conf = &UDPProxyConf{
|
|
|
|
BaseProxyConf: defaultBaseProxyConf(proxyType),
|
|
|
|
}
|
|
|
|
case consts.HTTPProxy:
|
|
|
|
conf = &HTTPProxyConf{
|
|
|
|
BaseProxyConf: defaultBaseProxyConf(proxyType),
|
|
|
|
}
|
|
|
|
case consts.HTTPSProxy:
|
|
|
|
conf = &HTTPSProxyConf{
|
|
|
|
BaseProxyConf: defaultBaseProxyConf(proxyType),
|
|
|
|
}
|
|
|
|
case consts.STCPProxy:
|
|
|
|
conf = &STCPProxyConf{
|
|
|
|
BaseProxyConf: defaultBaseProxyConf(proxyType),
|
|
|
|
Role: "server",
|
|
|
|
}
|
|
|
|
case consts.XTCPProxy:
|
|
|
|
conf = &XTCPProxyConf{
|
|
|
|
BaseProxyConf: defaultBaseProxyConf(proxyType),
|
|
|
|
Role: "server",
|
|
|
|
}
|
|
|
|
case consts.SUDPProxy:
|
|
|
|
conf = &SUDPProxyConf{
|
|
|
|
BaseProxyConf: defaultBaseProxyConf(proxyType),
|
|
|
|
Role: "server",
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy loaded from ini
|
|
|
|
func NewProxyConfFromIni(prefix, name string, section *ini.Section) (ProxyConf, error) {
|
|
|
|
// section.Key: if key not exists, section will set it with default value.
|
|
|
|
proxyType := section.Key("type").String()
|
|
|
|
if proxyType == "" {
|
|
|
|
proxyType = consts.TCPProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := DefaultProxyConf(proxyType)
|
|
|
|
if conf == nil {
|
2021-05-12 12:15:22 +08:00
|
|
|
return nil, fmt.Errorf("proxy %s has invalid type [%s]", name, proxyType)
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := conf.CheckForCli(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy loaded from msg
|
|
|
|
func NewProxyConfFromMsg(pMsg *msg.NewProxy, serverCfg ServerCommonConf) (ProxyConf, error) {
|
|
|
|
if pMsg.ProxyType == "" {
|
|
|
|
pMsg.ProxyType = consts.TCPProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := DefaultProxyConf(pMsg.ProxyType)
|
|
|
|
if conf == nil {
|
|
|
|
return nil, fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.UnmarshalFromMsg(pMsg)
|
|
|
|
|
|
|
|
err := conf.CheckForSvr(serverCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base
|
|
|
|
func defaultBaseProxyConf(proxyType string) BaseProxyConf {
|
|
|
|
return BaseProxyConf{
|
|
|
|
ProxyType: proxyType,
|
|
|
|
LocalSvrConf: LocalSvrConf{
|
|
|
|
LocalIP: "127.0.0.1",
|
|
|
|
},
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08:00
|
|
|
func (cfg *BaseProxyConf) compare(cmp *BaseProxyConf) bool {
|
|
|
|
if cfg.ProxyName != cmp.ProxyName ||
|
|
|
|
cfg.ProxyType != cmp.ProxyType ||
|
|
|
|
cfg.UseEncryption != cmp.UseEncryption ||
|
2018-05-22 23:59:35 +08:00
|
|
|
cfg.UseCompression != cmp.UseCompression ||
|
|
|
|
cfg.Group != cmp.Group ||
|
2019-03-29 19:01:18 +08:00
|
|
|
cfg.GroupKey != cmp.GroupKey ||
|
2019-11-03 01:20:49 +08:00
|
|
|
cfg.ProxyProtocolVersion != cmp.ProxyProtocolVersion ||
|
2020-03-02 11:20:08 +08:00
|
|
|
!cfg.BandwidthLimit.Equal(&cmp.BandwidthLimit) ||
|
2019-12-08 21:01:58 +08:00
|
|
|
!reflect.DeepEqual(cfg.Metas, cmp.Metas) {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(cfg.LocalSvrConf, cmp.LocalSvrConf) {
|
2018-06-25 18:22:35 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
if !reflect.DeepEqual(cfg.HealthCheckConf, cmp.HealthCheckConf) {
|
2018-06-25 18:22:35 +08:00
|
|
|
return false
|
|
|
|
}
|
2017-07-13 02:20:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return true
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// BaseProxyConf apply custom logic changes.
|
|
|
|
func (cfg *BaseProxyConf) decorate(prefix string, name string, section *ini.Section) error {
|
|
|
|
// proxy_name
|
2018-04-10 17:46:49 +08:00
|
|
|
cfg.ProxyName = prefix + name
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// metas_xxx
|
|
|
|
cfg.Metas = GetMapWithoutPrefix(section.KeysHash(), "meta_")
|
2019-11-03 01:20:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// bandwidth_limit
|
|
|
|
if bandwidth, err := section.GetKey("bandwidth_limit"); err == nil {
|
|
|
|
cfg.BandwidthLimit, err = NewBandwidthQuantity(bandwidth.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// plugin_xxx
|
|
|
|
cfg.LocalSvrConf.PluginParams = GetMapByPrefix(section.KeysHash(), "plugin_")
|
2018-12-07 17:05:36 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// custom logic code
|
2018-12-07 17:05:36 +08:00
|
|
|
if cfg.HealthCheckType == "tcp" && cfg.Plugin == "" {
|
2020-05-24 17:48:37 +08:00
|
|
|
cfg.HealthCheckAddr = cfg.LocalIP + fmt.Sprintf(":%d", cfg.LocalPort)
|
2018-12-07 17:05:36 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckURL != "" {
|
2022-06-14 14:24:34 +08:00
|
|
|
s := "http://" + net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort))
|
2020-05-24 17:48:37 +08:00
|
|
|
if !strings.HasPrefix(cfg.HealthCheckURL, "/") {
|
2018-12-09 21:56:46 +08:00
|
|
|
s += "/"
|
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
cfg.HealthCheckURL = s + cfg.HealthCheckURL
|
2018-12-09 21:56:46 +08:00
|
|
|
}
|
2019-12-08 21:01:58 +08:00
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *BaseProxyConf) marshalToMsg(pMsg *msg.NewProxy) {
|
2017-03-09 02:03:47 +08:00
|
|
|
pMsg.ProxyName = cfg.ProxyName
|
|
|
|
pMsg.ProxyType = cfg.ProxyType
|
|
|
|
pMsg.UseEncryption = cfg.UseEncryption
|
|
|
|
pMsg.UseCompression = cfg.UseCompression
|
2018-05-22 23:59:35 +08:00
|
|
|
pMsg.Group = cfg.Group
|
|
|
|
pMsg.GroupKey = cfg.GroupKey
|
2019-12-08 21:01:58 +08:00
|
|
|
pMsg.Metas = cfg.Metas
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *BaseProxyConf) unmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.ProxyName = pMsg.ProxyName
|
|
|
|
cfg.ProxyType = pMsg.ProxyType
|
|
|
|
cfg.UseEncryption = pMsg.UseEncryption
|
|
|
|
cfg.UseCompression = pMsg.UseCompression
|
|
|
|
cfg.Group = pMsg.Group
|
|
|
|
cfg.GroupKey = pMsg.GroupKey
|
|
|
|
cfg.Metas = pMsg.Metas
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
func (cfg *BaseProxyConf) checkForCli() (err error) {
|
2019-03-29 19:01:18 +08:00
|
|
|
if cfg.ProxyProtocolVersion != "" {
|
|
|
|
if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" {
|
|
|
|
return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.LocalSvrConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = cfg.HealthCheckConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// DomainConf
|
2017-03-09 02:03:47 +08:00
|
|
|
func (cfg *DomainConf) check() (err error) {
|
2018-04-10 17:46:49 +08: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
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) {
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.check(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
for _, domain := range cfg.CustomDomains {
|
2019-08-20 02:07:37 +08:00
|
|
|
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-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.SubDomain != "" {
|
2019-08-20 02:07:37 +08:00
|
|
|
if serverCfg.SubDomainHost == "" {
|
2018-05-18 10:58:08 +08:00
|
|
|
return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
|
|
|
|
return fmt.Errorf("'.' and '*' is not supported in subdomain")
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
return nil
|
2017-05-21 22:42:42 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// LocalSvrConf
|
2018-06-25 18:22:35 +08:00
|
|
|
func (cfg *LocalSvrConf) checkForCli() (err error) {
|
|
|
|
if cfg.Plugin == "" {
|
2020-05-24 17:48:37 +08:00
|
|
|
if cfg.LocalIP == "" {
|
2018-06-25 18:22:35 +08:00
|
|
|
err = fmt.Errorf("local ip or plugin is required")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cfg.LocalPort <= 0 {
|
|
|
|
err = fmt.Errorf("error local_port")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// HealthCheckConf
|
2018-06-25 18:22:35 +08:00
|
|
|
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 17:48:37 +08:00
|
|
|
if cfg.HealthCheckType == "http" && cfg.HealthCheckURL == "" {
|
2018-06-25 18:22:35 +08:00
|
|
|
return fmt.Errorf("health_check_url is required for health check type 'http'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini.Section) error {
|
|
|
|
err := section.MapTo(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cfg.GetBaseInfo().decorate(prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// TCP
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *TCPProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*TCPProxyConf)
|
2017-07-13 02:20:49 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic equal if exists.
|
|
|
|
if cfg.RemotePort != cmpConf.RemotePort {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *TCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.RemotePort = pMsg.RemotePort
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
|
|
|
err := preUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
|
|
|
|
return nil
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *TCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.marshalToMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic marshal if exists
|
|
|
|
pMsg.RemotePort = cfg.RemotePort
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *TCPProxyConf) CheckForCli() (err error) {
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
|
2021-01-26 11:31:08 +08:00
|
|
|
return
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic check if exists
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return
|
|
|
|
}
|
2020-03-05 21:47:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *TCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
|
|
|
|
return nil
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// TCPMux
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*TCPMuxProxyConf)
|
2020-03-05 21:47:49 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
|
2020-03-05 21:47:49 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic equal if exists.
|
|
|
|
if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
if cfg.Multiplexer != cmpConf.Multiplexer || cfg.RouteByHTTPUser != cmpConf.RouteByHTTPUser {
|
2021-01-26 11:31:08 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-05 21:47:49 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
|
|
|
err := preUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
|
|
|
|
return nil
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *TCPMuxProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
|
2020-03-05 21:47:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.CustomDomains = pMsg.CustomDomains
|
|
|
|
cfg.SubDomain = pMsg.SubDomain
|
|
|
|
cfg.Multiplexer = pMsg.Multiplexer
|
2022-05-26 23:57:30 +08:00
|
|
|
cfg.RouteByHTTPUser = pMsg.RouteByHTTPUser
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *TCPMuxProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.marshalToMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic marshal if exists
|
|
|
|
pMsg.CustomDomains = cfg.CustomDomains
|
|
|
|
pMsg.SubDomain = cfg.SubDomain
|
2020-03-05 21:47:49 +08:00
|
|
|
pMsg.Multiplexer = cfg.Multiplexer
|
2022-05-26 23:57:30 +08:00
|
|
|
pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *TCPMuxProxyConf) CheckForCli() (err error) {
|
2020-03-05 21:47:49 +08:00
|
|
|
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
|
2021-01-26 11:31:08 +08:00
|
|
|
return
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic check if exists
|
2020-03-05 21:47:49 +08:00
|
|
|
if err = cfg.DomainConf.checkForCli(); err != nil {
|
2021-01-26 11:31:08 +08:00
|
|
|
return
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
|
2020-03-05 21:47:49 +08:00
|
|
|
return fmt.Errorf("parse conf error: incorrect multiplexer [%s]", cfg.Multiplexer)
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2020-03-05 21:47:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *TCPMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
|
|
|
|
if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
|
2020-03-05 21:47:49 +08:00
|
|
|
return fmt.Errorf("proxy [%s] incorrect multiplexer [%s]", cfg.ProxyName, cfg.Multiplexer)
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
if cfg.Multiplexer == consts.HTTPConnectTCPMultiplexer && serverCfg.TCPMuxHTTPConnectPort == 0 {
|
2020-03-05 21:47:49 +08:00
|
|
|
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
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2020-03-05 21:47:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
// UDP
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*UDPProxyConf)
|
2017-07-13 02:20:49 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic equal if exists.
|
|
|
|
if cfg.RemotePort != cmpConf.RemotePort {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2017-07-13 02:20:49 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
|
|
|
err := preUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
|
|
|
|
return nil
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *UDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.RemotePort = pMsg.RemotePort
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *UDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.marshalToMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic marshal if exists
|
|
|
|
pMsg.RemotePort = cfg.RemotePort
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *UDPProxyConf) CheckForCli() (err error) {
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic check if exists
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *UDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
|
|
|
|
return nil
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// HTTP
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*HTTPProxyConf)
|
2017-07-13 02:20:49 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic equal if exists.
|
|
|
|
if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(cfg.Locations, cmpConf.Locations) ||
|
2020-05-24 17:48:37 +08:00
|
|
|
cfg.HTTPUser != cmpConf.HTTPUser ||
|
|
|
|
cfg.HTTPPwd != cmpConf.HTTPPwd ||
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
|
2022-05-26 23:57:30 +08:00
|
|
|
cfg.RouteByHTTPUser != cmpConf.RouteByHTTPUser ||
|
2021-01-26 11:31:08 +08:00
|
|
|
!reflect.DeepEqual(cfg.Headers, cmpConf.Headers) {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
2018-05-20 23:22:07 +08:00
|
|
|
|
2017-07-13 02:20:49 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
|
|
|
err := preUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.CustomDomains = pMsg.CustomDomains
|
|
|
|
cfg.SubDomain = pMsg.SubDomain
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.Locations = pMsg.Locations
|
|
|
|
cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
|
2020-05-24 17:48:37 +08:00
|
|
|
cfg.HTTPUser = pMsg.HTTPUser
|
|
|
|
cfg.HTTPPwd = pMsg.HTTPPwd
|
2018-05-20 23:22:07 +08:00
|
|
|
cfg.Headers = pMsg.Headers
|
2022-05-26 23:57:30 +08:00
|
|
|
cfg.RouteByHTTPUser = pMsg.RouteByHTTPUser
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.marshalToMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic marshal if exists
|
|
|
|
pMsg.CustomDomains = cfg.CustomDomains
|
|
|
|
pMsg.SubDomain = cfg.SubDomain
|
2017-03-09 02:03:47 +08:00
|
|
|
pMsg.Locations = cfg.Locations
|
|
|
|
pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
|
2020-05-24 17:48:37 +08:00
|
|
|
pMsg.HTTPUser = cfg.HTTPUser
|
|
|
|
pMsg.HTTPPwd = cfg.HTTPPwd
|
2018-05-20 23:22:07 +08:00
|
|
|
pMsg.Headers = cfg.Headers
|
2022-05-26 23:57:30 +08:00
|
|
|
pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPProxyConf) CheckForCli() (err error) {
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic check if exists
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.DomainConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
|
|
|
|
if serverCfg.VhostHTTPPort == 0 {
|
2018-05-18 00:21:11 +08:00
|
|
|
return fmt.Errorf("type [http] not support when vhost_http_port is not set")
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
|
2018-05-11 10:42:57 +08:00
|
|
|
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPS
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*HTTPSProxyConf)
|
2017-07-13 02:20:49 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic equal if exists.
|
|
|
|
if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2017-07-13 02:20:49 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
|
|
|
err := preUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
|
|
|
|
return nil
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *HTTPSProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.CustomDomains = pMsg.CustomDomains
|
|
|
|
cfg.SubDomain = pMsg.SubDomain
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPSProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.marshalToMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic marshal if exists
|
|
|
|
pMsg.CustomDomains = cfg.CustomDomains
|
|
|
|
pMsg.SubDomain = cfg.SubDomain
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPSProxyConf) CheckForCli() (err error) {
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic check if exists
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.DomainConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *HTTPSProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
|
|
|
|
if serverCfg.VhostHTTPSPort == 0 {
|
2017-03-09 02:03:47 +08:00
|
|
|
return fmt.Errorf("type [https] not support when vhost_https_port is not set")
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
|
2018-05-11 10:42:57 +08:00
|
|
|
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:37:45 +08:00
|
|
|
// SUDP
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*SUDPProxyConf)
|
2020-04-22 21:37:45 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic equal if exists.
|
|
|
|
if cfg.Role != cmpConf.Role ||
|
2020-04-22 21:37:45 +08:00
|
|
|
cfg.Sk != cmpConf.Sk {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2020-04-22 21:37:45 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
|
|
|
err := preUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic unmarshal if exists
|
2020-04-22 21:37:45 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-22 21:37:45 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Only for role server.
|
|
|
|
func (cfg *SUDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.Sk = pMsg.Sk
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *SUDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.marshalToMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic marshal if exists
|
2020-04-22 21:37:45 +08:00
|
|
|
pMsg.Sk = cfg.Sk
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *SUDPProxyConf) CheckForCli() (err error) {
|
2021-01-26 11:31:08 +08:00
|
|
|
if err := cfg.BaseProxyConf.checkForCli(); err != nil {
|
|
|
|
return err
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic check if exists
|
2020-04-22 21:37:45 +08:00
|
|
|
if cfg.Role != "server" {
|
2021-01-26 11:31:08 +08:00
|
|
|
return fmt.Errorf("role should be 'server'")
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return nil
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *SUDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
|
|
|
|
return nil
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 03:02:33 +08:00
|
|
|
// STCP
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*STCPProxyConf)
|
2017-07-13 02:20:49 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic equal if exists.
|
|
|
|
if cfg.Role != cmpConf.Role ||
|
2018-06-25 18:22:35 +08:00
|
|
|
cfg.Sk != cmpConf.Sk {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return true
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
|
|
|
err := preUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
if cfg.Role == "" {
|
|
|
|
cfg.Role = "server"
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-26 03:02:33 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Only for role server.
|
|
|
|
func (cfg *STCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.Sk = pMsg.Sk
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *STCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.marshalToMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic marshal if exists
|
2017-06-26 03:02:33 +08:00
|
|
|
pMsg.Sk = cfg.Sk
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *STCPProxyConf) CheckForCli() (err error) {
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic check if exists
|
2018-06-25 18:22:35 +08:00
|
|
|
if cfg.Role != "server" {
|
2021-01-26 11:31:08 +08:00
|
|
|
return fmt.Errorf("role should be 'server'")
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 03:02:33 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *STCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
|
|
|
|
return nil
|
2017-10-24 18:20:07 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// XTCP
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*XTCPProxyConf)
|
2017-10-24 18:20:07 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom logic equal if exists.
|
|
|
|
if cfg.Role != cmpConf.Role ||
|
2018-06-25 18:22:35 +08:00
|
|
|
cfg.Sk != cmpConf.Sk {
|
2017-10-24 18:20:07 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return true
|
2017-10-24 18:20:07 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
|
|
|
err := preUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-10-24 18:20:07 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
if cfg.Role == "" {
|
|
|
|
cfg.Role = "server"
|
2017-10-24 18:20:07 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-24 18:20:07 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Only for role server.
|
|
|
|
func (cfg *XTCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic unmarshal if exists
|
|
|
|
cfg.Sk = pMsg.Sk
|
2017-10-24 18:20:07 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *XTCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2021-01-26 11:31:08 +08:00
|
|
|
cfg.BaseProxyConf.marshalToMsg(pMsg)
|
|
|
|
|
|
|
|
// Add custom logic marshal if exists
|
2017-10-24 18:20:07 +08:00
|
|
|
pMsg.Sk = cfg.Sk
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *XTCPProxyConf) CheckForCli() (err error) {
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.BaseProxyConf.checkForCli(); err != nil {
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
|
|
|
}
|
2018-01-25 23:05:07 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Add custom logic check if exists
|
|
|
|
if cfg.Role != "server" {
|
|
|
|
return fmt.Errorf("role should be 'server'")
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
2018-01-25 23:05:07 +08:00
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-01-25 23:05:07 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *XTCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
|
|
|
|
return nil
|
2018-01-25 23:05:07 +08:00
|
|
|
}
|