proxy.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. "reflect"
  18. "strconv"
  19. "strings"
  20. "github.com/fatedier/frp/models/consts"
  21. "github.com/fatedier/frp/models/msg"
  22. "github.com/fatedier/frp/utils/util"
  23. ini "github.com/vaughan0/go-ini"
  24. )
  25. var proxyConfTypeMap map[string]reflect.Type
  26. func init() {
  27. proxyConfTypeMap = make(map[string]reflect.Type)
  28. proxyConfTypeMap[consts.TcpProxy] = reflect.TypeOf(TcpProxyConf{})
  29. proxyConfTypeMap[consts.UdpProxy] = reflect.TypeOf(UdpProxyConf{})
  30. proxyConfTypeMap[consts.HttpProxy] = reflect.TypeOf(HttpProxyConf{})
  31. proxyConfTypeMap[consts.HttpsProxy] = reflect.TypeOf(HttpsProxyConf{})
  32. proxyConfTypeMap[consts.StcpProxy] = reflect.TypeOf(StcpProxyConf{})
  33. }
  34. // NewConfByType creates a empty ProxyConf object by proxyType.
  35. // If proxyType isn't exist, return nil.
  36. func NewConfByType(proxyType string) ProxyConf {
  37. v, ok := proxyConfTypeMap[proxyType]
  38. if !ok {
  39. return nil
  40. }
  41. cfg := reflect.New(v).Interface().(ProxyConf)
  42. return cfg
  43. }
  44. type ProxyConf interface {
  45. GetName() string
  46. GetBaseInfo() *BaseProxyConf
  47. LoadFromMsg(pMsg *msg.NewProxy)
  48. LoadFromFile(name string, conf ini.Section) error
  49. UnMarshalToMsg(pMsg *msg.NewProxy)
  50. Check() error
  51. }
  52. func NewProxyConf(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
  53. if pMsg.ProxyType == "" {
  54. pMsg.ProxyType = consts.TcpProxy
  55. }
  56. cfg = NewConfByType(pMsg.ProxyType)
  57. if cfg == nil {
  58. err = fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
  59. return
  60. }
  61. cfg.LoadFromMsg(pMsg)
  62. err = cfg.Check()
  63. return
  64. }
  65. func NewProxyConfFromFile(name string, section ini.Section) (cfg ProxyConf, err error) {
  66. proxyType := section["type"]
  67. if proxyType == "" {
  68. proxyType = consts.TcpProxy
  69. section["type"] = consts.TcpProxy
  70. }
  71. cfg = NewConfByType(proxyType)
  72. if cfg == nil {
  73. err = fmt.Errorf("proxy [%s] type [%s] error", name, proxyType)
  74. return
  75. }
  76. err = cfg.LoadFromFile(name, section)
  77. return
  78. }
  79. // BaseProxy info
  80. type BaseProxyConf struct {
  81. ProxyName string `json:"proxy_name"`
  82. ProxyType string `json:"proxy_type"`
  83. UseEncryption bool `json:"use_encryption"`
  84. UseCompression bool `json:"use_compression"`
  85. }
  86. func (cfg *BaseProxyConf) GetName() string {
  87. return cfg.ProxyName
  88. }
  89. func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
  90. return cfg
  91. }
  92. func (cfg *BaseProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  93. cfg.ProxyName = pMsg.ProxyName
  94. cfg.ProxyType = pMsg.ProxyType
  95. cfg.UseEncryption = pMsg.UseEncryption
  96. cfg.UseCompression = pMsg.UseCompression
  97. }
  98. func (cfg *BaseProxyConf) LoadFromFile(name string, section ini.Section) error {
  99. var (
  100. tmpStr string
  101. ok bool
  102. )
  103. if ClientCommonCfg.User != "" {
  104. cfg.ProxyName = ClientCommonCfg.User + "." + name
  105. } else {
  106. cfg.ProxyName = name
  107. }
  108. cfg.ProxyType = section["type"]
  109. tmpStr, ok = section["use_encryption"]
  110. if ok && tmpStr == "true" {
  111. cfg.UseEncryption = true
  112. }
  113. tmpStr, ok = section["use_compression"]
  114. if ok && tmpStr == "true" {
  115. cfg.UseCompression = true
  116. }
  117. return nil
  118. }
  119. func (cfg *BaseProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  120. pMsg.ProxyName = cfg.ProxyName
  121. pMsg.ProxyType = cfg.ProxyType
  122. pMsg.UseEncryption = cfg.UseEncryption
  123. pMsg.UseCompression = cfg.UseCompression
  124. }
  125. // Bind info
  126. type BindInfoConf struct {
  127. BindAddr string `json:"bind_addr"`
  128. RemotePort int64 `json:"remote_port"`
  129. }
  130. func (cfg *BindInfoConf) LoadFromMsg(pMsg *msg.NewProxy) {
  131. cfg.BindAddr = ServerCommonCfg.BindAddr
  132. cfg.RemotePort = pMsg.RemotePort
  133. }
  134. func (cfg *BindInfoConf) LoadFromFile(name string, section ini.Section) (err error) {
  135. var (
  136. tmpStr string
  137. ok bool
  138. )
  139. if tmpStr, ok = section["remote_port"]; ok {
  140. if cfg.RemotePort, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  141. return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
  142. }
  143. } else {
  144. return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
  145. }
  146. return nil
  147. }
  148. func (cfg *BindInfoConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  149. pMsg.RemotePort = cfg.RemotePort
  150. }
  151. func (cfg *BindInfoConf) check() (err error) {
  152. if len(ServerCommonCfg.PrivilegeAllowPorts) != 0 {
  153. if ok := util.ContainsPort(ServerCommonCfg.PrivilegeAllowPorts, cfg.RemotePort); !ok {
  154. return fmt.Errorf("remote port [%d] isn't allowed", cfg.RemotePort)
  155. }
  156. }
  157. return nil
  158. }
  159. // Domain info
  160. type DomainConf struct {
  161. CustomDomains []string `json:"custom_domains"`
  162. SubDomain string `json:"sub_domain"`
  163. }
  164. func (cfg *DomainConf) LoadFromMsg(pMsg *msg.NewProxy) {
  165. cfg.CustomDomains = pMsg.CustomDomains
  166. cfg.SubDomain = pMsg.SubDomain
  167. }
  168. func (cfg *DomainConf) LoadFromFile(name string, section ini.Section) (err error) {
  169. var (
  170. tmpStr string
  171. ok bool
  172. )
  173. if tmpStr, ok = section["custom_domains"]; ok {
  174. cfg.CustomDomains = strings.Split(tmpStr, ",")
  175. for i, domain := range cfg.CustomDomains {
  176. cfg.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  177. }
  178. }
  179. if tmpStr, ok = section["subdomain"]; ok {
  180. cfg.SubDomain = tmpStr
  181. }
  182. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  183. return fmt.Errorf("Parse conf error: proxy [%s] custom_domains and subdomain should set at least one of them", name)
  184. }
  185. return
  186. }
  187. func (cfg *DomainConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  188. pMsg.CustomDomains = cfg.CustomDomains
  189. pMsg.SubDomain = cfg.SubDomain
  190. }
  191. func (cfg *DomainConf) check() (err error) {
  192. for _, domain := range cfg.CustomDomains {
  193. if ServerCommonCfg.SubDomainHost != "" && len(strings.Split(ServerCommonCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
  194. if strings.Contains(domain, ServerCommonCfg.SubDomainHost) {
  195. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, ServerCommonCfg.SubDomainHost)
  196. }
  197. }
  198. }
  199. if cfg.SubDomain != "" {
  200. if ServerCommonCfg.SubDomainHost == "" {
  201. return fmt.Errorf("subdomain is not supported because this feature is not enabled by frps")
  202. }
  203. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  204. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  205. }
  206. }
  207. return nil
  208. }
  209. // Local service info
  210. type LocalSvrConf struct {
  211. LocalIp string `json:"-"`
  212. LocalPort int `json:"-"`
  213. }
  214. func (cfg *LocalSvrConf) LoadFromFile(name string, section ini.Section) (err error) {
  215. if cfg.LocalIp = section["local_ip"]; cfg.LocalIp == "" {
  216. cfg.LocalIp = "127.0.0.1"
  217. }
  218. if tmpStr, ok := section["local_port"]; ok {
  219. if cfg.LocalPort, err = strconv.Atoi(tmpStr); err != nil {
  220. return fmt.Errorf("Parse conf error: proxy [%s] local_port error", name)
  221. }
  222. } else {
  223. return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", name)
  224. }
  225. return nil
  226. }
  227. type PluginConf struct {
  228. Plugin string `json:"-"`
  229. PluginParams map[string]string `json:"-"`
  230. }
  231. func (cfg *PluginConf) LoadFromFile(name string, section ini.Section) (err error) {
  232. cfg.Plugin = section["plugin"]
  233. cfg.PluginParams = make(map[string]string)
  234. if cfg.Plugin != "" {
  235. // get params begin with "plugin_"
  236. for k, v := range section {
  237. if strings.HasPrefix(k, "plugin_") {
  238. cfg.PluginParams[k] = v
  239. }
  240. }
  241. } else {
  242. return fmt.Errorf("Parse conf error: proxy [%s] no plugin info found", name)
  243. }
  244. return
  245. }
  246. // TCP
  247. type TcpProxyConf struct {
  248. BaseProxyConf
  249. BindInfoConf
  250. LocalSvrConf
  251. PluginConf
  252. }
  253. func (cfg *TcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  254. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  255. cfg.BindInfoConf.LoadFromMsg(pMsg)
  256. }
  257. func (cfg *TcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  258. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  259. return
  260. }
  261. if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
  262. return
  263. }
  264. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  265. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  266. return
  267. }
  268. }
  269. return
  270. }
  271. func (cfg *TcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  272. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  273. cfg.BindInfoConf.UnMarshalToMsg(pMsg)
  274. }
  275. func (cfg *TcpProxyConf) Check() (err error) {
  276. err = cfg.BindInfoConf.check()
  277. return
  278. }
  279. // UDP
  280. type UdpProxyConf struct {
  281. BaseProxyConf
  282. BindInfoConf
  283. LocalSvrConf
  284. }
  285. func (cfg *UdpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  286. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  287. cfg.BindInfoConf.LoadFromMsg(pMsg)
  288. }
  289. func (cfg *UdpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  290. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  291. return
  292. }
  293. if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
  294. return
  295. }
  296. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  297. return
  298. }
  299. return
  300. }
  301. func (cfg *UdpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  302. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  303. cfg.BindInfoConf.UnMarshalToMsg(pMsg)
  304. }
  305. func (cfg *UdpProxyConf) Check() (err error) {
  306. err = cfg.BindInfoConf.check()
  307. return
  308. }
  309. // HTTP
  310. type HttpProxyConf struct {
  311. BaseProxyConf
  312. DomainConf
  313. LocalSvrConf
  314. PluginConf
  315. Locations []string `json:"locations"`
  316. HostHeaderRewrite string `json:"host_header_rewrite"`
  317. HttpUser string `json:"-"`
  318. HttpPwd string `json:"-"`
  319. }
  320. func (cfg *HttpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  321. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  322. cfg.DomainConf.LoadFromMsg(pMsg)
  323. cfg.Locations = pMsg.Locations
  324. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  325. cfg.HttpUser = pMsg.HttpUser
  326. cfg.HttpPwd = pMsg.HttpPwd
  327. }
  328. func (cfg *HttpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  329. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  330. return
  331. }
  332. if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
  333. return
  334. }
  335. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  336. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  337. return
  338. }
  339. }
  340. var (
  341. tmpStr string
  342. ok bool
  343. )
  344. if tmpStr, ok = section["locations"]; ok {
  345. cfg.Locations = strings.Split(tmpStr, ",")
  346. } else {
  347. cfg.Locations = []string{""}
  348. }
  349. cfg.HostHeaderRewrite = section["host_header_rewrite"]
  350. cfg.HttpUser = section["http_user"]
  351. cfg.HttpPwd = section["http_pwd"]
  352. return
  353. }
  354. func (cfg *HttpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  355. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  356. cfg.DomainConf.UnMarshalToMsg(pMsg)
  357. pMsg.Locations = cfg.Locations
  358. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  359. pMsg.HttpUser = cfg.HttpUser
  360. pMsg.HttpPwd = cfg.HttpPwd
  361. }
  362. func (cfg *HttpProxyConf) Check() (err error) {
  363. if ServerCommonCfg.VhostHttpPort == 0 {
  364. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  365. }
  366. err = cfg.DomainConf.check()
  367. return
  368. }
  369. // HTTPS
  370. type HttpsProxyConf struct {
  371. BaseProxyConf
  372. DomainConf
  373. LocalSvrConf
  374. PluginConf
  375. }
  376. func (cfg *HttpsProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  377. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  378. cfg.DomainConf.LoadFromMsg(pMsg)
  379. }
  380. func (cfg *HttpsProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  381. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  382. return
  383. }
  384. if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
  385. return
  386. }
  387. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  388. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  389. return
  390. }
  391. }
  392. return
  393. }
  394. func (cfg *HttpsProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  395. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  396. cfg.DomainConf.UnMarshalToMsg(pMsg)
  397. }
  398. func (cfg *HttpsProxyConf) Check() (err error) {
  399. if ServerCommonCfg.VhostHttpsPort == 0 {
  400. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  401. }
  402. err = cfg.DomainConf.check()
  403. return
  404. }
  405. // STCP
  406. type StcpProxyConf struct {
  407. BaseProxyConf
  408. Role string `json:"role"`
  409. Sk string `json:"sk"`
  410. // used in role server
  411. LocalSvrConf
  412. PluginConf
  413. // used in role vistor
  414. ServerName string `json:"server_name"`
  415. BindAddr string `json:"bind_addr"`
  416. BindPort int `json:"bind_port"`
  417. }
  418. // Only for role server.
  419. func (cfg *StcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  420. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  421. cfg.Sk = pMsg.Sk
  422. }
  423. func (cfg *StcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  424. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  425. return
  426. }
  427. tmpStr := section["role"]
  428. if tmpStr == "server" || tmpStr == "vistor" {
  429. cfg.Role = tmpStr
  430. } else {
  431. cfg.Role = "server"
  432. }
  433. cfg.Sk = section["sk"]
  434. if tmpStr == "vistor" {
  435. prefix := section["prefix"]
  436. cfg.ServerName = prefix + section["server_name"]
  437. if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
  438. cfg.BindAddr = "127.0.0.1"
  439. }
  440. if tmpStr, ok := section["bind_port"]; ok {
  441. if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
  442. return fmt.Errorf("Parse conf error: proxy [%s] bind_port error", name)
  443. }
  444. } else {
  445. return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
  446. }
  447. } else {
  448. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  449. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  450. return
  451. }
  452. }
  453. }
  454. return
  455. }
  456. func (cfg *StcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  457. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  458. pMsg.Sk = cfg.Sk
  459. }
  460. func (cfg *StcpProxyConf) Check() (err error) {
  461. return
  462. }
  463. // if len(startProxy) is 0, start all
  464. // otherwise just start proxies in startProxy map
  465. func LoadProxyConfFromFile(prefix string, conf ini.File, startProxy map[string]struct{}) (
  466. proxyConfs map[string]ProxyConf, vistorConfs map[string]ProxyConf, err error) {
  467. if prefix != "" {
  468. prefix += "."
  469. }
  470. startAll := true
  471. if len(startProxy) > 0 {
  472. startAll = false
  473. }
  474. proxyConfs = make(map[string]ProxyConf)
  475. vistorConfs = make(map[string]ProxyConf)
  476. for name, section := range conf {
  477. _, shouldStart := startProxy[name]
  478. if name != "common" && (startAll || shouldStart) {
  479. // some proxy or visotr configure may be used this prefix
  480. section["prefix"] = prefix
  481. cfg, err := NewProxyConfFromFile(name, section)
  482. if err != nil {
  483. return proxyConfs, vistorConfs, err
  484. }
  485. role := section["role"]
  486. if role == "vistor" {
  487. vistorConfs[prefix+name] = cfg
  488. } else {
  489. proxyConfs[prefix+name] = cfg
  490. }
  491. }
  492. }
  493. return
  494. }