1
0

server_common.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 int
  27. BindUdpPort int
  28. KcpBindPort int
  29. ProxyBindAddr string
  30. // If VhostHttpPort equals 0, don't listen a public port for http protocol.
  31. VhostHttpPort int
  32. // if VhostHttpsPort equals 0, don't listen a public port for https protocol
  33. VhostHttpsPort int
  34. DashboardAddr string
  35. // if DashboardPort equals 0, dashboard is not available
  36. DashboardPort int
  37. DashboardUser string
  38. DashboardPwd string
  39. AssetsDir string
  40. LogFile string
  41. LogWay string // console or file
  42. LogLevel string
  43. LogMaxDays int64
  44. PrivilegeMode bool
  45. PrivilegeToken string
  46. AuthTimeout int64
  47. SubDomainHost string
  48. TcpMux bool
  49. PrivilegeAllowPorts map[int]struct{}
  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. BindUdpPort: 0,
  60. KcpBindPort: 0,
  61. ProxyBindAddr: "0.0.0.0",
  62. VhostHttpPort: 0,
  63. VhostHttpsPort: 0,
  64. DashboardAddr: "0.0.0.0",
  65. DashboardPort: 0,
  66. DashboardUser: "admin",
  67. DashboardPwd: "admin",
  68. AssetsDir: "",
  69. LogFile: "console",
  70. LogWay: "console",
  71. LogLevel: "info",
  72. LogMaxDays: 3,
  73. PrivilegeMode: true,
  74. PrivilegeToken: "",
  75. AuthTimeout: 900,
  76. SubDomainHost: "",
  77. TcpMux: true,
  78. PrivilegeAllowPorts: make(map[int]struct{}),
  79. MaxPoolCount: 5,
  80. HeartBeatTimeout: 90,
  81. UserConnTimeout: 10,
  82. }
  83. }
  84. // Load server common configure.
  85. func LoadServerCommonConf(conf ini.File) (cfg *ServerCommonConf, err error) {
  86. var (
  87. tmpStr string
  88. ok bool
  89. v int64
  90. )
  91. cfg = GetDefaultServerCommonConf()
  92. tmpStr, ok = conf.Get("common", "bind_addr")
  93. if ok {
  94. cfg.BindAddr = tmpStr
  95. }
  96. tmpStr, ok = conf.Get("common", "bind_port")
  97. if ok {
  98. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  99. err = fmt.Errorf("Parse conf error: invalid bind_port")
  100. return
  101. } else {
  102. cfg.BindPort = int(v)
  103. }
  104. }
  105. tmpStr, ok = conf.Get("common", "bind_udp_port")
  106. if ok {
  107. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  108. err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
  109. return
  110. } else {
  111. cfg.BindUdpPort = int(v)
  112. }
  113. }
  114. tmpStr, ok = conf.Get("common", "kcp_bind_port")
  115. if ok {
  116. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  117. err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
  118. return
  119. } else {
  120. cfg.KcpBindPort = int(v)
  121. }
  122. }
  123. tmpStr, ok = conf.Get("common", "proxy_bind_addr")
  124. if ok {
  125. cfg.ProxyBindAddr = tmpStr
  126. } else {
  127. cfg.ProxyBindAddr = cfg.BindAddr
  128. }
  129. tmpStr, ok = conf.Get("common", "vhost_http_port")
  130. if ok {
  131. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  132. err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
  133. return
  134. } else {
  135. cfg.VhostHttpPort = int(v)
  136. }
  137. } else {
  138. cfg.VhostHttpPort = 0
  139. }
  140. tmpStr, ok = conf.Get("common", "vhost_https_port")
  141. if ok {
  142. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  143. err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
  144. return
  145. } else {
  146. cfg.VhostHttpsPort = int(v)
  147. }
  148. } else {
  149. cfg.VhostHttpsPort = 0
  150. }
  151. tmpStr, ok = conf.Get("common", "dashboard_addr")
  152. if ok {
  153. cfg.DashboardAddr = tmpStr
  154. } else {
  155. cfg.DashboardAddr = cfg.BindAddr
  156. }
  157. tmpStr, ok = conf.Get("common", "dashboard_port")
  158. if ok {
  159. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  160. err = fmt.Errorf("Parse conf error: invalid dashboard_port")
  161. return
  162. } else {
  163. cfg.DashboardPort = int(v)
  164. }
  165. } else {
  166. cfg.DashboardPort = 0
  167. }
  168. tmpStr, ok = conf.Get("common", "dashboard_user")
  169. if ok {
  170. cfg.DashboardUser = tmpStr
  171. }
  172. tmpStr, ok = conf.Get("common", "dashboard_pwd")
  173. if ok {
  174. cfg.DashboardPwd = tmpStr
  175. }
  176. tmpStr, ok = conf.Get("common", "assets_dir")
  177. if ok {
  178. cfg.AssetsDir = tmpStr
  179. }
  180. tmpStr, ok = conf.Get("common", "log_file")
  181. if ok {
  182. cfg.LogFile = tmpStr
  183. if cfg.LogFile == "console" {
  184. cfg.LogWay = "console"
  185. } else {
  186. cfg.LogWay = "file"
  187. }
  188. }
  189. tmpStr, ok = conf.Get("common", "log_level")
  190. if ok {
  191. cfg.LogLevel = tmpStr
  192. }
  193. tmpStr, ok = conf.Get("common", "log_max_days")
  194. if ok {
  195. v, err = strconv.ParseInt(tmpStr, 10, 64)
  196. if err == nil {
  197. cfg.LogMaxDays = v
  198. }
  199. }
  200. tmpStr, ok = conf.Get("common", "privilege_mode")
  201. if ok {
  202. if tmpStr == "true" {
  203. cfg.PrivilegeMode = true
  204. }
  205. }
  206. // PrivilegeMode configure
  207. if cfg.PrivilegeMode == true {
  208. cfg.PrivilegeToken, _ = conf.Get("common", "privilege_token")
  209. allowPortsStr, ok := conf.Get("common", "privilege_allow_ports")
  210. if ok {
  211. // e.g. 1000-2000,2001,2002,3000-4000
  212. portRanges := strings.Split(allowPortsStr, ",")
  213. for _, portRangeStr := range portRanges {
  214. // 1000-2000 or 2001
  215. portArray := strings.Split(portRangeStr, "-")
  216. // length: only 1 or 2 is correct
  217. rangeType := len(portArray)
  218. if rangeType == 1 {
  219. // single port
  220. singlePort, errRet := strconv.ParseInt(portArray[0], 10, 64)
  221. if errRet != nil {
  222. err = fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", errRet)
  223. return
  224. }
  225. cfg.PrivilegeAllowPorts[int(singlePort)] = struct{}{}
  226. } else if rangeType == 2 {
  227. // range ports
  228. min, errRet := strconv.ParseInt(portArray[0], 10, 64)
  229. if errRet != nil {
  230. err = fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", errRet)
  231. return
  232. }
  233. max, errRet := strconv.ParseInt(portArray[1], 10, 64)
  234. if errRet != nil {
  235. err = fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect, %v", errRet)
  236. return
  237. }
  238. if max < min {
  239. err = fmt.Errorf("Parse conf error: privilege_allow_ports range incorrect")
  240. return
  241. }
  242. for i := min; i <= max; i++ {
  243. cfg.PrivilegeAllowPorts[int(i)] = struct{}{}
  244. }
  245. } else {
  246. err = fmt.Errorf("Parse conf error: privilege_allow_ports is incorrect")
  247. return
  248. }
  249. }
  250. }
  251. }
  252. tmpStr, ok = conf.Get("common", "max_pool_count")
  253. if ok {
  254. v, err = strconv.ParseInt(tmpStr, 10, 64)
  255. if err == nil && v >= 0 {
  256. cfg.MaxPoolCount = v
  257. }
  258. }
  259. tmpStr, ok = conf.Get("common", "authentication_timeout")
  260. if ok {
  261. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  262. if errRet != nil {
  263. err = fmt.Errorf("Parse conf error: authentication_timeout is incorrect")
  264. return
  265. } else {
  266. cfg.AuthTimeout = v
  267. }
  268. }
  269. tmpStr, ok = conf.Get("common", "subdomain_host")
  270. if ok {
  271. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  272. }
  273. tmpStr, ok = conf.Get("common", "tcp_mux")
  274. if ok && tmpStr == "false" {
  275. cfg.TcpMux = false
  276. } else {
  277. cfg.TcpMux = true
  278. }
  279. tmpStr, ok = conf.Get("common", "heartbeat_timeout")
  280. if ok {
  281. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  282. if errRet != nil {
  283. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  284. return
  285. } else {
  286. cfg.HeartBeatTimeout = v
  287. }
  288. }
  289. return
  290. }