123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- package legacy
- import (
- "fmt"
- "reflect"
- "gopkg.in/ini.v1"
- "github.com/fatedier/frp/pkg/config/types"
- )
- type ProxyType string
- const (
- ProxyTypeTCP ProxyType = "tcp"
- ProxyTypeUDP ProxyType = "udp"
- ProxyTypeTCPMUX ProxyType = "tcpmux"
- ProxyTypeHTTP ProxyType = "http"
- ProxyTypeHTTPS ProxyType = "https"
- ProxyTypeSTCP ProxyType = "stcp"
- ProxyTypeXTCP ProxyType = "xtcp"
- ProxyTypeSUDP ProxyType = "sudp"
- )
- var (
- proxyConfTypeMap = map[ProxyType]reflect.Type{
- ProxyTypeTCP: reflect.TypeOf(TCPProxyConf{}),
- ProxyTypeUDP: reflect.TypeOf(UDPProxyConf{}),
- ProxyTypeTCPMUX: reflect.TypeOf(TCPMuxProxyConf{}),
- ProxyTypeHTTP: reflect.TypeOf(HTTPProxyConf{}),
- ProxyTypeHTTPS: reflect.TypeOf(HTTPSProxyConf{}),
- ProxyTypeSTCP: reflect.TypeOf(STCPProxyConf{}),
- ProxyTypeXTCP: reflect.TypeOf(XTCPProxyConf{}),
- ProxyTypeSUDP: reflect.TypeOf(SUDPProxyConf{}),
- }
- )
- type ProxyConf interface {
-
- GetBaseConfig() *BaseProxyConf
-
-
- UnmarshalFromIni(string, string, *ini.Section) error
- }
- func NewConfByType(proxyType ProxyType) ProxyConf {
- v, ok := proxyConfTypeMap[proxyType]
- if !ok {
- return nil
- }
- cfg := reflect.New(v).Interface().(ProxyConf)
- return cfg
- }
- func DefaultProxyConf(proxyType ProxyType) ProxyConf {
- return NewConfByType(proxyType)
- }
- func NewProxyConfFromIni(prefix, name string, section *ini.Section) (ProxyConf, error) {
-
- proxyType := ProxyType(section.Key("type").String())
- if proxyType == "" {
- proxyType = ProxyTypeTCP
- }
- conf := DefaultProxyConf(proxyType)
- if conf == nil {
- return nil, fmt.Errorf("invalid type [%s]", proxyType)
- }
- if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
- return nil, err
- }
- return conf, nil
- }
- type LocalSvrConf struct {
-
- LocalIP string `ini:"local_ip" json:"local_ip"`
-
- LocalPort int `ini:"local_port" json:"local_port"`
-
-
-
- Plugin string `ini:"plugin" json:"plugin"`
-
-
- PluginParams map[string]string `ini:"-"`
- }
- type HealthCheckConf struct {
-
-
-
-
-
-
-
-
-
-
- HealthCheckType string `ini:"health_check_type" json:"health_check_type"`
-
-
-
- HealthCheckTimeoutS int `ini:"health_check_timeout_s" json:"health_check_timeout_s"`
-
-
- HealthCheckMaxFailed int `ini:"health_check_max_failed" json:"health_check_max_failed"`
-
-
- HealthCheckIntervalS int `ini:"health_check_interval_s" json:"health_check_interval_s"`
-
-
- HealthCheckURL string `ini:"health_check_url" json:"health_check_url"`
-
-
- HealthCheckAddr string `ini:"-"`
- }
- type BaseProxyConf struct {
-
- ProxyName string `ini:"name" json:"name"`
-
-
-
- ProxyType string `ini:"type" json:"type"`
-
-
-
- UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
-
-
- UseCompression bool `ini:"use_compression" json:"use_compression"`
-
-
-
- Group string `ini:"group" json:"group"`
-
-
- GroupKey string `ini:"group_key" json:"group_key"`
-
-
-
- ProxyProtocolVersion string `ini:"proxy_protocol_version" json:"proxy_protocol_version"`
-
-
- BandwidthLimit types.BandwidthQuantity `ini:"bandwidth_limit" json:"bandwidth_limit"`
-
-
-
- BandwidthLimitMode string `ini:"bandwidth_limit_mode" json:"bandwidth_limit_mode"`
-
- Metas map[string]string `ini:"-" json:"metas"`
- LocalSvrConf `ini:",extends"`
- HealthCheckConf `ini:",extends"`
- }
- func (cfg *BaseProxyConf) GetBaseConfig() *BaseProxyConf {
- return cfg
- }
- func (cfg *BaseProxyConf) decorate(_ string, name string, section *ini.Section) error {
- cfg.ProxyName = name
-
- cfg.Metas = GetMapWithoutPrefix(section.KeysHash(), "meta_")
-
- if bandwidth, err := section.GetKey("bandwidth_limit"); err == nil {
- cfg.BandwidthLimit, err = types.NewBandwidthQuantity(bandwidth.String())
- if err != nil {
- return err
- }
- }
-
- cfg.LocalSvrConf.PluginParams = GetMapByPrefix(section.KeysHash(), "plugin_")
- return nil
- }
- type DomainConf struct {
- CustomDomains []string `ini:"custom_domains" json:"custom_domains"`
- SubDomain string `ini:"subdomain" json:"subdomain"`
- }
- type RoleServerCommonConf struct {
- Role string `ini:"role" json:"role"`
- Sk string `ini:"sk" json:"sk"`
- AllowUsers []string `ini:"allow_users" json:"allow_users"`
- }
- type HTTPProxyConf struct {
- BaseProxyConf `ini:",extends"`
- DomainConf `ini:",extends"`
- Locations []string `ini:"locations" json:"locations"`
- HTTPUser string `ini:"http_user" json:"http_user"`
- HTTPPwd string `ini:"http_pwd" json:"http_pwd"`
- HostHeaderRewrite string `ini:"host_header_rewrite" json:"host_header_rewrite"`
- Headers map[string]string `ini:"-" json:"headers"`
- RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
- }
- func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
- err := preUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return err
- }
-
- cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_")
- return nil
- }
- type HTTPSProxyConf struct {
- BaseProxyConf `ini:",extends"`
- DomainConf `ini:",extends"`
- }
- func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
- err := preUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return err
- }
-
- return nil
- }
- type TCPProxyConf struct {
- BaseProxyConf `ini:",extends"`
- RemotePort int `ini:"remote_port" json:"remote_port"`
- }
- func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
- err := preUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return err
- }
-
- return nil
- }
- type UDPProxyConf struct {
- BaseProxyConf `ini:",extends"`
- RemotePort int `ini:"remote_port" json:"remote_port"`
- }
- func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
- err := preUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return err
- }
-
- return nil
- }
- type TCPMuxProxyConf struct {
- BaseProxyConf `ini:",extends"`
- DomainConf `ini:",extends"`
- HTTPUser string `ini:"http_user" json:"http_user,omitempty"`
- HTTPPwd string `ini:"http_pwd" json:"http_pwd,omitempty"`
- RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
- Multiplexer string `ini:"multiplexer"`
- }
- func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
- err := preUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return err
- }
-
- return nil
- }
- type STCPProxyConf struct {
- BaseProxyConf `ini:",extends"`
- RoleServerCommonConf `ini:",extends"`
- }
- func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
- err := preUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return err
- }
-
- if cfg.Role == "" {
- cfg.Role = "server"
- }
- return nil
- }
- type XTCPProxyConf struct {
- BaseProxyConf `ini:",extends"`
- RoleServerCommonConf `ini:",extends"`
- }
- func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
- err := preUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return err
- }
-
- if cfg.Role == "" {
- cfg.Role = "server"
- }
- return nil
- }
- type SUDPProxyConf struct {
- BaseProxyConf `ini:",extends"`
- RoleServerCommonConf `ini:",extends"`
- }
- func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
- err := preUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return err
- }
-
- return nil
- }
- func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini.Section) error {
- err := section.MapTo(cfg)
- if err != nil {
- return err
- }
- err = cfg.GetBaseConfig().decorate(prefix, name, section)
- if err != nil {
- return err
- }
- return nil
- }
|