root.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. "io/ioutil"
  18. "os"
  19. "os/signal"
  20. "strconv"
  21. "strings"
  22. "syscall"
  23. "time"
  24. "github.com/spf13/cobra"
  25. ini "github.com/vaughan0/go-ini"
  26. "github.com/fatedier/frp/client"
  27. "github.com/fatedier/frp/g"
  28. "github.com/fatedier/frp/models/config"
  29. "github.com/fatedier/frp/utils/log"
  30. "github.com/fatedier/frp/utils/version"
  31. )
  32. const (
  33. CfgFileTypeIni = iota
  34. CfgFileTypeCmd
  35. )
  36. var (
  37. cfgFile string
  38. showVersion bool
  39. serverAddr string
  40. user string
  41. protocol string
  42. token string
  43. logLevel string
  44. logFile string
  45. logMaxDays int
  46. proxyName string
  47. localIp string
  48. localPort int
  49. remotePort int
  50. useEncryption bool
  51. useCompression bool
  52. customDomains string
  53. subDomain string
  54. httpUser string
  55. httpPwd string
  56. locations string
  57. hostHeaderRewrite string
  58. role string
  59. sk string
  60. serverName string
  61. bindAddr string
  62. )
  63. func init() {
  64. rootCmd.PersistentFlags().StringVarP(&cfgFile, "", "c", "./frpc.ini", "config file of frpc")
  65. rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
  66. }
  67. var rootCmd = &cobra.Command{
  68. Use: "frpc",
  69. Short: "frpc is the client of frp (https://github.com/fatedier/frp)",
  70. RunE: func(cmd *cobra.Command, args []string) error {
  71. if showVersion {
  72. fmt.Println(version.Full())
  73. return nil
  74. }
  75. // Do not show command usage here.
  76. err := runClient(cfgFile)
  77. if err != nil {
  78. fmt.Println(err)
  79. os.Exit(1)
  80. }
  81. return nil
  82. },
  83. }
  84. func Execute() {
  85. if err := rootCmd.Execute(); err != nil {
  86. os.Exit(1)
  87. }
  88. }
  89. func handleSignal(svr *client.Service) {
  90. ch := make(chan os.Signal)
  91. signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  92. <-ch
  93. svr.Close()
  94. time.Sleep(250 * time.Millisecond)
  95. os.Exit(0)
  96. }
  97. func parseClientCommonCfg(fileType int, filePath string) (err error) {
  98. if fileType == CfgFileTypeIni {
  99. err = parseClientCommonCfgFromIni(filePath)
  100. } else if fileType == CfgFileTypeCmd {
  101. err = parseClientCommonCfgFromCmd()
  102. }
  103. if err != nil {
  104. return
  105. }
  106. g.GlbClientCfg.CfgFile = cfgFile
  107. err = g.GlbClientCfg.ClientCommonConf.Check()
  108. if err != nil {
  109. return
  110. }
  111. return
  112. }
  113. func parseClientCommonCfgFromIni(filePath string) (err error) {
  114. b, err := ioutil.ReadFile(filePath)
  115. if err != nil {
  116. return err
  117. }
  118. content := string(b)
  119. cfg, err := config.UnmarshalClientConfFromIni(&g.GlbClientCfg.ClientCommonConf, content)
  120. if err != nil {
  121. return err
  122. }
  123. g.GlbClientCfg.ClientCommonConf = *cfg
  124. return
  125. }
  126. func parseClientCommonCfgFromCmd() (err error) {
  127. strs := strings.Split(serverAddr, ":")
  128. if len(strs) < 2 {
  129. err = fmt.Errorf("invalid server_addr")
  130. return
  131. }
  132. if strs[0] != "" {
  133. g.GlbClientCfg.ServerAddr = strs[0]
  134. }
  135. g.GlbClientCfg.ServerPort, err = strconv.Atoi(strs[1])
  136. if err != nil {
  137. err = fmt.Errorf("invalid server_addr")
  138. return
  139. }
  140. g.GlbClientCfg.User = user
  141. g.GlbClientCfg.Protocol = protocol
  142. g.GlbClientCfg.Token = token
  143. g.GlbClientCfg.LogLevel = logLevel
  144. g.GlbClientCfg.LogFile = logFile
  145. g.GlbClientCfg.LogMaxDays = int64(logMaxDays)
  146. return nil
  147. }
  148. func runClient(cfgFilePath string) (err error) {
  149. err = parseClientCommonCfg(CfgFileTypeIni, cfgFilePath)
  150. if err != nil {
  151. return
  152. }
  153. conf, err := ini.LoadFile(cfgFilePath)
  154. if err != nil {
  155. return err
  156. }
  157. pxyCfgs, visitorCfgs, err := config.LoadProxyConfFromIni(g.GlbClientCfg.User, conf, g.GlbClientCfg.Start)
  158. if err != nil {
  159. return err
  160. }
  161. err = startService(pxyCfgs, visitorCfgs)
  162. return
  163. }
  164. func startService(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.ProxyConf) (err error) {
  165. log.InitLog(g.GlbClientCfg.LogWay, g.GlbClientCfg.LogFile, g.GlbClientCfg.LogLevel, g.GlbClientCfg.LogMaxDays)
  166. svr := client.NewService(pxyCfgs, visitorCfgs)
  167. // Capture the exit signal if we use kcp.
  168. if g.GlbClientCfg.Protocol == "kcp" {
  169. go handleSignal(svr)
  170. }
  171. err = svr.Run()
  172. return
  173. }