server_common.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. "github.com/fatedier/frp/utils/util"
  20. ini "github.com/vaughan0/go-ini"
  21. )
  22. var ServerCommonCfg *ServerCommonConf
  23. // common config
  24. type ServerCommonConf struct {
  25. ConfigFile string
  26. BindAddr string
  27. BindPort int64
  28. BindUdpPort int64
  29. KcpBindPort int64
  30. ProxyBindAddr string
  31. // If VhostHttpPort equals 0, don't listen a public port for http protocol.
  32. VhostHttpPort int64
  33. // if VhostHttpsPort equals 0, don't listen a public port for https protocol
  34. VhostHttpsPort int64
  35. DashboardAddr string
  36. // if DashboardPort equals 0, dashboard is not available
  37. DashboardPort int64
  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. // if PrivilegeAllowPorts is not nil, tcp proxies which remote port exist in this map can be connected
  51. PrivilegeAllowPorts [][2]int64
  52. MaxPoolCount 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. 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. v, err = strconv.ParseInt(tmpStr, 10, 64)
  100. if err == nil {
  101. cfg.BindPort = v
  102. }
  103. }
  104. tmpStr, ok = conf.Get("common", "bind_udp_port")
  105. if ok {
  106. v, err = strconv.ParseInt(tmpStr, 10, 64)
  107. if err == nil {
  108. cfg.BindUdpPort = v
  109. }
  110. }
  111. tmpStr, ok = conf.Get("common", "kcp_bind_port")
  112. if ok {
  113. v, err = strconv.ParseInt(tmpStr, 10, 64)
  114. if err == nil && v > 0 {
  115. cfg.KcpBindPort = v
  116. }
  117. }
  118. tmpStr, ok = conf.Get("common", "proxy_bind_addr")
  119. if ok {
  120. cfg.ProxyBindAddr = tmpStr
  121. } else {
  122. cfg.ProxyBindAddr = cfg.BindAddr
  123. }
  124. tmpStr, ok = conf.Get("common", "vhost_http_port")
  125. if ok {
  126. cfg.VhostHttpPort, err = strconv.ParseInt(tmpStr, 10, 64)
  127. if err != nil {
  128. err = fmt.Errorf("Parse conf error: vhost_http_port is incorrect")
  129. return
  130. }
  131. } else {
  132. cfg.VhostHttpPort = 0
  133. }
  134. tmpStr, ok = conf.Get("common", "vhost_https_port")
  135. if ok {
  136. cfg.VhostHttpsPort, err = strconv.ParseInt(tmpStr, 10, 64)
  137. if err != nil {
  138. err = fmt.Errorf("Parse conf error: vhost_https_port is incorrect")
  139. return
  140. }
  141. } else {
  142. cfg.VhostHttpsPort = 0
  143. }
  144. tmpStr, ok = conf.Get("common", "dashboard_addr")
  145. if ok {
  146. cfg.DashboardAddr = tmpStr
  147. } else {
  148. cfg.DashboardAddr = cfg.BindAddr
  149. }
  150. tmpStr, ok = conf.Get("common", "dashboard_port")
  151. if ok {
  152. cfg.DashboardPort, err = strconv.ParseInt(tmpStr, 10, 64)
  153. if err != nil {
  154. err = fmt.Errorf("Parse conf error: dashboard_port is incorrect")
  155. return
  156. }
  157. } else {
  158. cfg.DashboardPort = 0
  159. }
  160. tmpStr, ok = conf.Get("common", "dashboard_user")
  161. if ok {
  162. cfg.DashboardUser = tmpStr
  163. }
  164. tmpStr, ok = conf.Get("common", "dashboard_pwd")
  165. if ok {
  166. cfg.DashboardPwd = tmpStr
  167. }
  168. tmpStr, ok = conf.Get("common", "assets_dir")
  169. if ok {
  170. cfg.AssetsDir = tmpStr
  171. }
  172. tmpStr, ok = conf.Get("common", "log_file")
  173. if ok {
  174. cfg.LogFile = tmpStr
  175. if cfg.LogFile == "console" {
  176. cfg.LogWay = "console"
  177. } else {
  178. cfg.LogWay = "file"
  179. }
  180. }
  181. tmpStr, ok = conf.Get("common", "log_level")
  182. if ok {
  183. cfg.LogLevel = tmpStr
  184. }
  185. tmpStr, ok = conf.Get("common", "log_max_days")
  186. if ok {
  187. v, err = strconv.ParseInt(tmpStr, 10, 64)
  188. if err == nil {
  189. cfg.LogMaxDays = v
  190. }
  191. }
  192. tmpStr, ok = conf.Get("common", "privilege_mode")
  193. if ok {
  194. if tmpStr == "true" {
  195. cfg.PrivilegeMode = true
  196. }
  197. }
  198. // PrivilegeMode configure
  199. if cfg.PrivilegeMode == true {
  200. cfg.PrivilegeToken, _ = conf.Get("common", "privilege_token")
  201. allowPortsStr, ok := conf.Get("common", "privilege_allow_ports")
  202. // TODO: check if conflicts exist in port ranges
  203. if ok {
  204. cfg.PrivilegeAllowPorts, err = util.GetPortRanges(allowPortsStr)
  205. if err != nil {
  206. err = fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", err)
  207. return
  208. }
  209. }
  210. }
  211. tmpStr, ok = conf.Get("common", "max_pool_count")
  212. if ok {
  213. v, err = strconv.ParseInt(tmpStr, 10, 64)
  214. if err == nil && v >= 0 {
  215. cfg.MaxPoolCount = v
  216. }
  217. }
  218. tmpStr, ok = conf.Get("common", "authentication_timeout")
  219. if ok {
  220. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  221. if errRet != nil {
  222. err = fmt.Errorf("Parse conf error: authentication_timeout is incorrect")
  223. return
  224. } else {
  225. cfg.AuthTimeout = v
  226. }
  227. }
  228. tmpStr, ok = conf.Get("common", "subdomain_host")
  229. if ok {
  230. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  231. }
  232. tmpStr, ok = conf.Get("common", "tcp_mux")
  233. if ok && tmpStr == "false" {
  234. cfg.TcpMux = false
  235. } else {
  236. cfg.TcpMux = true
  237. }
  238. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  239. if ok {
  240. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  241. if errRet != nil {
  242. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  243. return
  244. } else {
  245. cfg.HeartBeatTimeout = v
  246. }
  247. }
  248. return
  249. }