proxy.go 6.9 KB

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