client.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "os"
  18. "path/filepath"
  19. "slices"
  20. "github.com/samber/lo"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. )
  23. func ValidateClientCommonConfig(c *v1.ClientCommonConfig) (Warning, error) {
  24. var (
  25. warnings Warning
  26. errs error
  27. )
  28. if !slices.Contains(SupportedAuthMethods, c.Auth.Method) {
  29. errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", SupportedAuthMethods))
  30. }
  31. if !lo.Every(SupportedAuthAdditionalScopes, c.Auth.AdditionalScopes) {
  32. errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", SupportedAuthAdditionalScopes))
  33. }
  34. if err := validateLogConfig(&c.Log); err != nil {
  35. errs = AppendError(errs, err)
  36. }
  37. if err := validateWebServerConfig(&c.WebServer); err != nil {
  38. errs = AppendError(errs, err)
  39. }
  40. if c.Transport.HeartbeatTimeout > 0 && c.Transport.HeartbeatInterval > 0 {
  41. if c.Transport.HeartbeatTimeout < c.Transport.HeartbeatInterval {
  42. errs = AppendError(errs, fmt.Errorf("invalid transport.heartbeatTimeout, heartbeat timeout should not less than heartbeat interval"))
  43. }
  44. }
  45. if !lo.FromPtr(c.Transport.TLS.Enable) {
  46. checkTLSConfig := func(name string, value string) Warning {
  47. if value != "" {
  48. return fmt.Errorf("%s is invalid when transport.tls.enable is false", name)
  49. }
  50. return nil
  51. }
  52. warnings = AppendError(warnings, checkTLSConfig("transport.tls.certFile", c.Transport.TLS.CertFile))
  53. warnings = AppendError(warnings, checkTLSConfig("transport.tls.keyFile", c.Transport.TLS.KeyFile))
  54. warnings = AppendError(warnings, checkTLSConfig("transport.tls.trustedCaFile", c.Transport.TLS.TrustedCaFile))
  55. }
  56. if !slices.Contains(SupportedTransportProtocols, c.Transport.Protocol) {
  57. errs = AppendError(errs, fmt.Errorf("invalid transport.protocol, optional values are %v", SupportedTransportProtocols))
  58. }
  59. for _, f := range c.IncludeConfigFiles {
  60. absDir, err := filepath.Abs(filepath.Dir(f))
  61. if err != nil {
  62. errs = AppendError(errs, fmt.Errorf("include: parse directory of %s failed: %v", f, err))
  63. continue
  64. }
  65. if _, err := os.Stat(absDir); os.IsNotExist(err) {
  66. errs = AppendError(errs, fmt.Errorf("include: directory of %s not exist", f))
  67. }
  68. }
  69. return warnings, errs
  70. }
  71. func ValidateAllClientConfig(c *v1.ClientCommonConfig, proxyCfgs []v1.ProxyConfigurer, visitorCfgs []v1.VisitorConfigurer) (Warning, error) {
  72. var warnings Warning
  73. if c != nil {
  74. warning, err := ValidateClientCommonConfig(c)
  75. warnings = AppendError(warnings, warning)
  76. if err != nil {
  77. return warnings, err
  78. }
  79. }
  80. for _, c := range proxyCfgs {
  81. if err := ValidateProxyConfigurerForClient(c); err != nil {
  82. return warnings, fmt.Errorf("proxy %s: %v", c.GetBaseConfig().Name, err)
  83. }
  84. }
  85. for _, c := range visitorCfgs {
  86. if err := ValidateVisitorConfigurer(c); err != nil {
  87. return warnings, fmt.Errorf("visitor %s: %v", c.GetBaseConfig().Name, err)
  88. }
  89. }
  90. return warnings, nil
  91. }