visitor.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2023 The frp Authors
  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. package legacy
  15. import (
  16. "fmt"
  17. "reflect"
  18. "gopkg.in/ini.v1"
  19. "github.com/fatedier/frp/pkg/consts"
  20. )
  21. // Visitor
  22. var (
  23. visitorConfTypeMap = map[string]reflect.Type{
  24. consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}),
  25. consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}),
  26. consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}),
  27. }
  28. )
  29. type VisitorConf interface {
  30. // GetBaseConfig returns the base config of visitor.
  31. GetBaseConfig() *BaseVisitorConf
  32. // UnmarshalFromIni unmarshals config from ini.
  33. UnmarshalFromIni(prefix string, name string, section *ini.Section) error
  34. }
  35. // DefaultVisitorConf creates a empty VisitorConf object by visitorType.
  36. // If visitorType doesn't exist, return nil.
  37. func DefaultVisitorConf(visitorType string) VisitorConf {
  38. v, ok := visitorConfTypeMap[visitorType]
  39. if !ok {
  40. return nil
  41. }
  42. return reflect.New(v).Interface().(VisitorConf)
  43. }
  44. type BaseVisitorConf struct {
  45. ProxyName string `ini:"name" json:"name"`
  46. ProxyType string `ini:"type" json:"type"`
  47. UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
  48. UseCompression bool `ini:"use_compression" json:"use_compression"`
  49. Role string `ini:"role" json:"role"`
  50. Sk string `ini:"sk" json:"sk"`
  51. // if the server user is not set, it defaults to the current user
  52. ServerUser string `ini:"server_user" json:"server_user"`
  53. ServerName string `ini:"server_name" json:"server_name"`
  54. BindAddr string `ini:"bind_addr" json:"bind_addr"`
  55. // BindPort is the port that visitor listens on.
  56. // It can be less than 0, it means don't bind to the port and only receive connections redirected from
  57. // other visitors. (This is not supported for SUDP now)
  58. BindPort int `ini:"bind_port" json:"bind_port"`
  59. }
  60. // Base
  61. func (cfg *BaseVisitorConf) GetBaseConfig() *BaseVisitorConf {
  62. return cfg
  63. }
  64. func (cfg *BaseVisitorConf) unmarshalFromIni(_ string, name string, _ *ini.Section) error {
  65. // Custom decoration after basic unmarshal:
  66. cfg.ProxyName = name
  67. // bind_addr
  68. if cfg.BindAddr == "" {
  69. cfg.BindAddr = "127.0.0.1"
  70. }
  71. return nil
  72. }
  73. func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
  74. err := section.MapTo(cfg)
  75. if err != nil {
  76. return err
  77. }
  78. err = cfg.GetBaseConfig().unmarshalFromIni(prefix, name, section)
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. type SUDPVisitorConf struct {
  85. BaseVisitorConf `ini:",extends"`
  86. }
  87. func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  88. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  89. if err != nil {
  90. return
  91. }
  92. // Add custom logic unmarshal, if exists
  93. return
  94. }
  95. type STCPVisitorConf struct {
  96. BaseVisitorConf `ini:",extends"`
  97. }
  98. func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  99. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  100. if err != nil {
  101. return
  102. }
  103. // Add custom logic unmarshal, if exists
  104. return
  105. }
  106. type XTCPVisitorConf struct {
  107. BaseVisitorConf `ini:",extends"`
  108. Protocol string `ini:"protocol" json:"protocol,omitempty"`
  109. KeepTunnelOpen bool `ini:"keep_tunnel_open" json:"keep_tunnel_open,omitempty"`
  110. MaxRetriesAnHour int `ini:"max_retries_an_hour" json:"max_retries_an_hour,omitempty"`
  111. MinRetryInterval int `ini:"min_retry_interval" json:"min_retry_interval,omitempty"`
  112. FallbackTo string `ini:"fallback_to" json:"fallback_to,omitempty"`
  113. FallbackTimeoutMs int `ini:"fallback_timeout_ms" json:"fallback_timeout_ms,omitempty"`
  114. }
  115. func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  116. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  117. if err != nil {
  118. return
  119. }
  120. // Add custom logic unmarshal, if exists
  121. if cfg.Protocol == "" {
  122. cfg.Protocol = "quic"
  123. }
  124. if cfg.MaxRetriesAnHour <= 0 {
  125. cfg.MaxRetriesAnHour = 8
  126. }
  127. if cfg.MinRetryInterval <= 0 {
  128. cfg.MinRetryInterval = 90
  129. }
  130. if cfg.FallbackTimeoutMs <= 0 {
  131. cfg.FallbackTimeoutMs = 1000
  132. }
  133. return
  134. }
  135. // Visitor loaded from ini
  136. func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) {
  137. // section.Key: if key not exists, section will set it with default value.
  138. visitorType := section.Key("type").String()
  139. if visitorType == "" {
  140. return nil, fmt.Errorf("type shouldn't be empty")
  141. }
  142. conf := DefaultVisitorConf(visitorType)
  143. if conf == nil {
  144. return nil, fmt.Errorf("type [%s] error", visitorType)
  145. }
  146. if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
  147. return nil, fmt.Errorf("type [%s] error", visitorType)
  148. }
  149. return conf, nil
  150. }