visitor.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2023 The frp Authors
  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 v1
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "reflect"
  21. "github.com/samber/lo"
  22. "github.com/fatedier/frp/pkg/util/util"
  23. )
  24. type VisitorTransport struct {
  25. UseEncryption bool `json:"useEncryption,omitempty"`
  26. UseCompression bool `json:"useCompression,omitempty"`
  27. }
  28. type VisitorBaseConfig struct {
  29. Name string `json:"name"`
  30. Type string `json:"type"`
  31. Transport VisitorTransport `json:"transport,omitempty"`
  32. SecretKey string `json:"secretKey,omitempty"`
  33. // if the server user is not set, it defaults to the current user
  34. ServerUser string `json:"serverUser,omitempty"`
  35. ServerName string `json:"serverName,omitempty"`
  36. BindAddr string `json:"bindAddr,omitempty"`
  37. // BindPort is the port that visitor listens on.
  38. // It can be less than 0, it means don't bind to the port and only receive connections redirected from
  39. // other visitors. (This is not supported for SUDP now)
  40. BindPort int `json:"bindPort,omitempty"`
  41. // Plugin specifies what plugin should be used.
  42. Plugin TypedVisitorPluginOptions `json:"plugin,omitempty"`
  43. }
  44. func (c *VisitorBaseConfig) GetBaseConfig() *VisitorBaseConfig {
  45. return c
  46. }
  47. func (c *VisitorBaseConfig) Complete(g *ClientCommonConfig) {
  48. if c.BindAddr == "" {
  49. c.BindAddr = "127.0.0.1"
  50. }
  51. namePrefix := ""
  52. if g.User != "" {
  53. namePrefix = g.User + "."
  54. }
  55. c.Name = namePrefix + c.Name
  56. if c.ServerUser != "" {
  57. c.ServerName = c.ServerUser + "." + c.ServerName
  58. } else {
  59. c.ServerName = namePrefix + c.ServerName
  60. }
  61. }
  62. type VisitorConfigurer interface {
  63. Complete(*ClientCommonConfig)
  64. GetBaseConfig() *VisitorBaseConfig
  65. }
  66. type VisitorType string
  67. const (
  68. VisitorTypeSTCP VisitorType = "stcp"
  69. VisitorTypeXTCP VisitorType = "xtcp"
  70. VisitorTypeSUDP VisitorType = "sudp"
  71. )
  72. var visitorConfigTypeMap = map[VisitorType]reflect.Type{
  73. VisitorTypeSTCP: reflect.TypeOf(STCPVisitorConfig{}),
  74. VisitorTypeXTCP: reflect.TypeOf(XTCPVisitorConfig{}),
  75. VisitorTypeSUDP: reflect.TypeOf(SUDPVisitorConfig{}),
  76. }
  77. type TypedVisitorConfig struct {
  78. Type string `json:"type"`
  79. VisitorConfigurer
  80. }
  81. func (c *TypedVisitorConfig) UnmarshalJSON(b []byte) error {
  82. if len(b) == 4 && string(b) == "null" {
  83. return errors.New("type is required")
  84. }
  85. typeStruct := struct {
  86. Type string `json:"type"`
  87. }{}
  88. if err := json.Unmarshal(b, &typeStruct); err != nil {
  89. return err
  90. }
  91. c.Type = typeStruct.Type
  92. configurer := NewVisitorConfigurerByType(VisitorType(typeStruct.Type))
  93. if configurer == nil {
  94. return fmt.Errorf("unknown visitor type: %s", typeStruct.Type)
  95. }
  96. decoder := json.NewDecoder(bytes.NewBuffer(b))
  97. if DisallowUnknownFields {
  98. decoder.DisallowUnknownFields()
  99. }
  100. if err := decoder.Decode(configurer); err != nil {
  101. return fmt.Errorf("unmarshal VisitorConfig error: %v", err)
  102. }
  103. c.VisitorConfigurer = configurer
  104. return nil
  105. }
  106. func (c *TypedVisitorConfig) MarshalJSON() ([]byte, error) {
  107. return json.Marshal(c.VisitorConfigurer)
  108. }
  109. func NewVisitorConfigurerByType(t VisitorType) VisitorConfigurer {
  110. v, ok := visitorConfigTypeMap[t]
  111. if !ok {
  112. return nil
  113. }
  114. vc := reflect.New(v).Interface().(VisitorConfigurer)
  115. vc.GetBaseConfig().Type = string(t)
  116. return vc
  117. }
  118. var _ VisitorConfigurer = &STCPVisitorConfig{}
  119. type STCPVisitorConfig struct {
  120. VisitorBaseConfig
  121. }
  122. var _ VisitorConfigurer = &SUDPVisitorConfig{}
  123. type SUDPVisitorConfig struct {
  124. VisitorBaseConfig
  125. }
  126. var _ VisitorConfigurer = &XTCPVisitorConfig{}
  127. type XTCPVisitorConfig struct {
  128. VisitorBaseConfig
  129. Protocol string `json:"protocol,omitempty"`
  130. KeepTunnelOpen bool `json:"keepTunnelOpen,omitempty"`
  131. MaxRetriesAnHour int `json:"maxRetriesAnHour,omitempty"`
  132. MinRetryInterval int `json:"minRetryInterval,omitempty"`
  133. FallbackTo string `json:"fallbackTo,omitempty"`
  134. FallbackTimeoutMs int `json:"fallbackTimeoutMs,omitempty"`
  135. }
  136. func (c *XTCPVisitorConfig) Complete(g *ClientCommonConfig) {
  137. c.VisitorBaseConfig.Complete(g)
  138. c.Protocol = util.EmptyOr(c.Protocol, "quic")
  139. c.MaxRetriesAnHour = util.EmptyOr(c.MaxRetriesAnHour, 8)
  140. c.MinRetryInterval = util.EmptyOr(c.MinRetryInterval, 90)
  141. c.FallbackTimeoutMs = util.EmptyOr(c.FallbackTimeoutMs, 1000)
  142. if c.FallbackTo != "" {
  143. c.FallbackTo = lo.Ternary(g.User == "", "", g.User+".") + c.FallbackTo
  144. }
  145. }