visitor.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/consts"
  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. var visitorConfigTypeMap = map[string]reflect.Type{
  65. consts.STCPProxy: reflect.TypeOf(STCPVisitorConfig{}),
  66. consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConfig{}),
  67. consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConfig{}),
  68. }
  69. type TypedVisitorConfig struct {
  70. Type string `json:"type"`
  71. VisitorConfigurer
  72. }
  73. func (c *TypedVisitorConfig) UnmarshalJSON(b []byte) error {
  74. if len(b) == 4 && string(b) == "null" {
  75. return errors.New("type is required")
  76. }
  77. typeStruct := struct {
  78. Type string `json:"type"`
  79. }{}
  80. if err := json.Unmarshal(b, &typeStruct); err != nil {
  81. return err
  82. }
  83. c.Type = typeStruct.Type
  84. configurer := NewVisitorConfigurerByType(typeStruct.Type)
  85. if configurer == nil {
  86. return fmt.Errorf("unknown visitor type: %s", typeStruct.Type)
  87. }
  88. if err := json.Unmarshal(b, configurer); err != nil {
  89. return err
  90. }
  91. c.VisitorConfigurer = configurer
  92. return nil
  93. }
  94. func NewVisitorConfigurerByType(t string) VisitorConfigurer {
  95. v, ok := visitorConfigTypeMap[t]
  96. if !ok {
  97. return nil
  98. }
  99. return reflect.New(v).Interface().(VisitorConfigurer)
  100. }
  101. var _ VisitorConfigurer = &STCPVisitorConfig{}
  102. type STCPVisitorConfig struct {
  103. VisitorBaseConfig
  104. }
  105. var _ VisitorConfigurer = &SUDPVisitorConfig{}
  106. type SUDPVisitorConfig struct {
  107. VisitorBaseConfig
  108. }
  109. var _ VisitorConfigurer = &XTCPVisitorConfig{}
  110. type XTCPVisitorConfig struct {
  111. VisitorBaseConfig
  112. Protocol string `json:"protocol,omitempty"`
  113. KeepTunnelOpen bool `json:"keepTunnelOpen,omitempty"`
  114. MaxRetriesAnHour int `json:"maxRetriesAnHour,omitempty"`
  115. MinRetryInterval int `json:"minRetryInterval,omitempty"`
  116. FallbackTo string `json:"fallbackTo,omitempty"`
  117. FallbackTimeoutMs int `json:"fallbackTimeoutMs,omitempty"`
  118. }
  119. func (c *XTCPVisitorConfig) Complete(g *ClientCommonConfig) {
  120. c.VisitorBaseConfig.Complete(g)
  121. c.Protocol = util.EmptyOr(c.Protocol, "quic")
  122. c.MaxRetriesAnHour = util.EmptyOr(c.MaxRetriesAnHour, 8)
  123. c.MinRetryInterval = util.EmptyOr(c.MinRetryInterval, 90)
  124. c.FallbackTimeoutMs = util.EmptyOr(c.FallbackTimeoutMs, 1000)
  125. if c.FallbackTo != "" {
  126. c.FallbackTo = lo.Ternary(g.User == "", "", g.User+".") + c.FallbackTo
  127. }
  128. }