http.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/spf13/cobra"
  20. "github.com/fatedier/frp/pkg/config"
  21. "github.com/fatedier/frp/pkg/consts"
  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. httpCmd.PersistentFlags().StringVarP(&bandwidthLimit, "bandwidth_limit", "", "", "bandwidth limit")
  37. httpCmd.PersistentFlags().StringVarP(&bandwidthLimitMode, "bandwidth_limit_mode", "", config.BandwidthLimitModeClient, "bandwidth limit mode")
  38. rootCmd.AddCommand(httpCmd)
  39. }
  40. var httpCmd = &cobra.Command{
  41. Use: "http",
  42. Short: "Run frpc with a single http proxy",
  43. RunE: func(cmd *cobra.Command, args []string) error {
  44. clientCfg, err := parseClientCommonCfgFromCmd()
  45. if err != nil {
  46. fmt.Println(err)
  47. os.Exit(1)
  48. }
  49. cfg := &config.HTTPProxyConf{}
  50. var prefix string
  51. if user != "" {
  52. prefix = user + "."
  53. }
  54. cfg.ProxyName = prefix + proxyName
  55. cfg.ProxyType = consts.HTTPProxy
  56. cfg.LocalIP = localIP
  57. cfg.LocalPort = localPort
  58. cfg.CustomDomains = strings.Split(customDomains, ",")
  59. cfg.SubDomain = subDomain
  60. cfg.Locations = strings.Split(locations, ",")
  61. cfg.HTTPUser = httpUser
  62. cfg.HTTPPwd = httpPwd
  63. cfg.HostHeaderRewrite = hostHeaderRewrite
  64. cfg.UseEncryption = useEncryption
  65. cfg.UseCompression = useCompression
  66. cfg.BandwidthLimit, err = config.NewBandwidthQuantity(bandwidthLimit)
  67. if err != nil {
  68. fmt.Println(err)
  69. os.Exit(1)
  70. }
  71. cfg.BandwidthLimitMode = bandwidthLimitMode
  72. err = cfg.CheckForCli()
  73. if err != nil {
  74. fmt.Println(err)
  75. os.Exit(1)
  76. }
  77. proxyConfs := map[string]config.ProxyConf{
  78. cfg.ProxyName: cfg,
  79. }
  80. err = startService(clientCfg, proxyConfs, nil, "")
  81. if err != nil {
  82. fmt.Println(err)
  83. os.Exit(1)
  84. }
  85. return nil
  86. },
  87. }