| // |
| 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 |
| 15 | package validation |
| 16 |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | "slices" |
| 21 | "strings" |
| 22 |
| 23 | "k8s.io/apimachinery/pkg/util/validation" |
| 24 |
| 25 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 26 | ) |
| 27 |
| 28 | func validateProxyBaseConfigForClient(c *v1.ProxyBaseConfig) error { |
| 29 | if c.Name == "" { |
| 30 | return errors.New("name should not be empty") |
| 31 | } |
| 32 |
| 33 | if err := ValidateAnnotations(c.Annotations); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if !slices.Contains([]string{"", "v1", "v2"}, c.Transport.ProxyProtocolVersion) { |
| 37 | return fmt.Errorf("not support proxy protocol version: %s", c.Transport.ProxyProtocolVersion) |
| 38 | } |
| 39 | if !slices.Contains([]string{"client", "server"}, c.Transport.BandwidthLimitMode) { |
| 40 | return fmt.Errorf("bandwidth limit mode should be client or server") |
| 41 | } |
| 42 |
| 43 | if c.Plugin.Type == "" { |
| 44 | if err := ValidatePort(c.LocalPort, "localPort"); err != nil { |
| 45 | return fmt.Errorf("localPort: %v", err) |
| 46 | } |
| 47 | } |
| 48 |
| 49 | if !slices.Contains([]string{"", "tcp", "http"}, c.HealthCheck.Type) { |
| 50 | return fmt.Errorf("not support health check type: %s", c.HealthCheck.Type) |
| 51 | } |
| 52 | if c.HealthCheck.Type != "" { |
| 53 | if c.HealthCheck.Type == "http" && |
| 54 | c.HealthCheck.Path == "" { |
| 55 | return fmt.Errorf("health check path should not be empty") |
| 56 | } |
| 57 | } |
| 58 |
| 59 | if c.Plugin.Type != "" { |
| 60 | if err := ValidateClientPluginOptions(c.Plugin.ClientPluginOptions); err != nil { |
| 61 | return fmt.Errorf("plugin %s: %v", c.Plugin.Type, err) |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 |
| 67 | func validateProxyBaseConfigForServer(c *v1.ProxyBaseConfig) error { |
| 68 | if err := ValidateAnnotations(c.Annotations); err != nil { |
| 69 | return err |
| 70 | } |
| 71 | return nil |
| 72 | } |
| 73 |
| 74 | func validateDomainConfigForClient(c *v1.DomainConfig) error { |
| 75 | if c.SubDomain == "" && len(c.CustomDomains) == 0 { |
| 76 | return errors.New("subdomain and custom domains should not be both empty") |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 |
| 81 | func validateDomainConfigForServer(c *v1.DomainConfig, s *v1.ServerConfig) error { |
| 82 | for _, domain := range c.CustomDomains { |
| 83 | if s.SubDomainHost != "" && len(strings.Split(s.SubDomainHost, ".")) < len(strings.Split(domain, ".")) { |
| 84 | if strings.HasSuffix(domain, "."+s.SubDomainHost) { |
| 85 | return fmt.Errorf("custom domain [%s] should not belong to subdomain host [%s]", domain, s.SubDomainHost) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 |
| 90 | if c.SubDomain != "" { |
| 91 | if s.SubDomainHost == "" { |
| 92 | return errors.New("subdomain is not supported because this feature is not enabled in server") |
| 93 | } |
| 94 |
| 95 | if strings.Contains(c.SubDomain, ".") || strings.Contains(c.SubDomain, "*") { |
| 96 | return errors.New("'.' and '*' are not supported in subdomain") |
| 97 | } |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 |
| 102 | func ValidateProxyConfigurerForClient(c v1.ProxyConfigurer) error { |
| 103 | base := c.GetBaseConfig() |
| 104 | if err := validateProxyBaseConfigForClient(base); err != nil { |
| 105 | return err |
| 106 | } |
| 107 |
| 108 | switch v := c.(type) { |
| 109 | case *v1.TCPProxyConfig: |
| 110 | return validateTCPProxyConfigForClient(v) |
| 111 | case *v1.UDPProxyConfig: |
| 112 | return validateUDPProxyConfigForClient(v) |
| 113 | case *v1.TCPMuxProxyConfig: |
| 114 | return validateTCPMuxProxyConfigForClient(v) |
| 115 | case *v1.HTTPProxyConfig: |
| 116 | return validateHTTPProxyConfigForClient(v) |
| 117 | case *v1.HTTPSProxyConfig: |
| 118 | return validateHTTPSProxyConfigForClient(v) |
| 119 | case *v1.STCPProxyConfig: |
| 120 | return validateSTCPProxyConfigForClient(v) |
| 121 | case *v1.XTCPProxyConfig: |
| 122 | return validateXTCPProxyConfigForClient(v) |
| 123 | case *v1.SUDPProxyConfig: |
| 124 | return validateSUDPProxyConfigForClient(v) |
| 125 | } |
| 126 | return errors.New("unknown proxy config type") |
| 127 | } |
| 128 |
| 129 | func validateTCPProxyConfigForClient(c *v1.TCPProxyConfig) error { |
| 130 | return nil |
| 131 | } |
| 132 |
| 133 | func validateUDPProxyConfigForClient(c *v1.UDPProxyConfig) error { |
| 134 | return nil |
| 135 | } |
| 136 |
| 137 | func validateTCPMuxProxyConfigForClient(c *v1.TCPMuxProxyConfig) error { |
| 138 | if err := validateDomainConfigForClient(&c.DomainConfig); err != nil { |
| 139 | return err |
| 140 | } |
| 141 |
| 142 | if !slices.Contains([]string{string(v1.TCPMultiplexerHTTPConnect)}, c.Multiplexer) { |
| 143 | return fmt.Errorf("not support multiplexer: %s", c.Multiplexer) |
| 144 | } |
| 145 | return nil |
| 146 | } |
| 147 |
| 148 | func validateHTTPProxyConfigForClient(c *v1.HTTPProxyConfig) error { |
| 149 | return validateDomainConfigForClient(&c.DomainConfig) |
| 150 | } |
| 151 |
| 152 | func validateHTTPSProxyConfigForClient(c *v1.HTTPSProxyConfig) error { |
| 153 | return validateDomainConfigForClient(&c.DomainConfig) |
| 154 | } |
| 155 |
| 156 | func validateSTCPProxyConfigForClient(c *v1.STCPProxyConfig) error { |
| 157 | return nil |
| 158 | } |
| 159 |
| 160 | func validateXTCPProxyConfigForClient(c *v1.XTCPProxyConfig) error { |
| 161 | return nil |
| 162 | } |
| 163 |
| 164 | func validateSUDPProxyConfigForClient(c *v1.SUDPProxyConfig) error { |
| 165 | return nil |
| 166 | } |
| 167 |
| 168 | func ValidateProxyConfigurerForServer(c v1.ProxyConfigurer, s *v1.ServerConfig) error { |
| 169 | base := c.GetBaseConfig() |
| 170 | if err := validateProxyBaseConfigForServer(base); err != nil { |
| 171 | return err |
| 172 | } |
| 173 |
| 174 | switch v := c.(type) { |
| 175 | case *v1.TCPProxyConfig: |
| 176 | return validateTCPProxyConfigForServer(v, s) |
| 177 | case *v1.UDPProxyConfig: |
| 178 | return validateUDPProxyConfigForServer(v, s) |
| 179 | case *v1.TCPMuxProxyConfig: |
| 180 | return validateTCPMuxProxyConfigForServer(v, s) |
| 181 | case *v1.HTTPProxyConfig: |
| 182 | return validateHTTPProxyConfigForServer(v, s) |
| 183 | case *v1.HTTPSProxyConfig: |
| 184 | return validateHTTPSProxyConfigForServer(v, s) |
| 185 | case *v1.STCPProxyConfig: |
| 186 | return validateSTCPProxyConfigForServer(v, s) |
| 187 | case *v1.XTCPProxyConfig: |
| 188 | return validateXTCPProxyConfigForServer(v, s) |
| 189 | case *v1.SUDPProxyConfig: |
| 190 | return validateSUDPProxyConfigForServer(v, s) |
| 191 | default: |
| 192 | return errors.New("unknown proxy config type") |
| 193 | } |
| 194 | } |
| 195 |
| 196 | func validateTCPProxyConfigForServer(c *v1.TCPProxyConfig, s *v1.ServerConfig) error { |
| 197 | return nil |
| 198 | } |
| 199 |
| 200 | func validateUDPProxyConfigForServer(c *v1.UDPProxyConfig, s *v1.ServerConfig) error { |
| 201 | return nil |
| 202 | } |
| 203 |
| 204 | func validateTCPMuxProxyConfigForServer(c *v1.TCPMuxProxyConfig, s *v1.ServerConfig) error { |
| 205 | if c.Multiplexer == string(v1.TCPMultiplexerHTTPConnect) && |
| 206 | s.TCPMuxHTTPConnectPort == 0 { |
| 207 | return fmt.Errorf("tcpmux with multiplexer httpconnect not supported because this feature is not enabled in server") |
| 208 | } |
| 209 |
| 210 | return validateDomainConfigForServer(&c.DomainConfig, s) |
| 211 | } |
| 212 |
| 213 | func validateHTTPProxyConfigForServer(c *v1.HTTPProxyConfig, s *v1.ServerConfig) error { |
| 214 | if s.VhostHTTPPort == 0 { |
| 215 | return fmt.Errorf("type [http] not supported when vhost http port is not set") |
| 216 | } |
| 217 |
| 218 | return validateDomainConfigForServer(&c.DomainConfig, s) |
| 219 | } |
| 220 |
| 221 | func validateHTTPSProxyConfigForServer(c *v1.HTTPSProxyConfig, s *v1.ServerConfig) error { |
| 222 | if s.VhostHTTPSPort == 0 { |
| 223 | return fmt.Errorf("type [https] not supported when vhost https port is not set") |
| 224 | } |
| 225 |
| 226 | return validateDomainConfigForServer(&c.DomainConfig, s) |
| 227 | } |
| 228 |
| 229 | func validateSTCPProxyConfigForServer(c *v1.STCPProxyConfig, s *v1.ServerConfig) error { |
| 230 | return nil |
| 231 | } |
| 232 |
| 233 | func validateXTCPProxyConfigForServer(c *v1.XTCPProxyConfig, s *v1.ServerConfig) error { |
| 234 | return nil |
| 235 | } |
| 236 |
| 237 | func validateSUDPProxyConfigForServer(c *v1.SUDPProxyConfig, s *v1.ServerConfig) error { |
| 238 | return nil |
| 239 | } |
| 240 |
| 241 | // ValidateAnnotations validates that a set of annotations are correctly defined. |
| 242 | func ValidateAnnotations(annotations map[string]string) error { |
| 243 | if len(annotations) == 0 { |
| 244 | return nil |
| 245 | } |
| 246 |
| 247 | var errs error |
| 248 | for k := range annotations { |
| 249 | for _, msg := range validation.IsQualifiedName(strings.ToLower(k)) { |
| 250 | errs = AppendError(errs, fmt.Errorf("annotation key %s is invalid: %s", k, msg)) |
| 251 | } |
| 252 | } |
| 253 | if err := ValidateAnnotationsSize(annotations); err != nil { |
| 254 | errs = AppendError(errs, err) |
| 255 | } |
| 256 | return errs |
| 257 | } |
| 258 |
| 259 | const TotalAnnotationSizeLimitB int = 256 * (1 << 10) // 256 kB |
| 260 |
| 261 | func ValidateAnnotationsSize(annotations map[string]string) error { |
| 262 | var totalSize int64 |
| 263 | for k, v := range annotations { |
| 264 | totalSize += (int64)(len(k)) + (int64)(len(v)) |
| 265 | } |
| 266 | if totalSize > (int64)(TotalAnnotationSizeLimitB) { |
| 267 | return fmt.Errorf("annotations size %d is larger than limit %d", totalSize, TotalAnnotationSizeLimitB) |
| 268 | } |
| 269 | return nil |
| 270 | } |