1
0

visitor.go 7.2 KB

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