server_common.go 5.8 KB

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