12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121 |
- // 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.TcpMuxProxy] = reflect.TypeOf(TcpMuxProxyConf{})
- proxyConfTypeMap[consts.UdpProxy] = reflect.TypeOf(UdpProxyConf{})
- proxyConfTypeMap[consts.HttpProxy] = reflect.TypeOf(HttpProxyConf{})
- proxyConfTypeMap[consts.HttpsProxy] = reflect.TypeOf(HttpsProxyConf{})
- proxyConfTypeMap[consts.StcpProxy] = reflect.TypeOf(StcpProxyConf{})
- proxyConfTypeMap[consts.XtcpProxy] = reflect.TypeOf(XtcpProxyConf{})
- }
- // 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 {
- GetBaseInfo() *BaseProxyConf
- UnmarshalFromMsg(pMsg *msg.NewProxy)
- UnmarshalFromIni(prefix string, name string, conf ini.Section) error
- MarshalToMsg(pMsg *msg.NewProxy)
- CheckForCli() error
- CheckForSvr(serverCfg ServerCommonConf) error
- Compare(conf ProxyConf) bool
- }
- func NewProxyConfFromMsg(pMsg *msg.NewProxy, serverCfg ServerCommonConf) (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.UnmarshalFromMsg(pMsg)
- err = cfg.CheckForSvr(serverCfg)
- return
- }
- func NewProxyConfFromIni(prefix string, 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
- }
- if err = cfg.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- if err = cfg.CheckForCli(); err != nil {
- return
- }
- return
- }
- // BaseProxyConf provides configuration info that is common to all proxy types.
- type BaseProxyConf struct {
- // ProxyName is the name of this proxy.
- ProxyName string `json:"proxy_name"`
- // ProxyType specifies the type of this proxy. Valid values include "tcp",
- // "udp", "http", "https", "stcp", and "xtcp". By default, this value is
- // "tcp".
- ProxyType string `json:"proxy_type"`
- // UseEncryption controls whether or not communication with the server will
- // be encrypted. Encryption is done using the tokens supplied in the server
- // and client configuration. By default, this value is false.
- UseEncryption bool `json:"use_encryption"`
- // UseCompression controls whether or not communication with the server
- // will be compressed. By default, this value is false.
- UseCompression bool `json:"use_compression"`
- // Group specifies which group the proxy is a part of. The server will use
- // this information to load balance proxies in the same group. If the value
- // is "", this proxy will not be in a group. By default, this value is "".
- Group string `json:"group"`
- // GroupKey specifies a group key, which should be the same among proxies
- // of the same group. By default, this value is "".
- GroupKey string `json:"group_key"`
- // ProxyProtocolVersion specifies which protocol version to use. Valid
- // values include "v1", "v2", and "". If the value is "", a protocol
- // version will be automatically selected. By default, this value is "".
- ProxyProtocolVersion string `json:"proxy_protocol_version"`
- // BandwidthLimit limit the proxy bandwidth
- // 0 means no limit
- BandwidthLimit BandwidthQuantity `json:"bandwidth_limit"`
- // meta info for each proxy
- Metas map[string]string `json:"metas"`
- LocalSvrConf
- HealthCheckConf
- }
- func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
- return cfg
- }
- func (cfg *BaseProxyConf) compare(cmp *BaseProxyConf) bool {
- if cfg.ProxyName != cmp.ProxyName ||
- cfg.ProxyType != cmp.ProxyType ||
- cfg.UseEncryption != cmp.UseEncryption ||
- cfg.UseCompression != cmp.UseCompression ||
- cfg.Group != cmp.Group ||
- cfg.GroupKey != cmp.GroupKey ||
- cfg.ProxyProtocolVersion != cmp.ProxyProtocolVersion ||
- !cfg.BandwidthLimit.Equal(&cmp.BandwidthLimit) ||
- !reflect.DeepEqual(cfg.Metas, cmp.Metas) {
- return false
- }
- if !cfg.LocalSvrConf.compare(&cmp.LocalSvrConf) {
- return false
- }
- if !cfg.HealthCheckConf.compare(&cmp.HealthCheckConf) {
- return false
- }
- return true
- }
- 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
- }
- func (cfg *BaseProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) error {
- var (
- tmpStr string
- ok bool
- err error
- )
- cfg.ProxyName = prefix + 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
- }
- cfg.Group = section["group"]
- cfg.GroupKey = section["group_key"]
- cfg.ProxyProtocolVersion = section["proxy_protocol_version"]
- if cfg.BandwidthLimit, err = NewBandwidthQuantity(section["bandwidth_limit"]); err != nil {
- return err
- }
- if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return err
- }
- if err = cfg.HealthCheckConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return err
- }
- if cfg.HealthCheckType == "tcp" && cfg.Plugin == "" {
- cfg.HealthCheckAddr = cfg.LocalIp + fmt.Sprintf(":%d", cfg.LocalPort)
- }
- if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckUrl != "" {
- s := fmt.Sprintf("http://%s:%d", cfg.LocalIp, cfg.LocalPort)
- if !strings.HasPrefix(cfg.HealthCheckUrl, "/") {
- s += "/"
- }
- cfg.HealthCheckUrl = s + cfg.HealthCheckUrl
- }
- cfg.Metas = make(map[string]string)
- for k, v := range section {
- if strings.HasPrefix(k, "meta_") {
- cfg.Metas[strings.TrimPrefix(k, "meta_")] = v
- }
- }
- return nil
- }
- func (cfg *BaseProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
- pMsg.ProxyName = cfg.ProxyName
- pMsg.ProxyType = cfg.ProxyType
- pMsg.UseEncryption = cfg.UseEncryption
- pMsg.UseCompression = cfg.UseCompression
- pMsg.Group = cfg.Group
- pMsg.GroupKey = cfg.GroupKey
- pMsg.Metas = cfg.Metas
- }
- func (cfg *BaseProxyConf) checkForCli() (err error) {
- if cfg.ProxyProtocolVersion != "" {
- if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" {
- return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion)
- }
- }
- if err = cfg.LocalSvrConf.checkForCli(); err != nil {
- return
- }
- if err = cfg.HealthCheckConf.checkForCli(); err != nil {
- return
- }
- return nil
- }
- // Bind info
- type BindInfoConf struct {
- RemotePort int `json:"remote_port"`
- }
- func (cfg *BindInfoConf) compare(cmp *BindInfoConf) bool {
- if cfg.RemotePort != cmp.RemotePort {
- return false
- }
- return true
- }
- func (cfg *BindInfoConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.RemotePort = pMsg.RemotePort
- }
- func (cfg *BindInfoConf) UnmarshalFromIni(prefix string, 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) MarshalToMsg(pMsg *msg.NewProxy) {
- pMsg.RemotePort = cfg.RemotePort
- }
- // 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) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.CustomDomains = pMsg.CustomDomains
- cfg.SubDomain = pMsg.SubDomain
- }
- func (cfg *DomainConf) UnmarshalFromIni(prefix string, 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
- }
- return
- }
- func (cfg *DomainConf) MarshalToMsg(pMsg *msg.NewProxy) {
- pMsg.CustomDomains = cfg.CustomDomains
- pMsg.SubDomain = cfg.SubDomain
- }
- func (cfg *DomainConf) check() (err error) {
- if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
- err = fmt.Errorf("custom_domains and subdomain should set at least one of them")
- return
- }
- return
- }
- func (cfg *DomainConf) checkForCli() (err error) {
- if err = cfg.check(); err != nil {
- return
- }
- return
- }
- func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) {
- if err = cfg.check(); err != nil {
- return
- }
- for _, domain := range cfg.CustomDomains {
- if serverCfg.SubDomainHost != "" && len(strings.Split(serverCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
- if strings.Contains(domain, serverCfg.SubDomainHost) {
- return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, serverCfg.SubDomainHost)
- }
- }
- }
- if cfg.SubDomain != "" {
- if serverCfg.SubDomainHost == "" {
- return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
- }
- if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
- return fmt.Errorf("'.' and '*' is not supported in subdomain")
- }
- }
- return
- }
- // LocalSvrConf configures what location the client will proxy to, or what
- // plugin will be used.
- type LocalSvrConf struct {
- // LocalIp specifies the IP address or host name to proxy to.
- LocalIp string `json:"local_ip"`
- // LocalPort specifies the port to proxy to.
- LocalPort int `json:"local_port"`
- // Plugin specifies what plugin should be used for proxying. If this value
- // is set, the LocalIp and LocalPort values will be ignored. By default,
- // this value is "".
- Plugin string `json:"plugin"`
- // PluginParams specify parameters to be passed to the plugin, if one is
- // being used. By default, this value is an empty map.
- PluginParams map[string]string `json:"plugin_params"`
- }
- 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
- }
- func (cfg *LocalSvrConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- cfg.Plugin = section["plugin"]
- cfg.PluginParams = make(map[string]string)
- if cfg.Plugin != "" {
- // get params begin with "plugin_"
- for k, v := range section {
- if strings.HasPrefix(k, "plugin_") {
- cfg.PluginParams[k] = v
- }
- }
- } else {
- 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
- }
- func (cfg *LocalSvrConf) checkForCli() (err error) {
- if cfg.Plugin == "" {
- if cfg.LocalIp == "" {
- err = fmt.Errorf("local ip or plugin is required")
- return
- }
- if cfg.LocalPort <= 0 {
- err = fmt.Errorf("error local_port")
- return
- }
- }
- return
- }
- // HealthCheckConf configures health checking. This can be useful for load
- // balancing purposes to detect and remove proxies to failing services.
- type HealthCheckConf struct {
- // HealthCheckType specifies what protocol to use for health checking.
- // Valid values include "tcp", "http", and "". If this value is "", health
- // checking will not be performed. By default, this value is "".
- //
- // If the type is "tcp", a connection will be attempted to the target
- // server. If a connection cannot be established, the health check fails.
- //
- // If the type is "http", a GET request will be made to the endpoint
- // specified by HealthCheckUrl. If the response is not a 200, the health
- // check fails.
- HealthCheckType string `json:"health_check_type"` // tcp | http
- // HealthCheckTimeoutS specifies the number of seconds to wait for a health
- // check attempt to connect. If the timeout is reached, this counts as a
- // health check failure. By default, this value is 3.
- HealthCheckTimeoutS int `json:"health_check_timeout_s"`
- // HealthCheckMaxFailed specifies the number of allowed failures before the
- // proxy is stopped. By default, this value is 1.
- HealthCheckMaxFailed int `json:"health_check_max_failed"`
- // HealthCheckIntervalS specifies the time in seconds between health
- // checks. By default, this value is 10.
- HealthCheckIntervalS int `json:"health_check_interval_s"`
- // HealthCheckUrl specifies the address to send health checks to if the
- // health check type is "http".
- HealthCheckUrl string `json:"health_check_url"`
- // HealthCheckAddr specifies the address to connect to if the health check
- // type is "tcp".
- HealthCheckAddr string `json:"-"`
- }
- func (cfg *HealthCheckConf) compare(cmp *HealthCheckConf) bool {
- if cfg.HealthCheckType != cmp.HealthCheckType ||
- cfg.HealthCheckTimeoutS != cmp.HealthCheckTimeoutS ||
- cfg.HealthCheckMaxFailed != cmp.HealthCheckMaxFailed ||
- cfg.HealthCheckIntervalS != cmp.HealthCheckIntervalS ||
- cfg.HealthCheckUrl != cmp.HealthCheckUrl {
- return false
- }
- return true
- }
- func (cfg *HealthCheckConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- cfg.HealthCheckType = section["health_check_type"]
- cfg.HealthCheckUrl = section["health_check_url"]
- if tmpStr, ok := section["health_check_timeout_s"]; ok {
- if cfg.HealthCheckTimeoutS, err = strconv.Atoi(tmpStr); err != nil {
- return fmt.Errorf("Parse conf error: proxy [%s] health_check_timeout_s error", name)
- }
- }
- if tmpStr, ok := section["health_check_max_failed"]; ok {
- if cfg.HealthCheckMaxFailed, err = strconv.Atoi(tmpStr); err != nil {
- return fmt.Errorf("Parse conf error: proxy [%s] health_check_max_failed error", name)
- }
- }
- if tmpStr, ok := section["health_check_interval_s"]; ok {
- if cfg.HealthCheckIntervalS, err = strconv.Atoi(tmpStr); err != nil {
- return fmt.Errorf("Parse conf error: proxy [%s] health_check_interval_s error", name)
- }
- }
- return
- }
- func (cfg *HealthCheckConf) checkForCli() error {
- if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
- return fmt.Errorf("unsupport health check type")
- }
- if cfg.HealthCheckType != "" {
- if cfg.HealthCheckType == "http" && cfg.HealthCheckUrl == "" {
- return fmt.Errorf("health_check_url is required for health check type 'http'")
- }
- }
- return nil
- }
- // TCP
- type TcpProxyConf struct {
- BaseProxyConf
- BindInfoConf
- }
- func (cfg *TcpProxyConf) Compare(cmp ProxyConf) bool {
- cmpConf, ok := cmp.(*TcpProxyConf)
- if !ok {
- return false
- }
- if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
- !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
- return false
- }
- return true
- }
- func (cfg *TcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
- cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
- }
- func (cfg *TcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- return
- }
- func (cfg *TcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.MarshalToMsg(pMsg)
- cfg.BindInfoConf.MarshalToMsg(pMsg)
- }
- func (cfg *TcpProxyConf) CheckForCli() (err error) {
- if err = cfg.BaseProxyConf.checkForCli(); err != nil {
- return err
- }
- return
- }
- func (cfg *TcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }
- // TCP Multiplexer
- type TcpMuxProxyConf struct {
- BaseProxyConf
- DomainConf
- Multiplexer string `json:"multiplexer"`
- }
- func (cfg *TcpMuxProxyConf) Compare(cmp ProxyConf) bool {
- cmpConf, ok := cmp.(*TcpMuxProxyConf)
- if !ok {
- return false
- }
- if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
- !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
- cfg.Multiplexer != cmpConf.Multiplexer {
- return false
- }
- return true
- }
- func (cfg *TcpMuxProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
- cfg.DomainConf.UnmarshalFromMsg(pMsg)
- cfg.Multiplexer = pMsg.Multiplexer
- }
- func (cfg *TcpMuxProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- cfg.Multiplexer = section["multiplexer"]
- if cfg.Multiplexer != consts.HttpConnectTcpMultiplexer {
- return fmt.Errorf("parse conf error: proxy [%s] incorrect multiplexer [%s]", name, cfg.Multiplexer)
- }
- return
- }
- func (cfg *TcpMuxProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.MarshalToMsg(pMsg)
- cfg.DomainConf.MarshalToMsg(pMsg)
- pMsg.Multiplexer = cfg.Multiplexer
- }
- func (cfg *TcpMuxProxyConf) CheckForCli() (err error) {
- if err = cfg.BaseProxyConf.checkForCli(); err != nil {
- return err
- }
- if err = cfg.DomainConf.checkForCli(); err != nil {
- return err
- }
- if cfg.Multiplexer != consts.HttpConnectTcpMultiplexer {
- return fmt.Errorf("parse conf error: incorrect multiplexer [%s]", cfg.Multiplexer)
- }
- return
- }
- func (cfg *TcpMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
- if cfg.Multiplexer != consts.HttpConnectTcpMultiplexer {
- return fmt.Errorf("proxy [%s] incorrect multiplexer [%s]", cfg.ProxyName, cfg.Multiplexer)
- }
- if cfg.Multiplexer == consts.HttpConnectTcpMultiplexer && serverCfg.TcpMuxHttpConnectPort == 0 {
- return fmt.Errorf("proxy [%s] type [tcpmux] with multiplexer [httpconnect] requires tcpmux_httpconnect_port configuration", cfg.ProxyName)
- }
- if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
- err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
- return
- }
- return
- }
- // UDP
- type UdpProxyConf struct {
- BaseProxyConf
- BindInfoConf
- }
- func (cfg *UdpProxyConf) Compare(cmp ProxyConf) bool {
- cmpConf, ok := cmp.(*UdpProxyConf)
- if !ok {
- return false
- }
- if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
- !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
- return false
- }
- return true
- }
- func (cfg *UdpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
- cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
- }
- func (cfg *UdpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- return
- }
- func (cfg *UdpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.MarshalToMsg(pMsg)
- cfg.BindInfoConf.MarshalToMsg(pMsg)
- }
- func (cfg *UdpProxyConf) CheckForCli() (err error) {
- if err = cfg.BaseProxyConf.checkForCli(); err != nil {
- return
- }
- return
- }
- func (cfg *UdpProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }
- // HTTP
- type HttpProxyConf struct {
- BaseProxyConf
- DomainConf
- 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"`
- }
- func (cfg *HttpProxyConf) Compare(cmp ProxyConf) bool {
- cmpConf, ok := cmp.(*HttpProxyConf)
- if !ok {
- return false
- }
- if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
- !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
- strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
- cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
- cfg.HttpUser != cmpConf.HttpUser ||
- cfg.HttpPwd != cmpConf.HttpPwd ||
- len(cfg.Headers) != len(cmpConf.Headers) {
- return false
- }
- for k, v := range cfg.Headers {
- if v2, ok := cmpConf.Headers[k]; !ok {
- return false
- } else {
- if v != v2 {
- return false
- }
- }
- }
- return true
- }
- func (cfg *HttpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
- cfg.DomainConf.UnmarshalFromMsg(pMsg)
- cfg.Locations = pMsg.Locations
- cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
- cfg.HttpUser = pMsg.HttpUser
- cfg.HttpPwd = pMsg.HttpPwd
- cfg.Headers = pMsg.Headers
- }
- func (cfg *HttpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- 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"]
- cfg.Headers = make(map[string]string)
- for k, v := range section {
- if strings.HasPrefix(k, "header_") {
- cfg.Headers[strings.TrimPrefix(k, "header_")] = v
- }
- }
- return
- }
- func (cfg *HttpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.MarshalToMsg(pMsg)
- cfg.DomainConf.MarshalToMsg(pMsg)
- pMsg.Locations = cfg.Locations
- pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
- pMsg.HttpUser = cfg.HttpUser
- pMsg.HttpPwd = cfg.HttpPwd
- pMsg.Headers = cfg.Headers
- }
- func (cfg *HttpProxyConf) CheckForCli() (err error) {
- if err = cfg.BaseProxyConf.checkForCli(); err != nil {
- return
- }
- if err = cfg.DomainConf.checkForCli(); err != nil {
- return
- }
- return
- }
- func (cfg *HttpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
- if serverCfg.VhostHttpPort == 0 {
- return fmt.Errorf("type [http] not support when vhost_http_port is not set")
- }
- if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
- err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
- return
- }
- return
- }
- // HTTPS
- type HttpsProxyConf struct {
- BaseProxyConf
- DomainConf
- }
- func (cfg *HttpsProxyConf) Compare(cmp ProxyConf) bool {
- cmpConf, ok := cmp.(*HttpsProxyConf)
- if !ok {
- return false
- }
- if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
- !cfg.DomainConf.compare(&cmpConf.DomainConf) {
- return false
- }
- return true
- }
- func (cfg *HttpsProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
- cfg.DomainConf.UnmarshalFromMsg(pMsg)
- }
- func (cfg *HttpsProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- return
- }
- func (cfg *HttpsProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.MarshalToMsg(pMsg)
- cfg.DomainConf.MarshalToMsg(pMsg)
- }
- func (cfg *HttpsProxyConf) CheckForCli() (err error) {
- if err = cfg.BaseProxyConf.checkForCli(); err != nil {
- return
- }
- if err = cfg.DomainConf.checkForCli(); err != nil {
- return
- }
- return
- }
- func (cfg *HttpsProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
- if serverCfg.VhostHttpsPort == 0 {
- return fmt.Errorf("type [https] not support when vhost_https_port is not set")
- }
- if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
- err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
- return
- }
- return
- }
- // STCP
- type StcpProxyConf struct {
- BaseProxyConf
- Role string `json:"role"`
- Sk string `json:"sk"`
- }
- func (cfg *StcpProxyConf) Compare(cmp ProxyConf) bool {
- cmpConf, ok := cmp.(*StcpProxyConf)
- if !ok {
- return false
- }
- if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
- cfg.Role != cmpConf.Role ||
- cfg.Sk != cmpConf.Sk {
- return false
- }
- return true
- }
- // Only for role server.
- func (cfg *StcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
- cfg.Sk = pMsg.Sk
- }
- func (cfg *StcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- cfg.Role = section["role"]
- if cfg.Role != "server" {
- return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
- }
- cfg.Sk = section["sk"]
- if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- return
- }
- func (cfg *StcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.MarshalToMsg(pMsg)
- pMsg.Sk = cfg.Sk
- }
- func (cfg *StcpProxyConf) CheckForCli() (err error) {
- if err = cfg.BaseProxyConf.checkForCli(); err != nil {
- return
- }
- if cfg.Role != "server" {
- err = fmt.Errorf("role should be 'server'")
- return
- }
- return
- }
- func (cfg *StcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
- return
- }
- // XTCP
- type XtcpProxyConf struct {
- BaseProxyConf
- Role string `json:"role"`
- Sk string `json:"sk"`
- }
- 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 {
- return false
- }
- return true
- }
- // Only for role server.
- func (cfg *XtcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
- cfg.Sk = pMsg.Sk
- }
- func (cfg *XtcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
- if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- cfg.Role = section["role"]
- if cfg.Role != "server" {
- return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
- }
- cfg.Sk = section["sk"]
- if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
- return
- }
- return
- }
- func (cfg *XtcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
- cfg.BaseProxyConf.MarshalToMsg(pMsg)
- pMsg.Sk = cfg.Sk
- }
- func (cfg *XtcpProxyConf) CheckForCli() (err error) {
- if err = cfg.BaseProxyConf.checkForCli(); err != nil {
- return
- }
- if cfg.Role != "server" {
- err = fmt.Errorf("role should be 'server'")
- return
- }
- return
- }
- func (cfg *XtcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (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", name)
- return
- }
- sections = make(map[string]ini.Section)
- for i, port := range localPorts {
- subName := fmt.Sprintf("%s_%d", name, i)
- subSection := copySection(section)
- subSection["local_port"] = fmt.Sprintf("%d", port)
- subSection["remote_port"] = fmt.Sprintf("%d", remotePorts[i])
- sections[subName] = subSection
- }
- return
- }
- // if len(startProxy) is 0, start all
- // otherwise just start proxies in startProxy map
- func LoadAllConfFromIni(prefix string, content string, startProxy map[string]struct{}) (
- proxyConfs map[string]ProxyConf, visitorConfs map[string]VisitorConf, err error) {
- conf, errRet := ini.Load(strings.NewReader(content))
- if errRet != nil {
- err = errRet
- return
- }
- if prefix != "" {
- prefix += "."
- }
- startAll := true
- if len(startProxy) > 0 {
- startAll = false
- }
- proxyConfs = make(map[string]ProxyConf)
- visitorConfs = make(map[string]VisitorConf)
- for name, section := range conf {
- if name == "common" {
- continue
- }
- _, shouldStart := startProxy[name]
- if !startAll && !shouldStart {
- continue
- }
- subSections := make(map[string]ini.Section)
- if strings.HasPrefix(name, "range:") {
- // range section
- rangePrefix := strings.TrimSpace(strings.TrimPrefix(name, "range:"))
- subSections, err = ParseRangeSection(rangePrefix, section)
- if err != nil {
- return
- }
- } else {
- subSections[name] = section
- }
- for subName, subSection := range subSections {
- if subSection["role"] == "" {
- subSection["role"] = "server"
- }
- role := subSection["role"]
- if role == "server" {
- cfg, errRet := NewProxyConfFromIni(prefix, subName, subSection)
- if errRet != nil {
- err = errRet
- return
- }
- proxyConfs[prefix+subName] = cfg
- } else if role == "visitor" {
- cfg, errRet := NewVisitorConfFromIni(prefix, subName, subSection)
- if errRet != nil {
- err = errRet
- return
- }
- visitorConfs[prefix+subName] = cfg
- } else {
- err = fmt.Errorf("role should be 'server' or 'visitor'")
- return
- }
- }
- }
- return
- }
- func copySection(section ini.Section) (out ini.Section) {
- out = make(ini.Section)
- for k, v := range section {
- out[k] = v
- }
- return
- }
|