server_common.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. // common config
  23. type ServerCommonConf struct {
  24. BindAddr string `json:"bind_addr"`
  25. BindPort int `json:"bind_port"`
  26. BindUdpPort int `json:"bind_udp_port"`
  27. KcpBindPort int `json:"kcp_bind_port"`
  28. ProxyBindAddr string `json:"proxy_bind_addr"`
  29. // If VhostHttpPort equals 0, don't listen a public port for http protocol.
  30. VhostHttpPort int `json:"vhost_http_port"`
  31. // if VhostHttpsPort equals 0, don't listen a public port for https protocol
  32. VhostHttpsPort int `json:"vhost_https_port"`
  33. VhostHttpTimeout int64 `json:"vhost_http_timeout"`
  34. DashboardAddr string `json:"dashboard_addr"`
  35. // if DashboardPort equals 0, dashboard is not available
  36. DashboardPort int `json:"dashboard_port"`
  37. DashboardUser string `json:"dashboard_user"`
  38. DashboardPwd string `json:"dashboard_pwd"`
  39. AssetsDir string `json:"asserts_dir"`
  40. LogFile string `json:"log_file"`
  41. LogWay string `json:"log_way"` // console or file
  42. LogLevel string `json:"log_level"`
  43. LogMaxDays int64 `json:"log_max_days"`
  44. DisableLogColor bool `json:"disable_log_color"`
  45. Token string `json:"token"`
  46. SubDomainHost string `json:"subdomain_host"`
  47. TcpMux bool `json:"tcp_mux"`
  48. Custom404Page string `json:"custom_404_page"`
  49. AllowPorts map[int]struct{}
  50. MaxPoolCount int64 `json:"max_pool_count"`
  51. MaxPortsPerClient int64 `json:"max_ports_per_client"`
  52. HeartBeatTimeout int64 `json:"heart_beat_timeout"`
  53. UserConnTimeout int64 `json:"user_conn_timeout"`
  54. }
  55. func GetDefaultServerConf() ServerCommonConf {
  56. return ServerCommonConf{
  57. BindAddr: "0.0.0.0",
  58. BindPort: 7000,
  59. BindUdpPort: 0,
  60. KcpBindPort: 0,
  61. ProxyBindAddr: "0.0.0.0",
  62. VhostHttpPort: 0,
  63. VhostHttpsPort: 0,
  64. VhostHttpTimeout: 60,
  65. DashboardAddr: "0.0.0.0",
  66. DashboardPort: 0,
  67. DashboardUser: "admin",
  68. DashboardPwd: "admin",
  69. AssetsDir: "",
  70. LogFile: "console",
  71. LogWay: "console",
  72. LogLevel: "info",
  73. LogMaxDays: 3,
  74. DisableLogColor: false,
  75. Token: "",
  76. SubDomainHost: "",
  77. TcpMux: true,
  78. AllowPorts: make(map[int]struct{}),
  79. MaxPoolCount: 5,
  80. MaxPortsPerClient: 0,
  81. HeartBeatTimeout: 90,
  82. UserConnTimeout: 10,
  83. Custom404Page: "",
  84. }
  85. }
  86. func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error) {
  87. cfg = GetDefaultServerConf()
  88. conf, err := ini.Load(strings.NewReader(content))
  89. if err != nil {
  90. err = fmt.Errorf("parse ini conf file error: %v", err)
  91. return ServerCommonConf{}, err
  92. }
  93. var (
  94. tmpStr string
  95. ok bool
  96. v int64
  97. )
  98. if tmpStr, ok = conf.Get("common", "bind_addr"); ok {
  99. cfg.BindAddr = tmpStr
  100. }
  101. if tmpStr, ok = conf.Get("common", "bind_port"); ok {
  102. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  103. err = fmt.Errorf("Parse conf error: invalid bind_port")
  104. return
  105. } else {
  106. cfg.BindPort = int(v)
  107. }
  108. }
  109. if tmpStr, ok = conf.Get("common", "bind_udp_port"); ok {
  110. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  111. err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
  112. return
  113. } else {
  114. cfg.BindUdpPort = int(v)
  115. }
  116. }
  117. if tmpStr, ok = conf.Get("common", "kcp_bind_port"); ok {
  118. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  119. err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
  120. return
  121. } else {
  122. cfg.KcpBindPort = int(v)
  123. }
  124. }
  125. if tmpStr, ok = conf.Get("common", "proxy_bind_addr"); ok {
  126. cfg.ProxyBindAddr = tmpStr
  127. } else {
  128. cfg.ProxyBindAddr = cfg.BindAddr
  129. }
  130. if tmpStr, ok = conf.Get("common", "vhost_http_port"); ok {
  131. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  132. err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
  133. return
  134. } else {
  135. cfg.VhostHttpPort = int(v)
  136. }
  137. } else {
  138. cfg.VhostHttpPort = 0
  139. }
  140. if tmpStr, ok = conf.Get("common", "vhost_https_port"); ok {
  141. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  142. err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
  143. return
  144. } else {
  145. cfg.VhostHttpsPort = int(v)
  146. }
  147. } else {
  148. cfg.VhostHttpsPort = 0
  149. }
  150. if tmpStr, ok = conf.Get("common", "vhost_http_timeout"); ok {
  151. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  152. if errRet != nil || v < 0 {
  153. err = fmt.Errorf("Parse conf error: invalid vhost_http_timeout")
  154. return
  155. } else {
  156. cfg.VhostHttpTimeout = v
  157. }
  158. }
  159. if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok {
  160. cfg.DashboardAddr = tmpStr
  161. } else {
  162. cfg.DashboardAddr = cfg.BindAddr
  163. }
  164. if tmpStr, ok = conf.Get("common", "dashboard_port"); ok {
  165. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  166. err = fmt.Errorf("Parse conf error: invalid dashboard_port")
  167. return
  168. } else {
  169. cfg.DashboardPort = int(v)
  170. }
  171. } else {
  172. cfg.DashboardPort = 0
  173. }
  174. if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
  175. cfg.DashboardUser = tmpStr
  176. }
  177. if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
  178. cfg.DashboardPwd = tmpStr
  179. }
  180. if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
  181. cfg.AssetsDir = tmpStr
  182. }
  183. if tmpStr, ok = conf.Get("common", "log_file"); ok {
  184. cfg.LogFile = tmpStr
  185. if cfg.LogFile == "console" {
  186. cfg.LogWay = "console"
  187. } else {
  188. cfg.LogWay = "file"
  189. }
  190. }
  191. if tmpStr, ok = conf.Get("common", "log_level"); ok {
  192. cfg.LogLevel = tmpStr
  193. }
  194. if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
  195. v, err = strconv.ParseInt(tmpStr, 10, 64)
  196. if err == nil {
  197. cfg.LogMaxDays = v
  198. }
  199. }
  200. if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
  201. cfg.DisableLogColor = true
  202. }
  203. cfg.Token, _ = conf.Get("common", "token")
  204. if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
  205. // e.g. 1000-2000,2001,2002,3000-4000
  206. ports, errRet := util.ParseRangeNumbers(allowPortsStr)
  207. if errRet != nil {
  208. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  209. return
  210. }
  211. for _, port := range ports {
  212. cfg.AllowPorts[int(port)] = struct{}{}
  213. }
  214. }
  215. if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
  216. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  217. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  218. return
  219. } else {
  220. if v < 0 {
  221. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  222. return
  223. }
  224. cfg.MaxPoolCount = v
  225. }
  226. }
  227. if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
  228. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  229. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  230. return
  231. } else {
  232. if v < 0 {
  233. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  234. return
  235. }
  236. cfg.MaxPortsPerClient = v
  237. }
  238. }
  239. if tmpStr, ok = conf.Get("common", "subdomain_host"); ok {
  240. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  241. }
  242. if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
  243. cfg.TcpMux = false
  244. } else {
  245. cfg.TcpMux = true
  246. }
  247. if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
  248. cfg.Custom404Page = tmpStr
  249. }
  250. if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
  251. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  252. if errRet != nil {
  253. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  254. return
  255. } else {
  256. cfg.HeartBeatTimeout = v
  257. }
  258. }
  259. return
  260. }
  261. func (cfg *ServerCommonConf) Check() (err error) {
  262. return
  263. }