| // |
| 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 |
| 15 | package sub |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "os" |
| 20 | "slices" |
| 21 |
| 22 | "github.com/spf13/cobra" |
| 23 |
| 24 | "github.com/fatedier/frp/pkg/config" |
| 25 | "github.com/fatedier/frp/pkg/config/source" |
| 26 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 27 | "github.com/fatedier/frp/pkg/config/v1/validation" |
| 28 | "github.com/fatedier/frp/pkg/policy/security" |
| 29 | ) |
| 30 |
| 31 | var proxyTypes = []v1.ProxyType{ |
| 32 | v1.ProxyTypeTCP, |
| 33 | v1.ProxyTypeUDP, |
| 34 | v1.ProxyTypeTCPMUX, |
| 35 | v1.ProxyTypeHTTP, |
| 36 | v1.ProxyTypeHTTPS, |
| 37 | v1.ProxyTypeSTCP, |
| 38 | v1.ProxyTypeSUDP, |
| 39 | v1.ProxyTypeXTCP, |
| 40 | } |
| 41 |
| 42 | var visitorTypes = []v1.VisitorType{ |
| 43 | v1.VisitorTypeSTCP, |
| 44 | v1.VisitorTypeSUDP, |
| 45 | v1.VisitorTypeXTCP, |
| 46 | } |
| 47 |
| 48 | func init() { |
| 49 | for _, typ := range proxyTypes { |
| 50 | c := v1.NewProxyConfigurerByType(typ) |
| 51 | if c == nil { |
| 52 | panic("proxy type: " + typ + " not support") |
| 53 | } |
| 54 | clientCfg := v1.ClientCommonConfig{} |
| 55 | cmd := NewProxyCommand(string(typ), c, &clientCfg) |
| 56 | config.RegisterClientCommonConfigFlags(cmd, &clientCfg) |
| 57 | config.RegisterProxyFlags(cmd, c) |
| 58 |
| 59 | // add sub command for visitor |
| 60 | if slices.Contains(visitorTypes, v1.VisitorType(typ)) { |
| 61 | vc := v1.NewVisitorConfigurerByType(v1.VisitorType(typ)) |
| 62 | if vc == nil { |
| 63 | panic("visitor type: " + typ + " not support") |
| 64 | } |
| 65 | visitorCmd := NewVisitorCommand(string(typ), vc, &clientCfg) |
| 66 | config.RegisterVisitorFlags(visitorCmd, vc) |
| 67 | cmd.AddCommand(visitorCmd) |
| 68 | } |
| 69 | rootCmd.AddCommand(cmd) |
| 70 | } |
| 71 | } |
| 72 |
| 73 | func NewProxyCommand(name string, c v1.ProxyConfigurer, clientCfg *v1.ClientCommonConfig) *cobra.Command { |
| 74 | return &cobra.Command{ |
| 75 | Use: name, |
| 76 | Short: fmt.Sprintf("Run frpc with a single %s proxy", name), |
| 77 | Run: func(cmd *cobra.Command, args []string) { |
| 78 | if err := clientCfg.Complete(); err != nil { |
| 79 | fmt.Println(err) |
| 80 | os.Exit(1) |
| 81 | } |
| 82 |
| 83 | unsafeFeatures := security.NewUnsafeFeatures(allowUnsafe) |
| 84 | validator := validation.NewConfigValidator(unsafeFeatures) |
| 85 | if _, err := validator.ValidateClientCommonConfig(clientCfg); err != nil { |
| 86 | fmt.Println(err) |
| 87 | os.Exit(1) |
| 88 | } |
| 89 |
| 90 | c.GetBaseConfig().Type = name |
| 91 | c.Complete() |
| 92 | proxyCfg := c |
| 93 | if err := validation.ValidateProxyConfigurerForClient(proxyCfg); err != nil { |
| 94 | fmt.Println(err) |
| 95 | os.Exit(1) |
| 96 | } |
| 97 | err := startService(clientCfg, []v1.ProxyConfigurer{proxyCfg}, nil, unsafeFeatures, "") |
| 98 | if err != nil { |
| 99 | fmt.Println(err) |
| 100 | os.Exit(1) |
| 101 | } |
| 102 | }, |
| 103 | } |
| 104 | } |
| 105 |
| 106 | func NewVisitorCommand(name string, c v1.VisitorConfigurer, clientCfg *v1.ClientCommonConfig) *cobra.Command { |
| 107 | return &cobra.Command{ |
| 108 | Use: "visitor", |
| 109 | Short: fmt.Sprintf("Run frpc with a single %s visitor", name), |
| 110 | Run: func(cmd *cobra.Command, args []string) { |
| 111 | if err := clientCfg.Complete(); err != nil { |
| 112 | fmt.Println(err) |
| 113 | os.Exit(1) |
| 114 | } |
| 115 | unsafeFeatures := security.NewUnsafeFeatures(allowUnsafe) |
| 116 | validator := validation.NewConfigValidator(unsafeFeatures) |
| 117 | if _, err := validator.ValidateClientCommonConfig(clientCfg); err != nil { |
| 118 | fmt.Println(err) |
| 119 | os.Exit(1) |
| 120 | } |
| 121 |
| 122 | c.GetBaseConfig().Type = name |
| 123 | c.Complete() |
| 124 | visitorCfg := c |
| 125 | if err := validation.ValidateVisitorConfigurer(visitorCfg); err != nil { |
| 126 | fmt.Println(err) |
| 127 | os.Exit(1) |
| 128 | } |
| 129 | err := startService(clientCfg, nil, []v1.VisitorConfigurer{visitorCfg}, unsafeFeatures, "") |
| 130 | if err != nil { |
| 131 | fmt.Println(err) |
| 132 | os.Exit(1) |
| 133 | } |
| 134 | }, |
| 135 | } |
| 136 | } |
| 137 |
| 138 | func startService( |
| 139 | cfg *v1.ClientCommonConfig, |
| 140 | proxyCfgs []v1.ProxyConfigurer, |
| 141 | visitorCfgs []v1.VisitorConfigurer, |
| 142 | unsafeFeatures *security.UnsafeFeatures, |
| 143 | cfgFile string, |
| 144 | ) error { |
| 145 | configSource := source.NewConfigSource() |
| 146 | if err := configSource.ReplaceAll(proxyCfgs, visitorCfgs); err != nil { |
| 147 | return fmt.Errorf("failed to set config source: %w", err) |
| 148 | } |
| 149 | aggregator := source.NewAggregator(configSource) |
| 150 | return startServiceWithAggregator(cfg, aggregator, unsafeFeatures, cfgFile) |
| 151 | } |