server_common.go 6.0 KB

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