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