1
0

proxy.go 3.2 KB

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