config.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. )
  16. var ProxyClients map[string]*models.ProxyClient = make(map[string]*models.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 := &models.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. }