client_common.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. "github.com/fatedier/frp/models/auth"
  22. )
  23. // ClientCommonConf contains information for a client service. It is
  24. // recommended to use GetDefaultClientConf instead of creating this object
  25. // directly, so that all unspecified fields have reasonable default values.
  26. type ClientCommonConf struct {
  27. auth.AuthClientConfig
  28. // ServerAddr specifies the address of the server to connect to. By
  29. // default, this value is "0.0.0.0".
  30. ServerAddr string `json:"server_addr"`
  31. // ServerPort specifies the port to connect to the server on. By default,
  32. // this value is 7000.
  33. ServerPort int `json:"server_port"`
  34. // HttpProxy specifies a proxy address to connect to the server through. If
  35. // this value is "", the server will be connected to directly. By default,
  36. // this value is read from the "http_proxy" environment variable.
  37. HttpProxy string `json:"http_proxy"`
  38. // LogFile specifies a file where logs will be written to. This value will
  39. // only be used if LogWay is set appropriately. By default, this value is
  40. // "console".
  41. LogFile string `json:"log_file"`
  42. // LogWay specifies the way logging is managed. Valid values are "console"
  43. // or "file". If "console" is used, logs will be printed to stdout. If
  44. // "file" is used, logs will be printed to LogFile. By default, this value
  45. // is "console".
  46. LogWay string `json:"log_way"`
  47. // LogLevel specifies the minimum log level. Valid values are "trace",
  48. // "debug", "info", "warn", and "error". By default, this value is "info".
  49. LogLevel string `json:"log_level"`
  50. // LogMaxDays specifies the maximum number of days to store log information
  51. // before deletion. This is only used if LogWay == "file". By default, this
  52. // value is 0.
  53. LogMaxDays int64 `json:"log_max_days"`
  54. // DisableLogColor disables log colors when LogWay == "console" when set to
  55. // true. By default, this value is false.
  56. DisableLogColor bool `json:"disable_log_color"`
  57. // AdminAddr specifies the address that the admin server binds to. By
  58. // default, this value is "127.0.0.1".
  59. AdminAddr string `json:"admin_addr"`
  60. // AdminPort specifies the port for the admin server to listen on. If this
  61. // value is 0, the admin server will not be started. By default, this value
  62. // is 0.
  63. AdminPort int `json:"admin_port"`
  64. // AdminUser specifies the username that the admin server will use for
  65. // login. By default, this value is "admin".
  66. AdminUser string `json:"admin_user"`
  67. // AdminPwd specifies the password that the admin server will use for
  68. // login. By default, this value is "admin".
  69. AdminPwd string `json:"admin_pwd"`
  70. // AssetsDir specifies the local directory that the admin server will load
  71. // resources from. If this value is "", assets will be loaded from the
  72. // bundled executable using statik. By default, this value is "".
  73. AssetsDir string `json:"assets_dir"`
  74. // PoolCount specifies the number of connections the client will make to
  75. // the server in advance. By default, this value is 0.
  76. PoolCount int `json:"pool_count"`
  77. // TcpMux toggles TCP stream multiplexing. This allows multiple requests
  78. // from a client to share a single TCP connection. If this value is true,
  79. // the server must have TCP multiplexing enabled as well. By default, this
  80. // value is true.
  81. TcpMux bool `json:"tcp_mux"`
  82. // User specifies a prefix for proxy names to distinguish them from other
  83. // clients. If this value is not "", proxy names will automatically be
  84. // changed to "{user}.{proxy_name}". By default, this value is "".
  85. User string `json:"user"`
  86. // DnsServer specifies a DNS server address for FRPC to use. If this value
  87. // is "", the default DNS will be used. By default, this value is "".
  88. DnsServer string `json:"dns_server"`
  89. // LoginFailExit controls whether or not the client should exit after a
  90. // failed login attempt. If false, the client will retry until a login
  91. // attempt succeeds. By default, this value is true.
  92. LoginFailExit bool `json:"login_fail_exit"`
  93. // Start specifies a set of enabled proxies by name. If this set is empty,
  94. // all supplied proxies are enabled. By default, this value is an empty
  95. // set.
  96. Start map[string]struct{} `json:"start"`
  97. // Protocol specifies the protocol to use when interacting with the server.
  98. // Valid values are "tcp", "kcp", and "websocket". By default, this value
  99. // is "tcp".
  100. Protocol string `json:"protocol"`
  101. // TLSEnable specifies whether or not TLS should be used when communicating
  102. // with the server.
  103. TLSEnable bool `json:"tls_enable"`
  104. // HeartBeatInterval specifies at what interval heartbeats are sent to the
  105. // server, in seconds. It is not recommended to change this value. By
  106. // default, this value is 30.
  107. HeartBeatInterval int64 `json:"heartbeat_interval"`
  108. // HeartBeatTimeout specifies the maximum allowed heartbeat response delay
  109. // before the connection is terminated, in seconds. It is not recommended
  110. // to change this value. By default, this value is 90.
  111. HeartBeatTimeout int64 `json:"heartbeat_timeout"`
  112. // Client meta info
  113. Metas map[string]string `json:"metas"`
  114. }
  115. // GetDefaultClientConf returns a client configuration with default values.
  116. func GetDefaultClientConf() ClientCommonConf {
  117. return ClientCommonConf{
  118. ServerAddr: "0.0.0.0",
  119. ServerPort: 7000,
  120. HttpProxy: os.Getenv("http_proxy"),
  121. LogFile: "console",
  122. LogWay: "console",
  123. LogLevel: "info",
  124. LogMaxDays: 3,
  125. DisableLogColor: false,
  126. AdminAddr: "127.0.0.1",
  127. AdminPort: 0,
  128. AdminUser: "",
  129. AdminPwd: "",
  130. AssetsDir: "",
  131. PoolCount: 1,
  132. TcpMux: true,
  133. User: "",
  134. DnsServer: "",
  135. LoginFailExit: true,
  136. Start: make(map[string]struct{}),
  137. Protocol: "tcp",
  138. TLSEnable: false,
  139. HeartBeatInterval: 30,
  140. HeartBeatTimeout: 90,
  141. Metas: make(map[string]string),
  142. }
  143. }
  144. func UnmarshalClientConfFromIni(content string) (cfg ClientCommonConf, err error) {
  145. cfg = GetDefaultClientConf()
  146. conf, err := ini.Load(strings.NewReader(content))
  147. if err != nil {
  148. return ClientCommonConf{}, fmt.Errorf("parse ini conf file error: %v", err)
  149. }
  150. cfg.AuthClientConfig = auth.UnmarshalAuthClientConfFromIni(conf)
  151. var (
  152. tmpStr string
  153. ok bool
  154. v int64
  155. )
  156. if tmpStr, ok = conf.Get("common", "server_addr"); ok {
  157. cfg.ServerAddr = tmpStr
  158. }
  159. if tmpStr, ok = conf.Get("common", "server_port"); ok {
  160. v, err = strconv.ParseInt(tmpStr, 10, 64)
  161. if err != nil {
  162. err = fmt.Errorf("Parse conf error: invalid server_port")
  163. return
  164. }
  165. cfg.ServerPort = int(v)
  166. }
  167. if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
  168. cfg.DisableLogColor = true
  169. }
  170. if tmpStr, ok = conf.Get("common", "http_proxy"); ok {
  171. cfg.HttpProxy = tmpStr
  172. }
  173. if tmpStr, ok = conf.Get("common", "log_file"); ok {
  174. cfg.LogFile = tmpStr
  175. if cfg.LogFile == "console" {
  176. cfg.LogWay = "console"
  177. } else {
  178. cfg.LogWay = "file"
  179. }
  180. }
  181. if tmpStr, ok = conf.Get("common", "log_level"); ok {
  182. cfg.LogLevel = tmpStr
  183. }
  184. if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
  185. if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
  186. cfg.LogMaxDays = v
  187. }
  188. }
  189. if tmpStr, ok = conf.Get("common", "admin_addr"); ok {
  190. cfg.AdminAddr = tmpStr
  191. }
  192. if tmpStr, ok = conf.Get("common", "admin_port"); ok {
  193. if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
  194. cfg.AdminPort = int(v)
  195. } else {
  196. err = fmt.Errorf("Parse conf error: invalid admin_port")
  197. return
  198. }
  199. }
  200. if tmpStr, ok = conf.Get("common", "admin_user"); ok {
  201. cfg.AdminUser = tmpStr
  202. }
  203. if tmpStr, ok = conf.Get("common", "admin_pwd"); ok {
  204. cfg.AdminPwd = tmpStr
  205. }
  206. if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
  207. cfg.AssetsDir = tmpStr
  208. }
  209. if tmpStr, ok = conf.Get("common", "pool_count"); ok {
  210. if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
  211. cfg.PoolCount = int(v)
  212. }
  213. }
  214. if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
  215. cfg.TcpMux = false
  216. } else {
  217. cfg.TcpMux = true
  218. }
  219. if tmpStr, ok = conf.Get("common", "user"); ok {
  220. cfg.User = tmpStr
  221. }
  222. if tmpStr, ok = conf.Get("common", "dns_server"); ok {
  223. cfg.DnsServer = tmpStr
  224. }
  225. if tmpStr, ok = conf.Get("common", "start"); ok {
  226. proxyNames := strings.Split(tmpStr, ",")
  227. for _, name := range proxyNames {
  228. cfg.Start[strings.TrimSpace(name)] = struct{}{}
  229. }
  230. }
  231. if tmpStr, ok = conf.Get("common", "login_fail_exit"); ok && tmpStr == "false" {
  232. cfg.LoginFailExit = false
  233. } else {
  234. cfg.LoginFailExit = true
  235. }
  236. if tmpStr, ok = conf.Get("common", "protocol"); ok {
  237. // Now it only support tcp and kcp and websocket.
  238. if tmpStr != "tcp" && tmpStr != "kcp" && tmpStr != "websocket" {
  239. err = fmt.Errorf("Parse conf error: invalid protocol")
  240. return
  241. }
  242. cfg.Protocol = tmpStr
  243. }
  244. if tmpStr, ok = conf.Get("common", "tls_enable"); ok && tmpStr == "true" {
  245. cfg.TLSEnable = true
  246. } else {
  247. cfg.TLSEnable = false
  248. }
  249. if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
  250. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  251. err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout")
  252. return
  253. } else {
  254. cfg.HeartBeatTimeout = v
  255. }
  256. }
  257. if tmpStr, ok = conf.Get("common", "heartbeat_interval"); ok {
  258. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  259. err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
  260. return
  261. } else {
  262. cfg.HeartBeatInterval = v
  263. }
  264. }
  265. for k, v := range conf.Section("common") {
  266. if strings.HasPrefix(k, "meta_") {
  267. cfg.Metas[strings.TrimPrefix(k, "meta_")] = v
  268. }
  269. }
  270. return
  271. }
  272. func (cfg *ClientCommonConf) Check() (err error) {
  273. if cfg.HeartBeatInterval <= 0 {
  274. err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
  275. return
  276. }
  277. if cfg.HeartBeatTimeout < cfg.HeartBeatInterval {
  278. err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout, heartbeat_timeout is less than heartbeat_interval")
  279. return
  280. }
  281. return
  282. }