config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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. // servers
  47. for name, section := range conf {
  48. if name != "common" {
  49. proxyClient := &models.ProxyClient{}
  50. proxyClient.Name = name
  51. proxyClient.Passwd, ok = section["passwd"]
  52. if !ok {
  53. return fmt.Errorf("Parse ini file error: proxy [%s] no passwd found", proxyClient.Name)
  54. }
  55. portStr, ok := section["local_port"]
  56. if ok {
  57. proxyClient.LocalPort, err = strconv.ParseInt(portStr, 10, 64)
  58. if err != nil {
  59. return fmt.Errorf("Parse ini file error: proxy [%s] local_port error", proxyClient.Name)
  60. }
  61. } else {
  62. return fmt.Errorf("Parse ini file error: proxy [%s] local_port not found", proxyClient.Name)
  63. }
  64. ProxyClients[proxyClient.Name] = proxyClient
  65. }
  66. }
  67. if len(ProxyClients) == 0 {
  68. return fmt.Errorf("Parse ini file error: no proxy config found")
  69. }
  70. return nil
  71. }