visitor.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. GetBaseInfo() *BaseVisitorConf
  32. Compare(cmp VisitorConf) bool
  33. UnmarshalFromIni(prefix string, name string, section *ini.Section) error
  34. Check() error
  35. }
  36. type BaseVisitorConf struct {
  37. ProxyName string `ini:"name" json:"name"`
  38. ProxyType string `ini:"type" json:"type"`
  39. UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
  40. UseCompression bool `ini:"use_compression" json:"use_compression"`
  41. Role string `ini:"role" json:"role"`
  42. Sk string `ini:"sk" json:"sk"`
  43. ServerName string `ini:"server_name" json:"server_name"`
  44. BindAddr string `ini:"bind_addr" json:"bind_addr"`
  45. // BindPort is the port that visitor listens on.
  46. // It can be less than 0, it means don't bind to the port and only receive connections redirected from
  47. // other visitors. (This is not supported for SUDP now)
  48. BindPort int `ini:"bind_port" json:"bind_port"`
  49. }
  50. type SUDPVisitorConf struct {
  51. BaseVisitorConf `ini:",extends"`
  52. }
  53. type STCPVisitorConf struct {
  54. BaseVisitorConf `ini:",extends"`
  55. }
  56. type XTCPVisitorConf struct {
  57. BaseVisitorConf `ini:",extends"`
  58. Protocol string `ini:"protocol" json:"protocol,omitempty"`
  59. KeepTunnelOpen bool `ini:"keep_tunnel_open" json:"keep_tunnel_open,omitempty"`
  60. MaxRetriesAnHour int `ini:"max_retries_an_hour" json:"max_retries_an_hour,omitempty"`
  61. MinRetryInterval int `ini:"min_retry_interval" json:"min_retry_interval,omitempty"`
  62. FallbackTo string `ini:"fallback_to" json:"fallback_to,omitempty"`
  63. FallbackTimeoutMs int `ini:"fallback_timeout_ms" json:"fallback_timeout_ms,omitempty"`
  64. }
  65. // DefaultVisitorConf creates a empty VisitorConf object by visitorType.
  66. // If visitorType doesn't exist, return nil.
  67. func DefaultVisitorConf(visitorType string) VisitorConf {
  68. v, ok := visitorConfTypeMap[visitorType]
  69. if !ok {
  70. return nil
  71. }
  72. return reflect.New(v).Interface().(VisitorConf)
  73. }
  74. // Visitor loaded from ini
  75. func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) {
  76. // section.Key: if key not exists, section will set it with default value.
  77. visitorType := section.Key("type").String()
  78. if visitorType == "" {
  79. return nil, fmt.Errorf("visitor [%s] type shouldn't be empty", name)
  80. }
  81. conf := DefaultVisitorConf(visitorType)
  82. if conf == nil {
  83. return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
  84. }
  85. if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
  86. return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
  87. }
  88. if err := conf.Check(); err != nil {
  89. return nil, err
  90. }
  91. return conf, nil
  92. }
  93. // Base
  94. func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf {
  95. return cfg
  96. }
  97. func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool {
  98. if cfg.ProxyName != cmp.ProxyName ||
  99. cfg.ProxyType != cmp.ProxyType ||
  100. cfg.UseEncryption != cmp.UseEncryption ||
  101. cfg.UseCompression != cmp.UseCompression ||
  102. cfg.Role != cmp.Role ||
  103. cfg.Sk != cmp.Sk ||
  104. cfg.ServerName != cmp.ServerName ||
  105. cfg.BindAddr != cmp.BindAddr ||
  106. cfg.BindPort != cmp.BindPort {
  107. return false
  108. }
  109. return true
  110. }
  111. func (cfg *BaseVisitorConf) check() (err error) {
  112. if cfg.Role != "visitor" {
  113. err = fmt.Errorf("invalid role")
  114. return
  115. }
  116. if cfg.BindAddr == "" {
  117. err = fmt.Errorf("bind_addr shouldn't be empty")
  118. return
  119. }
  120. // BindPort can be less than 0, it means don't bind to the port and only receive connections redirected from
  121. // other visitors
  122. if cfg.BindPort == 0 {
  123. err = fmt.Errorf("bind_port is required")
  124. return
  125. }
  126. return
  127. }
  128. func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section *ini.Section) error {
  129. _ = section
  130. // Custom decoration after basic unmarshal:
  131. // proxy name
  132. cfg.ProxyName = prefix + name
  133. // server_name
  134. cfg.ServerName = prefix + cfg.ServerName
  135. // bind_addr
  136. if cfg.BindAddr == "" {
  137. cfg.BindAddr = "127.0.0.1"
  138. }
  139. return nil
  140. }
  141. func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
  142. err := section.MapTo(cfg)
  143. if err != nil {
  144. return err
  145. }
  146. err = cfg.GetBaseInfo().unmarshalFromIni(prefix, name, section)
  147. if err != nil {
  148. return err
  149. }
  150. return nil
  151. }
  152. // SUDP
  153. var _ VisitorConf = &SUDPVisitorConf{}
  154. func (cfg *SUDPVisitorConf) Compare(cmp VisitorConf) bool {
  155. cmpConf, ok := cmp.(*SUDPVisitorConf)
  156. if !ok {
  157. return false
  158. }
  159. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  160. return false
  161. }
  162. // Add custom login equal, if exists
  163. return true
  164. }
  165. func (cfg *SUDPVisitorConf) 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 *SUDPVisitorConf) Check() (err error) {
  174. if err = cfg.BaseVisitorConf.check(); err != nil {
  175. return
  176. }
  177. // Add custom logic validate, if exists
  178. return
  179. }
  180. // STCP
  181. var _ VisitorConf = &STCPVisitorConf{}
  182. func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool {
  183. cmpConf, ok := cmp.(*STCPVisitorConf)
  184. if !ok {
  185. return false
  186. }
  187. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  188. return false
  189. }
  190. // Add custom login equal, if exists
  191. return true
  192. }
  193. func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  194. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  195. if err != nil {
  196. return
  197. }
  198. // Add custom logic unmarshal, if exists
  199. return
  200. }
  201. func (cfg *STCPVisitorConf) Check() (err error) {
  202. if err = cfg.BaseVisitorConf.check(); err != nil {
  203. return
  204. }
  205. // Add custom logic validate, if exists
  206. return
  207. }
  208. // XTCP
  209. var _ VisitorConf = &XTCPVisitorConf{}
  210. func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool {
  211. cmpConf, ok := cmp.(*XTCPVisitorConf)
  212. if !ok {
  213. return false
  214. }
  215. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  216. return false
  217. }
  218. // Add custom login equal, if exists
  219. if cfg.Protocol != cmpConf.Protocol ||
  220. cfg.KeepTunnelOpen != cmpConf.KeepTunnelOpen ||
  221. cfg.MaxRetriesAnHour != cmpConf.MaxRetriesAnHour ||
  222. cfg.MinRetryInterval != cmpConf.MinRetryInterval ||
  223. cfg.FallbackTo != cmpConf.FallbackTo ||
  224. cfg.FallbackTimeoutMs != cmpConf.FallbackTimeoutMs {
  225. return false
  226. }
  227. return true
  228. }
  229. func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  230. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  231. if err != nil {
  232. return
  233. }
  234. // Add custom logic unmarshal, if exists
  235. if cfg.Protocol == "" {
  236. cfg.Protocol = "quic"
  237. }
  238. if cfg.MaxRetriesAnHour <= 0 {
  239. cfg.MaxRetriesAnHour = 8
  240. }
  241. if cfg.MinRetryInterval <= 0 {
  242. cfg.MinRetryInterval = 90
  243. }
  244. if cfg.FallbackTimeoutMs <= 0 {
  245. cfg.FallbackTimeoutMs = 1000
  246. }
  247. return
  248. }
  249. func (cfg *XTCPVisitorConf) Check() (err error) {
  250. if err = cfg.BaseVisitorConf.check(); err != nil {
  251. return
  252. }
  253. // Add custom logic validate, if exists
  254. if !lo.Contains([]string{"", "kcp", "quic"}, cfg.Protocol) {
  255. return fmt.Errorf("protocol should be 'kcp' or 'quic'")
  256. }
  257. return
  258. }