https.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2019 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 proxy
  15. import (
  16. "strings"
  17. "golang.org/x/time/rate"
  18. "github.com/fatedier/frp/pkg/config"
  19. "github.com/fatedier/frp/pkg/util/util"
  20. "github.com/fatedier/frp/pkg/util/vhost"
  21. )
  22. type HTTPSProxy struct {
  23. *BaseProxy
  24. cfg *config.HTTPSProxyConf
  25. }
  26. func (pxy *HTTPSProxy) Run() (remoteAddr string, err error) {
  27. xl := pxy.xl
  28. routeConfig := &vhost.RouteConfig{}
  29. defer func() {
  30. if err != nil {
  31. pxy.Close()
  32. }
  33. }()
  34. addrs := make([]string, 0)
  35. for _, domain := range pxy.cfg.CustomDomains {
  36. if domain == "" {
  37. continue
  38. }
  39. routeConfig.Domain = domain
  40. l, errRet := pxy.rc.VhostHTTPSMuxer.Listen(pxy.ctx, routeConfig)
  41. if errRet != nil {
  42. err = errRet
  43. return
  44. }
  45. xl.Info("https proxy listen for host [%s]", routeConfig.Domain)
  46. pxy.listeners = append(pxy.listeners, l)
  47. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, pxy.serverCfg.VhostHTTPSPort))
  48. }
  49. if pxy.cfg.SubDomain != "" {
  50. routeConfig.Domain = pxy.cfg.SubDomain + "." + pxy.serverCfg.SubDomainHost
  51. l, errRet := pxy.rc.VhostHTTPSMuxer.Listen(pxy.ctx, routeConfig)
  52. if errRet != nil {
  53. err = errRet
  54. return
  55. }
  56. xl.Info("https proxy listen for host [%s]", routeConfig.Domain)
  57. pxy.listeners = append(pxy.listeners, l)
  58. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, pxy.serverCfg.VhostHTTPSPort))
  59. }
  60. pxy.startListenHandler(pxy, HandleUserTCPConnection)
  61. remoteAddr = strings.Join(addrs, ",")
  62. return
  63. }
  64. func (pxy *HTTPSProxy) GetConf() config.ProxyConf {
  65. return pxy.cfg
  66. }
  67. func (pxy *HTTPSProxy) GetLimiter() *rate.Limiter {
  68. return pxy.limiter
  69. }
  70. func (pxy *HTTPSProxy) Close() {
  71. pxy.BaseProxy.Close()
  72. }