"Fossies" - the Fresh Open Source Software Archive

Member "frp-0.36.2/pkg/config/visitor.go" (22 Mar 2021, 6512 Bytes) of package /linux/misc/frp-0.36.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "visitor.go": 0.36.1_vs_0.36.2.

    1 // Copyright 2018 fatedier, fatedier@gmail.com
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 //
    7 //     http://www.apache.org/licenses/LICENSE-2.0
    8 //
    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 config
   16 
   17 import (
   18     "fmt"
   19     "reflect"
   20 
   21     "github.com/fatedier/frp/pkg/consts"
   22 
   23     "gopkg.in/ini.v1"
   24 )
   25 
   26 // Visitor
   27 var (
   28     visitorConfTypeMap = map[string]reflect.Type{
   29         consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}),
   30         consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}),
   31         consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}),
   32     }
   33 )
   34 
   35 type VisitorConf interface {
   36     GetBaseInfo() *BaseVisitorConf
   37     Compare(cmp VisitorConf) bool
   38     UnmarshalFromIni(prefix string, name string, section *ini.Section) error
   39     Check() error
   40 }
   41 
   42 type BaseVisitorConf struct {
   43     ProxyName      string `ini:"name" json:"name"`
   44     ProxyType      string `ini:"type" json:"type"`
   45     UseEncryption  bool   `ini:"use_encryption" json:"use_encryption"`
   46     UseCompression bool   `ini:"use_compression" json:"use_compression"`
   47     Role           string `ini:"role" json:"role"`
   48     Sk             string `ini:"sk" json:"sk"`
   49     ServerName     string `ini:"server_name" json:"server_name"`
   50     BindAddr       string `ini:"bind_addr" json:"bind_addr"`
   51     BindPort       int    `ini:"bind_port" json:"bind_port"`
   52 }
   53 
   54 type SUDPVisitorConf struct {
   55     BaseVisitorConf `ini:",extends"`
   56 }
   57 
   58 type STCPVisitorConf struct {
   59     BaseVisitorConf `ini:",extends"`
   60 }
   61 
   62 type XTCPVisitorConf struct {
   63     BaseVisitorConf `ini:",extends"`
   64 }
   65 
   66 // DefaultVisitorConf creates a empty VisitorConf object by visitorType.
   67 // If visitorType doesn't exist, return nil.
   68 func DefaultVisitorConf(visitorType string) VisitorConf {
   69     v, ok := visitorConfTypeMap[visitorType]
   70     if !ok {
   71         return nil
   72     }
   73 
   74     return reflect.New(v).Interface().(VisitorConf)
   75 }
   76 
   77 // Visitor loaded from ini
   78 func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) {
   79     // section.Key: if key not exists, section will set it with default value.
   80     visitorType := section.Key("type").String()
   81 
   82     if visitorType == "" {
   83         return nil, fmt.Errorf("visitor [%s] type shouldn't be empty", name)
   84     }
   85 
   86     conf := DefaultVisitorConf(visitorType)
   87     if conf == nil {
   88         return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
   89     }
   90 
   91     if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
   92         return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
   93     }
   94 
   95     if err := conf.Check(); err != nil {
   96         return nil, err
   97     }
   98 
   99     return conf, nil
  100 }
  101 
  102 // Base
  103 func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf {
  104     return cfg
  105 }
  106 
  107 func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool {
  108     if cfg.ProxyName != cmp.ProxyName ||
  109         cfg.ProxyType != cmp.ProxyType ||
  110         cfg.UseEncryption != cmp.UseEncryption ||
  111         cfg.UseCompression != cmp.UseCompression ||
  112         cfg.Role != cmp.Role ||
  113         cfg.Sk != cmp.Sk ||
  114         cfg.ServerName != cmp.ServerName ||
  115         cfg.BindAddr != cmp.BindAddr ||
  116         cfg.BindPort != cmp.BindPort {
  117         return false
  118     }
  119     return true
  120 }
  121 
  122 func (cfg *BaseVisitorConf) check() (err error) {
  123     if cfg.Role != "visitor" {
  124         err = fmt.Errorf("invalid role")
  125         return
  126     }
  127     if cfg.BindAddr == "" {
  128         err = fmt.Errorf("bind_addr shouldn't be empty")
  129         return
  130     }
  131     if cfg.BindPort <= 0 {
  132         err = fmt.Errorf("bind_port is required")
  133         return
  134     }
  135     return
  136 }
  137 
  138 func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section *ini.Section) error {
  139 
  140     // Custom decoration after basic unmarshal:
  141     // proxy name
  142     cfg.ProxyName = prefix + name
  143 
  144     // server_name
  145     cfg.ServerName = prefix + cfg.ServerName
  146 
  147     // bind_addr
  148     if cfg.BindAddr == "" {
  149         cfg.BindAddr = "127.0.0.1"
  150     }
  151 
  152     return nil
  153 }
  154 
  155 func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
  156     err := section.MapTo(cfg)
  157     if err != nil {
  158         return err
  159     }
  160 
  161     err = cfg.GetBaseInfo().unmarshalFromIni(prefix, name, section)
  162     if err != nil {
  163         return err
  164     }
  165 
  166     return nil
  167 }
  168 
  169 // SUDP
  170 var _ VisitorConf = &SUDPVisitorConf{}
  171 
  172 func (cfg *SUDPVisitorConf) Compare(cmp VisitorConf) bool {
  173     cmpConf, ok := cmp.(*SUDPVisitorConf)
  174     if !ok {
  175         return false
  176     }
  177 
  178     if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  179         return false
  180     }
  181 
  182     // Add custom login equal, if exists
  183 
  184     return true
  185 }
  186 
  187 func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  188     err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  189     if err != nil {
  190         return
  191     }
  192 
  193     // Add custom logic unmarshal, if exists
  194 
  195     return
  196 }
  197 
  198 func (cfg *SUDPVisitorConf) Check() (err error) {
  199     if err = cfg.BaseVisitorConf.check(); err != nil {
  200         return
  201     }
  202 
  203     // Add custom logic validate, if exists
  204 
  205     return
  206 }
  207 
  208 // STCP
  209 var _ VisitorConf = &STCPVisitorConf{}
  210 
  211 func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool {
  212     cmpConf, ok := cmp.(*STCPVisitorConf)
  213     if !ok {
  214         return false
  215     }
  216 
  217     if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  218         return false
  219     }
  220 
  221     // Add custom login equal, if exists
  222 
  223     return true
  224 }
  225 
  226 func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  227     err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  228     if err != nil {
  229         return
  230     }
  231 
  232     // Add custom logic unmarshal, if exists
  233 
  234     return
  235 }
  236 
  237 func (cfg *STCPVisitorConf) Check() (err error) {
  238     if err = cfg.BaseVisitorConf.check(); err != nil {
  239         return
  240     }
  241 
  242     // Add custom logic validate, if exists
  243 
  244     return
  245 }
  246 
  247 // XTCP
  248 var _ VisitorConf = &XTCPVisitorConf{}
  249 
  250 func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool {
  251     cmpConf, ok := cmp.(*XTCPVisitorConf)
  252     if !ok {
  253         return false
  254     }
  255 
  256     if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  257         return false
  258     }
  259 
  260     // Add custom login equal, if exists
  261 
  262     return true
  263 }
  264 
  265 func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  266     err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  267     if err != nil {
  268         return
  269     }
  270 
  271     // Add custom logic unmarshal, if exists
  272 
  273     return
  274 }
  275 
  276 func (cfg *XTCPVisitorConf) Check() (err error) {
  277     if err = cfg.BaseVisitorConf.check(); err != nil {
  278         return
  279     }
  280 
  281     // Add custom logic validate, if exists
  282 
  283     return
  284 }