client_common.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. TcpMux bool
  35. User string
  36. LoginFailExit bool
  37. HeartBeatInterval int64
  38. HeartBeatTimeout int64
  39. }
  40. func GetDeaultClientCommonConf() *ClientCommonConf {
  41. return &ClientCommonConf{
  42. ConfigFile: "./frpc.ini",
  43. ServerAddr: "0.0.0.0",
  44. ServerPort: 7000,
  45. HttpProxy: "",
  46. LogFile: "console",
  47. LogWay: "console",
  48. LogLevel: "info",
  49. LogMaxDays: 3,
  50. PrivilegeToken: "",
  51. PoolCount: 1,
  52. TcpMux: true,
  53. User: "",
  54. LoginFailExit: true,
  55. HeartBeatInterval: 30,
  56. HeartBeatTimeout: 90,
  57. }
  58. }
  59. func LoadClientCommonConf(conf ini.File) (cfg *ClientCommonConf, err error) {
  60. var (
  61. tmpStr string
  62. ok bool
  63. v int64
  64. )
  65. cfg = GetDeaultClientCommonConf()
  66. tmpStr, ok = conf.Get("common", "server_addr")
  67. if ok {
  68. cfg.ServerAddr = tmpStr
  69. }
  70. tmpStr, ok = conf.Get("common", "server_port")
  71. if ok {
  72. cfg.ServerPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  73. }
  74. tmpStr, ok = conf.Get("common", "http_proxy")
  75. if ok {
  76. cfg.HttpProxy = tmpStr
  77. } else {
  78. // get http_proxy from env
  79. cfg.HttpProxy = os.Getenv("http_proxy")
  80. }
  81. tmpStr, ok = conf.Get("common", "log_file")
  82. if ok {
  83. cfg.LogFile = tmpStr
  84. if cfg.LogFile == "console" {
  85. cfg.LogWay = "console"
  86. } else {
  87. cfg.LogWay = "file"
  88. }
  89. }
  90. tmpStr, ok = conf.Get("common", "log_level")
  91. if ok {
  92. cfg.LogLevel = tmpStr
  93. }
  94. tmpStr, ok = conf.Get("common", "log_max_days")
  95. if ok {
  96. cfg.LogMaxDays, _ = strconv.ParseInt(tmpStr, 10, 64)
  97. }
  98. tmpStr, ok = conf.Get("common", "privilege_token")
  99. if ok {
  100. cfg.PrivilegeToken = tmpStr
  101. }
  102. tmpStr, ok = conf.Get("common", "pool_count")
  103. if ok {
  104. v, err = strconv.ParseInt(tmpStr, 10, 64)
  105. if err != nil {
  106. cfg.PoolCount = 1
  107. } else {
  108. cfg.PoolCount = int(v)
  109. }
  110. }
  111. tmpStr, ok = conf.Get("common", "tcp_mux")
  112. if ok && tmpStr == "false" {
  113. cfg.TcpMux = false
  114. } else {
  115. cfg.TcpMux = true
  116. }
  117. tmpStr, ok = conf.Get("common", "user")
  118. if ok {
  119. cfg.User = tmpStr
  120. }
  121. tmpStr, ok = conf.Get("common", "login_fail_exit")
  122. if ok && tmpStr == "false" {
  123. cfg.LoginFailExit = false
  124. } else {
  125. cfg.LoginFailExit = true
  126. }
  127. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  128. if ok {
  129. v, err = strconv.ParseInt(tmpStr, 10, 64)
  130. if err != nil {
  131. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  132. return
  133. } else {
  134. cfg.HeartBeatTimeout = v
  135. }
  136. }
  137. tmpStr, ok = conf.Get("common", "heartbeat_interval")
  138. if ok {
  139. v, err = strconv.ParseInt(tmpStr, 10, 64)
  140. if err != nil {
  141. err = fmt.Errorf("Parse conf error: heartbeat_interval is incorrect")
  142. return
  143. } else {
  144. cfg.HeartBeatInterval = v
  145. }
  146. }
  147. if cfg.HeartBeatInterval <= 0 {
  148. err = fmt.Errorf("Parse conf error: heartbeat_interval is incorrect")
  149. return
  150. }
  151. if cfg.HeartBeatTimeout < cfg.HeartBeatInterval {
  152. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect, heartbeat_timeout is less than heartbeat_interval")
  153. return
  154. }
  155. return
  156. }