1
0

visitor.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. package config
  15. import (
  16. "fmt"
  17. "reflect"
  18. "github.com/samber/lo"
  19. "gopkg.in/ini.v1"
  20. "github.com/fatedier/frp/pkg/consts"
  21. )
  22. // Visitor
  23. var (
  24. visitorConfTypeMap = map[string]reflect.Type{
  25. consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}),
  26. consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}),
  27. consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}),
  28. }
  29. )
  30. type VisitorConf interface {
  31. // GetBaseConfig returns the base config of visitor.
  32. GetBaseConfig() *BaseVisitorConf
  33. // UnmarshalFromIni unmarshals config from ini.
  34. UnmarshalFromIni(prefix string, name string, section *ini.Section) error
  35. // Validate validates config.
  36. Validate() error
  37. }
  38. type BaseVisitorConf struct {
  39. ProxyName string `ini:"name" json:"name"`
  40. ProxyType string `ini:"type" json:"type"`
  41. UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
  42. UseCompression bool `ini:"use_compression" json:"use_compression"`
  43. Role string `ini:"role" json:"role"`
  44. Sk string `ini:"sk" json:"sk"`
  45. // if the server user is not set, it defaults to the current user
  46. ServerUser string `ini:"server_user" json:"server_user"`
  47. ServerName string `ini:"server_name" json:"server_name"`
  48. BindAddr string `ini:"bind_addr" json:"bind_addr"`
  49. // BindPort is the port that visitor listens on.
  50. // It can be less than 0, it means don't bind to the port and only receive connections redirected from
  51. // other visitors. (This is not supported for SUDP now)
  52. BindPort int `ini:"bind_port" json:"bind_port"`
  53. }
  54. type SUDPVisitorConf struct {
  55. BaseVisitorConf `ini:",extends"`
  56. }
  57. type STCPVisitorConf struct {
  58. BaseVisitorConf `ini:",extends"`
  59. }
  60. type XTCPVisitorConf struct {
  61. BaseVisitorConf `ini:",extends"`
  62. Protocol string `ini:"protocol" json:"protocol,omitempty"`
  63. KeepTunnelOpen bool `ini:"keep_tunnel_open" json:"keep_tunnel_open,omitempty"`
  64. MaxRetriesAnHour int `ini:"max_retries_an_hour" json:"max_retries_an_hour,omitempty"`
  65. MinRetryInterval int `ini:"min_retry_interval" json:"min_retry_interval,omitempty"`
  66. FallbackTo string `ini:"fallback_to" json:"fallback_to,omitempty"`
  67. FallbackTimeoutMs int `ini:"fallback_timeout_ms" json:"fallback_timeout_ms,omitempty"`
  68. }
  69. // DefaultVisitorConf creates a empty VisitorConf object by visitorType.
  70. // If visitorType doesn't exist, return nil.
  71. func DefaultVisitorConf(visitorType string) VisitorConf {
  72. v, ok := visitorConfTypeMap[visitorType]
  73. if !ok {
  74. return nil
  75. }
  76. return reflect.New(v).Interface().(VisitorConf)
  77. }
  78. // Visitor loaded from ini
  79. func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) {
  80. // section.Key: if key not exists, section will set it with default value.
  81. visitorType := section.Key("type").String()
  82. if visitorType == "" {
  83. return nil, fmt.Errorf("type shouldn't be empty")
  84. }
  85. conf := DefaultVisitorConf(visitorType)
  86. if conf == nil {
  87. return nil, fmt.Errorf("type [%s] error", visitorType)
  88. }
  89. if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
  90. return nil, fmt.Errorf("type [%s] error", visitorType)
  91. }
  92. if err := conf.Validate(); err != nil {
  93. return nil, err
  94. }
  95. return conf, nil
  96. }
  97. // Base
  98. func (cfg *BaseVisitorConf) GetBaseConfig() *BaseVisitorConf {
  99. return cfg
  100. }
  101. func (cfg *BaseVisitorConf) validate() (err error) {
  102. if cfg.Role != "visitor" {
  103. err = fmt.Errorf("invalid role")
  104. return
  105. }
  106. if cfg.BindAddr == "" {
  107. err = fmt.Errorf("bind_addr shouldn't be empty")
  108. return
  109. }
  110. // BindPort can be less than 0, it means don't bind to the port and only receive connections redirected from
  111. // other visitors
  112. if cfg.BindPort == 0 {
  113. err = fmt.Errorf("bind_port is required")
  114. return
  115. }
  116. return
  117. }
  118. func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section *ini.Section) error {
  119. _ = section
  120. // Custom decoration after basic unmarshal:
  121. // proxy name
  122. cfg.ProxyName = prefix + name
  123. // server_name
  124. if cfg.ServerUser == "" {
  125. cfg.ServerName = prefix + cfg.ServerName
  126. } else {
  127. cfg.ServerName = cfg.ServerUser + "." + cfg.ServerName
  128. }
  129. // bind_addr
  130. if cfg.BindAddr == "" {
  131. cfg.BindAddr = "127.0.0.1"
  132. }
  133. return nil
  134. }
  135. func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
  136. err := section.MapTo(cfg)
  137. if err != nil {
  138. return err
  139. }
  140. err = cfg.GetBaseConfig().unmarshalFromIni(prefix, name, section)
  141. if err != nil {
  142. return err
  143. }
  144. return nil
  145. }
  146. // SUDP
  147. var _ VisitorConf = &SUDPVisitorConf{}
  148. func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  149. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  150. if err != nil {
  151. return
  152. }
  153. // Add custom logic unmarshal, if exists
  154. return
  155. }
  156. func (cfg *SUDPVisitorConf) Validate() (err error) {
  157. if err = cfg.BaseVisitorConf.validate(); err != nil {
  158. return
  159. }
  160. // Add custom logic validate, if exists
  161. return
  162. }
  163. // STCP
  164. var _ VisitorConf = &STCPVisitorConf{}
  165. func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  166. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  167. if err != nil {
  168. return
  169. }
  170. // Add custom logic unmarshal, if exists
  171. return
  172. }
  173. func (cfg *STCPVisitorConf) Validate() (err error) {
  174. if err = cfg.BaseVisitorConf.validate(); err != nil {
  175. return
  176. }
  177. // Add custom logic validate, if exists
  178. return
  179. }
  180. // XTCP
  181. var _ VisitorConf = &XTCPVisitorConf{}
  182. func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  183. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  184. if err != nil {
  185. return
  186. }
  187. // Add custom logic unmarshal, if exists
  188. if cfg.Protocol == "" {
  189. cfg.Protocol = "quic"
  190. }
  191. if cfg.MaxRetriesAnHour <= 0 {
  192. cfg.MaxRetriesAnHour = 8
  193. }
  194. if cfg.MinRetryInterval <= 0 {
  195. cfg.MinRetryInterval = 90
  196. }
  197. if cfg.FallbackTimeoutMs <= 0 {
  198. cfg.FallbackTimeoutMs = 1000
  199. }
  200. return
  201. }
  202. func (cfg *XTCPVisitorConf) Validate() (err error) {
  203. if err = cfg.BaseVisitorConf.validate(); err != nil {
  204. return
  205. }
  206. // Add custom logic validate, if exists
  207. if !lo.Contains([]string{"", "kcp", "quic"}, cfg.Protocol) {
  208. return fmt.Errorf("protocol should be 'kcp' or 'quic'")
  209. }
  210. return
  211. }