config: inline is NOT SUPPORTED in encoding/json

This commit is contained in:
bobo liu 2021-03-16 11:21:13 +08:00
parent 7e258b7820
commit fb3d834a70
4 changed files with 1350 additions and 1350 deletions

View File

@ -29,7 +29,7 @@ import (
// recommended to use GetDefaultClientConf instead of creating this object // recommended to use GetDefaultClientConf instead of creating this object
// directly, so that all unspecified fields have reasonable default values. // directly, so that all unspecified fields have reasonable default values.
type ClientCommonConf struct { type ClientCommonConf struct {
auth.ClientConfig `ini:",extends" json:"inline"` auth.ClientConfig `ini:",extends"`
// ServerAddr specifies the address of the server to connect to. By // ServerAddr specifies the address of the server to connect to. By
// default, this value is "0.0.0.0". // default, this value is "0.0.0.0".

File diff suppressed because it is too large Load Diff

View File

@ -29,7 +29,7 @@ import (
// recommended to use GetDefaultServerConf instead of creating this object // recommended to use GetDefaultServerConf instead of creating this object
// directly, so that all unspecified fields have reasonable default values. // directly, so that all unspecified fields have reasonable default values.
type ServerCommonConf struct { type ServerCommonConf struct {
auth.ServerConfig `ini:",extends" json:"inline"` auth.ServerConfig `ini:",extends"`
// BindAddr specifies the address that the server binds to. By default, // BindAddr specifies the address that the server binds to. By default,
// this value is "0.0.0.0". // this value is "0.0.0.0".

View File

@ -1,284 +1,284 @@
// Copyright 2018 fatedier, fatedier@gmail.com // Copyright 2018 fatedier, fatedier@gmail.com
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
// //
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package config package config
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"github.com/fatedier/frp/pkg/consts" "github.com/fatedier/frp/pkg/consts"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
// Visitor // Visitor
var ( var (
visitorConfTypeMap = map[string]reflect.Type{ visitorConfTypeMap = map[string]reflect.Type{
consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}), consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}),
consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}), consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}),
consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}), consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}),
} }
) )
type VisitorConf interface { type VisitorConf interface {
GetBaseInfo() *BaseVisitorConf GetBaseInfo() *BaseVisitorConf
Compare(cmp VisitorConf) bool Compare(cmp VisitorConf) bool
UnmarshalFromIni(prefix string, name string, section *ini.Section) error UnmarshalFromIni(prefix string, name string, section *ini.Section) error
Check() error Check() error
} }
type BaseVisitorConf struct { type BaseVisitorConf struct {
ProxyName string `ini:"name" json:"name"` ProxyName string `ini:"name" json:"name"`
ProxyType string `ini:"type" json:"type"` ProxyType string `ini:"type" json:"type"`
UseEncryption bool `ini:"use_encryption" json:"use_encryption"` UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
UseCompression bool `ini:"use_compression" json:"use_compression"` UseCompression bool `ini:"use_compression" json:"use_compression"`
Role string `ini:"role" json:"role"` Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"` Sk string `ini:"sk" json:"sk"`
ServerName string `ini:"server_name" json:"server_name"` ServerName string `ini:"server_name" json:"server_name"`
BindAddr string `ini:"bind_addr" json:"bind_addr"` BindAddr string `ini:"bind_addr" json:"bind_addr"`
BindPort int `ini:"bind_port" json:"bind_port"` BindPort int `ini:"bind_port" json:"bind_port"`
} }
type SUDPVisitorConf struct { type SUDPVisitorConf struct {
BaseVisitorConf `ini:",extends" json:"inline"` BaseVisitorConf `ini:",extends"`
} }
type STCPVisitorConf struct { type STCPVisitorConf struct {
BaseVisitorConf `ini:",extends" json:"inline"` BaseVisitorConf `ini:",extends"`
} }
type XTCPVisitorConf struct { type XTCPVisitorConf struct {
BaseVisitorConf `ini:",extends" json:"inline"` BaseVisitorConf `ini:",extends"`
} }
// DefaultVisitorConf creates a empty VisitorConf object by visitorType. // DefaultVisitorConf creates a empty VisitorConf object by visitorType.
// If visitorType doesn't exist, return nil. // If visitorType doesn't exist, return nil.
func DefaultVisitorConf(visitorType string) VisitorConf { func DefaultVisitorConf(visitorType string) VisitorConf {
v, ok := visitorConfTypeMap[visitorType] v, ok := visitorConfTypeMap[visitorType]
if !ok { if !ok {
return nil return nil
} }
return reflect.New(v).Interface().(VisitorConf) return reflect.New(v).Interface().(VisitorConf)
} }
// Visitor loaded from ini // Visitor loaded from ini
func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) { func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) {
// section.Key: if key not exists, section will set it with default value. // section.Key: if key not exists, section will set it with default value.
visitorType := section.Key("type").String() visitorType := section.Key("type").String()
if visitorType == "" { if visitorType == "" {
return nil, fmt.Errorf("visitor [%s] type shouldn't be empty", name) return nil, fmt.Errorf("visitor [%s] type shouldn't be empty", name)
} }
conf := DefaultVisitorConf(visitorType) conf := DefaultVisitorConf(visitorType)
if conf == nil { if conf == nil {
return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType) return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
} }
if err := conf.UnmarshalFromIni(prefix, name, section); err != nil { if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType) return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
} }
if err := conf.Check(); err != nil { if err := conf.Check(); err != nil {
return nil, err return nil, err
} }
return conf, nil return conf, nil
} }
// Base // Base
func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf { func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf {
return cfg return cfg
} }
func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool { func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool {
if cfg.ProxyName != cmp.ProxyName || if cfg.ProxyName != cmp.ProxyName ||
cfg.ProxyType != cmp.ProxyType || cfg.ProxyType != cmp.ProxyType ||
cfg.UseEncryption != cmp.UseEncryption || cfg.UseEncryption != cmp.UseEncryption ||
cfg.UseCompression != cmp.UseCompression || cfg.UseCompression != cmp.UseCompression ||
cfg.Role != cmp.Role || cfg.Role != cmp.Role ||
cfg.Sk != cmp.Sk || cfg.Sk != cmp.Sk ||
cfg.ServerName != cmp.ServerName || cfg.ServerName != cmp.ServerName ||
cfg.BindAddr != cmp.BindAddr || cfg.BindAddr != cmp.BindAddr ||
cfg.BindPort != cmp.BindPort { cfg.BindPort != cmp.BindPort {
return false return false
} }
return true return true
} }
func (cfg *BaseVisitorConf) check() (err error) { func (cfg *BaseVisitorConf) check() (err error) {
if cfg.Role != "visitor" { if cfg.Role != "visitor" {
err = fmt.Errorf("invalid role") err = fmt.Errorf("invalid role")
return return
} }
if cfg.BindAddr == "" { if cfg.BindAddr == "" {
err = fmt.Errorf("bind_addr shouldn't be empty") err = fmt.Errorf("bind_addr shouldn't be empty")
return return
} }
if cfg.BindPort <= 0 { if cfg.BindPort <= 0 {
err = fmt.Errorf("bind_port is required") err = fmt.Errorf("bind_port is required")
return return
} }
return return
} }
func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section *ini.Section) error {
// Custom decoration after basic unmarshal: // Custom decoration after basic unmarshal:
// proxy name // proxy name
cfg.ProxyName = prefix + name cfg.ProxyName = prefix + name
// server_name // server_name
cfg.ServerName = prefix + cfg.ServerName cfg.ServerName = prefix + cfg.ServerName
// bind_addr // bind_addr
if cfg.BindAddr == "" { if cfg.BindAddr == "" {
cfg.BindAddr = "127.0.0.1" cfg.BindAddr = "127.0.0.1"
} }
return nil return nil
} }
func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error { func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
err := section.MapTo(cfg) err := section.MapTo(cfg)
if err != nil { if err != nil {
return err return err
} }
err = cfg.GetBaseInfo().unmarshalFromIni(prefix, name, section) err = cfg.GetBaseInfo().unmarshalFromIni(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
// SUDP // SUDP
var _ VisitorConf = &SUDPVisitorConf{} var _ VisitorConf = &SUDPVisitorConf{}
func (cfg *SUDPVisitorConf) Compare(cmp VisitorConf) bool { func (cfg *SUDPVisitorConf) Compare(cmp VisitorConf) bool {
cmpConf, ok := cmp.(*SUDPVisitorConf) cmpConf, ok := cmp.(*SUDPVisitorConf)
if !ok { if !ok {
return false return false
} }
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) { if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
return false return false
} }
// Add custom login equal, if exists // Add custom login equal, if exists
return true return true
} }
func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
return return
} }
// Add custom logic unmarshal, if exists // Add custom logic unmarshal, if exists
return return
} }
func (cfg *SUDPVisitorConf) Check() (err error) { func (cfg *SUDPVisitorConf) Check() (err error) {
if err = cfg.BaseVisitorConf.check(); err != nil { if err = cfg.BaseVisitorConf.check(); err != nil {
return return
} }
// Add custom logic validate, if exists // Add custom logic validate, if exists
return return
} }
// STCP // STCP
var _ VisitorConf = &STCPVisitorConf{} var _ VisitorConf = &STCPVisitorConf{}
func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool { func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool {
cmpConf, ok := cmp.(*STCPVisitorConf) cmpConf, ok := cmp.(*STCPVisitorConf)
if !ok { if !ok {
return false return false
} }
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) { if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
return false return false
} }
// Add custom login equal, if exists // Add custom login equal, if exists
return true return true
} }
func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
return return
} }
// Add custom logic unmarshal, if exists // Add custom logic unmarshal, if exists
return return
} }
func (cfg *STCPVisitorConf) Check() (err error) { func (cfg *STCPVisitorConf) Check() (err error) {
if err = cfg.BaseVisitorConf.check(); err != nil { if err = cfg.BaseVisitorConf.check(); err != nil {
return return
} }
// Add custom logic validate, if exists // Add custom logic validate, if exists
return return
} }
// XTCP // XTCP
var _ VisitorConf = &XTCPVisitorConf{} var _ VisitorConf = &XTCPVisitorConf{}
func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool { func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool {
cmpConf, ok := cmp.(*XTCPVisitorConf) cmpConf, ok := cmp.(*XTCPVisitorConf)
if !ok { if !ok {
return false return false
} }
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) { if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
return false return false
} }
// Add custom login equal, if exists // Add custom login equal, if exists
return true return true
} }
func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
return return
} }
// Add custom logic unmarshal, if exists // Add custom logic unmarshal, if exists
return return
} }
func (cfg *XTCPVisitorConf) Check() (err error) { func (cfg *XTCPVisitorConf) Check() (err error) {
if err = cfg.BaseVisitorConf.check(); err != nil { if err = cfg.BaseVisitorConf.check(); err != nil {
return return
} }
// Add custom logic validate, if exists // Add custom logic validate, if exists
return return
} }