1
0

client_common.go 4.2 KB

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