visitor.go 4.3 KB

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