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