1
0

server.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "github.com/samber/lo"
  19. v1 "github.com/fatedier/frp/pkg/config/v1"
  20. )
  21. func ValidateServerConfig(c *v1.ServerConfig) (Warning, error) {
  22. var (
  23. warnings Warning
  24. errs error
  25. )
  26. if !slices.Contains(SupportedAuthMethods, c.Auth.Method) {
  27. errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", SupportedAuthMethods))
  28. }
  29. if !lo.Every(SupportedAuthAdditionalScopes, c.Auth.AdditionalScopes) {
  30. errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", SupportedAuthAdditionalScopes))
  31. }
  32. // Validate token/tokenSource mutual exclusivity
  33. if c.Auth.Token != "" && c.Auth.TokenSource != nil {
  34. errs = AppendError(errs, fmt.Errorf("cannot specify both auth.token and auth.tokenSource"))
  35. }
  36. // Validate tokenSource if specified
  37. if c.Auth.TokenSource != nil {
  38. if err := c.Auth.TokenSource.Validate(); err != nil {
  39. errs = AppendError(errs, fmt.Errorf("invalid auth.tokenSource: %v", err))
  40. }
  41. }
  42. if err := validateLogConfig(&c.Log); err != nil {
  43. errs = AppendError(errs, err)
  44. }
  45. if err := validateWebServerConfig(&c.WebServer); err != nil {
  46. errs = AppendError(errs, err)
  47. }
  48. errs = AppendError(errs, ValidatePort(c.BindPort, "bindPort"))
  49. errs = AppendError(errs, ValidatePort(c.KCPBindPort, "kcpBindPort"))
  50. errs = AppendError(errs, ValidatePort(c.QUICBindPort, "quicBindPort"))
  51. errs = AppendError(errs, ValidatePort(c.VhostHTTPPort, "vhostHTTPPort"))
  52. errs = AppendError(errs, ValidatePort(c.VhostHTTPSPort, "vhostHTTPSPort"))
  53. errs = AppendError(errs, ValidatePort(c.TCPMuxHTTPConnectPort, "tcpMuxHTTPConnectPort"))
  54. for _, p := range c.HTTPPlugins {
  55. if !lo.Every(SupportedHTTPPluginOps, p.Ops) {
  56. errs = AppendError(errs, fmt.Errorf("invalid http plugin ops, optional values are %v", SupportedHTTPPluginOps))
  57. }
  58. }
  59. return warnings, errs
  60. }