1
0

client_common.go 4.4 KB

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