http.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/types"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. "github.com/fatedier/frp/pkg/config/v1/validation"
  23. "github.com/fatedier/frp/pkg/consts"
  24. )
  25. func init() {
  26. RegisterCommonFlags(httpCmd)
  27. httpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  28. httpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
  29. httpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  30. httpCmd.PersistentFlags().StringVarP(&customDomains, "custom_domain", "d", "", "custom domain")
  31. httpCmd.PersistentFlags().StringVarP(&subDomain, "sd", "", "", "sub domain")
  32. httpCmd.PersistentFlags().StringVarP(&locations, "locations", "", "", "locations")
  33. httpCmd.PersistentFlags().StringVarP(&httpUser, "http_user", "", "", "http auth user")
  34. httpCmd.PersistentFlags().StringVarP(&httpPwd, "http_pwd", "", "", "http auth password")
  35. httpCmd.PersistentFlags().StringVarP(&hostHeaderRewrite, "host_header_rewrite", "", "", "host header rewrite")
  36. httpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  37. httpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  38. httpCmd.PersistentFlags().StringVarP(&bandwidthLimit, "bandwidth_limit", "", "", "bandwidth limit")
  39. httpCmd.PersistentFlags().StringVarP(&bandwidthLimitMode, "bandwidth_limit_mode", "", types.BandwidthLimitModeClient, "bandwidth limit mode")
  40. rootCmd.AddCommand(httpCmd)
  41. }
  42. var httpCmd = &cobra.Command{
  43. Use: "http",
  44. Short: "Run frpc with a single http proxy",
  45. RunE: func(cmd *cobra.Command, args []string) error {
  46. clientCfg, err := parseClientCommonCfgFromCmd()
  47. if err != nil {
  48. fmt.Println(err)
  49. os.Exit(1)
  50. }
  51. cfg := &v1.HTTPProxyConfig{}
  52. var prefix string
  53. if user != "" {
  54. prefix = user + "."
  55. }
  56. cfg.Name = prefix + proxyName
  57. cfg.Type = consts.HTTPProxy
  58. cfg.LocalIP = localIP
  59. cfg.LocalPort = localPort
  60. cfg.CustomDomains = strings.Split(customDomains, ",")
  61. cfg.SubDomain = subDomain
  62. cfg.Locations = strings.Split(locations, ",")
  63. cfg.HTTPUser = httpUser
  64. cfg.HTTPPassword = httpPwd
  65. cfg.HostHeaderRewrite = hostHeaderRewrite
  66. cfg.Transport.UseEncryption = useEncryption
  67. cfg.Transport.UseCompression = useCompression
  68. cfg.Transport.BandwidthLimit, err = types.NewBandwidthQuantity(bandwidthLimit)
  69. if err != nil {
  70. fmt.Println(err)
  71. os.Exit(1)
  72. }
  73. cfg.Transport.BandwidthLimitMode = bandwidthLimitMode
  74. if err := validation.ValidateProxyConfigurerForClient(cfg); err != nil {
  75. fmt.Println(err)
  76. os.Exit(1)
  77. }
  78. err = startService(clientCfg, []v1.ProxyConfigurer{cfg}, nil, "")
  79. if err != nil {
  80. fmt.Println(err)
  81. os.Exit(1)
  82. }
  83. return nil
  84. },
  85. }