server_common.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. HeartBeatTimeout int64
  53. UserConnTimeout int64
  54. }
  55. func GetDefaultServerCommonConf() *ServerCommonConf {
  56. return &ServerCommonConf{
  57. ConfigFile: "./frps.ini",
  58. BindAddr: "0.0.0.0",
  59. BindPort: 7000,
  60. BindUdpPort: 0,
  61. KcpBindPort: 0,
  62. ProxyBindAddr: "0.0.0.0",
  63. VhostHttpPort: 0,
  64. VhostHttpsPort: 0,
  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. PrivilegeMode: true,
  75. PrivilegeToken: "",
  76. AuthTimeout: 900,
  77. SubDomainHost: "",
  78. TcpMux: true,
  79. PrivilegeAllowPorts: make(map[int]struct{}),
  80. MaxPoolCount: 5,
  81. HeartBeatTimeout: 90,
  82. UserConnTimeout: 10,
  83. }
  84. }
  85. // Load server common configure.
  86. func LoadServerCommonConf(conf ini.File) (cfg *ServerCommonConf, err error) {
  87. var (
  88. tmpStr string
  89. ok bool
  90. v int64
  91. )
  92. cfg = GetDefaultServerCommonConf()
  93. tmpStr, ok = conf.Get("common", "bind_addr")
  94. if ok {
  95. cfg.BindAddr = tmpStr
  96. }
  97. tmpStr, ok = conf.Get("common", "bind_port")
  98. if ok {
  99. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  100. err = fmt.Errorf("Parse conf error: invalid bind_port")
  101. return
  102. } else {
  103. cfg.BindPort = int(v)
  104. }
  105. }
  106. tmpStr, ok = conf.Get("common", "bind_udp_port")
  107. if ok {
  108. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  109. err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
  110. return
  111. } else {
  112. cfg.BindUdpPort = int(v)
  113. }
  114. }
  115. tmpStr, ok = conf.Get("common", "kcp_bind_port")
  116. if ok {
  117. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  118. err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
  119. return
  120. } else {
  121. cfg.KcpBindPort = int(v)
  122. }
  123. }
  124. tmpStr, ok = conf.Get("common", "proxy_bind_addr")
  125. if ok {
  126. cfg.ProxyBindAddr = tmpStr
  127. } else {
  128. cfg.ProxyBindAddr = cfg.BindAddr
  129. }
  130. tmpStr, ok = conf.Get("common", "vhost_http_port")
  131. if ok {
  132. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  133. err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
  134. return
  135. } else {
  136. cfg.VhostHttpPort = int(v)
  137. }
  138. } else {
  139. cfg.VhostHttpPort = 0
  140. }
  141. tmpStr, ok = conf.Get("common", "vhost_https_port")
  142. if ok {
  143. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  144. err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
  145. return
  146. } else {
  147. cfg.VhostHttpsPort = int(v)
  148. }
  149. } else {
  150. cfg.VhostHttpsPort = 0
  151. }
  152. tmpStr, ok = conf.Get("common", "dashboard_addr")
  153. if ok {
  154. cfg.DashboardAddr = tmpStr
  155. } else {
  156. cfg.DashboardAddr = cfg.BindAddr
  157. }
  158. tmpStr, ok = conf.Get("common", "dashboard_port")
  159. if ok {
  160. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  161. err = fmt.Errorf("Parse conf error: invalid dashboard_port")
  162. return
  163. } else {
  164. cfg.DashboardPort = int(v)
  165. }
  166. } else {
  167. cfg.DashboardPort = 0
  168. }
  169. tmpStr, ok = conf.Get("common", "dashboard_user")
  170. if ok {
  171. cfg.DashboardUser = tmpStr
  172. }
  173. tmpStr, ok = conf.Get("common", "dashboard_pwd")
  174. if ok {
  175. cfg.DashboardPwd = tmpStr
  176. }
  177. tmpStr, ok = conf.Get("common", "assets_dir")
  178. if ok {
  179. cfg.AssetsDir = tmpStr
  180. }
  181. tmpStr, ok = conf.Get("common", "log_file")
  182. if ok {
  183. cfg.LogFile = tmpStr
  184. if cfg.LogFile == "console" {
  185. cfg.LogWay = "console"
  186. } else {
  187. cfg.LogWay = "file"
  188. }
  189. }
  190. tmpStr, ok = conf.Get("common", "log_level")
  191. if ok {
  192. cfg.LogLevel = tmpStr
  193. }
  194. tmpStr, ok = conf.Get("common", "log_max_days")
  195. if ok {
  196. v, err = strconv.ParseInt(tmpStr, 10, 64)
  197. if err == nil {
  198. cfg.LogMaxDays = v
  199. }
  200. }
  201. tmpStr, ok = conf.Get("common", "privilege_mode")
  202. if ok {
  203. if tmpStr == "true" {
  204. cfg.PrivilegeMode = true
  205. }
  206. }
  207. // PrivilegeMode configure
  208. if cfg.PrivilegeMode == true {
  209. cfg.PrivilegeToken, _ = conf.Get("common", "privilege_token")
  210. allowPortsStr, ok := conf.Get("common", "privilege_allow_ports")
  211. if ok {
  212. // e.g. 1000-2000,2001,2002,3000-4000
  213. ports, errRet := util.ParseRangeNumbers(allowPortsStr)
  214. if errRet != nil {
  215. err = fmt.Errorf("Parse conf error: privilege_allow_ports: %v", errRet)
  216. return
  217. }
  218. for _, port := range ports {
  219. cfg.PrivilegeAllowPorts[int(port)] = struct{}{}
  220. }
  221. }
  222. }
  223. tmpStr, ok = conf.Get("common", "max_pool_count")
  224. if ok {
  225. v, err = strconv.ParseInt(tmpStr, 10, 64)
  226. if err == nil && v >= 0 {
  227. cfg.MaxPoolCount = v
  228. }
  229. }
  230. tmpStr, ok = conf.Get("common", "authentication_timeout")
  231. if ok {
  232. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  233. if errRet != nil {
  234. err = fmt.Errorf("Parse conf error: authentication_timeout is incorrect")
  235. return
  236. } else {
  237. cfg.AuthTimeout = v
  238. }
  239. }
  240. tmpStr, ok = conf.Get("common", "subdomain_host")
  241. if ok {
  242. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  243. }
  244. tmpStr, ok = conf.Get("common", "tcp_mux")
  245. if ok && tmpStr == "false" {
  246. cfg.TcpMux = false
  247. } else {
  248. cfg.TcpMux = true
  249. }
  250. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  251. if ok {
  252. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  253. if errRet != nil {
  254. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  255. return
  256. } else {
  257. cfg.HeartBeatTimeout = v
  258. }
  259. }
  260. return
  261. }