2023-09-06 10:18:02 +08:00
|
|
|
// Copyright 2023 The frp Authors
|
2018-06-25 18:22:35 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
package legacy
|
2018-06-25 18:22:35 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
"gopkg.in/ini.v1"
|
2023-09-20 15:18:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type VisitorType string
|
2022-08-29 01:02:53 +08:00
|
|
|
|
2023-09-20 15:18:50 +08:00
|
|
|
const (
|
|
|
|
VisitorTypeSTCP VisitorType = "stcp"
|
|
|
|
VisitorTypeXTCP VisitorType = "xtcp"
|
|
|
|
VisitorTypeSUDP VisitorType = "sudp"
|
2018-06-25 18:22:35 +08:00
|
|
|
)
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Visitor
|
2018-06-25 18:22:35 +08:00
|
|
|
var (
|
2023-09-20 15:18:50 +08:00
|
|
|
visitorConfTypeMap = map[VisitorType]reflect.Type{
|
|
|
|
VisitorTypeSTCP: reflect.TypeOf(STCPVisitorConf{}),
|
|
|
|
VisitorTypeXTCP: reflect.TypeOf(XTCPVisitorConf{}),
|
|
|
|
VisitorTypeSUDP: reflect.TypeOf(SUDPVisitorConf{}),
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type VisitorConf interface {
|
2023-05-30 20:25:22 +08:00
|
|
|
// GetBaseConfig returns the base config of visitor.
|
|
|
|
GetBaseConfig() *BaseVisitorConf
|
|
|
|
// UnmarshalFromIni unmarshals config from ini.
|
2021-01-26 11:31:08 +08:00
|
|
|
UnmarshalFromIni(prefix string, name string, section *ini.Section) error
|
2023-09-06 10:18:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultVisitorConf creates a empty VisitorConf object by visitorType.
|
|
|
|
// If visitorType doesn't exist, return nil.
|
2023-09-20 15:18:50 +08:00
|
|
|
func DefaultVisitorConf(visitorType VisitorType) VisitorConf {
|
2023-09-06 10:18:02 +08:00
|
|
|
v, ok := visitorConfTypeMap[visitorType]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return reflect.New(v).Interface().(VisitorConf)
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
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"`
|
2023-05-30 20:25:22 +08:00
|
|
|
// if the server user is not set, it defaults to the current user
|
|
|
|
ServerUser string `ini:"server_user" json:"server_user"`
|
|
|
|
ServerName string `ini:"server_name" json:"server_name"`
|
|
|
|
BindAddr string `ini:"bind_addr" json:"bind_addr"`
|
2023-05-30 10:55:00 +08:00
|
|
|
// BindPort is the port that visitor listens on.
|
|
|
|
// It can be less than 0, it means don't bind to the port and only receive connections redirected from
|
|
|
|
// other visitors. (This is not supported for SUDP now)
|
|
|
|
BindPort int `ini:"bind_port" json:"bind_port"`
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Base
|
2023-05-30 20:25:22 +08:00
|
|
|
func (cfg *BaseVisitorConf) GetBaseConfig() *BaseVisitorConf {
|
2018-06-25 18:22:35 +08:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func (cfg *BaseVisitorConf) unmarshalFromIni(_ string, name string, _ *ini.Section) error {
|
2021-01-26 11:31:08 +08:00
|
|
|
// Custom decoration after basic unmarshal:
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg.ProxyName = name
|
2018-06-25 18:22:35 +08:00
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// bind_addr
|
|
|
|
if cfg.BindAddr == "" {
|
2018-06-25 18:22:35 +08:00
|
|
|
cfg.BindAddr = "127.0.0.1"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
|
|
|
|
err := section.MapTo(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-30 20:25:22 +08:00
|
|
|
err = cfg.GetBaseConfig().unmarshalFromIni(prefix, name, section)
|
2021-01-26 11:31:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type SUDPVisitorConf struct {
|
|
|
|
BaseVisitorConf `ini:",extends"`
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
|
|
|
|
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
2020-04-22 21:37:45 +08:00
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic unmarshal, if exists
|
|
|
|
|
2020-04-22 21:37:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type STCPVisitorConf struct {
|
|
|
|
BaseVisitorConf `ini:",extends"`
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
|
|
|
|
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
2018-06-25 18:22:35 +08:00
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic unmarshal, if exists
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type XTCPVisitorConf struct {
|
|
|
|
BaseVisitorConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
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"`
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
|
|
|
|
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
|
|
|
|
if err != nil {
|
2018-06-25 18:22:35 +08:00
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic unmarshal, if exists
|
2023-05-28 16:50:43 +08:00
|
|
|
if cfg.Protocol == "" {
|
|
|
|
cfg.Protocol = "quic"
|
|
|
|
}
|
|
|
|
if cfg.MaxRetriesAnHour <= 0 {
|
|
|
|
cfg.MaxRetriesAnHour = 8
|
|
|
|
}
|
|
|
|
if cfg.MinRetryInterval <= 0 {
|
|
|
|
cfg.MinRetryInterval = 90
|
|
|
|
}
|
2023-05-30 10:55:00 +08:00
|
|
|
if cfg.FallbackTimeoutMs <= 0 {
|
|
|
|
cfg.FallbackTimeoutMs = 1000
|
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
// Visitor loaded from ini
|
|
|
|
func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) {
|
|
|
|
// section.Key: if key not exists, section will set it with default value.
|
2023-09-20 15:18:50 +08:00
|
|
|
visitorType := VisitorType(section.Key("type").String())
|
2023-09-06 10:18:02 +08:00
|
|
|
|
|
|
|
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)
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return nil, fmt.Errorf("type [%s] error", visitorType)
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
return conf, nil
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|