proxy.go 11 KB

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