proxy.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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
  80. ProxyType string
  81. UseEncryption bool
  82. UseCompression bool
  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. cfg.ProxyName = ClientCommonCfg.User + "." + name
  102. cfg.ProxyType = section["type"]
  103. tmpStr, ok = section["use_encryption"]
  104. if ok && tmpStr == "true" {
  105. cfg.UseEncryption = true
  106. }
  107. tmpStr, ok = section["use_compression"]
  108. if ok && tmpStr == "true" {
  109. cfg.UseCompression = true
  110. }
  111. return nil
  112. }
  113. func (cfg *BaseProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  114. pMsg.ProxyName = cfg.ProxyName
  115. pMsg.ProxyType = cfg.ProxyType
  116. pMsg.UseEncryption = cfg.UseEncryption
  117. pMsg.UseCompression = cfg.UseCompression
  118. }
  119. // Bind info
  120. type BindInfoConf struct {
  121. BindAddr string
  122. RemotePort int64
  123. }
  124. func (cfg *BindInfoConf) LoadFromMsg(pMsg *msg.NewProxy) {
  125. cfg.BindAddr = ServerCommonCfg.BindAddr
  126. cfg.RemotePort = pMsg.RemotePort
  127. }
  128. func (cfg *BindInfoConf) LoadFromFile(name string, section ini.Section) (err error) {
  129. var (
  130. tmpStr string
  131. ok bool
  132. )
  133. if tmpStr, ok = section["remote_port"]; ok {
  134. if cfg.RemotePort, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  135. return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
  136. }
  137. } else {
  138. return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
  139. }
  140. return nil
  141. }
  142. func (cfg *BindInfoConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  143. pMsg.RemotePort = cfg.RemotePort
  144. }
  145. func (cfg *BindInfoConf) check() (err error) {
  146. if len(ServerCommonCfg.PrivilegeAllowPorts) != 0 {
  147. if _, ok := ServerCommonCfg.PrivilegeAllowPorts[cfg.RemotePort]; !ok {
  148. return fmt.Errorf("remote port [%d] isn't allowed", cfg.RemotePort)
  149. }
  150. }
  151. return nil
  152. }
  153. // Domain info
  154. type DomainConf struct {
  155. CustomDomains []string
  156. SubDomain string
  157. }
  158. func (cfg *DomainConf) LoadFromMsg(pMsg *msg.NewProxy) {
  159. cfg.CustomDomains = pMsg.CustomDomains
  160. cfg.SubDomain = pMsg.SubDomain
  161. }
  162. func (cfg *DomainConf) LoadFromFile(name string, section ini.Section) (err error) {
  163. var (
  164. tmpStr string
  165. ok bool
  166. )
  167. if tmpStr, ok = section["custom_domains"]; ok {
  168. cfg.CustomDomains = strings.Split(tmpStr, ",")
  169. for i, domain := range cfg.CustomDomains {
  170. cfg.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  171. }
  172. }
  173. if tmpStr, ok = section["subdomain"]; ok {
  174. cfg.SubDomain = tmpStr
  175. }
  176. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  177. return fmt.Errorf("Parse conf error: proxy [%s] custom_domains and subdomain should set at least one of them", name)
  178. }
  179. return
  180. }
  181. func (cfg *DomainConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  182. pMsg.CustomDomains = cfg.CustomDomains
  183. pMsg.SubDomain = cfg.SubDomain
  184. }
  185. func (cfg *DomainConf) check() (err error) {
  186. for _, domain := range cfg.CustomDomains {
  187. if ServerCommonCfg.SubDomainHost != "" && len(strings.Split(ServerCommonCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
  188. if strings.Contains(domain, ServerCommonCfg.SubDomainHost) {
  189. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, ServerCommonCfg.SubDomainHost)
  190. }
  191. }
  192. }
  193. if cfg.SubDomain != "" {
  194. if ServerCommonCfg.SubDomainHost == "" {
  195. return fmt.Errorf("subdomain is not supported because this feature is not enabled by frps")
  196. }
  197. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  198. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  199. }
  200. }
  201. return nil
  202. }
  203. type LocalSvrConf struct {
  204. LocalIp string
  205. LocalPort int
  206. }
  207. func (cfg *LocalSvrConf) LoadFromFile(name string, section ini.Section) (err error) {
  208. if cfg.LocalIp = section["local_ip"]; cfg.LocalIp == "" {
  209. cfg.LocalIp = "127.0.0.1"
  210. }
  211. if tmpStr, ok := section["local_port"]; ok {
  212. if cfg.LocalPort, err = strconv.Atoi(tmpStr); err != nil {
  213. return fmt.Errorf("Parse conf error: proxy [%s] local_port error", name)
  214. }
  215. } else {
  216. return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", name)
  217. }
  218. return nil
  219. }
  220. // TCP
  221. type TcpProxyConf struct {
  222. BaseProxyConf
  223. BindInfoConf
  224. LocalSvrConf
  225. }
  226. func (cfg *TcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  227. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  228. cfg.BindInfoConf.LoadFromMsg(pMsg)
  229. }
  230. func (cfg *TcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  231. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  232. return
  233. }
  234. if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
  235. return
  236. }
  237. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  238. return
  239. }
  240. return
  241. }
  242. func (cfg *TcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  243. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  244. cfg.BindInfoConf.UnMarshalToMsg(pMsg)
  245. }
  246. func (cfg *TcpProxyConf) Check() (err error) {
  247. err = cfg.BindInfoConf.check()
  248. return
  249. }
  250. // UDP
  251. type UdpProxyConf struct {
  252. BaseProxyConf
  253. BindInfoConf
  254. LocalSvrConf
  255. }
  256. func (cfg *UdpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  257. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  258. cfg.BindInfoConf.LoadFromMsg(pMsg)
  259. }
  260. func (cfg *UdpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  261. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  262. return
  263. }
  264. if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
  265. return
  266. }
  267. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  268. return
  269. }
  270. return
  271. }
  272. func (cfg *UdpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  273. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  274. cfg.BindInfoConf.UnMarshalToMsg(pMsg)
  275. }
  276. func (cfg *UdpProxyConf) Check() (err error) {
  277. err = cfg.BindInfoConf.check()
  278. return
  279. }
  280. // HTTP
  281. type HttpProxyConf struct {
  282. BaseProxyConf
  283. DomainConf
  284. LocalSvrConf
  285. Locations []string
  286. HostHeaderRewrite string
  287. HttpUser string
  288. HttpPwd string
  289. }
  290. func (cfg *HttpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  291. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  292. cfg.DomainConf.LoadFromMsg(pMsg)
  293. cfg.Locations = pMsg.Locations
  294. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  295. cfg.HttpUser = pMsg.HttpUser
  296. cfg.HttpPwd = pMsg.HttpPwd
  297. }
  298. func (cfg *HttpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  299. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  300. return
  301. }
  302. if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
  303. return
  304. }
  305. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  306. return
  307. }
  308. var (
  309. tmpStr string
  310. ok bool
  311. )
  312. if tmpStr, ok = section["locations"]; ok {
  313. cfg.Locations = strings.Split(tmpStr, ",")
  314. } else {
  315. cfg.Locations = []string{""}
  316. }
  317. cfg.HostHeaderRewrite = section["host_header_rewrite"]
  318. cfg.HttpUser = section["http_user"]
  319. cfg.HttpPwd = section["http_pwd"]
  320. return
  321. }
  322. func (cfg *HttpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  323. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  324. cfg.DomainConf.UnMarshalToMsg(pMsg)
  325. pMsg.Locations = cfg.Locations
  326. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  327. pMsg.HttpUser = cfg.HttpUser
  328. pMsg.HttpPwd = cfg.HttpPwd
  329. }
  330. func (cfg *HttpProxyConf) Check() (err error) {
  331. if ServerCommonCfg.VhostHttpPort == 0 {
  332. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  333. }
  334. err = cfg.DomainConf.check()
  335. return
  336. }
  337. // HTTPS
  338. type HttpsProxyConf struct {
  339. BaseProxyConf
  340. DomainConf
  341. LocalSvrConf
  342. }
  343. func (cfg *HttpsProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  344. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  345. cfg.DomainConf.LoadFromMsg(pMsg)
  346. }
  347. func (cfg *HttpsProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  348. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  349. return
  350. }
  351. if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
  352. return
  353. }
  354. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  355. return
  356. }
  357. return
  358. }
  359. func (cfg *HttpsProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  360. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  361. cfg.DomainConf.UnMarshalToMsg(pMsg)
  362. }
  363. func (cfg *HttpsProxyConf) Check() (err error) {
  364. if ServerCommonCfg.VhostHttpsPort == 0 {
  365. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  366. }
  367. err = cfg.DomainConf.check()
  368. return
  369. }
  370. func LoadProxyConfFromFile(conf ini.File) (proxyConfs map[string]ProxyConf, err error) {
  371. var prefix string
  372. if ClientCommonCfg.User != "" {
  373. prefix = ClientCommonCfg.User + "."
  374. }
  375. proxyConfs = make(map[string]ProxyConf)
  376. for name, section := range conf {
  377. if name != "common" {
  378. cfg, err := NewProxyConfFromFile(name, section)
  379. if err != nil {
  380. return proxyConfs, err
  381. }
  382. proxyConfs[prefix+name] = cfg
  383. }
  384. }
  385. return
  386. }