client_common.go 6.1 KB

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