main.go 3.2 KB

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