visitor.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  42. func (c *VisitorBaseConfig) GetBaseConfig() *VisitorBaseConfig {
  43. return c
  44. }
  45. func (c *VisitorBaseConfig) Complete(g *ClientCommonConfig) {
  46. if c.BindAddr == "" {
  47. c.BindAddr = "127.0.0.1"
  48. }
  49. namePrefix := ""
  50. if g.User != "" {
  51. namePrefix = g.User + "."
  52. }
  53. c.Name = namePrefix + c.Name
  54. if c.ServerUser != "" {
  55. c.ServerName = c.ServerUser + "." + c.ServerName
  56. } else {
  57. c.ServerName = namePrefix + c.ServerName
  58. }
  59. }
  60. type VisitorConfigurer interface {
  61. Complete(*ClientCommonConfig)
  62. GetBaseConfig() *VisitorBaseConfig
  63. }
  64. type VisitorType string
  65. const (
  66. VisitorTypeSTCP VisitorType = "stcp"
  67. VisitorTypeXTCP VisitorType = "xtcp"
  68. VisitorTypeSUDP VisitorType = "sudp"
  69. )
  70. var visitorConfigTypeMap = map[VisitorType]reflect.Type{
  71. VisitorTypeSTCP: reflect.TypeOf(STCPVisitorConfig{}),
  72. VisitorTypeXTCP: reflect.TypeOf(XTCPVisitorConfig{}),
  73. VisitorTypeSUDP: reflect.TypeOf(SUDPVisitorConfig{}),
  74. }
  75. type TypedVisitorConfig struct {
  76. Type string `json:"type"`
  77. VisitorConfigurer
  78. }
  79. func (c *TypedVisitorConfig) UnmarshalJSON(b []byte) error {
  80. if len(b) == 4 && string(b) == "null" {
  81. return errors.New("type is required")
  82. }
  83. typeStruct := struct {
  84. Type string `json:"type"`
  85. }{}
  86. if err := json.Unmarshal(b, &typeStruct); err != nil {
  87. return err
  88. }
  89. c.Type = typeStruct.Type
  90. configurer := NewVisitorConfigurerByType(VisitorType(typeStruct.Type))
  91. if configurer == nil {
  92. return fmt.Errorf("unknown visitor type: %s", typeStruct.Type)
  93. }
  94. decoder := json.NewDecoder(bytes.NewBuffer(b))
  95. if DisallowUnknownFields {
  96. decoder.DisallowUnknownFields()
  97. }
  98. if err := decoder.Decode(configurer); err != nil {
  99. return fmt.Errorf("unmarshal VisitorConfig error: %v", err)
  100. }
  101. c.VisitorConfigurer = configurer
  102. return nil
  103. }
  104. func (c *TypedVisitorConfig) MarshalJSON() ([]byte, error) {
  105. return json.Marshal(c.VisitorConfigurer)
  106. }
  107. func NewVisitorConfigurerByType(t VisitorType) VisitorConfigurer {
  108. v, ok := visitorConfigTypeMap[t]
  109. if !ok {
  110. return nil
  111. }
  112. vc := reflect.New(v).Interface().(VisitorConfigurer)
  113. vc.GetBaseConfig().Type = string(t)
  114. return vc
  115. }
  116. var _ VisitorConfigurer = &STCPVisitorConfig{}
  117. type STCPVisitorConfig struct {
  118. VisitorBaseConfig
  119. }
  120. var _ VisitorConfigurer = &SUDPVisitorConfig{}
  121. type SUDPVisitorConfig struct {
  122. VisitorBaseConfig
  123. }
  124. var _ VisitorConfigurer = &XTCPVisitorConfig{}
  125. type XTCPVisitorConfig struct {
  126. VisitorBaseConfig
  127. Protocol string `json:"protocol,omitempty"`
  128. KeepTunnelOpen bool `json:"keepTunnelOpen,omitempty"`
  129. MaxRetriesAnHour int `json:"maxRetriesAnHour,omitempty"`
  130. MinRetryInterval int `json:"minRetryInterval,omitempty"`
  131. FallbackTo string `json:"fallbackTo,omitempty"`
  132. FallbackTimeoutMs int `json:"fallbackTimeoutMs,omitempty"`
  133. }
  134. func (c *XTCPVisitorConfig) Complete(g *ClientCommonConfig) {
  135. c.VisitorBaseConfig.Complete(g)
  136. c.Protocol = util.EmptyOr(c.Protocol, "quic")
  137. c.MaxRetriesAnHour = util.EmptyOr(c.MaxRetriesAnHour, 8)
  138. c.MinRetryInterval = util.EmptyOr(c.MinRetryInterval, 90)
  139. c.FallbackTimeoutMs = util.EmptyOr(c.FallbackTimeoutMs, 1000)
  140. if c.FallbackTo != "" {
  141. c.FallbackTo = lo.Ternary(g.User == "", "", g.User+".") + c.FallbackTo
  142. }
  143. }