config.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package server
  2. import (
  3. "fmt"
  4. "strconv"
  5. ini "github.com/vaughan0/go-ini"
  6. )
  7. // common config
  8. var (
  9. BindAddr string = "0.0.0.0"
  10. BindPort int64 = 9527
  11. LogFile string = "./frps.log"
  12. LogLevel string = "warn"
  13. LogWay string = "file"
  14. HeartBeatTimeout int64 = 30
  15. )
  16. var ProxyServers map[string]*ProxyServer = make(map[string]*ProxyServer)
  17. func LoadConf(confFile string) (err error) {
  18. var tmpStr string
  19. var ok bool
  20. conf, err := ini.LoadFile(confFile)
  21. if err != nil {
  22. return err
  23. }
  24. // common
  25. tmpStr, ok = conf.Get("common", "bind_addr")
  26. if ok {
  27. BindAddr = tmpStr
  28. }
  29. tmpStr, ok = conf.Get("common", "bind_port")
  30. if ok {
  31. BindPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  32. }
  33. tmpStr, ok = conf.Get("common", "log_file")
  34. if ok {
  35. LogFile = tmpStr
  36. }
  37. tmpStr, ok = conf.Get("common", "log_level")
  38. if ok {
  39. LogLevel = tmpStr
  40. }
  41. tmpStr, ok = conf.Get("common", "log_way")
  42. if ok {
  43. LogWay = tmpStr
  44. }
  45. // servers
  46. for name, section := range conf {
  47. if name != "common" {
  48. proxyServer := &ProxyServer{}
  49. proxyServer.Name = name
  50. proxyServer.Passwd, ok = section["passwd"]
  51. if !ok {
  52. return fmt.Errorf("Parse ini file error: proxy [%s] no passwd found", proxyServer.Name)
  53. }
  54. proxyServer.BindAddr, ok = section["bind_addr"]
  55. if !ok {
  56. proxyServer.BindAddr = "0.0.0.0"
  57. }
  58. portStr, ok := section["listen_port"]
  59. if ok {
  60. proxyServer.ListenPort, err = strconv.ParseInt(portStr, 10, 64)
  61. if err != nil {
  62. return fmt.Errorf("Parse ini file error: proxy [%s] listen_port error", proxyServer.Name)
  63. }
  64. } else {
  65. return fmt.Errorf("Parse ini file error: proxy [%s] listen_port not found", proxyServer.Name)
  66. }
  67. proxyServer.Init()
  68. ProxyServers[proxyServer.Name] = proxyServer
  69. }
  70. }
  71. if len(ProxyServers) == 0 {
  72. return fmt.Errorf("Parse ini file error: no proxy config found")
  73. }
  74. return nil
  75. }