server_common.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. KcpBindPort int64
  29. // If VhostHttpPort equals 0, don't listen a public port for http protocol.
  30. VhostHttpPort int64
  31. // if VhostHttpsPort equals 0, don't listen a public port for https protocol
  32. VhostHttpsPort int64
  33. // if DashboardPort equals 0, dashboard is not available
  34. DashboardPort int64
  35. DashboardUser string
  36. DashboardPwd string
  37. AssetsDir string
  38. LogFile string
  39. LogWay string // console or file
  40. LogLevel string
  41. LogMaxDays int64
  42. PrivilegeMode bool
  43. PrivilegeToken string
  44. AuthTimeout int64
  45. SubDomainHost string
  46. TcpMux 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. KcpBindPort: 0,
  59. VhostHttpPort: 0,
  60. VhostHttpsPort: 0,
  61. DashboardPort: 0,
  62. DashboardUser: "admin",
  63. DashboardPwd: "admin",
  64. AssetsDir: "",
  65. LogFile: "console",
  66. LogWay: "console",
  67. LogLevel: "info",
  68. LogMaxDays: 3,
  69. PrivilegeMode: true,
  70. PrivilegeToken: "",
  71. AuthTimeout: 900,
  72. SubDomainHost: "",
  73. TcpMux: 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", "kcp_bind_port")
  99. if ok {
  100. v, err = strconv.ParseInt(tmpStr, 10, 64)
  101. if err == nil && v > 0 {
  102. cfg.KcpBindPort = v
  103. }
  104. }
  105. tmpStr, ok = conf.Get("common", "vhost_http_port")
  106. if ok {
  107. cfg.VhostHttpPort, err = strconv.ParseInt(tmpStr, 10, 64)
  108. if err != nil {
  109. err = fmt.Errorf("Parse conf error: vhost_http_port is incorrect")
  110. return
  111. }
  112. } else {
  113. cfg.VhostHttpPort = 0
  114. }
  115. tmpStr, ok = conf.Get("common", "vhost_https_port")
  116. if ok {
  117. cfg.VhostHttpsPort, err = strconv.ParseInt(tmpStr, 10, 64)
  118. if err != nil {
  119. err = fmt.Errorf("Parse conf error: vhost_https_port is incorrect")
  120. return
  121. }
  122. } else {
  123. cfg.VhostHttpsPort = 0
  124. }
  125. tmpStr, ok = conf.Get("common", "dashboard_port")
  126. if ok {
  127. cfg.DashboardPort, err = strconv.ParseInt(tmpStr, 10, 64)
  128. if err != nil {
  129. err = fmt.Errorf("Parse conf error: dashboard_port is incorrect")
  130. return
  131. }
  132. } else {
  133. cfg.DashboardPort = 0
  134. }
  135. tmpStr, ok = conf.Get("common", "dashboard_user")
  136. if ok {
  137. cfg.DashboardUser = tmpStr
  138. }
  139. tmpStr, ok = conf.Get("common", "dashboard_pwd")
  140. if ok {
  141. cfg.DashboardPwd = tmpStr
  142. }
  143. tmpStr, ok = conf.Get("common", "assets_dir")
  144. if ok {
  145. cfg.AssetsDir = tmpStr
  146. }
  147. tmpStr, ok = conf.Get("common", "log_file")
  148. if ok {
  149. cfg.LogFile = tmpStr
  150. if cfg.LogFile == "console" {
  151. cfg.LogWay = "console"
  152. } else {
  153. cfg.LogWay = "file"
  154. }
  155. }
  156. tmpStr, ok = conf.Get("common", "log_level")
  157. if ok {
  158. cfg.LogLevel = tmpStr
  159. }
  160. tmpStr, ok = conf.Get("common", "log_max_days")
  161. if ok {
  162. v, err = strconv.ParseInt(tmpStr, 10, 64)
  163. if err == nil {
  164. cfg.LogMaxDays = v
  165. }
  166. }
  167. tmpStr, ok = conf.Get("common", "privilege_mode")
  168. if ok {
  169. if tmpStr == "true" {
  170. cfg.PrivilegeMode = true
  171. }
  172. }
  173. // PrivilegeMode configure
  174. if cfg.PrivilegeMode == true {
  175. cfg.PrivilegeToken, _ = conf.Get("common", "privilege_token")
  176. allowPortsStr, ok := conf.Get("common", "privilege_allow_ports")
  177. // TODO: check if conflicts exist in port ranges
  178. if ok {
  179. cfg.PrivilegeAllowPorts, err = util.GetPortRanges(allowPortsStr)
  180. if err != nil {
  181. err = fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", err)
  182. return
  183. }
  184. }
  185. }
  186. tmpStr, ok = conf.Get("common", "max_pool_count")
  187. if ok {
  188. v, err = strconv.ParseInt(tmpStr, 10, 64)
  189. if err == nil && v >= 0 {
  190. cfg.MaxPoolCount = v
  191. }
  192. }
  193. tmpStr, ok = conf.Get("common", "authentication_timeout")
  194. if ok {
  195. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  196. if errRet != nil {
  197. err = fmt.Errorf("Parse conf error: authentication_timeout is incorrect")
  198. return
  199. } else {
  200. cfg.AuthTimeout = v
  201. }
  202. }
  203. tmpStr, ok = conf.Get("common", "subdomain_host")
  204. if ok {
  205. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  206. }
  207. tmpStr, ok = conf.Get("common", "tcp_mux")
  208. if ok && tmpStr == "false" {
  209. cfg.TcpMux = false
  210. } else {
  211. cfg.TcpMux = true
  212. }
  213. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  214. if ok {
  215. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  216. if errRet != nil {
  217. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  218. return
  219. } else {
  220. cfg.HeartBeatTimeout = v
  221. }
  222. }
  223. return
  224. }