client_common.go 6.8 KB

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