| // |
| 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 | "github.com/fatedier/frp/pkg/config/types" |
| 24 | ) |
| 25 |
| 26 | type ProxyType string |
| 27 |
| 28 | const ( |
| 29 | ProxyTypeTCP ProxyType = "tcp" |
| 30 | ProxyTypeUDP ProxyType = "udp" |
| 31 | ProxyTypeTCPMUX ProxyType = "tcpmux" |
| 32 | ProxyTypeHTTP ProxyType = "http" |
| 33 | ProxyTypeHTTPS ProxyType = "https" |
| 34 | ProxyTypeSTCP ProxyType = "stcp" |
| 35 | ProxyTypeXTCP ProxyType = "xtcp" |
| 36 | ProxyTypeSUDP ProxyType = "sudp" |
| 37 | ) |
| 38 |
| 39 | // Proxy |
| 40 | var ( |
| 41 | proxyConfTypeMap = map[ProxyType]reflect.Type{ |
| 42 | ProxyTypeTCP: reflect.TypeFor[TCPProxyConf](), |
| 43 | ProxyTypeUDP: reflect.TypeFor[UDPProxyConf](), |
| 44 | ProxyTypeTCPMUX: reflect.TypeFor[TCPMuxProxyConf](), |
| 45 | ProxyTypeHTTP: reflect.TypeFor[HTTPProxyConf](), |
| 46 | ProxyTypeHTTPS: reflect.TypeFor[HTTPSProxyConf](), |
| 47 | ProxyTypeSTCP: reflect.TypeFor[STCPProxyConf](), |
| 48 | ProxyTypeXTCP: reflect.TypeFor[XTCPProxyConf](), |
| 49 | ProxyTypeSUDP: reflect.TypeFor[SUDPProxyConf](), |
| 50 | } |
| 51 | ) |
| 52 |
| 53 | type ProxyConf interface { |
| 54 | // GetBaseConfig returns the BaseProxyConf for this config. |
| 55 | GetBaseConfig() *BaseProxyConf |
| 56 | // UnmarshalFromIni unmarshals a ini.Section into this config. This function |
| 57 | // will be called on the frpc side. |
| 58 | UnmarshalFromIni(string, string, *ini.Section) error |
| 59 | } |
| 60 |
| 61 | func NewConfByType(proxyType ProxyType) ProxyConf { |
| 62 | v, ok := proxyConfTypeMap[proxyType] |
| 63 | if !ok { |
| 64 | return nil |
| 65 | } |
| 66 | cfg := reflect.New(v).Interface().(ProxyConf) |
| 67 | return cfg |
| 68 | } |
| 69 |
| 70 | // Proxy Conf Loader |
| 71 | // DefaultProxyConf creates a empty ProxyConf object by proxyType. |
| 72 | // If proxyType doesn't exist, return nil. |
| 73 | func DefaultProxyConf(proxyType ProxyType) ProxyConf { |
| 74 | return NewConfByType(proxyType) |
| 75 | } |
| 76 |
| 77 | // Proxy loaded from ini |
| 78 | func NewProxyConfFromIni(prefix, name string, section *ini.Section) (ProxyConf, error) { |
| 79 | // section.Key: if key not exists, section will set it with default value. |
| 80 | proxyType := ProxyType(section.Key("type").String()) |
| 81 | if proxyType == "" { |
| 82 | proxyType = ProxyTypeTCP |
| 83 | } |
| 84 |
| 85 | conf := DefaultProxyConf(proxyType) |
| 86 | if conf == nil { |
| 87 | return nil, fmt.Errorf("invalid type [%s]", proxyType) |
| 88 | } |
| 89 |
| 90 | if err := conf.UnmarshalFromIni(prefix, name, section); err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | return conf, nil |
| 94 | } |
| 95 |
| 96 | // LocalSvrConf configures what location the client will to, or what |
| 97 | // plugin will be used. |
| 98 | type LocalSvrConf struct { |
| 99 | // LocalIP specifies the IP address or host name to to. |
| 100 | LocalIP string `ini:"local_ip" json:"local_ip"` |
| 101 | // LocalPort specifies the port to to. |
| 102 | LocalPort int `ini:"local_port" json:"local_port"` |
| 103 |
| 104 | // Plugin specifies what plugin should be used for ng. If this value |
| 105 | // is set, the LocalIp and LocalPort values will be ignored. By default, |
| 106 | // this value is "". |
| 107 | Plugin string `ini:"plugin" json:"plugin"` |
| 108 | // PluginParams specify parameters to be passed to the plugin, if one is |
| 109 | // being used. By default, this value is an empty map. |
| 110 | PluginParams map[string]string `ini:"-"` |
| 111 | } |
| 112 |
| 113 | // HealthCheckConf configures health checking. This can be useful for load |
| 114 | // balancing purposes to detect and remove proxies to failing services. |
| 115 | type HealthCheckConf struct { |
| 116 | // HealthCheckType specifies what protocol to use for health checking. |
| 117 | // Valid values include "tcp", "http", and "". If this value is "", health |
| 118 | // checking will not be performed. By default, this value is "". |
| 119 | // |
| 120 | // If the type is "tcp", a connection will be attempted to the target |
| 121 | // server. If a connection cannot be established, the health check fails. |
| 122 | // |
| 123 | // If the type is "http", a GET request will be made to the endpoint |
| 124 | // specified by HealthCheckURL. If the response is not a 200, the health |
| 125 | // check fails. |
| 126 | HealthCheckType string `ini:"health_check_type" json:"health_check_type"` // tcp | http |
| 127 | // HealthCheckTimeoutS specifies the number of seconds to wait for a health |
| 128 | // check attempt to connect. If the timeout is reached, this counts as a |
| 129 | // health check failure. By default, this value is 3. |
| 130 | HealthCheckTimeoutS int `ini:"health_check_timeout_s" json:"health_check_timeout_s"` |
| 131 | // HealthCheckMaxFailed specifies the number of allowed failures before the |
| 132 | // is stopped. By default, this value is 1. |
| 133 | HealthCheckMaxFailed int `ini:"health_check_max_failed" json:"health_check_max_failed"` |
| 134 | // HealthCheckIntervalS specifies the time in seconds between health |
| 135 | // checks. By default, this value is 10. |
| 136 | HealthCheckIntervalS int `ini:"health_check_interval_s" json:"health_check_interval_s"` |
| 137 | // HealthCheckURL specifies the address to send health checks to if the |
| 138 | // health check type is "http". |
| 139 | HealthCheckURL string `ini:"health_check_url" json:"health_check_url"` |
| 140 | // HealthCheckAddr specifies the address to connect to if the health check |
| 141 | // type is "tcp". |
| 142 | HealthCheckAddr string `ini:"-"` |
| 143 | } |
| 144 |
| 145 | // BaseProxyConf provides configuration info that is common to all types. |
| 146 | type BaseProxyConf struct { |
| 147 | // ProxyName is the name of this |
| 148 | ProxyName string `ini:"name" json:"name"` |
| 149 | // ProxyType specifies the type of this Valid values include "tcp", |
| 150 | // "udp", "http", "https", "stcp", and "xtcp". By default, this value is |
| 151 | // "tcp". |
| 152 | ProxyType string `ini:"type" json:"type"` |
| 153 |
| 154 | // UseEncryption controls whether or not communication with the server will |
| 155 | // be encrypted. Encryption is done using the tokens supplied in the server |
| 156 | // and client configuration. By default, this value is false. |
| 157 | UseEncryption bool `ini:"use_encryption" json:"use_encryption"` |
| 158 | // UseCompression controls whether or not communication with the server |
| 159 | // will be compressed. By default, this value is false. |
| 160 | UseCompression bool `ini:"use_compression" json:"use_compression"` |
| 161 | // Group specifies which group the is a part of. The server will use |
| 162 | // this information to load balance proxies in the same group. If the value |
| 163 | // is "", this will not be in a group. By default, this value is "". |
| 164 | Group string `ini:"group" json:"group"` |
| 165 | // GroupKey specifies a group key, which should be the same among proxies |
| 166 | // of the same group. By default, this value is "". |
| 167 | GroupKey string `ini:"group_key" json:"group_key"` |
| 168 |
| 169 | // ProxyProtocolVersion specifies which protocol version to use. Valid |
| 170 | // values include "v1", "v2", and "". If the value is "", a protocol |
| 171 | // version will be automatically selected. By default, this value is "". |
| 172 | ProxyProtocolVersion string `ini:"proxy_protocol_version" json:"proxy_protocol_version"` |
| 173 |
| 174 | // BandwidthLimit limit the bandwidth |
| 175 | // 0 means no limit |
| 176 | BandwidthLimit types.BandwidthQuantity `ini:"bandwidth_limit" json:"bandwidth_limit"` |
| 177 | // BandwidthLimitMode specifies whether to limit the bandwidth on the |
| 178 | // client or server side. Valid values include "client" and "server". |
| 179 | // By default, this value is "client". |
| 180 | BandwidthLimitMode string `ini:"bandwidth_limit_mode" json:"bandwidth_limit_mode"` |
| 181 |
| 182 | // meta info for each proxy |
| 183 | Metas map[string]string `ini:"-" json:"metas"` |
| 184 |
| 185 | LocalSvrConf `ini:",extends"` |
| 186 | HealthCheckConf `ini:",extends"` |
| 187 | } |
| 188 |
| 189 | // Base |
| 190 | func (cfg *BaseProxyConf) GetBaseConfig() *BaseProxyConf { |
| 191 | return cfg |
| 192 | } |
| 193 |
| 194 | // BaseProxyConf apply custom logic changes. |
| 195 | func (cfg *BaseProxyConf) decorate(_ string, name string, section *ini.Section) error { |
| 196 | cfg.ProxyName = name |
| 197 | // metas_xxx |
| 198 | cfg.Metas = GetMapWithoutPrefix(section.KeysHash(), "meta_") |
| 199 |
| 200 | // bandwidth_limit |
| 201 | if bandwidth, err := section.GetKey("bandwidth_limit"); err == nil { |
| 202 | cfg.BandwidthLimit, err = types.NewBandwidthQuantity(bandwidth.String()) |
| 203 | if err != nil { |
| 204 | return err |
| 205 | } |
| 206 | } |
| 207 |
| 208 | // plugin_xxx |
| 209 | cfg.PluginParams = GetMapByPrefix(section.KeysHash(), "plugin_") |
| 210 | return nil |
| 211 | } |
| 212 |
| 213 | type DomainConf struct { |
| 214 | CustomDomains []string `ini:"custom_domains" json:"custom_domains"` |
| 215 | SubDomain string `ini:"subdomain" json:"subdomain"` |
| 216 | } |
| 217 |
| 218 | type RoleServerCommonConf struct { |
| 219 | Role string `ini:"role" json:"role"` |
| 220 | Sk string `ini:"sk" json:"sk"` |
| 221 | AllowUsers []string `ini:"allow_users" json:"allow_users"` |
| 222 | } |
| 223 |
| 224 | // HTTP |
| 225 | type HTTPProxyConf struct { |
| 226 | BaseProxyConf `ini:",extends"` |
| 227 | DomainConf `ini:",extends"` |
| 228 |
| 229 | Locations []string `ini:"locations" json:"locations"` |
| 230 | HTTPUser string `ini:"http_user" json:"http_user"` |
| 231 | HTTPPwd string `ini:"http_pwd" json:"http_pwd"` |
| 232 | HostHeaderRewrite string `ini:"host_header_rewrite" json:"host_header_rewrite"` |
| 233 | Headers map[string]string `ini:"-" json:"headers"` |
| 234 | RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"` |
| 235 | } |
| 236 |
| 237 | func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { |
| 238 | err := preUnmarshalFromIni(cfg, prefix, name, section) |
| 239 | if err != nil { |
| 240 | return err |
| 241 | } |
| 242 |
| 243 | // Add custom logic unmarshal if exists |
| 244 | cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_") |
| 245 | return nil |
| 246 | } |
| 247 |
| 248 | // HTTPS |
| 249 | type HTTPSProxyConf struct { |
| 250 | BaseProxyConf `ini:",extends"` |
| 251 | DomainConf `ini:",extends"` |
| 252 | } |
| 253 |
| 254 | func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { |
| 255 | err := preUnmarshalFromIni(cfg, prefix, name, section) |
| 256 | if err != nil { |
| 257 | return err |
| 258 | } |
| 259 |
| 260 | // Add custom logic unmarshal if exists |
| 261 | return nil |
| 262 | } |
| 263 |
| 264 | // TCP |
| 265 | type TCPProxyConf struct { |
| 266 | BaseProxyConf `ini:",extends"` |
| 267 | RemotePort int `ini:"remote_port" json:"remote_port"` |
| 268 | } |
| 269 |
| 270 | func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { |
| 271 | err := preUnmarshalFromIni(cfg, prefix, name, section) |
| 272 | if err != nil { |
| 273 | return err |
| 274 | } |
| 275 |
| 276 | // Add custom logic unmarshal if exists |
| 277 |
| 278 | return nil |
| 279 | } |
| 280 |
| 281 | // UDP |
| 282 | type UDPProxyConf struct { |
| 283 | BaseProxyConf `ini:",extends"` |
| 284 |
| 285 | RemotePort int `ini:"remote_port" json:"remote_port"` |
| 286 | } |
| 287 |
| 288 | func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { |
| 289 | err := preUnmarshalFromIni(cfg, prefix, name, section) |
| 290 | if err != nil { |
| 291 | return err |
| 292 | } |
| 293 |
| 294 | // Add custom logic unmarshal if exists |
| 295 |
| 296 | return nil |
| 297 | } |
| 298 |
| 299 | // TCPMux |
| 300 | type TCPMuxProxyConf struct { |
| 301 | BaseProxyConf `ini:",extends"` |
| 302 | DomainConf `ini:",extends"` |
| 303 | HTTPUser string `ini:"http_user" json:"http_user,omitempty"` |
| 304 | HTTPPwd string `ini:"http_pwd" json:"http_pwd,omitempty"` |
| 305 | RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"` |
| 306 |
| 307 | Multiplexer string `ini:"multiplexer"` |
| 308 | } |
| 309 |
| 310 | func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { |
| 311 | err := preUnmarshalFromIni(cfg, prefix, name, section) |
| 312 | if err != nil { |
| 313 | return err |
| 314 | } |
| 315 |
| 316 | // Add custom logic unmarshal if exists |
| 317 |
| 318 | return nil |
| 319 | } |
| 320 |
| 321 | // STCP |
| 322 | type STCPProxyConf struct { |
| 323 | BaseProxyConf `ini:",extends"` |
| 324 | RoleServerCommonConf `ini:",extends"` |
| 325 | } |
| 326 |
| 327 | func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { |
| 328 | err := preUnmarshalFromIni(cfg, prefix, name, section) |
| 329 | if err != nil { |
| 330 | return err |
| 331 | } |
| 332 |
| 333 | // Add custom logic unmarshal if exists |
| 334 | if cfg.Role == "" { |
| 335 | cfg.Role = "server" |
| 336 | } |
| 337 | return nil |
| 338 | } |
| 339 |
| 340 | // XTCP |
| 341 | type XTCPProxyConf struct { |
| 342 | BaseProxyConf `ini:",extends"` |
| 343 | RoleServerCommonConf `ini:",extends"` |
| 344 | } |
| 345 |
| 346 | func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { |
| 347 | err := preUnmarshalFromIni(cfg, prefix, name, section) |
| 348 | if err != nil { |
| 349 | return err |
| 350 | } |
| 351 |
| 352 | // Add custom logic unmarshal if exists |
| 353 | if cfg.Role == "" { |
| 354 | cfg.Role = "server" |
| 355 | } |
| 356 | return nil |
| 357 | } |
| 358 |
| 359 | // SUDP |
| 360 | type SUDPProxyConf struct { |
| 361 | BaseProxyConf `ini:",extends"` |
| 362 | RoleServerCommonConf `ini:",extends"` |
| 363 | } |
| 364 |
| 365 | func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { |
| 366 | err := preUnmarshalFromIni(cfg, prefix, name, section) |
| 367 | if err != nil { |
| 368 | return err |
| 369 | } |
| 370 |
| 371 | // Add custom logic unmarshal if exists |
| 372 | return nil |
| 373 | } |
| 374 |
| 375 | func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini.Section) error { |
| 376 | err := section.MapTo(cfg) |
| 377 | if err != nil { |
| 378 | return err |
| 379 | } |
| 380 |
| 381 | err = cfg.GetBaseConfig().decorate(prefix, name, section) |
| 382 | if err != nil { |
| 383 | return err |
| 384 | } |
| 385 |
| 386 | return nil |
| 387 | } |