config.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 client
  15. import (
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. ini "github.com/vaughan0/go-ini"
  21. )
  22. // common config
  23. var (
  24. ServerAddr string = "0.0.0.0"
  25. ServerPort int64 = 7000
  26. HttpProxy string = ""
  27. LogFile string = "console"
  28. LogWay string = "console"
  29. LogLevel string = "info"
  30. LogMaxDays int64 = 3
  31. PrivilegeToken string = ""
  32. HeartBeatInterval int64 = 10
  33. HeartBeatTimeout int64 = 30
  34. )
  35. var ProxyClients map[string]*ProxyClient = make(map[string]*ProxyClient)
  36. func LoadConf(confFile string) (err error) {
  37. var tmpStr string
  38. var ok bool
  39. conf, err := ini.LoadFile(confFile)
  40. if err != nil {
  41. return err
  42. }
  43. // common
  44. tmpStr, ok = conf.Get("common", "server_addr")
  45. if ok {
  46. ServerAddr = tmpStr
  47. }
  48. tmpStr, ok = conf.Get("common", "server_port")
  49. if ok {
  50. ServerPort, _ = strconv.ParseInt(tmpStr, 10, 64)
  51. }
  52. tmpStr, ok = conf.Get("common", "http_proxy")
  53. if ok {
  54. HttpProxy = tmpStr
  55. } else {
  56. // get http_proxy from env
  57. HttpProxy = os.Getenv("http_proxy")
  58. }
  59. tmpStr, ok = conf.Get("common", "log_file")
  60. if ok {
  61. LogFile = tmpStr
  62. if LogFile == "console" {
  63. LogWay = "console"
  64. } else {
  65. LogWay = "file"
  66. }
  67. }
  68. tmpStr, ok = conf.Get("common", "log_level")
  69. if ok {
  70. LogLevel = tmpStr
  71. }
  72. tmpStr, ok = conf.Get("common", "log_max_days")
  73. if ok {
  74. LogMaxDays, _ = strconv.ParseInt(tmpStr, 10, 64)
  75. }
  76. tmpStr, ok = conf.Get("common", "privilege_token")
  77. if ok {
  78. PrivilegeToken = tmpStr
  79. }
  80. var authToken string
  81. tmpStr, ok = conf.Get("common", "auth_token")
  82. if ok {
  83. authToken = tmpStr
  84. }
  85. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  86. if ok {
  87. v, err := strconv.ParseInt(tmpStr, 10, 64)
  88. if err != nil {
  89. return fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  90. } else {
  91. HeartBeatTimeout = v
  92. }
  93. }
  94. tmpStr, ok = conf.Get("common", "heartbeat_interval")
  95. if ok {
  96. v, err := strconv.ParseInt(tmpStr, 10, 64)
  97. if err != nil {
  98. return fmt.Errorf("Parse conf error: heartbeat_interval is incorrect")
  99. } else {
  100. HeartBeatInterval = v
  101. }
  102. }
  103. if HeartBeatInterval <= 0 {
  104. return fmt.Errorf("Parse conf error: heartbeat_interval is incorrect")
  105. }
  106. if HeartBeatTimeout < HeartBeatInterval {
  107. return fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect, heartbeat_timeout is less than heartbeat_interval")
  108. }
  109. // proxies
  110. for name, section := range conf {
  111. if name != "common" {
  112. proxyClient := &ProxyClient{}
  113. // name
  114. proxyClient.Name = name
  115. // auth_token
  116. proxyClient.AuthToken = authToken
  117. // local_ip
  118. proxyClient.LocalIp, ok = section["local_ip"]
  119. if !ok {
  120. // use 127.0.0.1 as default
  121. proxyClient.LocalIp = "127.0.0.1"
  122. }
  123. // local_port
  124. tmpStr, ok = section["local_port"]
  125. if ok {
  126. proxyClient.LocalPort, err = strconv.ParseInt(tmpStr, 10, 64)
  127. if err != nil {
  128. return fmt.Errorf("Parse conf error: proxy [%s] local_port error", proxyClient.Name)
  129. }
  130. } else {
  131. return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", proxyClient.Name)
  132. }
  133. // type
  134. proxyClient.Type = "tcp"
  135. tmpStr, ok = section["type"]
  136. if ok {
  137. if tmpStr != "tcp" && tmpStr != "http" && tmpStr != "https" && tmpStr != "udp" {
  138. return fmt.Errorf("Parse conf error: proxy [%s] type error", proxyClient.Name)
  139. }
  140. proxyClient.Type = tmpStr
  141. }
  142. // use_encryption
  143. proxyClient.UseEncryption = false
  144. tmpStr, ok = section["use_encryption"]
  145. if ok && tmpStr == "true" {
  146. proxyClient.UseEncryption = true
  147. }
  148. // use_gzip
  149. proxyClient.UseGzip = false
  150. tmpStr, ok = section["use_gzip"]
  151. if ok && tmpStr == "true" {
  152. proxyClient.UseGzip = true
  153. }
  154. if proxyClient.Type == "http" {
  155. // host_header_rewrite
  156. tmpStr, ok = section["host_header_rewrite"]
  157. if ok {
  158. proxyClient.HostHeaderRewrite = tmpStr
  159. }
  160. // http_user
  161. tmpStr, ok = section["http_user"]
  162. if ok {
  163. proxyClient.HttpUserName = tmpStr
  164. }
  165. // http_pwd
  166. tmpStr, ok = section["http_pwd"]
  167. if ok {
  168. proxyClient.HttpPassWord = tmpStr
  169. }
  170. }
  171. if proxyClient.Type == "http" || proxyClient.Type == "https" {
  172. // subdomain
  173. tmpStr, ok = section["subdomain"]
  174. if ok {
  175. proxyClient.SubDomain = tmpStr
  176. }
  177. }
  178. // privilege_mode
  179. proxyClient.PrivilegeMode = false
  180. tmpStr, ok = section["privilege_mode"]
  181. if ok && tmpStr == "true" {
  182. proxyClient.PrivilegeMode = true
  183. }
  184. // pool_count
  185. proxyClient.PoolCount = 0
  186. tmpStr, ok = section["pool_count"]
  187. if ok {
  188. tmpInt, err := strconv.ParseInt(tmpStr, 10, 64)
  189. if err != nil || tmpInt < 0 {
  190. return fmt.Errorf("Parse conf error: proxy [%s] pool_count error", proxyClient.Name)
  191. }
  192. proxyClient.PoolCount = tmpInt
  193. }
  194. // configures used in privilege mode
  195. if proxyClient.PrivilegeMode == true {
  196. if PrivilegeToken == "" {
  197. return fmt.Errorf("Parse conf error: proxy [%s] privilege_token must be set when privilege_mode = true", proxyClient.Name)
  198. } else {
  199. proxyClient.PrivilegeToken = PrivilegeToken
  200. }
  201. if proxyClient.Type == "tcp" || proxyClient.Type == "udp" {
  202. // remote_port
  203. tmpStr, ok = section["remote_port"]
  204. if ok {
  205. proxyClient.RemotePort, err = strconv.ParseInt(tmpStr, 10, 64)
  206. if err != nil {
  207. return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", proxyClient.Name)
  208. }
  209. } else {
  210. return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", proxyClient.Name)
  211. }
  212. } else if proxyClient.Type == "http" {
  213. // custom_domains
  214. domainStr, ok := section["custom_domains"]
  215. if ok {
  216. proxyClient.CustomDomains = strings.Split(domainStr, ",")
  217. if len(proxyClient.CustomDomains) == 0 {
  218. ok = false
  219. } else {
  220. for i, domain := range proxyClient.CustomDomains {
  221. proxyClient.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  222. }
  223. }
  224. }
  225. if !ok && proxyClient.SubDomain == "" {
  226. return fmt.Errorf("Parse conf error: proxy [%s] custom_domains and subdomain should set at least one of them when type is http", proxyClient.Name)
  227. }
  228. // locations
  229. locations, ok := section["locations"]
  230. if ok {
  231. proxyClient.Locations = strings.Split(locations, ",")
  232. } else {
  233. proxyClient.Locations = []string{""}
  234. }
  235. } else if proxyClient.Type == "https" {
  236. // custom_domains
  237. domainStr, ok := section["custom_domains"]
  238. if ok {
  239. proxyClient.CustomDomains = strings.Split(domainStr, ",")
  240. if len(proxyClient.CustomDomains) == 0 {
  241. ok = false
  242. } else {
  243. for i, domain := range proxyClient.CustomDomains {
  244. proxyClient.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  245. }
  246. }
  247. }
  248. if !ok && proxyClient.SubDomain == "" {
  249. return fmt.Errorf("Parse conf error: proxy [%s] custom_domains and subdomain should set at least one of them when type is https", proxyClient.Name)
  250. }
  251. }
  252. }
  253. ProxyClients[proxyClient.Name] = proxyClient
  254. }
  255. }
  256. if len(ProxyClients) == 0 {
  257. return fmt.Errorf("Parse conf error: no proxy config found")
  258. }
  259. return nil
  260. }