client.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "github.com/samber/lo"
  20. v1 "github.com/fatedier/frp/pkg/config/v1"
  21. )
  22. func ValidateClientCommonConfig(c *v1.ClientCommonConfig) (Warning, error) {
  23. var (
  24. warnings Warning
  25. errs error
  26. )
  27. if c.Transport.HeartbeatTimeout > 0 && c.Transport.HeartbeatInterval > 0 {
  28. if c.Transport.HeartbeatTimeout < c.Transport.HeartbeatInterval {
  29. errs = AppendError(errs, fmt.Errorf("invalid transport.heartbeatTimeout, heartbeat timeout should not less than heartbeat interval"))
  30. }
  31. }
  32. if !lo.FromPtr(c.Transport.TLS.Enable) {
  33. checkTLSConfig := func(name string, value string) Warning {
  34. if value != "" {
  35. return fmt.Errorf("%s is invalid when transport.tls.enable is false", name)
  36. }
  37. return nil
  38. }
  39. warnings = AppendError(warnings, checkTLSConfig("transport.tls.certFile", c.Transport.TLS.CertFile))
  40. warnings = AppendError(warnings, checkTLSConfig("transport.tls.keyFile", c.Transport.TLS.KeyFile))
  41. warnings = AppendError(warnings, checkTLSConfig("transport.tls.trustedCaFile", c.Transport.TLS.TrustedCaFile))
  42. }
  43. if !lo.Contains([]string{"tcp", "kcp", "quic", "websocket", "wss"}, c.Transport.Protocol) {
  44. errs = AppendError(errs, fmt.Errorf("invalid transport.protocol, only support tcp, kcp, quic, websocket, wss"))
  45. }
  46. for _, f := range c.IncludeConfigFiles {
  47. absDir, err := filepath.Abs(filepath.Dir(f))
  48. if err != nil {
  49. errs = AppendError(errs, fmt.Errorf("include: parse directory of %s failed: %v", f, err))
  50. continue
  51. }
  52. if _, err := os.Stat(absDir); os.IsNotExist(err) {
  53. errs = AppendError(errs, fmt.Errorf("include: directory of %s not exist", f))
  54. }
  55. }
  56. return warnings, errs
  57. }
  58. func ValidateAllClientConfig(c *v1.ClientCommonConfig, pxyCfgs []v1.ProxyConfigurer, visitorCfgs []v1.VisitorConfigurer) (Warning, error) {
  59. var warnings Warning
  60. if c != nil {
  61. warning, err := ValidateClientCommonConfig(c)
  62. warnings = AppendError(warnings, warning)
  63. if err != nil {
  64. return warnings, err
  65. }
  66. }
  67. for _, c := range pxyCfgs {
  68. if err := ValidateProxyConfigurerForClient(c); err != nil {
  69. return warnings, fmt.Errorf("proxy %s: %v", c.GetBaseConfig().Name, err)
  70. }
  71. }
  72. for _, c := range visitorCfgs {
  73. if err := ValidateVisitorConfigurer(c); err != nil {
  74. return warnings, fmt.Errorf("visitor %s: %v", c.GetBaseConfig().Name, err)
  75. }
  76. }
  77. return warnings, nil
  78. }