visitor.go 5.3 KB

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