1
0

server_common.go 6.7 KB

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