proxy.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 validation
  15. import (
  16. "errors"
  17. "fmt"
  18. "strings"
  19. "github.com/samber/lo"
  20. v1 "github.com/fatedier/frp/pkg/config/v1"
  21. "github.com/fatedier/frp/pkg/consts"
  22. )
  23. func validateProxyBaseConfigForClient(c *v1.ProxyBaseConfig) error {
  24. if c.Name == "" {
  25. return errors.New("name should not be empty")
  26. }
  27. if !lo.Contains([]string{"", "v1", "v2"}, c.Transport.ProxyProtocolVersion) {
  28. return fmt.Errorf("not support proxy protocol version: %s", c.Transport.ProxyProtocolVersion)
  29. }
  30. if !lo.Contains([]string{"client", "server"}, c.Transport.BandwidthLimitMode) {
  31. return fmt.Errorf("bandwidth limit mode should be client or server")
  32. }
  33. if c.Plugin.Type == "" {
  34. if err := ValidatePort(c.LocalPort, "localPort"); err != nil {
  35. return fmt.Errorf("localPort: %v", err)
  36. }
  37. }
  38. if !lo.Contains([]string{"", "tcp", "http"}, c.HealthCheck.Type) {
  39. return fmt.Errorf("not support health check type: %s", c.HealthCheck.Type)
  40. }
  41. if c.HealthCheck.Type != "" {
  42. if c.HealthCheck.Type == "http" &&
  43. c.HealthCheck.Path == "" {
  44. return fmt.Errorf("health check path should not be empty")
  45. }
  46. }
  47. if c.Plugin.Type != "" {
  48. if err := ValidateClientPluginOptions(c.Plugin.ClientPluginOptions); err != nil {
  49. return fmt.Errorf("plugin %s: %v", c.Plugin.Type, err)
  50. }
  51. }
  52. return nil
  53. }
  54. func validateProxyBaseConfigForServer(c *v1.ProxyBaseConfig, s *v1.ServerConfig) error {
  55. return nil
  56. }
  57. func validateDomainConfigForClient(c *v1.DomainConfig) error {
  58. if c.SubDomain == "" && len(c.CustomDomains) == 0 {
  59. return errors.New("subdomain and custom domains should not be both empty")
  60. }
  61. return nil
  62. }
  63. func validateDomainConfigForServer(c *v1.DomainConfig, s *v1.ServerConfig) error {
  64. for _, domain := range c.CustomDomains {
  65. if s.SubDomainHost != "" && len(strings.Split(s.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
  66. if strings.Contains(domain, s.SubDomainHost) {
  67. return fmt.Errorf("custom domain [%s] should not belong to subdomain host [%s]", domain, s.SubDomainHost)
  68. }
  69. }
  70. }
  71. if c.SubDomain != "" {
  72. if s.SubDomainHost == "" {
  73. return errors.New("subdomain is not supported because this feature is not enabled in server")
  74. }
  75. if strings.Contains(c.SubDomain, ".") || strings.Contains(c.SubDomain, "*") {
  76. return errors.New("'.' and '*' are not supported in subdomain")
  77. }
  78. }
  79. return nil
  80. }
  81. func ValidateProxyConfigurerForClient(c v1.ProxyConfigurer) error {
  82. base := c.GetBaseConfig()
  83. if err := validateProxyBaseConfigForClient(base); err != nil {
  84. return err
  85. }
  86. switch v := c.(type) {
  87. case *v1.TCPProxyConfig:
  88. return validateTCPProxyConfigForClient(v)
  89. case *v1.UDPProxyConfig:
  90. return validateUDPProxyConfigForClient(v)
  91. case *v1.TCPMuxProxyConfig:
  92. return validateTCPMuxProxyConfigForClient(v)
  93. case *v1.HTTPProxyConfig:
  94. return validateHTTPProxyConfigForClient(v)
  95. case *v1.HTTPSProxyConfig:
  96. return validateHTTPSProxyConfigForClient(v)
  97. case *v1.STCPProxyConfig:
  98. return validateSTCPProxyConfigForClient(v)
  99. case *v1.XTCPProxyConfig:
  100. return validateXTCPProxyConfigForClient(v)
  101. case *v1.SUDPProxyConfig:
  102. return validateSUDPProxyConfigForClient(v)
  103. }
  104. return errors.New("unknown proxy config type")
  105. }
  106. func validateTCPProxyConfigForClient(c *v1.TCPProxyConfig) error {
  107. return nil
  108. }
  109. func validateUDPProxyConfigForClient(c *v1.UDPProxyConfig) error {
  110. return nil
  111. }
  112. func validateTCPMuxProxyConfigForClient(c *v1.TCPMuxProxyConfig) error {
  113. if err := validateDomainConfigForClient(&c.DomainConfig); err != nil {
  114. return err
  115. }
  116. if !lo.Contains([]string{consts.HTTPConnectTCPMultiplexer}, c.Multiplexer) {
  117. return fmt.Errorf("not support multiplexer: %s", c.Multiplexer)
  118. }
  119. return nil
  120. }
  121. func validateHTTPProxyConfigForClient(c *v1.HTTPProxyConfig) error {
  122. return validateDomainConfigForClient(&c.DomainConfig)
  123. }
  124. func validateHTTPSProxyConfigForClient(c *v1.HTTPSProxyConfig) error {
  125. return validateDomainConfigForClient(&c.DomainConfig)
  126. }
  127. func validateSTCPProxyConfigForClient(c *v1.STCPProxyConfig) error {
  128. return nil
  129. }
  130. func validateXTCPProxyConfigForClient(c *v1.XTCPProxyConfig) error {
  131. return nil
  132. }
  133. func validateSUDPProxyConfigForClient(c *v1.SUDPProxyConfig) error {
  134. return nil
  135. }
  136. func ValidateProxyConfigurerForServer(c v1.ProxyConfigurer, s *v1.ServerConfig) error {
  137. base := c.GetBaseConfig()
  138. if err := validateProxyBaseConfigForServer(base, s); err != nil {
  139. return err
  140. }
  141. switch v := c.(type) {
  142. case *v1.TCPProxyConfig:
  143. return validateTCPProxyConfigForServer(v, s)
  144. case *v1.UDPProxyConfig:
  145. return validateUDPProxyConfigForServer(v, s)
  146. case *v1.TCPMuxProxyConfig:
  147. return validateTCPMuxProxyConfigForServer(v, s)
  148. case *v1.HTTPProxyConfig:
  149. return validateHTTPProxyConfigForServer(v, s)
  150. case *v1.HTTPSProxyConfig:
  151. return validateHTTPSProxyConfigForServer(v, s)
  152. case *v1.STCPProxyConfig:
  153. return validateSTCPProxyConfigForServer(v, s)
  154. case *v1.XTCPProxyConfig:
  155. return validateXTCPProxyConfigForServer(v, s)
  156. case *v1.SUDPProxyConfig:
  157. return validateSUDPProxyConfigForServer(v, s)
  158. default:
  159. return errors.New("unknown proxy config type")
  160. }
  161. }
  162. func validateTCPProxyConfigForServer(c *v1.TCPProxyConfig, s *v1.ServerConfig) error {
  163. return nil
  164. }
  165. func validateUDPProxyConfigForServer(c *v1.UDPProxyConfig, s *v1.ServerConfig) error {
  166. return nil
  167. }
  168. func validateTCPMuxProxyConfigForServer(c *v1.TCPMuxProxyConfig, s *v1.ServerConfig) error {
  169. if c.Multiplexer == consts.HTTPConnectTCPMultiplexer &&
  170. s.TCPMuxHTTPConnectPort == 0 {
  171. return fmt.Errorf("tcpmux with multiplexer httpconnect not supported because this feature is not enabled in server")
  172. }
  173. return validateDomainConfigForServer(&c.DomainConfig, s)
  174. }
  175. func validateHTTPProxyConfigForServer(c *v1.HTTPProxyConfig, s *v1.ServerConfig) error {
  176. if s.VhostHTTPPort == 0 {
  177. return fmt.Errorf("type [http] not supported when vhost http port is not set")
  178. }
  179. return validateDomainConfigForServer(&c.DomainConfig, s)
  180. }
  181. func validateHTTPSProxyConfigForServer(c *v1.HTTPSProxyConfig, s *v1.ServerConfig) error {
  182. if s.VhostHTTPSPort == 0 {
  183. return fmt.Errorf("type [https] not supported when vhost https port is not set")
  184. }
  185. return validateDomainConfigForServer(&c.DomainConfig, s)
  186. }
  187. func validateSTCPProxyConfigForServer(c *v1.STCPProxyConfig, s *v1.ServerConfig) error {
  188. return nil
  189. }
  190. func validateXTCPProxyConfigForServer(c *v1.XTCPProxyConfig, s *v1.ServerConfig) error {
  191. return nil
  192. }
  193. func validateSUDPProxyConfigForServer(c *v1.SUDPProxyConfig, s *v1.ServerConfig) error {
  194. return nil
  195. }