proxy.go 12 KB

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