config: add some validations (#3610)

This commit is contained in:
fatedier
2023-09-13 18:59:51 +08:00
committed by GitHub
parent 7cd02f5bd8
commit 74255f711e
9 changed files with 144 additions and 20 deletions

View File

@@ -17,6 +17,8 @@ package validation
import (
"fmt"
"github.com/samber/lo"
v1 "github.com/fatedier/frp/pkg/config/v1"
)
@@ -29,13 +31,21 @@ func validateWebServerConfig(c *v1.WebServerConfig) error {
return fmt.Errorf("tls.keyFile must be specified when tls is enabled")
}
}
return nil
return ValidatePort(c.Port, "webServer.port")
}
// ValidatePort checks that the network port is in range
func ValidatePort(port int) error {
func ValidatePort(port int, fieldPath string) error {
if 0 <= port && port <= 65535 {
return nil
}
return fmt.Errorf("port number %d must be in the range 0..65535", port)
return fmt.Errorf("%s: port number %d must be in the range 0..65535", fieldPath, port)
}
func validateLogConfig(c *v1.LogConfig) error {
if !lo.Contains(supportedLogLevels, c.Level) {
return fmt.Errorf("invalid log level, optional values are %v", supportedLogLevels)
}
return nil
}