client_common.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // ClientCommonConf contains information for a client service. It is
  23. // recommended to use GetDefaultClientConf instead of creating this object
  24. // directly, so that all unspecified fields have reasonable default values.
  25. type ClientCommonConf struct {
  26. // ServerAddr specifies the address of the server to connect to. By
  27. // default, this value is "0.0.0.0".
  28. ServerAddr string `json:"server_addr"`
  29. // ServerPort specifies the port to connect to the server on. By default,
  30. // this value is 7000.
  31. ServerPort int `json:"server_port"`
  32. // HttpProxy specifies a proxy address to connect to the server through. If
  33. // this value is "", the server will be connected to directly. By default,
  34. // this value is read from the "http_proxy" environment variable.
  35. HttpProxy string `json:"http_proxy"`
  36. // LogFile specifies a file where logs will be written to. This value will
  37. // only be used if LogWay is set appropriately. By default, this value is
  38. // "console".
  39. LogFile string `json:"log_file"`
  40. // LogWay specifies the way logging is managed. Valid values are "console"
  41. // or "file". If "console" is used, logs will be printed to stdout. If
  42. // "file" is used, logs will be printed to LogFile. By default, this value
  43. // is "console".
  44. LogWay string `json:"log_way"`
  45. // LogLevel specifies the minimum log level. Valid values are "trace",
  46. // "debug", "info", "warn", and "error". By default, this value is "info".
  47. LogLevel string `json:"log_level"`
  48. // LogMaxDays specifies the maximum number of days to store log information
  49. // before deletion. This is only used if LogWay == "file". By default, this
  50. // value is 0.
  51. LogMaxDays int64 `json:"log_max_days"`
  52. // DisableLogColor disables log colors when LogWay == "console" when set to
  53. // true. By default, this value is false.
  54. DisableLogColor bool `json:"disable_log_color"`
  55. // Token specifies the authorization token used to create keys to be sent
  56. // to the server. The server must have a matching token for authorization
  57. // to succeed. By default, this value is "".
  58. Token string `json:"token"`
  59. // AdminAddr specifies the address that the admin server binds to. By
  60. // default, this value is "127.0.0.1".
  61. AdminAddr string `json:"admin_addr"`
  62. // AdminPort specifies the port for the admin server to listen on. If this
  63. // value is 0, the admin server will not be started. By default, this value
  64. // is 0.
  65. AdminPort int `json:"admin_port"`
  66. // AdminUser specifies the username that the admin server will use for
  67. // login. By default, this value is "admin".
  68. AdminUser string `json:"admin_user"`
  69. // AdminPwd specifies the password that the admin server will use for
  70. // login. By default, this value is "admin".
  71. AdminPwd string `json:"admin_pwd"`
  72. // AssetsDir specifies the local directory that the admin server will load
  73. // resources from. If this value is "", assets will be loaded from the
  74. // bundled executable using statik. By default, this value is "".
  75. AssetsDir string `json:"assets_dir"`
  76. // PoolCount specifies the number of connections the client will make to
  77. // the server in advance. By default, this value is 0.
  78. PoolCount int `json:"pool_count"`
  79. // TcpMux toggles TCP stream multiplexing. This allows multiple requests
  80. // from a client to share a single TCP connection. If this value is true,
  81. // the server must have TCP multiplexing enabled as well. By default, this
  82. // value is true.
  83. TcpMux bool `json:"tcp_mux"`
  84. // User specifies a prefix for proxy names to distinguish them from other
  85. // clients. If this value is not "", proxy names will automatically be
  86. // changed to "{user}.{proxy_name}". By default, this value is "".
  87. User string `json:"user"`
  88. // DnsServer specifies a DNS server address for FRPC to use. If this value
  89. // is "", the default DNS will be used. By default, this value is "".
  90. DnsServer string `json:"dns_server"`
  91. // LoginFailExit controls whether or not the client should exit after a
  92. // failed login attempt. If false, the client will retry until a login
  93. // attempt succeeds. By default, this value is true.
  94. LoginFailExit bool `json:"login_fail_exit"`
  95. // Start specifies a set of enabled proxies by name. If this set is empty,
  96. // all supplied proxies are enabled. By default, this value is an empty
  97. // set.
  98. Start map[string]struct{} `json:"start"`
  99. // Protocol specifies the protocol to use when interacting with the server.
  100. // Valid values are "tcp", "kcp", and "websocket". By default, this value
  101. // is "tcp".
  102. Protocol string `json:"protocol"`
  103. // TLSEnable specifies whether or not TLS should be used when communicating
  104. // with the server.
  105. TLSEnable bool `json:"tls_enable"`
  106. // HeartBeatInterval specifies at what interval heartbeats are sent to the
  107. // server, in seconds. It is not recommended to change this value. By
  108. // default, this value is 30.
  109. HeartBeatInterval int64 `json:"heartbeat_interval"`
  110. // HeartBeatTimeout specifies the maximum allowed heartbeat response delay
  111. // before the connection is terminated, in seconds. It is not recommended
  112. // to change this value. By default, this value is 90.
  113. HeartBeatTimeout int64 `json:"heartbeat_timeout"`
  114. // Client meta info
  115. Metas map[string]string `json:"metas"`
  116. }
  117. // GetDefaultClientConf returns a client configuration with default values.
  118. func GetDefaultClientConf() ClientCommonConf {
  119. return ClientCommonConf{
  120. ServerAddr: "0.0.0.0",
  121. ServerPort: 7000,
  122. HttpProxy: os.Getenv("http_proxy"),
  123. LogFile: "console",
  124. LogWay: "console",
  125. LogLevel: "info",
  126. LogMaxDays: 3,
  127. DisableLogColor: false,
  128. Token: "",
  129. AdminAddr: "127.0.0.1",
  130. AdminPort: 0,
  131. AdminUser: "",
  132. AdminPwd: "",
  133. AssetsDir: "",
  134. PoolCount: 1,
  135. TcpMux: true,
  136. User: "",
  137. DnsServer: "",
  138. LoginFailExit: true,
  139. Start: make(map[string]struct{}),
  140. Protocol: "tcp",
  141. TLSEnable: false,
  142. HeartBeatInterval: 30,
  143. HeartBeatTimeout: 90,
  144. Metas: make(map[string]string),
  145. }
  146. }
  147. func UnmarshalClientConfFromIni(content string) (cfg ClientCommonConf, err error) {
  148. cfg = GetDefaultClientConf()
  149. conf, err := ini.Load(strings.NewReader(content))
  150. if err != nil {
  151. return ClientCommonConf{}, fmt.Errorf("parse ini conf file error: %v", err)
  152. }
  153. var (
  154. tmpStr string
  155. ok bool
  156. v int64
  157. )
  158. if tmpStr, ok = conf.Get("common", "server_addr"); ok {
  159. cfg.ServerAddr = tmpStr
  160. }
  161. if tmpStr, ok = conf.Get("common", "server_port"); ok {
  162. v, err = strconv.ParseInt(tmpStr, 10, 64)
  163. if err != nil {
  164. err = fmt.Errorf("Parse conf error: invalid server_port")
  165. return
  166. }
  167. cfg.ServerPort = int(v)
  168. }
  169. if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
  170. cfg.DisableLogColor = true
  171. }
  172. if tmpStr, ok = conf.Get("common", "http_proxy"); ok {
  173. cfg.HttpProxy = tmpStr
  174. }
  175. if tmpStr, ok = conf.Get("common", "log_file"); ok {
  176. cfg.LogFile = tmpStr
  177. if cfg.LogFile == "console" {
  178. cfg.LogWay = "console"
  179. } else {
  180. cfg.LogWay = "file"
  181. }
  182. }
  183. if tmpStr, ok = conf.Get("common", "log_level"); ok {
  184. cfg.LogLevel = tmpStr
  185. }
  186. if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
  187. if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
  188. cfg.LogMaxDays = v
  189. }
  190. }
  191. if tmpStr, ok = conf.Get("common", "token"); ok {
  192. cfg.Token = tmpStr
  193. }
  194. if tmpStr, ok = conf.Get("common", "admin_addr"); ok {
  195. cfg.AdminAddr = tmpStr
  196. }
  197. if tmpStr, ok = conf.Get("common", "admin_port"); ok {
  198. if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
  199. cfg.AdminPort = int(v)
  200. } else {
  201. err = fmt.Errorf("Parse conf error: invalid admin_port")
  202. return
  203. }
  204. }
  205. if tmpStr, ok = conf.Get("common", "admin_user"); ok {
  206. cfg.AdminUser = tmpStr
  207. }
  208. if tmpStr, ok = conf.Get("common", "admin_pwd"); ok {
  209. cfg.AdminPwd = tmpStr
  210. }
  211. if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
  212. cfg.AssetsDir = tmpStr
  213. }
  214. if tmpStr, ok = conf.Get("common", "pool_count"); ok {
  215. if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
  216. cfg.PoolCount = int(v)
  217. }
  218. }
  219. if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
  220. cfg.TcpMux = false
  221. } else {
  222. cfg.TcpMux = true
  223. }
  224. if tmpStr, ok = conf.Get("common", "user"); ok {
  225. cfg.User = tmpStr
  226. }
  227. if tmpStr, ok = conf.Get("common", "dns_server"); ok {
  228. cfg.DnsServer = tmpStr
  229. }
  230. if tmpStr, ok = conf.Get("common", "start"); ok {
  231. proxyNames := strings.Split(tmpStr, ",")
  232. for _, name := range proxyNames {
  233. cfg.Start[strings.TrimSpace(name)] = struct{}{}
  234. }
  235. }
  236. if tmpStr, ok = conf.Get("common", "login_fail_exit"); ok && tmpStr == "false" {
  237. cfg.LoginFailExit = false
  238. } else {
  239. cfg.LoginFailExit = true
  240. }
  241. if tmpStr, ok = conf.Get("common", "protocol"); ok {
  242. // Now it only support tcp and kcp and websocket.
  243. if tmpStr != "tcp" && tmpStr != "kcp" && tmpStr != "websocket" {
  244. err = fmt.Errorf("Parse conf error: invalid protocol")
  245. return
  246. }
  247. cfg.Protocol = tmpStr
  248. }
  249. if tmpStr, ok = conf.Get("common", "tls_enable"); ok && tmpStr == "true" {
  250. cfg.TLSEnable = true
  251. } else {
  252. cfg.TLSEnable = false
  253. }
  254. if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
  255. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  256. err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout")
  257. return
  258. } else {
  259. cfg.HeartBeatTimeout = v
  260. }
  261. }
  262. if tmpStr, ok = conf.Get("common", "heartbeat_interval"); ok {
  263. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  264. err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
  265. return
  266. } else {
  267. cfg.HeartBeatInterval = v
  268. }
  269. }
  270. for k, v := range conf.Section("common") {
  271. if strings.HasPrefix(k, "meta_") {
  272. cfg.Metas[strings.TrimPrefix(k, "meta_")] = v
  273. }
  274. }
  275. return
  276. }
  277. func (cfg *ClientCommonConf) Check() (err error) {
  278. if cfg.HeartBeatInterval <= 0 {
  279. err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
  280. return
  281. }
  282. if cfg.HeartBeatTimeout < cfg.HeartBeatInterval {
  283. err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout, heartbeat_timeout is less than heartbeat_interval")
  284. return
  285. }
  286. return
  287. }