proxy.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 sub
  15. import (
  16. "fmt"
  17. "os"
  18. "slices"
  19. "github.com/spf13/cobra"
  20. "github.com/fatedier/frp/pkg/config"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. "github.com/fatedier/frp/pkg/config/v1/validation"
  23. "github.com/fatedier/frp/pkg/policy/security"
  24. )
  25. var proxyTypes = []v1.ProxyType{
  26. v1.ProxyTypeTCP,
  27. v1.ProxyTypeUDP,
  28. v1.ProxyTypeTCPMUX,
  29. v1.ProxyTypeHTTP,
  30. v1.ProxyTypeHTTPS,
  31. v1.ProxyTypeSTCP,
  32. v1.ProxyTypeSUDP,
  33. v1.ProxyTypeXTCP,
  34. }
  35. var visitorTypes = []v1.VisitorType{
  36. v1.VisitorTypeSTCP,
  37. v1.VisitorTypeSUDP,
  38. v1.VisitorTypeXTCP,
  39. }
  40. func init() {
  41. for _, typ := range proxyTypes {
  42. c := v1.NewProxyConfigurerByType(typ)
  43. if c == nil {
  44. panic("proxy type: " + typ + " not support")
  45. }
  46. clientCfg := v1.ClientCommonConfig{}
  47. cmd := NewProxyCommand(string(typ), c, &clientCfg)
  48. config.RegisterClientCommonConfigFlags(cmd, &clientCfg)
  49. config.RegisterProxyFlags(cmd, c)
  50. // add sub command for visitor
  51. if slices.Contains(visitorTypes, v1.VisitorType(typ)) {
  52. vc := v1.NewVisitorConfigurerByType(v1.VisitorType(typ))
  53. if vc == nil {
  54. panic("visitor type: " + typ + " not support")
  55. }
  56. visitorCmd := NewVisitorCommand(string(typ), vc, &clientCfg)
  57. config.RegisterVisitorFlags(visitorCmd, vc)
  58. cmd.AddCommand(visitorCmd)
  59. }
  60. rootCmd.AddCommand(cmd)
  61. }
  62. }
  63. func NewProxyCommand(name string, c v1.ProxyConfigurer, clientCfg *v1.ClientCommonConfig) *cobra.Command {
  64. return &cobra.Command{
  65. Use: name,
  66. Short: fmt.Sprintf("Run frpc with a single %s proxy", name),
  67. Run: func(cmd *cobra.Command, args []string) {
  68. if err := clientCfg.Complete(); err != nil {
  69. fmt.Println(err)
  70. os.Exit(1)
  71. }
  72. unsafeFeatures := security.NewUnsafeFeatures(allowUnsafe)
  73. if _, err := validation.ValidateClientCommonConfig(clientCfg, unsafeFeatures); err != nil {
  74. fmt.Println(err)
  75. os.Exit(1)
  76. }
  77. c.Complete(clientCfg.User)
  78. c.GetBaseConfig().Type = name
  79. if err := validation.ValidateProxyConfigurerForClient(c); err != nil {
  80. fmt.Println(err)
  81. os.Exit(1)
  82. }
  83. err := startService(clientCfg, []v1.ProxyConfigurer{c}, nil, unsafeFeatures, "")
  84. if err != nil {
  85. fmt.Println(err)
  86. os.Exit(1)
  87. }
  88. },
  89. }
  90. }
  91. func NewVisitorCommand(name string, c v1.VisitorConfigurer, clientCfg *v1.ClientCommonConfig) *cobra.Command {
  92. return &cobra.Command{
  93. Use: "visitor",
  94. Short: fmt.Sprintf("Run frpc with a single %s visitor", name),
  95. Run: func(cmd *cobra.Command, args []string) {
  96. if err := clientCfg.Complete(); err != nil {
  97. fmt.Println(err)
  98. os.Exit(1)
  99. }
  100. unsafeFeatures := security.NewUnsafeFeatures(allowUnsafe)
  101. if _, err := validation.ValidateClientCommonConfig(clientCfg, unsafeFeatures); err != nil {
  102. fmt.Println(err)
  103. os.Exit(1)
  104. }
  105. c.Complete(clientCfg)
  106. c.GetBaseConfig().Type = name
  107. if err := validation.ValidateVisitorConfigurer(c); err != nil {
  108. fmt.Println(err)
  109. os.Exit(1)
  110. }
  111. err := startService(clientCfg, nil, []v1.VisitorConfigurer{c}, unsafeFeatures, "")
  112. if err != nil {
  113. fmt.Println(err)
  114. os.Exit(1)
  115. }
  116. },
  117. }
  118. }