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