http.go 2.8 KB

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