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"
|
2017-03-10 02:01:17 +08:00
|
|
|
"reflect"
|
2017-03-09 02:03:47 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/models/consts"
|
|
|
|
"github.com/fatedier/frp/models/msg"
|
2018-01-25 23:05:07 +08:00
|
|
|
"github.com/fatedier/frp/utils/util"
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
ini "github.com/vaughan0/go-ini"
|
|
|
|
)
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
var (
|
|
|
|
proxyConfTypeMap map[string]reflect.Type
|
|
|
|
)
|
2017-03-10 02:01:17 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
proxyConfTypeMap = make(map[string]reflect.Type)
|
|
|
|
proxyConfTypeMap[consts.TcpProxy] = reflect.TypeOf(TcpProxyConf{})
|
|
|
|
proxyConfTypeMap[consts.UdpProxy] = reflect.TypeOf(UdpProxyConf{})
|
|
|
|
proxyConfTypeMap[consts.HttpProxy] = reflect.TypeOf(HttpProxyConf{})
|
|
|
|
proxyConfTypeMap[consts.HttpsProxy] = reflect.TypeOf(HttpsProxyConf{})
|
2017-06-26 03:02:33 +08:00
|
|
|
proxyConfTypeMap[consts.StcpProxy] = reflect.TypeOf(StcpProxyConf{})
|
2017-10-24 18:20:07 +08:00
|
|
|
proxyConfTypeMap[consts.XtcpProxy] = reflect.TypeOf(XtcpProxyConf{})
|
2017-03-10 02:01:17 +08: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-09 02:03:47 +08:00
|
|
|
type ProxyConf interface {
|
|
|
|
GetBaseInfo() *BaseProxyConf
|
2018-04-10 17:46:49 +08:00
|
|
|
UnmarshalFromMsg(pMsg *msg.NewProxy)
|
|
|
|
UnmarshalFromIni(prefix string, name string, conf ini.Section) error
|
|
|
|
MarshalToMsg(pMsg *msg.NewProxy)
|
|
|
|
CheckForCli() error
|
|
|
|
CheckForSvr() error
|
2017-07-13 02:20:49 +08:00
|
|
|
Compare(conf ProxyConf) bool
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func NewProxyConfFromMsg(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
|
2017-03-09 02:03:47 +08:00
|
|
|
if pMsg.ProxyType == "" {
|
|
|
|
pMsg.ProxyType = consts.TcpProxy
|
|
|
|
}
|
2017-03-10 02:01:17 +08:00
|
|
|
|
|
|
|
cfg = NewConfByType(pMsg.ProxyType)
|
|
|
|
if cfg == nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
err = fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
|
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
cfg.UnmarshalFromMsg(pMsg)
|
|
|
|
err = cfg.CheckForSvr()
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func NewProxyConfFromIni(prefix string, name string, section ini.Section) (cfg ProxyConf, err error) {
|
2017-03-09 02:03:47 +08:00
|
|
|
proxyType := section["type"]
|
|
|
|
if proxyType == "" {
|
|
|
|
proxyType = consts.TcpProxy
|
|
|
|
section["type"] = consts.TcpProxy
|
|
|
|
}
|
2017-03-10 02:01:17 +08:00
|
|
|
cfg = NewConfByType(proxyType)
|
|
|
|
if cfg == nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
err = fmt.Errorf("proxy [%s] type [%s] error", name, proxyType)
|
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = cfg.CheckForCli()
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// BaseProxy info
|
|
|
|
type BaseProxyConf struct {
|
2017-03-23 02:01:25 +08:00
|
|
|
ProxyName string `json:"proxy_name"`
|
|
|
|
ProxyType string `json:"proxy_type"`
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2018-05-22 23:59:35 +08:00
|
|
|
UseEncryption bool `json:"use_encryption"`
|
|
|
|
UseCompression bool `json:"use_compression"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
GroupKey string `json:"group_key"`
|
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 ||
|
|
|
|
cfg.GroupKey != cmp.GroupKey {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *BaseProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.ProxyName = pMsg.ProxyName
|
|
|
|
cfg.ProxyType = pMsg.ProxyType
|
|
|
|
cfg.UseEncryption = pMsg.UseEncryption
|
|
|
|
cfg.UseCompression = pMsg.UseCompression
|
2018-05-22 23:59:35 +08:00
|
|
|
cfg.Group = pMsg.Group
|
|
|
|
cfg.GroupKey = pMsg.GroupKey
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *BaseProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) error {
|
2017-03-09 02:03:47 +08:00
|
|
|
var (
|
|
|
|
tmpStr string
|
|
|
|
ok bool
|
|
|
|
)
|
2018-04-10 17:46:49 +08:00
|
|
|
cfg.ProxyName = prefix + name
|
2017-03-09 02:03:47 +08: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
|
|
|
|
}
|
2018-05-22 23:59:35 +08:00
|
|
|
|
|
|
|
cfg.Group = section["group"]
|
|
|
|
cfg.GroupKey = section["group_key"]
|
2017-03-09 02:03:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +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
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bind info
|
|
|
|
type BindInfoConf struct {
|
2018-04-10 17:46:49 +08:00
|
|
|
RemotePort int `json:"remote_port"`
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08:00
|
|
|
func (cfg *BindInfoConf) compare(cmp *BindInfoConf) bool {
|
2018-04-10 17:46:49 +08:00
|
|
|
if cfg.RemotePort != cmp.RemotePort {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *BindInfoConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.RemotePort = pMsg.RemotePort
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *BindInfoConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
2017-03-09 02:03:47 +08:00
|
|
|
var (
|
|
|
|
tmpStr string
|
|
|
|
ok bool
|
2018-01-17 21:49:37 +08:00
|
|
|
v int64
|
2017-03-09 02:03:47 +08:00
|
|
|
)
|
|
|
|
if tmpStr, ok = section["remote_port"]; ok {
|
2018-01-17 21:49:37 +08:00
|
|
|
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
|
2018-01-17 21:49:37 +08:00
|
|
|
} else {
|
|
|
|
cfg.RemotePort = int(v)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *BindInfoConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2017-03-09 02:03:47 +08:00
|
|
|
pMsg.RemotePort = cfg.RemotePort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Domain info
|
|
|
|
type DomainConf struct {
|
2017-03-23 02:01:25 +08:00
|
|
|
CustomDomains []string `json:"custom_domains"`
|
|
|
|
SubDomain string `json:"sub_domain"`
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08: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 17:46:49 +08:00
|
|
|
func (cfg *DomainConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.CustomDomains = pMsg.CustomDomains
|
|
|
|
cfg.SubDomain = pMsg.SubDomain
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *DomainConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
2017-03-09 02:03:47 +08: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 17:46:49 +08:00
|
|
|
func (cfg *DomainConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
2017-03-09 02:03:47 +08:00
|
|
|
pMsg.CustomDomains = cfg.CustomDomains
|
|
|
|
pMsg.SubDomain = cfg.SubDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *DomainConf) checkForSvr() (err error) {
|
|
|
|
if err = cfg.check(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
for _, domain := range cfg.CustomDomains {
|
2018-04-10 17:46:49 +08:00
|
|
|
if subDomainHost != "" && len(strings.Split(subDomainHost, ".")) < len(strings.Split(domain, ".")) {
|
|
|
|
if strings.Contains(domain, subDomainHost) {
|
|
|
|
return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, subDomainHost)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.SubDomain != "" {
|
2018-04-10 17:46:49 +08:00
|
|
|
if 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")
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-05-21 22:42:42 +08:00
|
|
|
// Local service info
|
2017-03-09 02:03:47 +08:00
|
|
|
type LocalSvrConf struct {
|
2018-04-10 17:46:49 +08:00
|
|
|
LocalIp string `json:"local_ip"`
|
|
|
|
LocalPort int `json:"local_port"`
|
|
|
|
|
|
|
|
Plugin string `json:"plugin"`
|
|
|
|
PluginParams map[string]string `json:"plugin_params"`
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08:00
|
|
|
func (cfg *LocalSvrConf) compare(cmp *LocalSvrConf) bool {
|
|
|
|
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 17:46:49 +08:00
|
|
|
func (cfg *LocalSvrConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
2017-05-21 22:42:42 +08:00
|
|
|
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 {
|
2018-04-10 17:46:49 +08:00
|
|
|
if cfg.LocalIp = section["local_ip"]; cfg.LocalIp == "" {
|
|
|
|
cfg.LocalIp = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2017-05-21 22:42:42 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
// TCP
|
|
|
|
type TcpProxyConf struct {
|
|
|
|
BaseProxyConf
|
|
|
|
BindInfoConf
|
|
|
|
|
|
|
|
LocalSvrConf
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08: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) ||
|
2018-04-10 17:46:49 +08:00
|
|
|
!cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *TcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
|
|
|
|
cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *TcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
|
|
|
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *TcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.MarshalToMsg(pMsg)
|
|
|
|
cfg.BindInfoConf.MarshalToMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *TcpProxyConf) CheckForCli() error { return nil }
|
|
|
|
|
|
|
|
func (cfg *TcpProxyConf) CheckForSvr() error { return nil }
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
// UDP
|
|
|
|
type UdpProxyConf struct {
|
|
|
|
BaseProxyConf
|
|
|
|
BindInfoConf
|
|
|
|
|
|
|
|
LocalSvrConf
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08: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) ||
|
|
|
|
!cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *UdpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
|
|
|
|
cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *UdpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
|
|
|
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *UdpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.MarshalToMsg(pMsg)
|
|
|
|
cfg.BindInfoConf.MarshalToMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *UdpProxyConf) CheckForCli() error { return nil }
|
|
|
|
|
|
|
|
func (cfg *UdpProxyConf) CheckForSvr() error { return nil }
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
// HTTP
|
|
|
|
type HttpProxyConf struct {
|
|
|
|
BaseProxyConf
|
|
|
|
DomainConf
|
|
|
|
|
|
|
|
LocalSvrConf
|
|
|
|
|
2018-05-20 23:22:07 +08:00
|
|
|
Locations []string `json:"locations"`
|
|
|
|
HttpUser string `json:"http_user"`
|
|
|
|
HttpPwd string `json:"http_pwd"`
|
|
|
|
HostHeaderRewrite string `json:"host_header_rewrite"`
|
|
|
|
Headers map[string]string `json:"headers"`
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08: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) ||
|
|
|
|
!cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
|
|
|
|
strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
|
|
|
|
cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
|
|
|
|
cfg.HttpUser != cmpConf.HttpUser ||
|
2018-05-20 23:22:07 +08:00
|
|
|
cfg.HttpPwd != cmpConf.HttpPwd ||
|
|
|
|
len(cfg.Headers) != len(cmpConf.Headers) {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
2018-05-20 23:22:07 +08:00
|
|
|
|
|
|
|
for k, v := range cfg.Headers {
|
|
|
|
if v2, ok := cmpConf.Headers[k]; !ok {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
if v != v2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-13 02:20:49 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *HttpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
|
|
|
|
cfg.DomainConf.UnmarshalFromMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
cfg.Locations = pMsg.Locations
|
|
|
|
cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
|
|
|
|
cfg.HttpUser = pMsg.HttpUser
|
|
|
|
cfg.HttpPwd = pMsg.HttpPwd
|
2018-05-20 23:22:07 +08:00
|
|
|
cfg.Headers = pMsg.Headers
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *HttpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
|
|
|
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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"]
|
|
|
|
cfg.HttpUser = section["http_user"]
|
|
|
|
cfg.HttpPwd = section["http_pwd"]
|
2018-05-20 23:22:07 +08: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-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *HttpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.MarshalToMsg(pMsg)
|
|
|
|
cfg.DomainConf.MarshalToMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
pMsg.Locations = cfg.Locations
|
|
|
|
pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
|
|
|
|
pMsg.HttpUser = cfg.HttpUser
|
|
|
|
pMsg.HttpPwd = cfg.HttpPwd
|
2018-05-20 23:22:07 +08:00
|
|
|
pMsg.Headers = cfg.Headers
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *HttpProxyConf) CheckForCli() (err error) {
|
|
|
|
if err = cfg.DomainConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *HttpProxyConf) CheckForSvr() (err error) {
|
|
|
|
if 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
|
|
|
}
|
|
|
|
if err = cfg.DomainConf.checkForSvr(); 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
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPS
|
|
|
|
type HttpsProxyConf struct {
|
|
|
|
BaseProxyConf
|
|
|
|
DomainConf
|
|
|
|
|
|
|
|
LocalSvrConf
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08: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) ||
|
2018-04-10 17:46:49 +08:00
|
|
|
!cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) {
|
2017-07-13 02:20:49 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *HttpsProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
|
|
|
|
cfg.DomainConf.UnmarshalFromMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *HttpsProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
|
|
|
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *HttpsProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.MarshalToMsg(pMsg)
|
|
|
|
cfg.DomainConf.MarshalToMsg(pMsg)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *HttpsProxyConf) CheckForCli() (err error) {
|
|
|
|
if err = cfg.DomainConf.checkForCli(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *HttpsProxyConf) CheckForSvr() (err error) {
|
|
|
|
if vhostHttpsPort == 0 {
|
2017-03-09 02:03:47 +08:00
|
|
|
return fmt.Errorf("type [https] not support when vhost_https_port is not set")
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.DomainConf.checkForSvr(); 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
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:02:33 +08:00
|
|
|
// STCP
|
|
|
|
type StcpProxyConf struct {
|
|
|
|
BaseProxyConf
|
|
|
|
|
|
|
|
Role string `json:"role"`
|
|
|
|
Sk string `json:"sk"`
|
|
|
|
|
|
|
|
// used in role server
|
|
|
|
LocalSvrConf
|
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
// used in role visitor
|
2017-06-26 03:02:33 +08:00
|
|
|
ServerName string `json:"server_name"`
|
|
|
|
BindAddr string `json:"bind_addr"`
|
|
|
|
BindPort int `json:"bind_port"`
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:20:49 +08:00
|
|
|
func (cfg *StcpProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*StcpProxyConf)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
|
|
|
|
!cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
|
|
|
|
cfg.Role != cmpConf.Role ||
|
|
|
|
cfg.Sk != cmpConf.Sk ||
|
|
|
|
cfg.ServerName != cmpConf.ServerName ||
|
|
|
|
cfg.BindAddr != cmpConf.BindAddr ||
|
|
|
|
cfg.BindPort != cmpConf.BindPort {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:02:33 +08:00
|
|
|
// Only for role server.
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *StcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
|
2017-06-26 03:02:33 +08:00
|
|
|
cfg.Sk = pMsg.Sk
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *StcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
|
|
|
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-06-26 03:02:33 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpStr := section["role"]
|
2017-12-15 11:40:08 +08:00
|
|
|
if tmpStr == "" {
|
|
|
|
tmpStr = "server"
|
|
|
|
}
|
2017-12-05 01:34:33 +08:00
|
|
|
if tmpStr == "server" || tmpStr == "visitor" {
|
2017-06-26 03:02:33 +08:00
|
|
|
cfg.Role = tmpStr
|
|
|
|
} else {
|
2017-12-16 21:56:13 +08:00
|
|
|
return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Sk = section["sk"]
|
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
if tmpStr == "visitor" {
|
2017-06-26 03:02:33 +08:00
|
|
|
cfg.ServerName = prefix + section["server_name"]
|
|
|
|
if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
|
|
|
|
cfg.BindAddr = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpStr, ok := section["bind_port"]; ok {
|
|
|
|
if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
|
|
|
|
return fmt.Errorf("Parse conf error: proxy [%s] bind_port error", name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *StcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.MarshalToMsg(pMsg)
|
2017-06-26 03:02:33 +08:00
|
|
|
pMsg.Sk = cfg.Sk
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *StcpProxyConf) CheckForCli() (err error) {
|
|
|
|
if cfg.Role != "server" && cfg.Role != "visitor" {
|
|
|
|
err = fmt.Errorf("role should be 'server' or 'visitor'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cfg.Role == "visitor" {
|
|
|
|
if cfg.BindAddr == "" {
|
|
|
|
err = fmt.Errorf("bind_addr shouldn't be empty")
|
|
|
|
return
|
|
|
|
}
|
2018-05-16 23:45:44 +08:00
|
|
|
if cfg.BindPort == 0 {
|
|
|
|
err = fmt.Errorf("bind_port should be set")
|
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *StcpProxyConf) CheckForSvr() (err error) {
|
2017-06-26 03:02:33 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-24 18:20:07 +08:00
|
|
|
// XTCP
|
|
|
|
type XtcpProxyConf struct {
|
|
|
|
BaseProxyConf
|
|
|
|
|
|
|
|
Role string `json:"role"`
|
|
|
|
Sk string `json:"sk"`
|
|
|
|
|
|
|
|
// used in role server
|
|
|
|
LocalSvrConf
|
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
// used in role visitor
|
2017-10-24 18:20:07 +08:00
|
|
|
ServerName string `json:"server_name"`
|
|
|
|
BindAddr string `json:"bind_addr"`
|
|
|
|
BindPort int `json:"bind_port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *XtcpProxyConf) Compare(cmp ProxyConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*XtcpProxyConf)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
|
|
|
|
!cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
|
|
|
|
cfg.Role != cmpConf.Role ||
|
|
|
|
cfg.Sk != cmpConf.Sk ||
|
|
|
|
cfg.ServerName != cmpConf.ServerName ||
|
|
|
|
cfg.BindAddr != cmpConf.BindAddr ||
|
|
|
|
cfg.BindPort != cmpConf.BindPort {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only for role server.
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *XtcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
|
2017-10-24 18:20:07 +08:00
|
|
|
cfg.Sk = pMsg.Sk
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *XtcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
|
|
|
|
if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
2017-10-24 18:20:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpStr := section["role"]
|
2017-12-15 11:40:08 +08:00
|
|
|
if tmpStr == "" {
|
|
|
|
tmpStr = "server"
|
|
|
|
}
|
2017-12-05 01:34:33 +08:00
|
|
|
if tmpStr == "server" || tmpStr == "visitor" {
|
2017-10-24 18:20:07 +08:00
|
|
|
cfg.Role = tmpStr
|
|
|
|
} else {
|
2017-12-16 21:56:13 +08:00
|
|
|
return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
|
2017-10-24 18:20:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Sk = section["sk"]
|
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
if tmpStr == "visitor" {
|
2017-10-24 18:20:07 +08:00
|
|
|
cfg.ServerName = prefix + section["server_name"]
|
|
|
|
if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
|
|
|
|
cfg.BindAddr = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpStr, ok := section["bind_port"]; ok {
|
|
|
|
if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
|
|
|
|
return fmt.Errorf("Parse conf error: proxy [%s] bind_port error", name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-10 17:46:49 +08:00
|
|
|
if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return
|
2017-10-24 18:20:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *XtcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
|
|
|
|
cfg.BaseProxyConf.MarshalToMsg(pMsg)
|
2017-10-24 18:20:07 +08:00
|
|
|
pMsg.Sk = cfg.Sk
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *XtcpProxyConf) CheckForCli() (err error) {
|
|
|
|
if cfg.Role != "server" && cfg.Role != "visitor" {
|
|
|
|
err = fmt.Errorf("role should be 'server' or 'visitor'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cfg.Role == "visitor" {
|
|
|
|
if cfg.BindAddr == "" {
|
|
|
|
err = fmt.Errorf("bind_addr shouldn't be empty")
|
|
|
|
return
|
|
|
|
}
|
2018-05-16 23:45:44 +08:00
|
|
|
if cfg.BindPort == 0 {
|
|
|
|
err = fmt.Errorf("bind_port should be set")
|
|
|
|
return
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *XtcpProxyConf) CheckForSvr() (err error) {
|
2017-10-24 18:20:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-25 23:05:07 +08:00
|
|
|
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 11:54:51 +08:00
|
|
|
err = fmt.Errorf("Parse conf error: range section [%s] local_port and remote_port is necessary", name)
|
2018-01-25 23:05:07 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-25 01:45:38 +08:00
|
|
|
// if len(startProxy) is 0, start all
|
|
|
|
// otherwise just start proxies in startProxy map
|
2018-04-10 17:46:49 +08:00
|
|
|
func LoadProxyConfFromIni(prefix string, conf ini.File, startProxy map[string]struct{}) (
|
2017-12-05 01:34:33 +08:00
|
|
|
proxyConfs map[string]ProxyConf, visitorConfs map[string]ProxyConf, err error) {
|
2017-06-26 03:02:33 +08:00
|
|
|
|
2017-05-25 01:45:38 +08:00
|
|
|
if prefix != "" {
|
|
|
|
prefix += "."
|
|
|
|
}
|
|
|
|
|
|
|
|
startAll := true
|
|
|
|
if len(startProxy) > 0 {
|
|
|
|
startAll = false
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
proxyConfs = make(map[string]ProxyConf)
|
2017-12-05 01:34:33 +08:00
|
|
|
visitorConfs = make(map[string]ProxyConf)
|
2017-03-09 02:03:47 +08:00
|
|
|
for name, section := range conf {
|
2018-01-25 23:05:07 +08:00
|
|
|
if name == "common" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-25 01:45:38 +08:00
|
|
|
_, shouldStart := startProxy[name]
|
2018-01-25 23:05:07 +08:00
|
|
|
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 {
|
2018-04-10 17:46:49 +08:00
|
|
|
cfg, err := NewProxyConfFromIni(prefix, subName, subSection)
|
2017-03-09 02:03:47 +08:00
|
|
|
if err != nil {
|
2017-12-05 01:34:33 +08:00
|
|
|
return proxyConfs, visitorConfs, err
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
2018-01-25 23:05:07 +08:00
|
|
|
role := subSection["role"]
|
2017-12-05 01:34:33 +08:00
|
|
|
if role == "visitor" {
|
2018-01-25 23:05:07 +08:00
|
|
|
visitorConfs[prefix+subName] = cfg
|
2017-06-26 03:02:33 +08:00
|
|
|
} else {
|
2018-01-25 23:05:07 +08:00
|
|
|
proxyConfs[prefix+subName] = cfg
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-01-25 23:05:07 +08:00
|
|
|
|
|
|
|
func copySection(section ini.Section) (out ini.Section) {
|
|
|
|
out = make(ini.Section)
|
|
|
|
for k, v := range section {
|
|
|
|
out[k] = v
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|