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