main.go 3.2 KB

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