123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package legacy
- import (
- "fmt"
- "reflect"
- "gopkg.in/ini.v1"
- )
- type VisitorType string
- const (
- VisitorTypeSTCP VisitorType = "stcp"
- VisitorTypeXTCP VisitorType = "xtcp"
- VisitorTypeSUDP VisitorType = "sudp"
- )
- var (
- visitorConfTypeMap = map[VisitorType]reflect.Type{
- VisitorTypeSTCP: reflect.TypeOf(STCPVisitorConf{}),
- VisitorTypeXTCP: reflect.TypeOf(XTCPVisitorConf{}),
- VisitorTypeSUDP: reflect.TypeOf(SUDPVisitorConf{}),
- }
- )
- type VisitorConf interface {
-
- GetBaseConfig() *BaseVisitorConf
-
- UnmarshalFromIni(prefix string, name string, section *ini.Section) error
- }
- func DefaultVisitorConf(visitorType VisitorType) VisitorConf {
- v, ok := visitorConfTypeMap[visitorType]
- if !ok {
- return nil
- }
- return reflect.New(v).Interface().(VisitorConf)
- }
- type BaseVisitorConf 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"`
- Role string `ini:"role" json:"role"`
- Sk string `ini:"sk" json:"sk"`
-
- ServerUser string `ini:"server_user" json:"server_user"`
- ServerName string `ini:"server_name" json:"server_name"`
- BindAddr string `ini:"bind_addr" json:"bind_addr"`
-
-
-
- BindPort int `ini:"bind_port" json:"bind_port"`
- }
- func (cfg *BaseVisitorConf) GetBaseConfig() *BaseVisitorConf {
- return cfg
- }
- func (cfg *BaseVisitorConf) unmarshalFromIni(_ string, name string, _ *ini.Section) error {
-
- cfg.ProxyName = name
-
- if cfg.BindAddr == "" {
- cfg.BindAddr = "127.0.0.1"
- }
- return nil
- }
- func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
- err := section.MapTo(cfg)
- if err != nil {
- return err
- }
- err = cfg.GetBaseConfig().unmarshalFromIni(prefix, name, section)
- if err != nil {
- return err
- }
- return nil
- }
- type SUDPVisitorConf struct {
- BaseVisitorConf `ini:",extends"`
- }
- func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
- err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return
- }
-
- return
- }
- type STCPVisitorConf struct {
- BaseVisitorConf `ini:",extends"`
- }
- func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
- err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return
- }
-
- return
- }
- type XTCPVisitorConf struct {
- BaseVisitorConf `ini:",extends"`
- Protocol string `ini:"protocol" json:"protocol,omitempty"`
- KeepTunnelOpen bool `ini:"keep_tunnel_open" json:"keep_tunnel_open,omitempty"`
- MaxRetriesAnHour int `ini:"max_retries_an_hour" json:"max_retries_an_hour,omitempty"`
- MinRetryInterval int `ini:"min_retry_interval" json:"min_retry_interval,omitempty"`
- FallbackTo string `ini:"fallback_to" json:"fallback_to,omitempty"`
- FallbackTimeoutMs int `ini:"fallback_timeout_ms" json:"fallback_timeout_ms,omitempty"`
- }
- func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
- err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
- if err != nil {
- return
- }
-
- if cfg.Protocol == "" {
- cfg.Protocol = "quic"
- }
- if cfg.MaxRetriesAnHour <= 0 {
- cfg.MaxRetriesAnHour = 8
- }
- if cfg.MinRetryInterval <= 0 {
- cfg.MinRetryInterval = 90
- }
- if cfg.FallbackTimeoutMs <= 0 {
- cfg.FallbackTimeoutMs = 1000
- }
- return
- }
- func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) {
-
- visitorType := VisitorType(section.Key("type").String())
- if visitorType == "" {
- return nil, fmt.Errorf("type shouldn't be empty")
- }
- conf := DefaultVisitorConf(visitorType)
- if conf == nil {
- return nil, fmt.Errorf("type [%s] error", visitorType)
- }
- if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
- return nil, fmt.Errorf("type [%s] error", visitorType)
- }
- return conf, nil
- }
|