|
| 1 | +// Copyright 2018 fatedier, fatedier@gmail.com |
| 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 | + |
| 15 | +package frpc |
| 16 | + |
| 17 | +import ( |
| 18 | + "Erfrp/pkg/config" |
| 19 | + "Erfrp/pkg/consts" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/Gogods/cobra" |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + RegisterCommonFlags(httpCmd) |
| 29 | + |
| 30 | + httpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name") |
| 31 | + httpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip") |
| 32 | + httpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port") |
| 33 | + httpCmd.PersistentFlags().StringVarP(&customDomains, "custom_domain", "d", "", "custom domain") |
| 34 | + httpCmd.PersistentFlags().StringVarP(&subDomain, "sd", "", "", "sub domain") |
| 35 | + httpCmd.PersistentFlags().StringVarP(&locations, "locations", "", "", "locations") |
| 36 | + httpCmd.PersistentFlags().StringVarP(&httpUser, "http_user", "", "", "http auth user") |
| 37 | + httpCmd.PersistentFlags().StringVarP(&httpPwd, "http_pwd", "", "", "http auth password") |
| 38 | + httpCmd.PersistentFlags().StringVarP(&hostHeaderRewrite, "host_header_rewrite", "", "", "host header rewrite") |
| 39 | + httpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption") |
| 40 | + httpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression") |
| 41 | + |
| 42 | + rootCmd.AddCommand(httpCmd) |
| 43 | +} |
| 44 | + |
| 45 | +var httpCmd = &cobra.Command{ |
| 46 | + Use: "http", |
| 47 | + Short: "Run frpc with a single http proxy", |
| 48 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | + clientCfg, err := parseClientCommonCfgFromCmd() |
| 50 | + if err != nil { |
| 51 | + fmt.Println(err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + |
| 55 | + cfg := &config.HTTPProxyConf{} |
| 56 | + var prefix string |
| 57 | + if user != "" { |
| 58 | + prefix = user + "." |
| 59 | + } |
| 60 | + cfg.ProxyName = prefix + proxyName |
| 61 | + cfg.ProxyType = consts.HTTPProxy |
| 62 | + cfg.LocalIP = localIP |
| 63 | + cfg.LocalPort = localPort |
| 64 | + cfg.CustomDomains = strings.Split(customDomains, ",") |
| 65 | + cfg.SubDomain = subDomain |
| 66 | + cfg.Locations = strings.Split(locations, ",") |
| 67 | + cfg.HTTPUser = httpUser |
| 68 | + cfg.HTTPPwd = httpPwd |
| 69 | + cfg.HostHeaderRewrite = hostHeaderRewrite |
| 70 | + cfg.UseEncryption = useEncryption |
| 71 | + cfg.UseCompression = useCompression |
| 72 | + |
| 73 | + err = cfg.CheckForCli() |
| 74 | + if err != nil { |
| 75 | + fmt.Println(err) |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + |
| 79 | + proxyConfs := map[string]config.ProxyConf{ |
| 80 | + cfg.ProxyName: cfg, |
| 81 | + } |
| 82 | + err = startService(clientCfg, proxyConfs, nil, "") |
| 83 | + if err != nil { |
| 84 | + fmt.Println(err) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + return nil |
| 88 | + }, |
| 89 | +} |
0 commit comments