server_common.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_http_port"`
  46. DashboardAddr string `json:"dashboard_addr"`
  47. // if DashboardPort equals 0, dashboard is not available
  48. DashboardPort int `json:"dashboard_port"`
  49. DashboardUser string `json:"dashboard_user"`
  50. DashboardPwd string `json:"dashboard_pwd"`
  51. AssetsDir string `json:"asserts_dir"`
  52. LogFile string `json:"log_file"`
  53. LogWay string `json:"log_way"` // console or file
  54. LogLevel string `json:"log_level"`
  55. LogMaxDays int64 `json:"log_max_days"`
  56. Token string `json:"token"`
  57. AuthTimeout int64 `json:"auth_timeout"`
  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. DashboardAddr: "0.0.0.0",
  76. DashboardPort: 0,
  77. DashboardUser: "admin",
  78. DashboardPwd: "admin",
  79. AssetsDir: "",
  80. LogFile: "console",
  81. LogWay: "console",
  82. LogLevel: "info",
  83. LogMaxDays: 3,
  84. Token: "",
  85. AuthTimeout: 900,
  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", "dashboard_addr"); ok {
  163. cfg.DashboardAddr = tmpStr
  164. } else {
  165. cfg.DashboardAddr = cfg.BindAddr
  166. }
  167. if tmpStr, ok = conf.Get("common", "dashboard_port"); ok {
  168. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  169. err = fmt.Errorf("Parse conf error: invalid dashboard_port")
  170. return
  171. } else {
  172. cfg.DashboardPort = int(v)
  173. }
  174. } else {
  175. cfg.DashboardPort = 0
  176. }
  177. if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
  178. cfg.DashboardUser = tmpStr
  179. }
  180. if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
  181. cfg.DashboardPwd = tmpStr
  182. }
  183. if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
  184. cfg.AssetsDir = tmpStr
  185. }
  186. if tmpStr, ok = conf.Get("common", "log_file"); ok {
  187. cfg.LogFile = tmpStr
  188. if cfg.LogFile == "console" {
  189. cfg.LogWay = "console"
  190. } else {
  191. cfg.LogWay = "file"
  192. }
  193. }
  194. if tmpStr, ok = conf.Get("common", "log_level"); ok {
  195. cfg.LogLevel = tmpStr
  196. }
  197. if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
  198. v, err = strconv.ParseInt(tmpStr, 10, 64)
  199. if err == nil {
  200. cfg.LogMaxDays = v
  201. }
  202. }
  203. cfg.Token, _ = conf.Get("common", "token")
  204. if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
  205. // e.g. 1000-2000,2001,2002,3000-4000
  206. ports, errRet := util.ParseRangeNumbers(allowPortsStr)
  207. if errRet != nil {
  208. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  209. return
  210. }
  211. for _, port := range ports {
  212. cfg.AllowPorts[int(port)] = struct{}{}
  213. }
  214. }
  215. if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
  216. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  217. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  218. return
  219. } else {
  220. if v < 0 {
  221. err = fmt.Errorf("Parse conf error: invalid max_pool_count")
  222. return
  223. }
  224. cfg.MaxPoolCount = v
  225. }
  226. }
  227. if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
  228. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  229. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  230. return
  231. } else {
  232. if v < 0 {
  233. err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
  234. return
  235. }
  236. cfg.MaxPortsPerClient = v
  237. }
  238. }
  239. if tmpStr, ok = conf.Get("common", "authentication_timeout"); ok {
  240. v, errRet := strconv.ParseInt(tmpStr, 10, 64)
  241. if errRet != nil {
  242. err = fmt.Errorf("Parse conf error: authentication_timeout is incorrect")
  243. return
  244. } else {
  245. cfg.AuthTimeout = 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. }