config.go 1.9 KB

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