1
0

common.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. "fmt"
  17. "slices"
  18. v1 "github.com/fatedier/frp/pkg/config/v1"
  19. )
  20. func validateWebServerConfig(c *v1.WebServerConfig) error {
  21. if c.TLS != nil {
  22. if c.TLS.CertFile == "" {
  23. return fmt.Errorf("tls.certFile must be specified when tls is enabled")
  24. }
  25. if c.TLS.KeyFile == "" {
  26. return fmt.Errorf("tls.keyFile must be specified when tls is enabled")
  27. }
  28. }
  29. return ValidatePort(c.Port, "webServer.port")
  30. }
  31. // ValidatePort checks that the network port is in range
  32. func ValidatePort(port int, fieldPath string) error {
  33. if 0 <= port && port <= 65535 {
  34. return nil
  35. }
  36. return fmt.Errorf("%s: port number %d must be in the range 0..65535", fieldPath, port)
  37. }
  38. func validateLogConfig(c *v1.LogConfig) error {
  39. if !slices.Contains(SupportedLogLevels, c.Level) {
  40. return fmt.Errorf("invalid log level, optional values are %v", SupportedLogLevels)
  41. }
  42. return nil
  43. }