1
0

proxy.go 3.2 KB

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