2018-06-25 18:22:35 +08:00
|
|
|
// Copyright 2018 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
"gopkg.in/ini.v1"
|
2022-08-29 01:02:53 +08:00
|
|
|
|
|
|
|
"github.com/fatedier/frp/pkg/consts"
|
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 (
|
2021-01-26 11:31:08 +08:00
|
|
|
visitorConfTypeMap = map[string]reflect.Type{
|
|
|
|
consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}),
|
|
|
|
consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}),
|
|
|
|
consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}),
|
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type VisitorConf interface {
|
|
|
|
GetBaseInfo() *BaseVisitorConf
|
|
|
|
Compare(cmp VisitorConf) bool
|
2021-01-26 11:31:08 +08:00
|
|
|
UnmarshalFromIni(prefix string, name string, section *ini.Section) error
|
2018-06-25 18:22:35 +08:00
|
|
|
Check() error
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
ServerName string `ini:"server_name" json:"server_name"`
|
|
|
|
BindAddr string `ini:"bind_addr" json:"bind_addr"`
|
|
|
|
BindPort int `ini:"bind_port" json:"bind_port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SUDPVisitorConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseVisitorConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type STCPVisitorConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseVisitorConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type XTCPVisitorConf struct {
|
2021-03-19 17:36:39 +08:00
|
|
|
BaseVisitorConf `ini:",extends"`
|
2021-01-26 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultVisitorConf creates a empty VisitorConf object by visitorType.
|
|
|
|
// If visitorType doesn't exist, return nil.
|
|
|
|
func DefaultVisitorConf(visitorType string) VisitorConf {
|
|
|
|
v, ok := visitorConfTypeMap[visitorType]
|
2018-06-25 18:22:35 +08:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
return reflect.New(v).Interface().(VisitorConf)
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +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.
|
|
|
|
visitorType := section.Key("type").String()
|
|
|
|
|
|
|
|
if visitorType == "" {
|
|
|
|
return nil, fmt.Errorf("visitor [%s] type shouldn't be empty", name)
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
conf := DefaultVisitorConf(visitorType)
|
|
|
|
if conf == nil {
|
|
|
|
return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
|
|
|
|
return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
if err := conf.Check(); err != nil {
|
|
|
|
return nil, err
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
return conf, nil
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// Base
|
2018-06-25 18:22:35 +08:00
|
|
|
func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf {
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool {
|
|
|
|
if cfg.ProxyName != cmp.ProxyName ||
|
|
|
|
cfg.ProxyType != cmp.ProxyType ||
|
|
|
|
cfg.UseEncryption != cmp.UseEncryption ||
|
|
|
|
cfg.UseCompression != cmp.UseCompression ||
|
|
|
|
cfg.Role != cmp.Role ||
|
|
|
|
cfg.Sk != cmp.Sk ||
|
|
|
|
cfg.ServerName != cmp.ServerName ||
|
|
|
|
cfg.BindAddr != cmp.BindAddr ||
|
|
|
|
cfg.BindPort != cmp.BindPort {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *BaseVisitorConf) check() (err error) {
|
|
|
|
if cfg.Role != "visitor" {
|
|
|
|
err = fmt.Errorf("invalid role")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cfg.BindAddr == "" {
|
|
|
|
err = fmt.Errorf("bind_addr shouldn't be empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cfg.BindPort <= 0 {
|
|
|
|
err = fmt.Errorf("bind_port is required")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section *ini.Section) error {
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = section
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Custom decoration after basic unmarshal:
|
|
|
|
// proxy name
|
2018-06-25 18:22:35 +08:00
|
|
|
cfg.ProxyName = prefix + name
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// server_name
|
|
|
|
cfg.ServerName = prefix + cfg.ServerName
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cfg.GetBaseInfo().unmarshalFromIni(prefix, name, section)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// SUDP
|
|
|
|
var _ VisitorConf = &SUDPVisitorConf{}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *SUDPVisitorConf) Compare(cmp VisitorConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*SUDPVisitorConf)
|
2020-04-22 21:37:45 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom login equal, if exists
|
|
|
|
|
2020-04-22 21:37:45 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *SUDPVisitorConf) Check() (err error) {
|
2020-04-22 21:37:45 +08:00
|
|
|
if err = cfg.BaseVisitorConf.check(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic validate, if exists
|
|
|
|
|
2020-04-22 21:37:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// STCP
|
|
|
|
var _ VisitorConf = &STCPVisitorConf{}
|
2018-06-25 18:22:35 +08:00
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*STCPVisitorConf)
|
2018-06-25 18:22:35 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom login equal, if exists
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *STCPVisitorConf) Check() (err error) {
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.BaseVisitorConf.check(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic validate, if exists
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
// XTCP
|
|
|
|
var _ VisitorConf = &XTCPVisitorConf{}
|
2018-06-25 18:22:35 +08:00
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool {
|
|
|
|
cmpConf, ok := cmp.(*XTCPVisitorConf)
|
2018-06-25 18:22:35 +08:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom login equal, if exists
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (cfg *XTCPVisitorConf) Check() (err error) {
|
2018-06-25 18:22:35 +08:00
|
|
|
if err = cfg.BaseVisitorConf.check(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 11:31:08 +08:00
|
|
|
|
|
|
|
// Add custom logic validate, if exists
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
return
|
|
|
|
}
|