server_common.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // if DashboardPort equals 0, dashboard is not available
  36. DashboardPort int64
  37. DashboardUser string
  38. DashboardPwd string
  39. AssetsDir string
  40. LogFile string
  41. LogWay string // console or file
  42. LogLevel string
  43. LogMaxDays int64
  44. PrivilegeMode bool
  45. PrivilegeToken string
  46. AuthTimeout int64
  47. SubDomainHost string
  48. TcpMux bool
  49. // if PrivilegeAllowPorts is not nil, tcp proxies which remote port exist in this map can be connected
  50. PrivilegeAllowPorts [][2]int64
  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. DashboardPort: 0,
  66. DashboardUser: "admin",
  67. DashboardPwd: "admin",
  68. AssetsDir: "",
  69. LogFile: "console",
  70. LogWay: "console",
  71. LogLevel: "info",
  72. LogMaxDays: 3,
  73. PrivilegeMode: true,
  74. PrivilegeToken: "",
  75. AuthTimeout: 900,
  76. SubDomainHost: "",
  77. TcpMux: true,
  78. MaxPoolCount: 5,
  79. HeartBeatTimeout: 90,
  80. UserConnTimeout: 10,
  81. }
  82. }
  83. // Load server common configure.
  84. func LoadServerCommonConf(conf ini.File) (cfg *ServerCommonConf, err error) {
  85. var (
  86. tmpStr string
  87. ok bool
  88. v int64
  89. )
  90. cfg = GetDefaultServerCommonConf()
  91. tmpStr, ok = conf.Get("common", "bind_addr")
  92. if ok {
  93. cfg.BindAddr = tmpStr
  94. }
  95. tmpStr, ok = conf.Get("common", "bind_port")
  96. if ok {
  97. v, err = strconv.ParseInt(tmpStr, 10, 64)
  98. if err == nil {
  99. cfg.BindPort = v
  100. }
  101. }
  102. tmpStr, ok = conf.Get("common", "bind_udp_port")
  103. if ok {
  104. v, err = strconv.ParseInt(tmpStr, 10, 64)
  105. if err == nil {
  106. cfg.BindUdpPort = v
  107. }
  108. }
  109. tmpStr, ok = conf.Get("common", "kcp_bind_port")
  110. if ok {
  111. v, err = strconv.ParseInt(tmpStr, 10, 64)
  112. if err == nil && v > 0 {
  113. cfg.KcpBindPort = v
  114. }
  115. }
  116. tmpStr, ok = conf.Get("common", "proxy_bind_addr")
  117. if ok {
  118. cfg.ProxyBindAddr = tmpStr
  119. } else {
  120. cfg.ProxyBindAddr = cfg.BindAddr
  121. }
  122. tmpStr, ok = conf.Get("common", "vhost_http_port")
  123. if ok {
  124. cfg.VhostHttpPort, err = strconv.ParseInt(tmpStr, 10, 64)
  125. if err != nil {
  126. err = fmt.Errorf("Parse conf error: vhost_http_port is incorrect")
  127. return
  128. }
  129. } else {
  130. cfg.VhostHttpPort = 0
  131. }
  132. tmpStr, ok = conf.Get("common", "vhost_https_port")
  133. if ok {
  134. cfg.VhostHttpsPort, err = strconv.ParseInt(tmpStr, 10, 64)
  135. if err != nil {
  136. err = fmt.Errorf("Parse conf error: vhost_https_port is incorrect")
  137. return
  138. }
  139. } else {
  140. cfg.VhostHttpsPort = 0
  141. }
  142. tmpStr, ok = conf.Get("common", "dashboard_port")
  143. if ok {
  144. cfg.DashboardPort, err = strconv.ParseInt(tmpStr, 10, 64)
  145. if err != nil {
  146. err = fmt.Errorf("Parse conf error: dashboard_port is incorrect")
  147. return
  148. }
  149. } else {
  150. cfg.DashboardPort = 0
  151. }
  152. tmpStr, ok = conf.Get("common", "dashboard_user")
  153. if ok {
  154. cfg.DashboardUser = tmpStr
  155. }
  156. tmpStr, ok = conf.Get("common", "dashboard_pwd")
  157. if ok {
  158. cfg.DashboardPwd = tmpStr
  159. }
  160. tmpStr, ok = conf.Get("common", "assets_dir")
  161. if ok {
  162. cfg.AssetsDir = tmpStr
  163. }
  164. tmpStr, ok = conf.Get("common", "log_file")
  165. if ok {
  166. cfg.LogFile = tmpStr
  167. if cfg.LogFile == "console" {
  168. cfg.LogWay = "console"
  169. } else {
  170. cfg.LogWay = "file"
  171. }
  172. }
  173. tmpStr, ok = conf.Get("common", "log_level")
  174. if ok {
  175. cfg.LogLevel = tmpStr
  176. }
  177. tmpStr, ok = conf.Get("common", "log_max_days")
  178. if ok {
  179. v, err = strconv.ParseInt(tmpStr, 10, 64)
  180. if err == nil {
  181. cfg.LogMaxDays = v
  182. }
  183. }
  184. tmpStr, ok = conf.Get("common", "privilege_mode")
  185. if ok {
  186. if tmpStr == "true" {
  187. cfg.PrivilegeMode = true
  188. }
  189. }
  190. // PrivilegeMode configure
  191. if cfg.PrivilegeMode == true {
  192. cfg.PrivilegeToken, _ = conf.Get("common", "privilege_token")
  193. allowPortsStr, ok := conf.Get("common", "privilege_allow_ports")
  194. // TODO: check if conflicts exist in port ranges
  195. if ok {
  196. cfg.PrivilegeAllowPorts, err = util.GetPortRanges(allowPortsStr)
  197. if err != nil {
  198. err = fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", err)
  199. return
  200. }
  201. }
  202. }
  203. tmpStr, ok = conf.Get("common", "max_pool_count")
  204. if ok {
  205. v, err = strconv.ParseInt(tmpStr, 10, 64)
  206. if err == nil && v >= 0 {
  207. cfg.MaxPoolCount = v
  208. }
  209. }
  210. tmpStr, ok = conf.Get("common", "authentication_timeout")
  211. if ok {
  212. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  213. if errRet != nil {
  214. err = fmt.Errorf("Parse conf error: authentication_timeout is incorrect")
  215. return
  216. } else {
  217. cfg.AuthTimeout = v
  218. }
  219. }
  220. tmpStr, ok = conf.Get("common", "subdomain_host")
  221. if ok {
  222. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  223. }
  224. tmpStr, ok = conf.Get("common", "tcp_mux")
  225. if ok && tmpStr == "false" {
  226. cfg.TcpMux = false
  227. } else {
  228. cfg.TcpMux = true
  229. }
  230. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  231. if ok {
  232. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  233. if errRet != nil {
  234. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  235. return
  236. } else {
  237. cfg.HeartBeatTimeout = v
  238. }
  239. }
  240. return
  241. }