server_common.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. "strconv"
  18. "strings"
  19. ini "github.com/vaughan0/go-ini"
  20. "github.com/fatedier/frp/utils/util"
  21. )
  22. // ServerCommonConf contains information for a server service. It is
  23. // recommended to use GetDefaultServerConf instead of creating this object
  24. // directly, so that all unspecified fields have reasonable default values.
  25. type ServerCommonConf struct {
  26. // BindAddr specifies the address that the server binds to. By default,
  27. // this value is "0.0.0.0".
  28. BindAddr string `json:"bind_addr"`
  29. // BindPort specifies the port that the server listens on. By default, this
  30. // value is 7000.
  31. BindPort int `json:"bind_port"`
  32. // BindUdpPort specifies the UDP port that the server listens on. If this
  33. // value is 0, the server will not listen for UDP connections. By default,
  34. // this value is 0
  35. BindUdpPort int `json:"bind_udp_port"`
  36. // BindKcpPort specifies the KCP port that the server listens on. If this
  37. // value is 0, the server will not listen for KCP connections. By default,
  38. // this value is 0.
  39. KcpBindPort int `json:"kcp_bind_port"`
  40. // ProxyBindAddr specifies the address that the proxy binds to. This value
  41. // may be the same as BindAddr. By default, this value is "0.0.0.0".
  42. ProxyBindAddr string `json:"proxy_bind_addr"`
  43. // VhostHttpPort specifies the port that the server listens for HTTP Vhost
  44. // requests. If this value is 0, the server will not listen for HTTP
  45. // requests. By default, this value is 0.
  46. VhostHttpPort int `json:"vhost_http_port"`
  47. // VhostHttpsPort specifies the port that the server listens for HTTPS
  48. // Vhost requests. If this value is 0, the server will not listen for HTTPS
  49. // requests. By default, this value is 0.
  50. VhostHttpsPort int `json:"vhost_https_port"`
  51. // VhostHttpTimeout specifies the response header timeout for the Vhost
  52. // HTTP server, in seconds. By default, this value is 60.
  53. VhostHttpTimeout int64 `json:"vhost_http_timeout"`
  54. // DashboardAddr specifies the address that the dashboard binds to. By
  55. // default, this value is "0.0.0.0".
  56. DashboardAddr string `json:"dashboard_addr"`
  57. // DashboardPort specifies the port that the dashboard listens on. If this
  58. // value is 0, the dashboard will not be started. By default, this value is
  59. // 0.
  60. DashboardPort int `json:"dashboard_port"`
  61. // DashboardUser specifies the username that the dashboard will use for
  62. // login. By default, this value is "admin".
  63. DashboardUser string `json:"dashboard_user"`
  64. // DashboardUser specifies the password that the dashboard will use for
  65. // login. By default, this value is "admin".
  66. DashboardPwd string `json:"dashboard_pwd"`
  67. // AssetsDir specifies the local directory that the dashboard will load
  68. // resources from. If this value is "", assets will be loaded from the
  69. // bundled executable using statik. By default, this value is "".
  70. AssetsDir string `json:"asserts_dir"`
  71. // LogFile specifies a file where logs will be written to. This value will
  72. // only be used if LogWay is set appropriately. By default, this value is
  73. // "console".
  74. LogFile string `json:"log_file"`
  75. // LogWay specifies the way logging is managed. Valid values are "console"
  76. // or "file". If "console" is used, logs will be printed to stdout. If
  77. // "file" is used, logs will be printed to LogFile. By default, this value
  78. // is "console".
  79. LogWay string `json:"log_way"`
  80. // LogLevel specifies the minimum log level. Valid values are "trace",
  81. // "debug", "info", "warn", and "error". By default, this value is "info".
  82. LogLevel string `json:"log_level"`
  83. // LogMaxDays specifies the maximum number of days to store log information
  84. // before deletion. This is only used if LogWay == "file". By default, this
  85. // value is 0.
  86. LogMaxDays int64 `json:"log_max_days"`
  87. // DisableLogColor disables log colors when LogWay == "console" when set to
  88. // true. By default, this value is false.
  89. DisableLogColor bool `json:"disable_log_color"`
  90. // Token specifies the authorization token used to authenticate keys
  91. // received from clients. Clients must have a matching token to be
  92. // authorized to use the server. By default, this value is "".
  93. Token string `json:"token"`
  94. // SubDomainHost specifies the domain that will be attached to sub-domains
  95. // requested by the client when using Vhost proxying. For example, if this
  96. // value is set to "frps.com" and the client requested the subdomain
  97. // "test", the resulting URL would be "test.frps.com". By default, this
  98. // value is "".
  99. SubDomainHost string `json:"subdomain_host"`
  100. // TcpMux toggles TCP stream multiplexing. This allows multiple requests
  101. // from a client to share a single TCP connection. By default, this value
  102. // is true.
  103. TcpMux bool `json:"tcp_mux"`
  104. // Custom404Page specifies a path to a custom 404 page to display. If this
  105. // value is "", a default page will be displayed. By default, this value is
  106. // "".
  107. Custom404Page string `json:"custom_404_page"`
  108. // AllowPorts specifies a set of ports that clients are able to proxy to.
  109. // If the length of this value is 0, all ports are allowed. By default,
  110. // this value is an empty set.
  111. AllowPorts map[int]struct{}
  112. // MaxPoolCount specifies the maximum pool size for each proxy. By default,
  113. // this value is 5.
  114. MaxPoolCount int64 `json:"max_pool_count"`
  115. // MaxPortsPerClient specifies the maximum number of ports a single client
  116. // may proxy to. If this value is 0, no limit will be applied. By default,
  117. // this value is 0.
  118. MaxPortsPerClient int64 `json:"max_ports_per_client"`
  119. // HeartBeatTimeout specifies the maximum time to wait for a heartbeat
  120. // before terminating the connection. It is not recommended to change this
  121. // value. By default, this value is 90.
  122. HeartBeatTimeout int64 `json:"heart_beat_timeout"`
  123. // UserConnTimeout specifies the maximum time to wait for a work
  124. // connection. By default, this value is 10.
  125. UserConnTimeout int64 `json:"user_conn_timeout"`
  126. }
  127. // GetDefaultServerConf returns a server configuration with reasonable
  128. // defaults.
  129. func GetDefaultServerConf() ServerCommonConf {
  130. return ServerCommonConf{
  131. BindAddr: "0.0.0.0",
  132. BindPort: 7000,
  133. BindUdpPort: 0,
  134. KcpBindPort: 0,
  135. ProxyBindAddr: "0.0.0.0",
  136. VhostHttpPort: 0,
  137. VhostHttpsPort: 0,
  138. VhostHttpTimeout: 60,
  139. DashboardAddr: "0.0.0.0",
  140. DashboardPort: 0,
  141. DashboardUser: "admin",
  142. DashboardPwd: "admin",
  143. AssetsDir: "",
  144. LogFile: "console",
  145. LogWay: "console",
  146. LogLevel: "info",
  147. LogMaxDays: 3,
  148. DisableLogColor: false,
  149. Token: "",
  150. SubDomainHost: "",
  151. TcpMux: true,
  152. AllowPorts: make(map[int]struct{}),
  153. MaxPoolCount: 5,
  154. MaxPortsPerClient: 0,
  155. HeartBeatTimeout: 90,
  156. UserConnTimeout: 10,
  157. Custom404Page: "",
  158. }
  159. }
  160. // UnmarshalServerConfFromIni parses the contents of a server configuration ini
  161. // file and returns the resulting server configuration.
  162. func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error) {
  163. cfg = GetDefaultServerConf()
  164. conf, err := ini.Load(strings.NewReader(content))
  165. if err != nil {
  166. err = fmt.Errorf("parse ini conf file error: %v", err)
  167. return ServerCommonConf{}, err
  168. }
  169. var (
  170. tmpStr string
  171. ok bool
  172. v int64
  173. )
  174. if tmpStr, ok = conf.Get("common", "bind_addr"); ok {
  175. cfg.BindAddr = tmpStr
  176. }
  177. if tmpStr, ok = conf.Get("common", "bind_port"); ok {
  178. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  179. err = fmt.Errorf("Parse conf error: invalid bind_port")
  180. return
  181. } else {
  182. cfg.BindPort = int(v)
  183. }
  184. }
  185. if tmpStr, ok = conf.Get("common", "bind_udp_port"); ok {
  186. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  187. err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
  188. return
  189. } else {
  190. cfg.BindUdpPort = int(v)
  191. }
  192. }
  193. if tmpStr, ok = conf.Get("common", "kcp_bind_port"); ok {
  194. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  195. err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
  196. return
  197. } else {
  198. cfg.KcpBindPort = int(v)
  199. }
  200. }
  201. if tmpStr, ok = conf.Get("common", "proxy_bind_addr"); ok {
  202. cfg.ProxyBindAddr = tmpStr
  203. } else {
  204. cfg.ProxyBindAddr = cfg.BindAddr
  205. }
  206. if tmpStr, ok = conf.Get("common", "vhost_http_port"); ok {
  207. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  208. err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
  209. return
  210. } else {
  211. cfg.VhostHttpPort = int(v)
  212. }
  213. } else {
  214. cfg.VhostHttpPort = 0
  215. }
  216. if tmpStr, ok = conf.Get("common", "vhost_https_port"); ok {
  217. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  218. err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
  219. return
  220. } else {
  221. cfg.VhostHttpsPort = int(v)
  222. }
  223. } else {
  224. cfg.VhostHttpsPort = 0
  225. }
  226. if tmpStr, ok = conf.Get("common", "vhost_http_timeout"); ok {
  227. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  228. if errRet != nil || v < 0 {
  229. err = fmt.Errorf("Parse conf error: invalid vhost_http_timeout")
  230. return
  231. } else {
  232. cfg.VhostHttpTimeout = v
  233. }
  234. }
  235. if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok {
  236. cfg.DashboardAddr = tmpStr
  237. } else {
  238. cfg.DashboardAddr = cfg.BindAddr
  239. }
  240. if tmpStr, ok = conf.Get("common", "dashboard_port"); ok {
  241. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  242. err = fmt.Errorf("Parse conf error: invalid dashboard_port")
  243. return
  244. } else {
  245. cfg.DashboardPort = int(v)
  246. }
  247. } else {
  248. cfg.DashboardPort = 0
  249. }
  250. if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
  251. cfg.DashboardUser = tmpStr
  252. }
  253. if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
  254. cfg.DashboardPwd = tmpStr
  255. }
  256. if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
  257. cfg.AssetsDir = tmpStr
  258. }
  259. if tmpStr, ok = conf.Get("common", "log_file"); ok {
  260. cfg.LogFile = tmpStr
  261. if cfg.LogFile == "console" {
  262. cfg.LogWay = "console"
  263. } else {
  264. cfg.LogWay = "file"
  265. }
  266. }
  267. if tmpStr, ok = conf.Get("common", "log_level"); ok {
  268. cfg.LogLevel = tmpStr
  269. }
  270. if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
  271. v, err = strconv.ParseInt(tmpStr, 10, 64)
  272. if err == nil {
  273. cfg.LogMaxDays = v
  274. }
  275. }
  276. if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
  277. cfg.DisableLogColor = true
  278. }
  279. cfg.Token, _ = conf.Get("common", "token")
  280. if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
  281. // e.g. 1000-2000,2001,2002,3000-4000
  282. ports, errRet := util.ParseRangeNumbers(allowPortsStr)
  283. if errRet != nil {
  284. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  285. return
  286. }
  287. for _, port := range ports {
  288. cfg.AllowPorts[int(port)] = struct{}{}
  289. }
  290. }
  291. if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
  292. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  293. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  294. return
  295. } else {
  296. if v < 0 {
  297. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  298. return
  299. }
  300. cfg.MaxPoolCount = v
  301. }
  302. }
  303. if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
  304. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  305. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  306. return
  307. } else {
  308. if v < 0 {
  309. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  310. return
  311. }
  312. cfg.MaxPortsPerClient = v
  313. }
  314. }
  315. if tmpStr, ok = conf.Get("common", "subdomain_host"); ok {
  316. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  317. }
  318. if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
  319. cfg.TcpMux = false
  320. } else {
  321. cfg.TcpMux = true
  322. }
  323. if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
  324. cfg.Custom404Page = tmpStr
  325. }
  326. if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
  327. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  328. if errRet != nil {
  329. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  330. return
  331. } else {
  332. cfg.HeartBeatTimeout = v
  333. }
  334. }
  335. return
  336. }
  337. func (cfg *ServerCommonConf) Check() (err error) {
  338. return
  339. }