123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869 |
- // 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"
- "reflect"
- "strconv"
- "strings"
- "github.com/fatedier/frp/models/consts"
- "github.com/fatedier/frp/models/msg"
- "github.com/fatedier/frp/utils/util"
- ini "github.com/vaughan0/go-ini"
- )
- var proxyConfTypeMap map[string]reflect.Type
- 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{})
- proxyConfTypeMap[consts.StcpProxy] = reflect.TypeOf(StcpProxyConf{})
- proxyConfTypeMap[consts.XtcpProxy] = reflect.TypeOf(XtcpProxyConf{})
- }
- // 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
- }
- type ProxyConf interface {
- GetName() string
- GetType() string
- GetBaseInfo() *BaseProxyConf
- LoadFromMsg(pMsg *msg.NewProxy)
- LoadFromFile(name string, conf ini.Section) error
- UnMarshalToMsg(pMsg *msg.NewProxy)
- Check() error
- Compare(conf ProxyConf) bool
- }
- func NewProxyConf(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
- if pMsg.ProxyType == "" {
- pMsg.ProxyType = consts.TcpProxy
- }
- cfg = NewConfByType(pMsg.ProxyType)
- if cfg == nil {
- err = fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
- return
- }
- cfg.LoadFromMsg(pMsg)
- err = cfg.Check()
- return
- }
- func NewProxyConfFromFile(name string, section ini.Section) (cfg ProxyConf, err error) {
- proxyType := section["type"]
- if proxyType == "" {
- proxyType = consts.TcpProxy
- section["type"] = consts.TcpProxy
- }
- cfg = NewConfByType(proxyType)
- if cfg == nil {
- err = fmt.Errorf("proxy [%s] type [%s] error", name, proxyType)
- return
- }
- err = cfg.LoadFromFile(name, section)
- return
- }
- // BaseProxy info
- type BaseProxyConf struct {
- ProxyName string `json:"proxy_name"`
- ProxyType string `json:"proxy_type"`
- UseEncryption bool `json:"use_encryption"`
- UseCompression bool `json:"use_compression"`
- }
- func (cfg *BaseProxyConf) GetName() string {
- return cfg.ProxyName
- }
- func (cfg *BaseProxyConf) GetType() string {
- return cfg.ProxyType
- }
- 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 {
- return false
- }
- return true
- }
- func (cfg *BaseProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.ProxyName = pMsg.ProxyName
- cfg.ProxyType = pMsg.ProxyType
- cfg.UseEncryption = pMsg.UseEncryption
- cfg.UseCompression = pMsg.UseCompression
- }
- func (cfg *BaseProxyConf) LoadFromFile(name string, section ini.Section) error {
- var (
- tmpStr string
- ok bool
- )
- if ClientCommonCfg.User != "" {
- cfg.ProxyName = ClientCommonCfg.User + "." + name
- } else {
- cfg.ProxyName = name
- }
- 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
- }
- return nil
- }
- func (cfg *BaseProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- pMsg.ProxyName = cfg.ProxyName
- pMsg.ProxyType = cfg.ProxyType
- pMsg.UseEncryption = cfg.UseEncryption
- pMsg.UseCompression = cfg.UseCompression
- }
- // Bind info
- type BindInfoConf struct {
- BindAddr string `json:"bind_addr"`
- RemotePort int `json:"remote_port"`
- }
- func (cfg *BindInfoConf) compare(cmp *BindInfoConf) bool {
- if cfg.BindAddr != cmp.BindAddr ||
- cfg.RemotePort != cmp.RemotePort {
- return false
- }
- return true
- }
- func (cfg *BindInfoConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.BindAddr = ServerCommonCfg.ProxyBindAddr
- cfg.RemotePort = pMsg.RemotePort
- }
- func (cfg *BindInfoConf) LoadFromFile(name string, section ini.Section) (err error) {
- var (
- tmpStr string
- ok bool
- v int64
- )
- if tmpStr, ok = section["remote_port"]; ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
- } else {
- cfg.RemotePort = int(v)
- }
- } else {
- return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
- }
- return nil
- }
- func (cfg *BindInfoConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- pMsg.RemotePort = cfg.RemotePort
- }
- func (cfg *BindInfoConf) check() (err error) {
- return nil
- }
- // Domain info
- type DomainConf struct {
- CustomDomains []string `json:"custom_domains"`
- SubDomain string `json:"sub_domain"`
- }
- func (cfg *DomainConf) compare(cmp *DomainConf) bool {
- if strings.Join(cfg.CustomDomains, " ") != strings.Join(cmp.CustomDomains, " ") ||
- cfg.SubDomain != cmp.SubDomain {
- return false
- }
- return true
- }
- func (cfg *DomainConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.CustomDomains = pMsg.CustomDomains
- cfg.SubDomain = pMsg.SubDomain
- }
- func (cfg *DomainConf) LoadFromFile(name string, section ini.Section) (err error) {
- 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
- }
- if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
- return fmt.Errorf("Parse conf error: proxy [%s] custom_domains and subdomain should set at least one of them", name)
- }
- return
- }
- func (cfg *DomainConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- pMsg.CustomDomains = cfg.CustomDomains
- pMsg.SubDomain = cfg.SubDomain
- }
- func (cfg *DomainConf) check() (err error) {
- for _, domain := range cfg.CustomDomains {
- if ServerCommonCfg.SubDomainHost != "" && len(strings.Split(ServerCommonCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
- if strings.Contains(domain, ServerCommonCfg.SubDomainHost) {
- return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, ServerCommonCfg.SubDomainHost)
- }
- }
- }
- if cfg.SubDomain != "" {
- if ServerCommonCfg.SubDomainHost == "" {
- return fmt.Errorf("subdomain is not supported because this feature is not enabled by frps")
- }
- if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
- return fmt.Errorf("'.' and '*' is not supported in subdomain")
- }
- }
- return nil
- }
- // Local service info
- type LocalSvrConf struct {
- LocalIp string `json:"-"`
- LocalPort int `json:"-"`
- }
- func (cfg *LocalSvrConf) compare(cmp *LocalSvrConf) bool {
- if cfg.LocalIp != cmp.LocalIp ||
- cfg.LocalPort != cmp.LocalPort {
- return false
- }
- return true
- }
- func (cfg *LocalSvrConf) LoadFromFile(name string, section ini.Section) (err error) {
- 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)
- }
- return nil
- }
- type PluginConf struct {
- Plugin string `json:"-"`
- PluginParams map[string]string `json:"-"`
- }
- func (cfg *PluginConf) compare(cmp *PluginConf) bool {
- 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
- }
- func (cfg *PluginConf) LoadFromFile(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 {
- return fmt.Errorf("Parse conf error: proxy [%s] no plugin info found", name)
- }
- return
- }
- // TCP
- type TcpProxyConf struct {
- BaseProxyConf
- BindInfoConf
- LocalSvrConf
- PluginConf
- }
- 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) ||
- !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
- !cfg.PluginConf.compare(&cmpConf.PluginConf) {
- return false
- }
- return true
- }
- func (cfg *TcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.LoadFromMsg(pMsg)
- cfg.BindInfoConf.LoadFromMsg(pMsg)
- }
- func (cfg *TcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
- return
- }
- if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
- return
- }
- if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
- if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
- return
- }
- }
- return
- }
- func (cfg *TcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
- cfg.BindInfoConf.UnMarshalToMsg(pMsg)
- }
- func (cfg *TcpProxyConf) Check() (err error) {
- err = cfg.BindInfoConf.check()
- return
- }
- // UDP
- type UdpProxyConf struct {
- BaseProxyConf
- BindInfoConf
- LocalSvrConf
- }
- 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
- }
- func (cfg *UdpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.LoadFromMsg(pMsg)
- cfg.BindInfoConf.LoadFromMsg(pMsg)
- }
- func (cfg *UdpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
- return
- }
- if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
- return
- }
- if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
- return
- }
- return
- }
- func (cfg *UdpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
- cfg.BindInfoConf.UnMarshalToMsg(pMsg)
- }
- func (cfg *UdpProxyConf) Check() (err error) {
- err = cfg.BindInfoConf.check()
- return
- }
- // HTTP
- type HttpProxyConf struct {
- BaseProxyConf
- DomainConf
- LocalSvrConf
- PluginConf
- Locations []string `json:"locations"`
- HostHeaderRewrite string `json:"host_header_rewrite"`
- HttpUser string `json:"-"`
- HttpPwd string `json:"-"`
- }
- 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) ||
- !cfg.PluginConf.compare(&cmpConf.PluginConf) ||
- strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
- cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
- cfg.HttpUser != cmpConf.HttpUser ||
- cfg.HttpPwd != cmpConf.HttpPwd {
- return false
- }
- return true
- }
- func (cfg *HttpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.LoadFromMsg(pMsg)
- cfg.DomainConf.LoadFromMsg(pMsg)
- cfg.Locations = pMsg.Locations
- cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
- cfg.HttpUser = pMsg.HttpUser
- cfg.HttpPwd = pMsg.HttpPwd
- }
- func (cfg *HttpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
- return
- }
- if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
- return
- }
- if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
- if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
- 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"]
- cfg.HttpUser = section["http_user"]
- cfg.HttpPwd = section["http_pwd"]
- return
- }
- func (cfg *HttpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
- cfg.DomainConf.UnMarshalToMsg(pMsg)
- pMsg.Locations = cfg.Locations
- pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
- pMsg.HttpUser = cfg.HttpUser
- pMsg.HttpPwd = cfg.HttpPwd
- }
- func (cfg *HttpProxyConf) Check() (err error) {
- if ServerCommonCfg.VhostHttpPort == 0 {
- return fmt.Errorf("type [http] not support when vhost_http_port is not set")
- }
- err = cfg.DomainConf.check()
- return
- }
- // HTTPS
- type HttpsProxyConf struct {
- BaseProxyConf
- DomainConf
- LocalSvrConf
- PluginConf
- }
- 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) ||
- !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
- !cfg.PluginConf.compare(&cmpConf.PluginConf) {
- return false
- }
- return true
- }
- func (cfg *HttpsProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.LoadFromMsg(pMsg)
- cfg.DomainConf.LoadFromMsg(pMsg)
- }
- func (cfg *HttpsProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
- return
- }
- if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
- return
- }
- if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
- if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
- return
- }
- }
- return
- }
- func (cfg *HttpsProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
- cfg.DomainConf.UnMarshalToMsg(pMsg)
- }
- func (cfg *HttpsProxyConf) Check() (err error) {
- if ServerCommonCfg.VhostHttpsPort == 0 {
- return fmt.Errorf("type [https] not support when vhost_https_port is not set")
- }
- err = cfg.DomainConf.check()
- return
- }
- // STCP
- type StcpProxyConf struct {
- BaseProxyConf
- Role string `json:"role"`
- Sk string `json:"sk"`
- // used in role server
- LocalSvrConf
- PluginConf
- // used in role visitor
- ServerName string `json:"server_name"`
- BindAddr string `json:"bind_addr"`
- BindPort int `json:"bind_port"`
- }
- 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.PluginConf.compare(&cmpConf.PluginConf) ||
- 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.
- func (cfg *StcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.LoadFromMsg(pMsg)
- cfg.Sk = pMsg.Sk
- }
- func (cfg *StcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
- return
- }
- tmpStr := section["role"]
- if tmpStr == "" {
- tmpStr = "server"
- }
- if tmpStr == "server" || tmpStr == "visitor" {
- cfg.Role = tmpStr
- } else {
- return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
- }
- cfg.Sk = section["sk"]
- if tmpStr == "visitor" {
- prefix := section["prefix"]
- 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 {
- if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
- if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
- return
- }
- }
- }
- return
- }
- func (cfg *StcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
- pMsg.Sk = cfg.Sk
- }
- func (cfg *StcpProxyConf) Check() (err error) {
- return
- }
- // XTCP
- type XtcpProxyConf struct {
- BaseProxyConf
- Role string `json:"role"`
- Sk string `json:"sk"`
- // used in role server
- LocalSvrConf
- PluginConf
- // used in role visitor
- 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.PluginConf.compare(&cmpConf.PluginConf) ||
- 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.
- func (cfg *XtcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.LoadFromMsg(pMsg)
- cfg.Sk = pMsg.Sk
- }
- func (cfg *XtcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
- return
- }
- tmpStr := section["role"]
- if tmpStr == "" {
- tmpStr = "server"
- }
- if tmpStr == "server" || tmpStr == "visitor" {
- cfg.Role = tmpStr
- } else {
- return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
- }
- cfg.Sk = section["sk"]
- if tmpStr == "visitor" {
- prefix := section["prefix"]
- 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 {
- if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
- if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
- return
- }
- }
- }
- return
- }
- func (cfg *XtcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
- pMsg.Sk = cfg.Sk
- }
- func (cfg *XtcpProxyConf) Check() (err error) {
- 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 {
- err = fmt.Errorf("Parse conf error: range section [%s] local_port and remote_port is necessary")
- 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 LoadProxyConfFromFile(prefix string, conf ini.File, startProxy map[string]struct{}) (
- proxyConfs map[string]ProxyConf, visitorConfs map[string]ProxyConf, err error) {
- if prefix != "" {
- prefix += "."
- }
- startAll := true
- if len(startProxy) > 0 {
- startAll = false
- }
- proxyConfs = make(map[string]ProxyConf)
- visitorConfs = make(map[string]ProxyConf)
- 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 {
- // some proxy or visotr configure may be used this prefix
- subSection["prefix"] = prefix
- cfg, err := NewProxyConfFromFile(subName, subSection)
- if err != nil {
- return proxyConfs, visitorConfs, err
- }
- role := subSection["role"]
- if role == "visitor" {
- visitorConfs[prefix+subName] = cfg
- } else {
- proxyConfs[prefix+subName] = cfg
- }
- }
- }
- return
- }
- func copySection(section ini.Section) (out ini.Section) {
- out = make(ini.Section)
- for k, v := range section {
- out[k] = v
- }
- return
- }
|