client_common.go 6.7 KB

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