config.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/fatedier/frp/pkg/models"
  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. )
  16. var ProxyServers map[string]*models.ProxyServer = make(map[string]*models.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 := &models.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. }