2023-09-06 10:18:02 +08:00
|
|
|
package validation
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2024-02-20 12:01:41 +08:00
|
|
|
"slices"
|
2023-09-06 10:18:02 +08:00
|
|
|
|
|
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ValidateVisitorConfigurer(c v1.VisitorConfigurer) error {
|
|
|
|
|
base := c.GetBaseConfig()
|
|
|
|
|
if err := validateVisitorBaseConfig(base); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch v := c.(type) {
|
|
|
|
|
case *v1.STCPVisitorConfig:
|
|
|
|
|
case *v1.SUDPVisitorConfig:
|
|
|
|
|
case *v1.XTCPVisitorConfig:
|
|
|
|
|
return validateXTCPVisitorConfig(v)
|
|
|
|
|
default:
|
|
|
|
|
return errors.New("unknown visitor config type")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateVisitorBaseConfig(c *v1.VisitorBaseConfig) error {
|
|
|
|
|
if c.Name == "" {
|
2023-09-15 10:33:32 +08:00
|
|
|
return errors.New("name is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.ServerName == "" {
|
|
|
|
|
return errors.New("server name is required")
|
2023-09-06 10:18:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.BindPort == 0 {
|
|
|
|
|
return errors.New("bind port is required")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateXTCPVisitorConfig(c *v1.XTCPVisitorConfig) error {
|
2024-02-20 12:01:41 +08:00
|
|
|
if !slices.Contains([]string{"kcp", "quic"}, c.Protocol) {
|
2023-09-06 10:18:02 +08:00
|
|
|
return fmt.Errorf("protocol should be kcp or quic")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|