server_common.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. "github.com/fatedier/frp/utils/util"
  21. )
  22. var (
  23. // server global configure used for generate proxy conf used in frps
  24. proxyBindAddr string
  25. subDomainHost string
  26. vhostHttpPort int
  27. vhostHttpsPort int
  28. )
  29. func InitServerCfg(cfg *ServerCommonConf) {
  30. proxyBindAddr = cfg.ProxyBindAddr
  31. subDomainHost = cfg.SubDomainHost
  32. vhostHttpPort = cfg.VhostHttpPort
  33. vhostHttpsPort = cfg.VhostHttpsPort
  34. }
  35. // common config
  36. type ServerCommonConf struct {
  37. BindAddr string `json:"bind_addr"`
  38. BindPort int `json:"bind_port"`
  39. BindUdpPort int `json:"bind_udp_port"`
  40. KcpBindPort int `json:"kcp_bind_port"`
  41. ProxyBindAddr string `json:"proxy_bind_addr"`
  42. // If VhostHttpPort equals 0, don't listen a public port for http protocol.
  43. VhostHttpPort int `json:"vhost_http_port"`
  44. // if VhostHttpsPort equals 0, don't listen a public port for https protocol
  45. VhostHttpsPort int `json:"vhost_https_port"`
  46. VhostHttpTimeout int64 `json:"vhost_http_timeout"`
  47. DashboardAddr string `json:"dashboard_addr"`
  48. // if DashboardPort equals 0, dashboard is not available
  49. DashboardPort int `json:"dashboard_port"`
  50. DashboardUser string `json:"dashboard_user"`
  51. DashboardPwd string `json:"dashboard_pwd"`
  52. AssetsDir string `json:"asserts_dir"`
  53. LogFile string `json:"log_file"`
  54. LogWay string `json:"log_way"` // console or file
  55. LogLevel string `json:"log_level"`
  56. LogMaxDays int64 `json:"log_max_days"`
  57. Token string `json:"token"`
  58. SubDomainHost string `json:"subdomain_host"`
  59. TcpMux bool `json:"tcp_mux"`
  60. AllowPorts map[int]struct{}
  61. MaxPoolCount int64 `json:"max_pool_count"`
  62. MaxPortsPerClient int64 `json:"max_ports_per_client"`
  63. HeartBeatTimeout int64 `json:"heart_beat_timeout"`
  64. UserConnTimeout int64 `json:"user_conn_timeout"`
  65. }
  66. func GetDefaultServerConf() *ServerCommonConf {
  67. return &ServerCommonConf{
  68. BindAddr: "0.0.0.0",
  69. BindPort: 7000,
  70. BindUdpPort: 0,
  71. KcpBindPort: 0,
  72. ProxyBindAddr: "0.0.0.0",
  73. VhostHttpPort: 0,
  74. VhostHttpsPort: 0,
  75. VhostHttpTimeout: 60,
  76. DashboardAddr: "0.0.0.0",
  77. DashboardPort: 0,
  78. DashboardUser: "admin",
  79. DashboardPwd: "admin",
  80. AssetsDir: "",
  81. LogFile: "console",
  82. LogWay: "console",
  83. LogLevel: "info",
  84. LogMaxDays: 3,
  85. Token: "",
  86. SubDomainHost: "",
  87. TcpMux: true,
  88. AllowPorts: make(map[int]struct{}),
  89. MaxPoolCount: 5,
  90. MaxPortsPerClient: 0,
  91. HeartBeatTimeout: 90,
  92. UserConnTimeout: 10,
  93. }
  94. }
  95. func UnmarshalServerConfFromIni(defaultCfg *ServerCommonConf, content string) (cfg *ServerCommonConf, err error) {
  96. cfg = defaultCfg
  97. if cfg == nil {
  98. cfg = GetDefaultServerConf()
  99. }
  100. conf, err := ini.Load(strings.NewReader(content))
  101. if err != nil {
  102. err = fmt.Errorf("parse ini conf file error: %v", err)
  103. return nil, err
  104. }
  105. var (
  106. tmpStr string
  107. ok bool
  108. v int64
  109. )
  110. if tmpStr, ok = conf.Get("common", "bind_addr"); ok {
  111. cfg.BindAddr = tmpStr
  112. }
  113. if tmpStr, ok = conf.Get("common", "bind_port"); ok {
  114. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  115. err = fmt.Errorf("Parse conf error: invalid bind_port")
  116. return
  117. } else {
  118. cfg.BindPort = int(v)
  119. }
  120. }
  121. if tmpStr, ok = conf.Get("common", "bind_udp_port"); ok {
  122. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  123. err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
  124. return
  125. } else {
  126. cfg.BindUdpPort = int(v)
  127. }
  128. }
  129. if tmpStr, ok = conf.Get("common", "kcp_bind_port"); ok {
  130. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  131. err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
  132. return
  133. } else {
  134. cfg.KcpBindPort = int(v)
  135. }
  136. }
  137. if tmpStr, ok = conf.Get("common", "proxy_bind_addr"); ok {
  138. cfg.ProxyBindAddr = tmpStr
  139. } else {
  140. cfg.ProxyBindAddr = cfg.BindAddr
  141. }
  142. if tmpStr, ok = conf.Get("common", "vhost_http_port"); ok {
  143. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  144. err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
  145. return
  146. } else {
  147. cfg.VhostHttpPort = int(v)
  148. }
  149. } else {
  150. cfg.VhostHttpPort = 0
  151. }
  152. if tmpStr, ok = conf.Get("common", "vhost_https_port"); ok {
  153. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  154. err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
  155. return
  156. } else {
  157. cfg.VhostHttpsPort = int(v)
  158. }
  159. } else {
  160. cfg.VhostHttpsPort = 0
  161. }
  162. if tmpStr, ok = conf.Get("common", "vhost_http_timeout"); ok {
  163. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  164. if errRet != nil || v < 0 {
  165. err = fmt.Errorf("Parse conf error: invalid vhost_http_timeout")
  166. return
  167. } else {
  168. cfg.VhostHttpTimeout = v
  169. }
  170. }
  171. if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok {
  172. cfg.DashboardAddr = tmpStr
  173. } else {
  174. cfg.DashboardAddr = cfg.BindAddr
  175. }
  176. if tmpStr, ok = conf.Get("common", "dashboard_port"); ok {
  177. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  178. err = fmt.Errorf("Parse conf error: invalid dashboard_port")
  179. return
  180. } else {
  181. cfg.DashboardPort = int(v)
  182. }
  183. } else {
  184. cfg.DashboardPort = 0
  185. }
  186. if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
  187. cfg.DashboardUser = tmpStr
  188. }
  189. if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
  190. cfg.DashboardPwd = tmpStr
  191. }
  192. if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
  193. cfg.AssetsDir = tmpStr
  194. }
  195. if tmpStr, ok = conf.Get("common", "log_file"); ok {
  196. cfg.LogFile = tmpStr
  197. if cfg.LogFile == "console" {
  198. cfg.LogWay = "console"
  199. } else {
  200. cfg.LogWay = "file"
  201. }
  202. }
  203. if tmpStr, ok = conf.Get("common", "log_level"); ok {
  204. cfg.LogLevel = tmpStr
  205. }
  206. if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
  207. v, err = strconv.ParseInt(tmpStr, 10, 64)
  208. if err == nil {
  209. cfg.LogMaxDays = v
  210. }
  211. }
  212. cfg.Token, _ = conf.Get("common", "token")
  213. if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
  214. // e.g. 1000-2000,2001,2002,3000-4000
  215. ports, errRet := util.ParseRangeNumbers(allowPortsStr)
  216. if errRet != nil {
  217. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  218. return
  219. }
  220. for _, port := range ports {
  221. cfg.AllowPorts[int(port)] = struct{}{}
  222. }
  223. }
  224. if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
  225. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  226. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  227. return
  228. } else {
  229. if v < 0 {
  230. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  231. return
  232. }
  233. cfg.MaxPoolCount = v
  234. }
  235. }
  236. if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
  237. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  238. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  239. return
  240. } else {
  241. if v < 0 {
  242. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  243. return
  244. }
  245. cfg.MaxPortsPerClient = v
  246. }
  247. }
  248. if tmpStr, ok = conf.Get("common", "subdomain_host"); ok {
  249. cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
  250. }
  251. if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
  252. cfg.TcpMux = false
  253. } else {
  254. cfg.TcpMux = true
  255. }
  256. if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
  257. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  258. if errRet != nil {
  259. err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
  260. return
  261. } else {
  262. cfg.HeartBeatTimeout = v
  263. }
  264. }
  265. return
  266. }
  267. func (cfg *ServerCommonConf) Check() (err error) {
  268. return
  269. }