main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "fmt"
  17. "os"
  18. "os/signal"
  19. "strconv"
  20. "strings"
  21. "syscall"
  22. "time"
  23. docopt "github.com/docopt/docopt-go"
  24. ini "github.com/vaughan0/go-ini"
  25. "github.com/fatedier/frp/client"
  26. "github.com/fatedier/frp/models/config"
  27. "github.com/fatedier/frp/utils/log"
  28. "github.com/fatedier/frp/utils/version"
  29. )
  30. var (
  31. configFile string = "./frpc.ini"
  32. )
  33. var usage string = `frpc is the client of frp
  34. Usage:
  35. frpc [-c config_file] [-L log_file] [--log-level=<log_level>] [--server-addr=<server_addr>]
  36. frpc -h | --help
  37. frpc -v | --version
  38. Options:
  39. -c config_file set config file
  40. -L log_file set output log file, including console
  41. --log-level=<log_level> set log level: debug, info, warn, error
  42. --server-addr=<server_addr> addr which frps is listening for, example: 0.0.0.0:7000
  43. -h --help show this screen
  44. -v --version show version
  45. `
  46. func main() {
  47. var err error
  48. confFile := "./frpc.ini"
  49. // the configures parsed from file will be replaced by those from command line if exist
  50. args, err := docopt.Parse(usage, nil, true, version.Full(), false)
  51. if args["-c"] != nil {
  52. confFile = args["-c"].(string)
  53. }
  54. conf, err := ini.LoadFile(confFile)
  55. if err != nil {
  56. fmt.Println(err)
  57. os.Exit(1)
  58. }
  59. config.ClientCommonCfg, err = config.LoadClientCommonConf(conf)
  60. if err != nil {
  61. fmt.Println(err)
  62. os.Exit(1)
  63. }
  64. if args["-L"] != nil {
  65. if args["-L"].(string) == "console" {
  66. config.ClientCommonCfg.LogWay = "console"
  67. } else {
  68. config.ClientCommonCfg.LogWay = "file"
  69. config.ClientCommonCfg.LogFile = args["-L"].(string)
  70. }
  71. }
  72. if args["--log-level"] != nil {
  73. config.ClientCommonCfg.LogLevel = args["--log-level"].(string)
  74. }
  75. if args["--server-addr"] != nil {
  76. addr := strings.Split(args["--server-addr"].(string), ":")
  77. if len(addr) != 2 {
  78. fmt.Println("--server-addr format error: example 0.0.0.0:7000")
  79. os.Exit(1)
  80. }
  81. serverPort, err := strconv.ParseInt(addr[1], 10, 64)
  82. if err != nil {
  83. fmt.Println("--server-addr format error, example 0.0.0.0:7000")
  84. os.Exit(1)
  85. }
  86. config.ClientCommonCfg.ServerAddr = addr[0]
  87. config.ClientCommonCfg.ServerPort = serverPort
  88. }
  89. if args["-v"] != nil {
  90. if args["-v"].(bool) {
  91. fmt.Println(version.Full())
  92. os.Exit(0)
  93. }
  94. }
  95. pxyCfgs, vistorCfgs, err := config.LoadProxyConfFromFile(config.ClientCommonCfg.User, conf, config.ClientCommonCfg.Start)
  96. if err != nil {
  97. fmt.Println(err)
  98. os.Exit(1)
  99. }
  100. log.InitLog(config.ClientCommonCfg.LogWay, config.ClientCommonCfg.LogFile,
  101. config.ClientCommonCfg.LogLevel, config.ClientCommonCfg.LogMaxDays)
  102. svr := client.NewService(pxyCfgs, vistorCfgs)
  103. // Capture the exit signal if we use kcp.
  104. if config.ClientCommonCfg.Protocol == "kcp" {
  105. go HandleSignal(svr)
  106. }
  107. err = svr.Run()
  108. if err != nil {
  109. fmt.Println(err)
  110. os.Exit(1)
  111. }
  112. }
  113. func HandleSignal(svr *client.Service) {
  114. ch := make(chan os.Signal)
  115. signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  116. <-ch
  117. svr.Close()
  118. time.Sleep(250 * time.Millisecond)
  119. os.Exit(0)
  120. }