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. // if PrivilegeAllowPorts is not nil, tcp proxies which remote port exist in this map can be connected
  47. PrivilegeAllowPorts [][2]int64
  48. MaxPoolCount int64
  49. HeartBeatTimeout int64
  50. UserConnTimeout int64
  51. }
  52. func GetDefaultServerCommonConf() *ServerCommonConf {
  53. return &ServerCommonConf{
  54. ConfigFile: "./frps.ini",
  55. BindAddr: "0.0.0.0",
  56. BindPort: 7000,
  57. VhostHttpPort: 0,
  58. VhostHttpsPort: 0,
  59. DashboardPort: 0,
  60. DashboardUser: "admin",
  61. DashboardPwd: "admin",
  62. AssetsDir: "",
  63. LogFile: "console",
  64. LogWay: "console",
  65. LogLevel: "info",
  66. LogMaxDays: 3,
  67. PrivilegeMode: true,
  68. PrivilegeToken: "",
  69. AuthTimeout: 900,
  70. SubDomainHost: "",
  71. TcpMux: true,
  72. MaxPoolCount: 5,
  73. HeartBeatTimeout: 90,
  74. UserConnTimeout: 10,
  75. }
  76. }
  77. // Load server common configure.
  78. func LoadServerCommonConf(conf ini.File) (cfg *ServerCommonConf, err error) {
  79. var (
  80. tmpStr string
  81. ok bool
  82. v int64
  83. )
  84. cfg = GetDefaultServerCommonConf()
  85. tmpStr, ok = conf.Get("common", "bind_addr")
  86. if ok {
  87. cfg.BindAddr = tmpStr
  88. }
  89. tmpStr, ok = conf.Get("common", "bind_port")
  90. if ok {
  91. v, err = strconv.ParseInt(tmpStr, 10, 64)
  92. if err == nil {
  93. cfg.BindPort = v
  94. }
  95. }
  96. tmpStr, ok = conf.Get("common", "vhost_http_port")
  97. if ok {
  98. cfg.VhostHttpPort, err = strconv.ParseInt(tmpStr, 10, 64)
  99. if err != nil {
  100. err = fmt.Errorf("Parse conf error: vhost_http_port is incorrect")
  101. return
  102. }
  103. } else {
  104. cfg.VhostHttpPort = 0
  105. }
  106. tmpStr, ok = conf.Get("common", "vhost_https_port")
  107. if ok {
  108. cfg.VhostHttpsPort, err = strconv.ParseInt(tmpStr, 10, 64)
  109. if err != nil {
  110. err = fmt.Errorf("Parse conf error: vhost_https_port is incorrect")
  111. return
  112. }
  113. } else {
  114. cfg.VhostHttpsPort = 0
  115. }
  116. tmpStr, ok = conf.Get("common", "dashboard_port")
  117. if ok {
  118. cfg.DashboardPort, err = strconv.ParseInt(tmpStr, 10, 64)
  119. if err != nil {
  120. err = fmt.Errorf("Parse conf error: dashboard_port is incorrect")
  121. return
  122. }
  123. } else {
  124. cfg.DashboardPort = 0
  125. }
  126. tmpStr, ok = conf.Get("common", "dashboard_user")
  127. if ok {
  128. cfg.DashboardUser = tmpStr
  129. }
  130. tmpStr, ok = conf.Get("common", "dashboard_pwd")
  131. if ok {
  132. cfg.DashboardPwd = tmpStr
  133. }
  134. tmpStr, ok = conf.Get("common", "assets_dir")
  135. if ok {
  136. cfg.AssetsDir = tmpStr
  137. }
  138. tmpStr, ok = conf.Get("common", "log_file")
  139. if ok {
  140. cfg.LogFile = tmpStr
  141. if cfg.LogFile == "console" {
  142. cfg.LogWay = "console"
  143. } else {
  144. cfg.LogWay = "file"
  145. }
  146. }
  147. tmpStr, ok = conf.Get("common", "log_level")
  148. if ok {
  149. cfg.LogLevel = tmpStr
  150. }
  151. tmpStr, ok = conf.Get("common", "log_max_days")
  152. if ok {
  153. v, err = strconv.ParseInt(tmpStr, 10, 64)
  154. if err == nil {
  155. cfg.LogMaxDays = v
  156. }
  157. }
  158. tmpStr, ok = conf.Get("common", "privilege_mode")
  159. if ok {
  160. if tmpStr == "true" {
  161. cfg.PrivilegeMode = true
  162. }
  163. }
  164. // PrivilegeMode configure
  165. if cfg.PrivilegeMode == true {
  166. tmpStr, ok = conf.Get("common", "privilege_token")
  167. if ok {
  168. if tmpStr == "" {
  169. err = fmt.Errorf("Parse conf error: privilege_token can not be empty")
  170. return
  171. }
  172. cfg.PrivilegeToken = tmpStr
  173. } else {
  174. err = fmt.Errorf("Parse conf error: privilege_token must be set if privilege_mode is enabled")
  175. return
  176. }
  177. allowPortsStr, ok := conf.Get("common", "privilege_allow_ports")
  178. // TODO: check if conflicts exist in port ranges
  179. if ok {
  180. cfg.PrivilegeAllowPorts, err = util.GetPortRanges(allowPortsStr)
  181. if err != nil {
  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. }