mirror of https://github.com/fatedier/frp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
5.1 KiB
180 lines
5.1 KiB
1 year ago
|
// Copyright 2023 The frp Authors
|
||
7 years ago
|
//
|
||
|
// 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.
|
||
|
|
||
1 year ago
|
package legacy
|
||
7 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
|
||
4 years ago
|
"gopkg.in/ini.v1"
|
||
2 years ago
|
|
||
|
"github.com/fatedier/frp/pkg/consts"
|
||
7 years ago
|
)
|
||
|
|
||
4 years ago
|
// Visitor
|
||
7 years ago
|
var (
|
||
4 years ago
|
visitorConfTypeMap = map[string]reflect.Type{
|
||
|
consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}),
|
||
|
consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}),
|
||
|
consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}),
|
||
|
}
|
||
7 years ago
|
)
|
||
|
|
||
|
type VisitorConf interface {
|
||
2 years ago
|
// GetBaseConfig returns the base config of visitor.
|
||
|
GetBaseConfig() *BaseVisitorConf
|
||
|
// UnmarshalFromIni unmarshals config from ini.
|
||
4 years ago
|
UnmarshalFromIni(prefix string, name string, section *ini.Section) error
|
||
1 year ago
|
}
|
||
|
|
||
|
// DefaultVisitorConf creates a empty VisitorConf object by visitorType.
|
||
|
// If visitorType doesn't exist, return nil.
|
||
|
func DefaultVisitorConf(visitorType string) VisitorConf {
|
||
|
v, ok := visitorConfTypeMap[visitorType]
|
||
|
if !ok {
|
||
|
return nil
|
||
|
}
|
||
|
return reflect.New(v).Interface().(VisitorConf)
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
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"`
|
||
2 years ago
|
// 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"`
|
||
2 years ago
|
// 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"`
|
||
4 years ago
|
}
|
||
|
|
||
|
// Base
|
||
2 years ago
|
func (cfg *BaseVisitorConf) GetBaseConfig() *BaseVisitorConf {
|
||
7 years ago
|
return cfg
|
||
|
}
|
||
|
|
||
1 year ago
|
func (cfg *BaseVisitorConf) unmarshalFromIni(_ string, name string, _ *ini.Section) error {
|
||
4 years ago
|
// Custom decoration after basic unmarshal:
|
||
1 year ago
|
cfg.ProxyName = name
|
||
7 years ago
|
|
||
4 years ago
|
// bind_addr
|
||
|
if cfg.BindAddr == "" {
|
||
7 years ago
|
cfg.BindAddr = "127.0.0.1"
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
|
||
|
err := section.MapTo(cfg)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
2 years ago
|
err = cfg.GetBaseConfig().unmarshalFromIni(prefix, name, section)
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
5 years ago
|
}
|
||
|
|
||
1 year ago
|
type SUDPVisitorConf struct {
|
||
|
BaseVisitorConf `ini:",extends"`
|
||
|
}
|
||
4 years ago
|
|
||
|
func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
|
||
|
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
|
||
|
if err != nil {
|
||
5 years ago
|
return
|
||
|
}
|
||
4 years ago
|
|
||
|
// Add custom logic unmarshal, if exists
|
||
|
|
||
5 years ago
|
return
|
||
|
}
|
||
|
|
||
1 year ago
|
type STCPVisitorConf struct {
|
||
|
BaseVisitorConf `ini:",extends"`
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
|
||
|
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
|
||
|
if err != nil {
|
||
7 years ago
|
return
|
||
|
}
|
||
4 years ago
|
|
||
|
// Add custom logic unmarshal, if exists
|
||
|
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
1 year ago
|
type XTCPVisitorConf struct {
|
||
|
BaseVisitorConf `ini:",extends"`
|
||
4 years ago
|
|
||
1 year ago
|
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"`
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
|
||
|
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
|
||
|
if err != nil {
|
||
7 years ago
|
return
|
||
|
}
|
||
4 years ago
|
|
||
|
// Add custom logic unmarshal, if exists
|
||
2 years ago
|
if cfg.Protocol == "" {
|
||
|
cfg.Protocol = "quic"
|
||
|
}
|
||
|
if cfg.MaxRetriesAnHour <= 0 {
|
||
|
cfg.MaxRetriesAnHour = 8
|
||
|
}
|
||
|
if cfg.MinRetryInterval <= 0 {
|
||
|
cfg.MinRetryInterval = 90
|
||
|
}
|
||
2 years ago
|
if cfg.FallbackTimeoutMs <= 0 {
|
||
|
cfg.FallbackTimeoutMs = 1000
|
||
|
}
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
1 year ago
|
// 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.
|
||
|
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)
|
||
7 years ago
|
}
|
||
4 years ago
|
|
||
1 year ago
|
if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
|
||
|
return nil, fmt.Errorf("type [%s] error", visitorType)
|
||
2 years ago
|
}
|
||
1 year ago
|
return conf, nil
|
||
7 years ago
|
}
|