server_common.go 6.8 KB

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