main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2016 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 main
  15. import (
  16. "encoding/base64"
  17. "encoding/json"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "os"
  22. "os/signal"
  23. "strconv"
  24. "strings"
  25. "syscall"
  26. "time"
  27. docopt "github.com/docopt/docopt-go"
  28. ini "github.com/vaughan0/go-ini"
  29. "github.com/fatedier/frp/client"
  30. "github.com/fatedier/frp/models/config"
  31. "github.com/fatedier/frp/utils/log"
  32. "github.com/fatedier/frp/utils/version"
  33. )
  34. var (
  35. configFile string = "./frpc.ini"
  36. )
  37. var usage string = `frpc is the client of frp
  38. Usage:
  39. frpc [-c config_file] [-L log_file] [--log-level=<log_level>] [--server-addr=<server_addr>]
  40. frpc [-c config_file] --reload
  41. frpc -h | --help
  42. frpc -v | --version
  43. Options:
  44. -c config_file set config file
  45. -L log_file set output log file, including console
  46. --log-level=<log_level> set log level: debug, info, warn, error
  47. --server-addr=<server_addr> addr which frps is listening for, example: 0.0.0.0:7000
  48. --reload reload configure file without program exit
  49. -h --help show this screen
  50. -v --version show version
  51. `
  52. func main() {
  53. var err error
  54. confFile := "./frpc.ini"
  55. // the configures parsed from file will be replaced by those from command line if exist
  56. args, err := docopt.Parse(usage, nil, true, version.Full(), false)
  57. if args["-c"] != nil {
  58. confFile = args["-c"].(string)
  59. }
  60. conf, err := ini.LoadFile(confFile)
  61. if err != nil {
  62. fmt.Println(err)
  63. os.Exit(1)
  64. }
  65. config.ClientCommonCfg, err = config.LoadClientCommonConf(conf)
  66. if err != nil {
  67. fmt.Println(err)
  68. os.Exit(1)
  69. }
  70. config.ClientCommonCfg.ConfigFile = confFile
  71. // check if reload command
  72. if args["--reload"] != nil {
  73. if args["--reload"].(bool) {
  74. req, err := http.NewRequest("GET", "http://"+
  75. config.ClientCommonCfg.AdminAddr+":"+fmt.Sprintf("%d", config.ClientCommonCfg.AdminPort)+"/api/reload", nil)
  76. if err != nil {
  77. fmt.Printf("frps reload error: %v\n", err)
  78. os.Exit(1)
  79. }
  80. authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(config.ClientCommonCfg.AdminUser+":"+
  81. config.ClientCommonCfg.AdminPwd))
  82. req.Header.Add("Authorization", authStr)
  83. resp, err := http.DefaultClient.Do(req)
  84. if err != nil {
  85. fmt.Printf("frpc reload error: %v\n", err)
  86. os.Exit(1)
  87. } else {
  88. defer resp.Body.Close()
  89. body, err := ioutil.ReadAll(resp.Body)
  90. if err != nil {
  91. fmt.Printf("frpc reload error: %v\n", err)
  92. os.Exit(1)
  93. }
  94. res := &client.GeneralResponse{}
  95. err = json.Unmarshal(body, &res)
  96. if err != nil {
  97. fmt.Printf("http response error: %s\n", strings.TrimSpace(string(body)))
  98. os.Exit(1)
  99. } else if res.Code != 0 {
  100. fmt.Printf("reload error: %s\n", res.Msg)
  101. os.Exit(1)
  102. }
  103. fmt.Printf("reload success\n")
  104. os.Exit(0)
  105. }
  106. }
  107. }
  108. if args["-L"] != nil {
  109. if args["-L"].(string) == "console" {
  110. config.ClientCommonCfg.LogWay = "console"
  111. } else {
  112. config.ClientCommonCfg.LogWay = "file"
  113. config.ClientCommonCfg.LogFile = args["-L"].(string)
  114. }
  115. }
  116. if args["--log-level"] != nil {
  117. config.ClientCommonCfg.LogLevel = args["--log-level"].(string)
  118. }
  119. if args["--server-addr"] != nil {
  120. addr := strings.Split(args["--server-addr"].(string), ":")
  121. if len(addr) != 2 {
  122. fmt.Println("--server-addr format error: example 0.0.0.0:7000")
  123. os.Exit(1)
  124. }
  125. serverPort, err := strconv.ParseInt(addr[1], 10, 64)
  126. if err != nil {
  127. fmt.Println("--server-addr format error, example 0.0.0.0:7000")
  128. os.Exit(1)
  129. }
  130. config.ClientCommonCfg.ServerAddr = addr[0]
  131. config.ClientCommonCfg.ServerPort = serverPort
  132. }
  133. if args["-v"] != nil {
  134. if args["-v"].(bool) {
  135. fmt.Println(version.Full())
  136. os.Exit(0)
  137. }
  138. }
  139. pxyCfgs, vistorCfgs, err := config.LoadProxyConfFromFile(config.ClientCommonCfg.User, conf, config.ClientCommonCfg.Start)
  140. if err != nil {
  141. fmt.Println(err)
  142. os.Exit(1)
  143. }
  144. log.InitLog(config.ClientCommonCfg.LogWay, config.ClientCommonCfg.LogFile,
  145. config.ClientCommonCfg.LogLevel, config.ClientCommonCfg.LogMaxDays)
  146. svr := client.NewService(pxyCfgs, vistorCfgs)
  147. // Capture the exit signal if we use kcp.
  148. if config.ClientCommonCfg.Protocol == "kcp" {
  149. go HandleSignal(svr)
  150. }
  151. err = svr.Run()
  152. if err != nil {
  153. fmt.Println(err)
  154. os.Exit(1)
  155. }
  156. }
  157. func HandleSignal(svr *client.Service) {
  158. ch := make(chan os.Signal)
  159. signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  160. <-ch
  161. svr.Close()
  162. time.Sleep(250 * time.Millisecond)
  163. os.Exit(0)
  164. }