server.go 2.6 KB

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