server_common.go 8.3 KB

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