config.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "frp/pkg/models"
  6. ini "github.com/vaughan0/go-ini"
  7. )
  8. // common config
  9. var (
  10. ServerAddr string = "0.0.0.0"
  11. ServerPort int64 = 7000
  12. LogFile string = "./frpc.log"
  13. LogLevel string = "warn"
  14. LogWay string = "file"
  15. HeartBeatInterval int64 = 5
  16. )
  17. var ProxyClients map[string]*models.ProxyClient = make(map[string]*models.ProxyClient)
  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", "server_addr")
  27. if ok {
  28. ServerAddr = tmpStr
  29. }
  30. tmpStr, ok = conf.Get("common", "server_port")
  31. if ok {
  32. ServerPort, _ = 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. tmpStr, ok = conf.Get("common", "heartbeat_interval")
  47. if ok {
  48. HeartBeatInterval, _ = strconv.ParseInt(tmpStr, 10, 64)
  49. }
  50. // servers
  51. for name, section := range conf {
  52. if name != "common" {
  53. proxyClient := &models.ProxyClient{}
  54. proxyClient.Name = name
  55. proxyClient.Passwd, ok = section["passwd"]
  56. if !ok {
  57. return fmt.Errorf("Parse ini file error: proxy [%s] no passwd found", proxyClient.Name)
  58. }
  59. portStr, ok := section["local_port"]
  60. if ok {
  61. proxyClient.LocalPort, err = strconv.ParseInt(portStr, 10, 64)
  62. if err != nil {
  63. return fmt.Errorf("Parse ini file error: proxy [%s] local_port error", proxyClient.Name)
  64. }
  65. } else {
  66. return fmt.Errorf("Parse ini file error: proxy [%s] local_port not found", proxyClient.Name)
  67. }
  68. ProxyClients[proxyClient.Name] = proxyClient
  69. }
  70. }
  71. if len(ProxyClients) == 0 {
  72. return fmt.Errorf("Parse ini file error: no proxy config found")
  73. }
  74. return nil
  75. }