visitor.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. }
  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. func NewVisitorConfByType(cfgType string) VisitorConf {
  37. v, ok := visitorConfTypeMap[cfgType]
  38. if !ok {
  39. return nil
  40. }
  41. cfg := reflect.New(v).Interface().(VisitorConf)
  42. return cfg
  43. }
  44. func NewVisitorConfFromIni(prefix string, name string, section ini.Section) (cfg VisitorConf, err error) {
  45. cfgType := section["type"]
  46. if cfgType == "" {
  47. err = fmt.Errorf("visitor [%s] type shouldn't be empty", name)
  48. return
  49. }
  50. cfg = NewVisitorConfByType(cfgType)
  51. if cfg == nil {
  52. err = fmt.Errorf("visitor [%s] type [%s] error", name, cfgType)
  53. return
  54. }
  55. if err = cfg.UnmarshalFromIni(prefix, name, section); err != nil {
  56. return
  57. }
  58. if err = cfg.Check(); err != nil {
  59. return
  60. }
  61. return
  62. }
  63. type BaseVisitorConf struct {
  64. ProxyName string `json:"proxy_name"`
  65. ProxyType string `json:"proxy_type"`
  66. UseEncryption bool `json:"use_encryption"`
  67. UseCompression bool `json:"use_compression"`
  68. Role string `json:"role"`
  69. Sk string `json:"sk"`
  70. ServerName string `json:"server_name"`
  71. BindAddr string `json:"bind_addr"`
  72. BindPort int `json:"bind_port"`
  73. }
  74. func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf {
  75. return cfg
  76. }
  77. func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool {
  78. if cfg.ProxyName != cmp.ProxyName ||
  79. cfg.ProxyType != cmp.ProxyType ||
  80. cfg.UseEncryption != cmp.UseEncryption ||
  81. cfg.UseCompression != cmp.UseCompression ||
  82. cfg.Role != cmp.Role ||
  83. cfg.Sk != cmp.Sk ||
  84. cfg.ServerName != cmp.ServerName ||
  85. cfg.BindAddr != cmp.BindAddr ||
  86. cfg.BindPort != cmp.BindPort {
  87. return false
  88. }
  89. return true
  90. }
  91. func (cfg *BaseVisitorConf) check() (err error) {
  92. if cfg.Role != "visitor" {
  93. err = fmt.Errorf("invalid role")
  94. return
  95. }
  96. if cfg.BindAddr == "" {
  97. err = fmt.Errorf("bind_addr shouldn't be empty")
  98. return
  99. }
  100. if cfg.BindPort <= 0 {
  101. err = fmt.Errorf("bind_port is required")
  102. return
  103. }
  104. return
  105. }
  106. func (cfg *BaseVisitorConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  107. var (
  108. tmpStr string
  109. ok bool
  110. )
  111. cfg.ProxyName = prefix + name
  112. cfg.ProxyType = section["type"]
  113. if tmpStr, ok = section["use_encryption"]; ok && tmpStr == "true" {
  114. cfg.UseEncryption = true
  115. }
  116. if tmpStr, ok = section["use_compression"]; ok && tmpStr == "true" {
  117. cfg.UseCompression = true
  118. }
  119. cfg.Role = section["role"]
  120. if cfg.Role != "visitor" {
  121. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  122. }
  123. cfg.Sk = section["sk"]
  124. cfg.ServerName = prefix + section["server_name"]
  125. if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
  126. cfg.BindAddr = "127.0.0.1"
  127. }
  128. if tmpStr, ok = section["bind_port"]; ok {
  129. if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
  130. return fmt.Errorf("Parse conf error: proxy [%s] bind_port incorrect", name)
  131. }
  132. } else {
  133. return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
  134. }
  135. return nil
  136. }
  137. type StcpVisitorConf struct {
  138. BaseVisitorConf
  139. }
  140. func (cfg *StcpVisitorConf) Compare(cmp VisitorConf) bool {
  141. cmpConf, ok := cmp.(*StcpVisitorConf)
  142. if !ok {
  143. return false
  144. }
  145. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  146. return false
  147. }
  148. return true
  149. }
  150. func (cfg *StcpVisitorConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  151. if err = cfg.BaseVisitorConf.UnmarshalFromIni(prefix, name, section); err != nil {
  152. return
  153. }
  154. return
  155. }
  156. func (cfg *StcpVisitorConf) Check() (err error) {
  157. if err = cfg.BaseVisitorConf.check(); err != nil {
  158. return
  159. }
  160. return
  161. }
  162. type XtcpVisitorConf struct {
  163. BaseVisitorConf
  164. }
  165. func (cfg *XtcpVisitorConf) Compare(cmp VisitorConf) bool {
  166. cmpConf, ok := cmp.(*XtcpVisitorConf)
  167. if !ok {
  168. return false
  169. }
  170. if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
  171. return false
  172. }
  173. return true
  174. }
  175. func (cfg *XtcpVisitorConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  176. if err = cfg.BaseVisitorConf.UnmarshalFromIni(prefix, name, section); err != nil {
  177. return
  178. }
  179. return
  180. }
  181. func (cfg *XtcpVisitorConf) Check() (err error) {
  182. if err = cfg.BaseVisitorConf.check(); err != nil {
  183. return
  184. }
  185. return
  186. }