client_common.go 5.1 KB

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