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. util "github.com/fatedier/frp/utils/util"
  20. "github.com/prometheus/common/log"
  21. ini "github.com/vaughan0/go-ini"
  22. )
  23. var ServerCommonCfg *ServerCommonConf
  24. // common config
  25. type ServerCommonConf struct {
  26. ConfigFile string
  27. BindAddr string
  28. BindPort 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. 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. MaxPoolCount: 5,
  74. HeartBeatTimeout: 90,
  75. UserConnTimeout: 10,
  76. }
  77. }
  78. // Load server common configure.
  79. func LoadServerCommonConf(conf ini.File) (cfg *ServerCommonConf, err error) {
  80. var (
  81. tmpStr string
  82. ok bool
  83. v int64
  84. )
  85. cfg = GetDefaultServerCommonConf()
  86. tmpStr, ok = conf.Get("common", "bind_addr")
  87. if ok {
  88. cfg.BindAddr = tmpStr
  89. }
  90. tmpStr, ok = conf.Get("common", "bind_port")
  91. if ok {
  92. v, err = strconv.ParseInt(tmpStr, 10, 64)
  93. if err == nil {
  94. cfg.BindPort = v
  95. }
  96. }
  97. tmpStr, ok = conf.Get("common", "vhost_http_port")
  98. if ok {
  99. cfg.VhostHttpPort, err = strconv.ParseInt(tmpStr, 10, 64)
  100. if err != nil {
  101. err = fmt.Errorf("Parse conf error: vhost_http_port is incorrect")
  102. return
  103. }
  104. } else {
  105. cfg.VhostHttpPort = 0
  106. }
  107. tmpStr, ok = conf.Get("common", "vhost_https_port")
  108. if ok {
  109. cfg.VhostHttpsPort, err = strconv.ParseInt(tmpStr, 10, 64)
  110. if err != nil {
  111. err = fmt.Errorf("Parse conf error: vhost_https_port is incorrect")
  112. return
  113. }
  114. } else {
  115. cfg.VhostHttpsPort = 0
  116. }
  117. tmpStr, ok = conf.Get("common", "dashboard_port")
  118. if ok {
  119. cfg.DashboardPort, err = strconv.ParseInt(tmpStr, 10, 64)
  120. if err != nil {
  121. err = fmt.Errorf("Parse conf error: dashboard_port is incorrect")
  122. return
  123. }
  124. } else {
  125. cfg.DashboardPort = 0
  126. }
  127. tmpStr, ok = conf.Get("common", "dashboard_user")
  128. if ok {
  129. cfg.DashboardUser = tmpStr
  130. }
  131. tmpStr, ok = conf.Get("common", "dashboard_pwd")
  132. if ok {
  133. cfg.DashboardPwd = tmpStr
  134. }
  135. tmpStr, ok = conf.Get("common", "assets_dir")
  136. if ok {
  137. cfg.AssetsDir = tmpStr
  138. }
  139. tmpStr, ok = conf.Get("common", "log_file")
  140. if ok {
  141. cfg.LogFile = tmpStr
  142. if cfg.LogFile == "console" {
  143. cfg.LogWay = "console"
  144. } else {
  145. cfg.LogWay = "file"
  146. }
  147. }
  148. tmpStr, ok = conf.Get("common", "log_level")
  149. if ok {
  150. cfg.LogLevel = tmpStr
  151. }
  152. tmpStr, ok = conf.Get("common", "log_max_days")
  153. if ok {
  154. v, err = strconv.ParseInt(tmpStr, 10, 64)
  155. if err == nil {
  156. cfg.LogMaxDays = v
  157. }
  158. }
  159. tmpStr, ok = conf.Get("common", "privilege_mode")
  160. if ok {
  161. if tmpStr == "true" {
  162. cfg.PrivilegeMode = true
  163. }
  164. }
  165. // PrivilegeMode configure
  166. if cfg.PrivilegeMode == true {
  167. tmpStr, ok = conf.Get("common", "privilege_token")
  168. if ok {
  169. if tmpStr == "" {
  170. err = fmt.Errorf("Parse conf error: privilege_token can not be empty")
  171. return
  172. }
  173. cfg.PrivilegeToken = tmpStr
  174. } else {
  175. err = fmt.Errorf("Parse conf error: privilege_token must be set if privilege_mode is enabled")
  176. return
  177. }
  178. allowPortsStr, ok := conf.Get("common", "privilege_allow_ports")
  179. // TODO: check if conflicts exist in port ranges
  180. if ok {
  181. cfg.PrivilegeAllowPorts, err = util.GetPortRanges(allowPortsStr)
  182. if err != nil {
  183. return
  184. }
  185. }
  186. }
  187. tmpStr, ok = conf.Get("common", "max_pool_count")
  188. if ok {
  189. v, err = strconv.ParseInt(tmpStr, 10, 64)
  190. if err == nil && v >= 0 {
  191. cfg.MaxPoolCount = v
  192. }
  193. }
  194. tmpStr, ok = conf.Get("common", "authentication_timeout")
  195. if ok {
  196. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  197. if errRet != nil {
  198. err = fmt.Errorf("Parse conf error: authentication_timeout is incorrect")
  199. return
  200. } else {
  201. cfg.AuthTimeout = v
  202. }
  203. }
  204. tmpStr, ok = conf.Get("common", "subdomain_host")
  205. if ok {
  206. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  207. }
  208. tmpStr, ok = conf.Get("common", "tcp_mux")
  209. if ok && tmpStr == "false" {
  210. cfg.TcpMux = false
  211. } else {
  212. cfg.TcpMux = true
  213. }
  214. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  215. if ok {
  216. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  217. if errRet != nil {
  218. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  219. return
  220. } else {
  221. cfg.HeartBeatTimeout = v
  222. }
  223. }
  224. return
  225. }