client_common.go 5.9 KB

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