proxy.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. // TODO: once linstenPort used, should remove the port from privilege ports
  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. 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. // TCP
  227. type TcpProxyConf struct {
  228. BaseProxyConf
  229. BindInfoConf
  230. LocalSvrConf
  231. }
  232. func (cfg *TcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  233. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  234. cfg.BindInfoConf.LoadFromMsg(pMsg)
  235. }
  236. func (cfg *TcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  237. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  238. return
  239. }
  240. if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
  241. return
  242. }
  243. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  244. return
  245. }
  246. return
  247. }
  248. func (cfg *TcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  249. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  250. cfg.BindInfoConf.UnMarshalToMsg(pMsg)
  251. }
  252. func (cfg *TcpProxyConf) Check() (err error) {
  253. err = cfg.BindInfoConf.check()
  254. return
  255. }
  256. // UDP
  257. type UdpProxyConf struct {
  258. BaseProxyConf
  259. BindInfoConf
  260. LocalSvrConf
  261. }
  262. func (cfg *UdpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  263. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  264. cfg.BindInfoConf.LoadFromMsg(pMsg)
  265. }
  266. func (cfg *UdpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  267. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  268. return
  269. }
  270. if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
  271. return
  272. }
  273. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  274. return
  275. }
  276. return
  277. }
  278. func (cfg *UdpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  279. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  280. cfg.BindInfoConf.UnMarshalToMsg(pMsg)
  281. }
  282. func (cfg *UdpProxyConf) Check() (err error) {
  283. err = cfg.BindInfoConf.check()
  284. return
  285. }
  286. // HTTP
  287. type HttpProxyConf struct {
  288. BaseProxyConf
  289. DomainConf
  290. LocalSvrConf
  291. Locations []string `json:"locations"`
  292. HostHeaderRewrite string `json:"host_header_rewrite"`
  293. HttpUser string `json:"-"`
  294. HttpPwd string `json:"-"`
  295. }
  296. func (cfg *HttpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  297. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  298. cfg.DomainConf.LoadFromMsg(pMsg)
  299. cfg.Locations = pMsg.Locations
  300. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  301. cfg.HttpUser = pMsg.HttpUser
  302. cfg.HttpPwd = pMsg.HttpPwd
  303. }
  304. func (cfg *HttpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  305. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  306. return
  307. }
  308. if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
  309. return
  310. }
  311. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  312. return
  313. }
  314. var (
  315. tmpStr string
  316. ok bool
  317. )
  318. if tmpStr, ok = section["locations"]; ok {
  319. cfg.Locations = strings.Split(tmpStr, ",")
  320. } else {
  321. cfg.Locations = []string{""}
  322. }
  323. cfg.HostHeaderRewrite = section["host_header_rewrite"]
  324. cfg.HttpUser = section["http_user"]
  325. cfg.HttpPwd = section["http_pwd"]
  326. return
  327. }
  328. func (cfg *HttpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  329. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  330. cfg.DomainConf.UnMarshalToMsg(pMsg)
  331. pMsg.Locations = cfg.Locations
  332. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  333. pMsg.HttpUser = cfg.HttpUser
  334. pMsg.HttpPwd = cfg.HttpPwd
  335. }
  336. func (cfg *HttpProxyConf) Check() (err error) {
  337. if ServerCommonCfg.VhostHttpPort == 0 {
  338. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  339. }
  340. err = cfg.DomainConf.check()
  341. return
  342. }
  343. // HTTPS
  344. type HttpsProxyConf struct {
  345. BaseProxyConf
  346. DomainConf
  347. LocalSvrConf
  348. }
  349. func (cfg *HttpsProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  350. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  351. cfg.DomainConf.LoadFromMsg(pMsg)
  352. }
  353. func (cfg *HttpsProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  354. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  355. return
  356. }
  357. if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
  358. return
  359. }
  360. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  361. return
  362. }
  363. return
  364. }
  365. func (cfg *HttpsProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  366. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  367. cfg.DomainConf.UnMarshalToMsg(pMsg)
  368. }
  369. func (cfg *HttpsProxyConf) Check() (err error) {
  370. if ServerCommonCfg.VhostHttpsPort == 0 {
  371. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  372. }
  373. err = cfg.DomainConf.check()
  374. return
  375. }
  376. func LoadProxyConfFromFile(conf ini.File) (proxyConfs map[string]ProxyConf, err error) {
  377. var prefix string
  378. if ClientCommonCfg.User != "" {
  379. prefix = ClientCommonCfg.User + "."
  380. }
  381. proxyConfs = make(map[string]ProxyConf)
  382. for name, section := range conf {
  383. if name != "common" {
  384. cfg, err := NewProxyConfFromFile(name, section)
  385. if err != nil {
  386. return proxyConfs, err
  387. }
  388. proxyConfs[prefix+name] = cfg
  389. }
  390. }
  391. return
  392. }