proxy.go 11 KB

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