client_common.go 3.7 KB

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