client_common.go 6.4 KB

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