client_common.go 5.2 KB

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