server_common.go 8.4 KB

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