visitor.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. "strconv"
  19. "github.com/fatedier/frp/models/consts"
  20. ini "github.com/vaughan0/go-ini"
  21. )
  22. var (
  23. visitorConfTypeMap map[string]reflect.Type
  24. )
  25. func init() {
  26. visitorConfTypeMap = make(map[string]reflect.Type)
  27. visitorConfTypeMap[consts.STCPProxy] = reflect.TypeOf(STCPVisitorConf{})
  28. visitorConfTypeMap[consts.XTCPProxy] = reflect.TypeOf(XTCPVisitorConf{})
  29. visitorConfTypeMap[consts.SUDPProxy] = reflect.TypeOf(SUDPVisitorConf{})
  30. }
  31. type VisitorConf interface {
  32. GetBaseInfo() *BaseVisitorConf
  33. Compare(cmp VisitorConf) bool
  34. UnmarshalFromIni(prefix string, name string, section ini.Section) error
  35. Check() error
  36. }
  37. func NewVisitorConfByType(cfgType string) VisitorConf {
  38. v, ok := visitorConfTypeMap[cfgType]
  39. if !ok {
  40. return nil
  41. }
  42. cfg := reflect.New(v).Interface().(VisitorConf)
  43. return cfg
  44. }
  45. func NewVisitorConfFromIni(prefix string, name string, section ini.Section) (cfg VisitorConf, err error) {
  46. cfgType := section["type"]
  47. if cfgType == "" {
  48. err = fmt.Errorf("visitor [%s] type shouldn't be empty", name)
  49. return
  50. }
  51. cfg = NewVisitorConfByType(cfgType)
  52. if cfg == nil {
  53. err = fmt.Errorf("visitor [%s] type [%s] error", name, cfgType)
  54. return
  55. }
  56. if err = cfg.UnmarshalFromIni(prefix, name, section); err != nil {
  57. return
  58. }
  59. if err = cfg.Check(); err != nil {
  60. return
  61. }
  62. return
  63. }
  64. type BaseVisitorConf struct {
  65. ProxyName string `json:"proxy_name"`
  66. ProxyType string `json:"proxy_type"`
  67. UseEncryption bool `json:"use_encryption"`
  68. UseCompression bool `json:"use_compression"`
  69. Role string `json:"role"`
  70. Sk string `json:"sk"`
  71. ServerName string `json:"server_name"`
  72. BindAddr string `json:"bind_addr"`
  73. BindPort int `json:"bind_port"`
  74. }
  75. func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf {
  76. return cfg
  77. }
  78. func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool {
  79. if cfg.ProxyName != cmp.ProxyName ||
  80. cfg.ProxyType != cmp.ProxyType ||
  81. cfg.UseEncryption != cmp.UseEncryption ||
  82. cfg.UseCompression != cmp.UseCompression ||
  83. cfg.Role != cmp.Role ||
  84. cfg.Sk != cmp.Sk ||
  85. cfg.ServerName != cmp.ServerName ||
  86. cfg.BindAddr != cmp.BindAddr ||
  87. cfg.BindPort != cmp.BindPort {
  88. return false
  89. }
  90. return true
  91. }
  92. func (cfg *BaseVisitorConf) check() (err error) {
  93. if cfg.Role != "visitor" {
  94. err = fmt.Errorf("invalid role")
  95. return
  96. }
  97. if cfg.BindAddr == "" {
  98. err = fmt.Errorf("bind_addr shouldn't be empty")
  99. return
  100. }
  101. if cfg.BindPort <= 0 {
  102. err = fmt.Errorf("bind_port is required")
  103. return
  104. }
  105. return
  106. }
  107. func (cfg *BaseVisitorConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  108. var (
  109. tmpStr string
  110. ok bool
  111. )
  112. cfg.ProxyName = prefix + name
  113. cfg.ProxyType = section["type"]
  114. if tmpStr, ok = section["use_encryption"]; ok && tmpStr == "true" {
  115. cfg.UseEncryption = true
  116. }
  117. if tmpStr, ok = section["use_compression"]; ok && tmpStr == "true" {
  118. cfg.UseCompression = true
  119. }
  120. cfg.Role = section["role"]
  121. if cfg.Role != "visitor" {
  122. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  123. }
  124. cfg.Sk = section["sk"]
  125. cfg.ServerName = prefix + section["server_name"]
  126. if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
  127. cfg.BindAddr = "127.0.0.1"
  128. }
  129. if tmpStr, ok = section["bind_port"]; ok {
  130. if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
  131. return fmt.Errorf("Parse conf error: proxy [%s] bind_port incorrect", name)
  132. }
  133. } else {
  134. return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
  135. }
  136. return nil
  137. }
  138. type SUDPVisitorConf struct {
  139. BaseVisitorConf
  140. }
  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. return true
  150. }
  151. func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  152. if err = cfg.BaseVisitorConf.UnmarshalFromIni(prefix, name, section); err != nil {
  153. return
  154. }
  155. return
  156. }
  157. func (cfg *SUDPVisitorConf) Check() (err error) {
  158. if err = cfg.BaseVisitorConf.check(); err != nil {
  159. return
  160. }
  161. return
  162. }
  163. type STCPVisitorConf struct {
  164. BaseVisitorConf
  165. }
  166. func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool {
  167. cmpConf, ok := cmp.(*STCPVisitorConf)
  168. if !ok {
  169. return false
  170. }
  171. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  172. return false
  173. }
  174. return true
  175. }
  176. func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  177. if err = cfg.BaseVisitorConf.UnmarshalFromIni(prefix, name, section); err != nil {
  178. return
  179. }
  180. return
  181. }
  182. func (cfg *STCPVisitorConf) Check() (err error) {
  183. if err = cfg.BaseVisitorConf.check(); err != nil {
  184. return
  185. }
  186. return
  187. }
  188. type XTCPVisitorConf struct {
  189. BaseVisitorConf
  190. }
  191. func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool {
  192. cmpConf, ok := cmp.(*XTCPVisitorConf)
  193. if !ok {
  194. return false
  195. }
  196. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  197. return false
  198. }
  199. return true
  200. }
  201. func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  202. if err = cfg.BaseVisitorConf.UnmarshalFromIni(prefix, name, section); err != nil {
  203. return
  204. }
  205. return
  206. }
  207. func (cfg *XTCPVisitorConf) Check() (err error) {
  208. if err = cfg.BaseVisitorConf.check(); err != nil {
  209. return
  210. }
  211. return
  212. }