visitor.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/fatedier/frp/pkg/consts"
  19. "gopkg.in/ini.v1"
  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. GetBaseInfo() *BaseVisitorConf
  31. Compare(cmp VisitorConf) bool
  32. UnmarshalFromIni(prefix string, name string, section *ini.Section) error
  33. Check() error
  34. }
  35. type BaseVisitorConf struct {
  36. ProxyName string `ini:"name" json:"name"`
  37. ProxyType string `ini:"type" json:"type"`
  38. UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
  39. UseCompression bool `ini:"use_compression" json:"use_compression"`
  40. Role string `ini:"role" json:"role"`
  41. Sk string `ini:"sk" json:"sk"`
  42. ServerName string `ini:"server_name" json:"server_name"`
  43. BindAddr string `ini:"bind_addr" json:"bind_addr"`
  44. BindPort int `ini:"bind_port" json:"bind_port"`
  45. }
  46. type SUDPVisitorConf struct {
  47. BaseVisitorConf `ini:",extends"`
  48. }
  49. type STCPVisitorConf struct {
  50. BaseVisitorConf `ini:",extends"`
  51. }
  52. type XTCPVisitorConf struct {
  53. BaseVisitorConf `ini:",extends"`
  54. }
  55. // DefaultVisitorConf creates a empty VisitorConf object by visitorType.
  56. // If visitorType doesn't exist, return nil.
  57. func DefaultVisitorConf(visitorType string) VisitorConf {
  58. v, ok := visitorConfTypeMap[visitorType]
  59. if !ok {
  60. return nil
  61. }
  62. return reflect.New(v).Interface().(VisitorConf)
  63. }
  64. // Visitor loaded from ini
  65. func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (VisitorConf, error) {
  66. // section.Key: if key not exists, section will set it with default value.
  67. visitorType := section.Key("type").String()
  68. if visitorType == "" {
  69. return nil, fmt.Errorf("visitor [%s] type shouldn't be empty", name)
  70. }
  71. conf := DefaultVisitorConf(visitorType)
  72. if conf == nil {
  73. return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
  74. }
  75. if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
  76. return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
  77. }
  78. if err := conf.Check(); err != nil {
  79. return nil, err
  80. }
  81. return conf, nil
  82. }
  83. // Base
  84. func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf {
  85. return cfg
  86. }
  87. func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool {
  88. if cfg.ProxyName != cmp.ProxyName ||
  89. cfg.ProxyType != cmp.ProxyType ||
  90. cfg.UseEncryption != cmp.UseEncryption ||
  91. cfg.UseCompression != cmp.UseCompression ||
  92. cfg.Role != cmp.Role ||
  93. cfg.Sk != cmp.Sk ||
  94. cfg.ServerName != cmp.ServerName ||
  95. cfg.BindAddr != cmp.BindAddr ||
  96. cfg.BindPort != cmp.BindPort {
  97. return false
  98. }
  99. return true
  100. }
  101. func (cfg *BaseVisitorConf) check() (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. if cfg.BindPort <= 0 {
  111. err = fmt.Errorf("bind_port is required")
  112. return
  113. }
  114. return
  115. }
  116. func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section *ini.Section) error {
  117. // Custom decoration after basic unmarshal:
  118. // proxy name
  119. cfg.ProxyName = prefix + name
  120. // server_name
  121. cfg.ServerName = prefix + cfg.ServerName
  122. // bind_addr
  123. if cfg.BindAddr == "" {
  124. cfg.BindAddr = "127.0.0.1"
  125. }
  126. return nil
  127. }
  128. func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
  129. err := section.MapTo(cfg)
  130. if err != nil {
  131. return err
  132. }
  133. err = cfg.GetBaseInfo().unmarshalFromIni(prefix, name, section)
  134. if err != nil {
  135. return err
  136. }
  137. return nil
  138. }
  139. // SUDP
  140. var _ VisitorConf = &SUDPVisitorConf{}
  141. func (cfg *SUDPVisitorConf) Compare(cmp VisitorConf) bool {
  142. cmpConf, ok := cmp.(*SUDPVisitorConf)
  143. if !ok {
  144. return false
  145. }
  146. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  147. return false
  148. }
  149. // Add custom login equal, if exists
  150. return true
  151. }
  152. func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  153. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  154. if err != nil {
  155. return
  156. }
  157. // Add custom logic unmarshal, if exists
  158. return
  159. }
  160. func (cfg *SUDPVisitorConf) Check() (err error) {
  161. if err = cfg.BaseVisitorConf.check(); err != nil {
  162. return
  163. }
  164. // Add custom logic validate, if exists
  165. return
  166. }
  167. // STCP
  168. var _ VisitorConf = &STCPVisitorConf{}
  169. func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool {
  170. cmpConf, ok := cmp.(*STCPVisitorConf)
  171. if !ok {
  172. return false
  173. }
  174. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  175. return false
  176. }
  177. // Add custom login equal, if exists
  178. return true
  179. }
  180. func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  181. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  182. if err != nil {
  183. return
  184. }
  185. // Add custom logic unmarshal, if exists
  186. return
  187. }
  188. func (cfg *STCPVisitorConf) Check() (err error) {
  189. if err = cfg.BaseVisitorConf.check(); err != nil {
  190. return
  191. }
  192. // Add custom logic validate, if exists
  193. return
  194. }
  195. // XTCP
  196. var _ VisitorConf = &XTCPVisitorConf{}
  197. func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool {
  198. cmpConf, ok := cmp.(*XTCPVisitorConf)
  199. if !ok {
  200. return false
  201. }
  202. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  203. return false
  204. }
  205. // Add custom login equal, if exists
  206. return true
  207. }
  208. func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
  209. err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
  210. if err != nil {
  211. return
  212. }
  213. // Add custom logic unmarshal, if exists
  214. return
  215. }
  216. func (cfg *XTCPVisitorConf) Check() (err error) {
  217. if err = cfg.BaseVisitorConf.check(); err != nil {
  218. return
  219. }
  220. // Add custom logic validate, if exists
  221. return
  222. }