client_common.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "fmt"
  17. "os"
  18. "strconv"
  19. ini "github.com/vaughan0/go-ini"
  20. )
  21. var ClientCommonCfg *ClientCommonConf
  22. // client common config
  23. type ClientCommonConf struct {
  24. ConfigFile string
  25. ServerAddr string
  26. ServerPort int64
  27. HttpProxy string
  28. LogFile string
  29. LogWay string
  30. LogLevel string
  31. LogMaxDays int64
  32. PrivilegeToken string
  33. PoolCount int
  34. User string
  35. HeartBeatInterval int64
  36. HeartBeatTimeout int64
  37. }
  38. func GetDeaultClientCommonConf() *ClientCommonConf {
  39. return &ClientCommonConf{
  40. ConfigFile: "./frpc.ini",
  41. ServerAddr: "0.0.0.0",
  42. ServerPort: 7000,
  43. HttpProxy: "",
  44. LogFile: "console",
  45. LogWay: "console",
  46. LogLevel: "info",
  47. LogMaxDays: 3,
  48. PrivilegeToken: "",
  49. PoolCount: 1,
  50. User: "",
  51. HeartBeatInterval: 30,
  52. HeartBeatTimeout: 90,
  53. }
  54. }
  55. func LoadClientCommonConf(conf ini.File) (cfg *ClientCommonConf, err error) {
  56. var (
  57. tmpStr string
  58. ok bool
  59. v int64
  60. )
  61. cfg = GetDeaultClientCommonConf()
  62. tmpStr, ok = conf.Get("common", "server_addr")
  63. if ok {
  64. cfg.ServerAddr = tmpStr
  65. }
  66. tmpStr, ok = conf.Get("common", "server_port")
  67. if ok {
  68. cfg.ServerPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  69. }
  70. tmpStr, ok = conf.Get("common", "http_proxy")
  71. if ok {
  72. cfg.HttpProxy = tmpStr
  73. } else {
  74. // get http_proxy from env
  75. cfg.HttpProxy = os.Getenv("http_proxy")
  76. }
  77. tmpStr, ok = conf.Get("common", "log_file")
  78. if ok {
  79. cfg.LogFile = tmpStr
  80. if cfg.LogFile == "console" {
  81. cfg.LogWay = "console"
  82. } else {
  83. cfg.LogWay = "file"
  84. }
  85. }
  86. tmpStr, ok = conf.Get("common", "log_level")
  87. if ok {
  88. cfg.LogLevel = tmpStr
  89. }
  90. tmpStr, ok = conf.Get("common", "log_max_days")
  91. if ok {
  92. cfg.LogMaxDays, _ = strconv.ParseInt(tmpStr, 10, 64)
  93. }
  94. tmpStr, ok = conf.Get("common", "privilege_token")
  95. if ok {
  96. cfg.PrivilegeToken = tmpStr
  97. }
  98. tmpStr, ok = conf.Get("common", "pool_count")
  99. if ok {
  100. v, err = strconv.ParseInt(tmpStr, 10, 64)
  101. if err != nil {
  102. cfg.PoolCount = 1
  103. } else {
  104. cfg.PoolCount = int(v)
  105. }
  106. }
  107. tmpStr, ok = conf.Get("common", "user")
  108. if ok {
  109. cfg.User = tmpStr
  110. }
  111. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  112. if ok {
  113. v, err = strconv.ParseInt(tmpStr, 10, 64)
  114. if err != nil {
  115. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  116. return
  117. } else {
  118. cfg.HeartBeatTimeout = v
  119. }
  120. }
  121. tmpStr, ok = conf.Get("common", "heartbeat_interval")
  122. if ok {
  123. v, err = strconv.ParseInt(tmpStr, 10, 64)
  124. if err != nil {
  125. err = fmt.Errorf("Parse conf error: heartbeat_interval is incorrect")
  126. return
  127. } else {
  128. cfg.HeartBeatInterval = v
  129. }
  130. }
  131. if cfg.HeartBeatInterval <= 0 {
  132. err = fmt.Errorf("Parse conf error: heartbeat_interval is incorrect")
  133. return
  134. }
  135. if cfg.HeartBeatTimeout < cfg.HeartBeatInterval {
  136. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect, heartbeat_timeout is less than heartbeat_interval")
  137. return
  138. }
  139. return
  140. }