| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package legacy |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
| 20 |
| 21 | "gopkg.in/ini.v1" |
| 22 | ) |
| 23 |
| 24 | type VisitorType string |
| 25 |
| 26 | const ( |
| 27 | VisitorTypeSTCP VisitorType = "stcp" |
| 28 | VisitorTypeXTCP VisitorType = "xtcp" |
| 29 | VisitorTypeSUDP VisitorType = "sudp" |
| 30 | ) |
| 31 |
| 32 | // Visitor |
| 33 | var ( |
| 34 | visitorConfTypeMap = map[VisitorType]reflect.Type{ |
| 35 | VisitorTypeSTCP: reflect.TypeFor[STCPVisitorConf](), |
| 36 | VisitorTypeXTCP: reflect.TypeFor[XTCPVisitorConf](), |
| 37 | VisitorTypeSUDP: reflect.TypeFor[SUDPVisitorConf](), |
| 38 | } |
| 39 | ) |
| 40 |
| 41 | type VisitorConf interface { |
| 42 | // GetBaseConfig returns the base config of visitor. |
| 43 | GetBaseConfig() *BaseVisitorConf |
| 44 | // UnmarshalFromIni unmarshals config from ini. |
| 45 | UnmarshalFromIni(prefix string, name string, section *ini.Section) error |
| 46 | } |
| 47 |
| 48 | // DefaultVisitorConf creates a empty VisitorConf object by visitorType. |
| 49 | // If visitorType doesn't exist, return nil. |
| 50 | func DefaultVisitorConf(visitorType VisitorType) VisitorConf { |
| 51 | v, ok := visitorConfTypeMap[visitorType] |
| 52 | if !ok { |
| 53 | return nil |
| 54 | } |
| 55 | return reflect.New(v).Interface().(VisitorConf) |
| 56 | } |
| 57 |
| 58 | type BaseVisitorConf struct { |
| 59 | ProxyName string `ini:"name" json:"name"` |
| 60 | ProxyType string `ini:"type" json:"type"` |
| 61 | UseEncryption bool `ini:"use_encryption" json:"use_encryption"` |
| 62 | UseCompression bool `ini:"use_compression" json:"use_compression"` |
| 63 | Role string `ini:"role" json:"role"` |
| 64 | Sk string `ini:"sk" json:"sk"` |
| 65 | // if the server user is not set, it defaults to the current user |
| 66 | ServerUser string `ini:"server_user" json:"server_user"` |
| 67 | ServerName string `ini:"server_name" json:"server_name"` |
| 68 | BindAddr string `ini:"bind_addr" json:"bind_addr"` |
| 69 | // BindPort is the port that visitor listens on. |
| 70 | // It can be less than 0, it means don't bind to the port and only receive connections redirected from |
| 71 | // other visitors. (This is not supported for SUDP now) |
| 72 | BindPort int `ini:"bind_port" json:"bind_port"` |
| 73 | } |
| 74 |
| 75 | // Base |
| 76 | func (cfg *BaseVisitorConf) GetBaseConfig() *BaseVisitorConf { |
| 77 | return cfg |
| 78 | } |
| 79 |
| 80 | func (cfg *BaseVisitorConf) unmarshalFromIni(_ string, name string, _ *ini.Section) error { |
| 81 | // Custom decoration after basic unmarshal: |
| 82 | cfg.ProxyName = name |
| 83 |
| 84 | // bind_addr |
| 85 | if cfg.BindAddr == "" { |
| 86 | cfg.BindAddr = "127.0.0.1" |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 |
| 91 | func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error { |
| 92 | err := section.MapTo(cfg) |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 |
| 97 | err = cfg.GetBaseConfig().unmarshalFromIni(prefix, name, section) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 |
| 104 | type SUDPVisitorConf struct { |
| 105 | BaseVisitorConf `ini:",extends"` |
| 106 | } |
| 107 |
| 108 | func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { |
| 109 | err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) |
| 110 | if err != nil { |
| 111 | return |
| 112 | } |
| 113 |
| 114 | // Add custom logic unmarshal, if exists |
| 115 |
| 116 | return |
| 117 | } |
| 118 |
| 119 | type STCPVisitorConf struct { |
| 120 | BaseVisitorConf `ini:",extends"` |
| 121 | } |
| 122 |
| 123 | func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { |
| 124 | err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) |
| 125 | if err != nil { |
| 126 | return |
| 127 | } |
| 128 |
| 129 | // Add custom logic unmarshal, if exists |
| 130 |
| 131 | return |
| 132 | } |
| 133 |
| 134 | type XTCPVisitorConf struct { |
| 135 | BaseVisitorConf `ini:",extends"` |
| 136 |
| 137 | Protocol string `ini:"protocol" json:"protocol,omitempty"` |
| 138 | KeepTunnelOpen bool `ini:"keep_tunnel_open" json:"keep_tunnel_open,omitempty"` |
| 139 | MaxRetriesAnHour int `ini:"max_retries_an_hour" json:"max_retries_an_hour,omitempty"` |
| 140 | MinRetryInterval int `ini:"min_retry_interval" json:"min_retry_interval,omitempty"` |
| 141 | FallbackTo string `ini:"fallback_to" json:"fallback_to,omitempty"` |
| 142 | FallbackTimeoutMs int `ini:"fallback_timeout_ms" json:"fallback_timeout_ms,omitempty"` |
| 143 | } |
| 144 |
| 145 | func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { |
| 146 | err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) |
| 147 | if err != nil { |
| 148 | return |
| 149 | } |
| 150 |
| 151 | // Add custom logic unmarshal, if exists |
| 152 | if cfg.Protocol == "" { |
| 153 | cfg.Protocol = "quic" |
| 154 | } |
| 155 | if cfg.MaxRetriesAnHour <= 0 { |
| 156 | cfg.MaxRetriesAnHour = 8 |
| 157 | } |
| 158 | if cfg.MinRetryInterval <= 0 { |
| 159 | cfg.MinRetryInterval = 90 |
| 160 | } |
| 161 | if cfg.FallbackTimeoutMs <= 0 { |
| 162 | cfg.FallbackTimeoutMs = 1000 |
| 163 | } |
| 164 | return |
| 165 | } |
| 166 |
| 167 | // Visitor loaded from ini |
| 168 | func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) { |
| 169 | // section.Key: if key not exists, section will set it with default value. |
| 170 | visitorType := VisitorType(section.Key("type").String()) |
| 171 |
| 172 | if visitorType == "" { |
| 173 | return nil, fmt.Errorf("type shouldn't be empty") |
| 174 | } |
| 175 |
| 176 | conf := DefaultVisitorConf(visitorType) |
| 177 | if conf == nil { |
| 178 | return nil, fmt.Errorf("type [%s] error", visitorType) |
| 179 | } |
| 180 |
| 181 | if err := conf.UnmarshalFromIni(prefix, name, section); err != nil { |
| 182 | return nil, fmt.Errorf("type [%s] error", visitorType) |
| 183 | } |
| 184 | return conf, nil |
| 185 | } |