1
0

visitor.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "slices"
  19. v1 "github.com/fatedier/frp/pkg/config/v1"
  20. )
  21. func ValidateVisitorConfigurer(c v1.VisitorConfigurer) error {
  22. base := c.GetBaseConfig()
  23. if err := validateVisitorBaseConfig(base); err != nil {
  24. return err
  25. }
  26. switch v := c.(type) {
  27. case *v1.STCPVisitorConfig:
  28. case *v1.SUDPVisitorConfig:
  29. case *v1.XTCPVisitorConfig:
  30. return validateXTCPVisitorConfig(v)
  31. default:
  32. return errors.New("unknown visitor config type")
  33. }
  34. return nil
  35. }
  36. func validateVisitorBaseConfig(c *v1.VisitorBaseConfig) error {
  37. if c.Name == "" {
  38. return errors.New("name is required")
  39. }
  40. if c.ServerName == "" {
  41. return errors.New("server name is required")
  42. }
  43. if c.BindPort == 0 {
  44. return errors.New("bind port is required")
  45. }
  46. return nil
  47. }
  48. func validateXTCPVisitorConfig(c *v1.XTCPVisitorConfig) error {
  49. if !slices.Contains([]string{"kcp", "quic"}, c.Protocol) {
  50. return fmt.Errorf("protocol should be kcp or quic")
  51. }
  52. return nil
  53. }