http.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/models/config"
  21. "github.com/fatedier/frp/models/consts"
  22. )
  23. func init() {
  24. httpCmd.PersistentFlags().StringVarP(&serverAddr, "server_addr", "s", "127.0.0.1:7000", "frp server's address")
  25. httpCmd.PersistentFlags().StringVarP(&user, "user", "u", "", "user")
  26. httpCmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp or kcp or websocket")
  27. httpCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
  28. httpCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
  29. httpCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "console or file path")
  30. httpCmd.PersistentFlags().IntVarP(&logMaxDays, "log_max_days", "", 3, "log file reversed days")
  31. httpCmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
  32. httpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  33. httpCmd.PersistentFlags().StringVarP(&localIp, "local_ip", "i", "127.0.0.1", "local ip")
  34. httpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  35. httpCmd.PersistentFlags().StringVarP(&customDomains, "custom_domain", "d", "", "custom domain")
  36. httpCmd.PersistentFlags().StringVarP(&subDomain, "sd", "", "", "sub domain")
  37. httpCmd.PersistentFlags().StringVarP(&locations, "locations", "", "", "locations")
  38. httpCmd.PersistentFlags().StringVarP(&httpUser, "http_user", "", "", "http auth user")
  39. httpCmd.PersistentFlags().StringVarP(&httpPwd, "http_pwd", "", "", "http auth password")
  40. httpCmd.PersistentFlags().StringVarP(&hostHeaderRewrite, "host_header_rewrite", "", "", "host header rewrite")
  41. httpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  42. httpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  43. rootCmd.AddCommand(httpCmd)
  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. err := parseClientCommonCfg(CfgFileTypeCmd, "")
  50. if err != nil {
  51. fmt.Println(err)
  52. os.Exit(1)
  53. }
  54. cfg := &config.HttpProxyConf{}
  55. var prefix string
  56. if user != "" {
  57. prefix = user + "."
  58. }
  59. cfg.ProxyName = prefix + proxyName
  60. cfg.ProxyType = consts.HttpProxy
  61. cfg.LocalIp = localIp
  62. cfg.LocalPort = localPort
  63. cfg.CustomDomains = strings.Split(customDomains, ",")
  64. cfg.SubDomain = subDomain
  65. cfg.Locations = strings.Split(locations, ",")
  66. cfg.HttpUser = httpUser
  67. cfg.HttpPwd = httpPwd
  68. cfg.HostHeaderRewrite = hostHeaderRewrite
  69. cfg.UseEncryption = useEncryption
  70. cfg.UseCompression = useCompression
  71. err = cfg.CheckForCli()
  72. if err != nil {
  73. fmt.Println(err)
  74. os.Exit(1)
  75. }
  76. proxyConfs := map[string]config.ProxyConf{
  77. cfg.ProxyName: cfg,
  78. }
  79. err = startService(proxyConfs, nil)
  80. if err != nil {
  81. fmt.Println(err)
  82. os.Exit(1)
  83. }
  84. return nil
  85. },
  86. }