1
0

client_common.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 int
  28. ServerUdpPort int // 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 int
  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. v, err = strconv.ParseInt(tmpStr, 10, 64)
  88. if err != nil {
  89. err = fmt.Errorf("Parse conf error: invalid server_port")
  90. return
  91. }
  92. cfg.ServerPort = int(v)
  93. }
  94. tmpStr, ok = conf.Get("common", "http_proxy")
  95. if ok {
  96. cfg.HttpProxy = tmpStr
  97. } else {
  98. // get http_proxy from env
  99. cfg.HttpProxy = os.Getenv("http_proxy")
  100. }
  101. tmpStr, ok = conf.Get("common", "log_file")
  102. if ok {
  103. cfg.LogFile = tmpStr
  104. if cfg.LogFile == "console" {
  105. cfg.LogWay = "console"
  106. } else {
  107. cfg.LogWay = "file"
  108. }
  109. }
  110. tmpStr, ok = conf.Get("common", "log_level")
  111. if ok {
  112. cfg.LogLevel = tmpStr
  113. }
  114. tmpStr, ok = conf.Get("common", "log_max_days")
  115. if ok {
  116. if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
  117. cfg.LogMaxDays = v
  118. }
  119. }
  120. tmpStr, ok = conf.Get("common", "privilege_token")
  121. if ok {
  122. cfg.PrivilegeToken = tmpStr
  123. }
  124. tmpStr, ok = conf.Get("common", "admin_addr")
  125. if ok {
  126. cfg.AdminAddr = tmpStr
  127. }
  128. tmpStr, ok = conf.Get("common", "admin_port")
  129. if ok {
  130. if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
  131. cfg.AdminPort = int(v)
  132. } else {
  133. err = fmt.Errorf("Parse conf error: invalid admin_port")
  134. return
  135. }
  136. }
  137. tmpStr, ok = conf.Get("common", "admin_user")
  138. if ok {
  139. cfg.AdminUser = tmpStr
  140. }
  141. tmpStr, ok = conf.Get("common", "admin_pwd")
  142. if ok {
  143. cfg.AdminPwd = tmpStr
  144. }
  145. tmpStr, ok = conf.Get("common", "pool_count")
  146. if ok {
  147. v, err = strconv.ParseInt(tmpStr, 10, 64)
  148. if err != nil {
  149. cfg.PoolCount = 1
  150. } else {
  151. cfg.PoolCount = int(v)
  152. }
  153. }
  154. tmpStr, ok = conf.Get("common", "tcp_mux")
  155. if ok && tmpStr == "false" {
  156. cfg.TcpMux = false
  157. } else {
  158. cfg.TcpMux = true
  159. }
  160. tmpStr, ok = conf.Get("common", "user")
  161. if ok {
  162. cfg.User = tmpStr
  163. }
  164. tmpStr, ok = conf.Get("common", "start")
  165. if ok {
  166. proxyNames := strings.Split(tmpStr, ",")
  167. for _, name := range proxyNames {
  168. cfg.Start[strings.TrimSpace(name)] = struct{}{}
  169. }
  170. }
  171. tmpStr, ok = conf.Get("common", "login_fail_exit")
  172. if ok && tmpStr == "false" {
  173. cfg.LoginFailExit = false
  174. } else {
  175. cfg.LoginFailExit = true
  176. }
  177. tmpStr, ok = conf.Get("common", "protocol")
  178. if ok {
  179. // Now it only support tcp and kcp.
  180. if tmpStr != "kcp" {
  181. tmpStr = "tcp"
  182. }
  183. cfg.Protocol = tmpStr
  184. }
  185. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  186. if ok {
  187. v, err = strconv.ParseInt(tmpStr, 10, 64)
  188. if err != nil {
  189. err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout")
  190. return
  191. } else {
  192. cfg.HeartBeatTimeout = v
  193. }
  194. }
  195. tmpStr, ok = conf.Get("common", "heartbeat_interval")
  196. if ok {
  197. v, err = strconv.ParseInt(tmpStr, 10, 64)
  198. if err != nil {
  199. err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
  200. return
  201. } else {
  202. cfg.HeartBeatInterval = v
  203. }
  204. }
  205. if cfg.HeartBeatInterval <= 0 {
  206. err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
  207. return
  208. }
  209. if cfg.HeartBeatTimeout < cfg.HeartBeatInterval {
  210. err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout, heartbeat_timeout is less than heartbeat_interval")
  211. return
  212. }
  213. return
  214. }