config.go 1.7 KB

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