http.go 3.6 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/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. httpCmd.PersistentFlags().BoolVarP(&tlsEnable, "tls_enable", "", false, "enable frpc tls")
  44. rootCmd.AddCommand(httpCmd)
  45. }
  46. var httpCmd = &cobra.Command{
  47. Use: "http",
  48. Short: "Run frpc with a single http proxy",
  49. RunE: func(cmd *cobra.Command, args []string) error {
  50. clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "")
  51. if err != nil {
  52. fmt.Println(err)
  53. os.Exit(1)
  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. 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. }