config.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/fatedier/frp/models/server"
  6. ini "github.com/vaughan0/go-ini"
  7. )
  8. // common config
  9. var (
  10. BindAddr string = "0.0.0.0"
  11. BindPort int64 = 9527
  12. LogFile string = "./frps.log"
  13. LogLevel string = "warn"
  14. LogWay string = "file"
  15. HeartBeatTimeout int64 = 30
  16. )
  17. var ProxyServers map[string]*server.ProxyServer = make(map[string]*server.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 := &server.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. }