1
0

https.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "net"
  17. "reflect"
  18. "strings"
  19. v1 "github.com/fatedier/frp/pkg/config/v1"
  20. "github.com/fatedier/frp/pkg/util/util"
  21. "github.com/fatedier/frp/pkg/util/vhost"
  22. )
  23. func init() {
  24. RegisterProxyFactory(reflect.TypeOf(&v1.HTTPSProxyConfig{}), NewHTTPSProxy)
  25. }
  26. type HTTPSProxy struct {
  27. *BaseProxy
  28. cfg *v1.HTTPSProxyConfig
  29. }
  30. func NewHTTPSProxy(baseProxy *BaseProxy) Proxy {
  31. unwrapped, ok := baseProxy.GetConfigurer().(*v1.HTTPSProxyConfig)
  32. if !ok {
  33. return nil
  34. }
  35. return &HTTPSProxy{
  36. BaseProxy: baseProxy,
  37. cfg: unwrapped,
  38. }
  39. }
  40. func (pxy *HTTPSProxy) Run() (remoteAddr string, err error) {
  41. xl := pxy.xl
  42. routeConfig := &vhost.RouteConfig{}
  43. defer func() {
  44. if err != nil {
  45. pxy.Close()
  46. }
  47. }()
  48. addrs := make([]string, 0)
  49. for _, domain := range pxy.cfg.CustomDomains {
  50. if domain == "" {
  51. continue
  52. }
  53. l, err := pxy.listenForDomain(routeConfig, domain)
  54. if err != nil {
  55. return "", err
  56. }
  57. pxy.listeners = append(pxy.listeners, l)
  58. addrs = append(addrs, util.CanonicalAddr(domain, pxy.serverCfg.VhostHTTPSPort))
  59. xl.Infof("https proxy listen for host [%s] group [%s]", domain, pxy.cfg.LoadBalancer.Group)
  60. }
  61. if pxy.cfg.SubDomain != "" {
  62. domain := pxy.cfg.SubDomain + "." + pxy.serverCfg.SubDomainHost
  63. l, err := pxy.listenForDomain(routeConfig, domain)
  64. if err != nil {
  65. return "", err
  66. }
  67. pxy.listeners = append(pxy.listeners, l)
  68. addrs = append(addrs, util.CanonicalAddr(domain, pxy.serverCfg.VhostHTTPSPort))
  69. xl.Infof("https proxy listen for host [%s] group [%s]", domain, pxy.cfg.LoadBalancer.Group)
  70. }
  71. pxy.startCommonTCPListenersHandler()
  72. remoteAddr = strings.Join(addrs, ",")
  73. return
  74. }
  75. func (pxy *HTTPSProxy) Close() {
  76. pxy.BaseProxy.Close()
  77. }
  78. func (pxy *HTTPSProxy) listenForDomain(routeConfig *vhost.RouteConfig, domain string) (net.Listener, error) {
  79. tmpRouteConfig := *routeConfig
  80. tmpRouteConfig.Domain = domain
  81. if pxy.cfg.LoadBalancer.Group != "" {
  82. return pxy.rc.HTTPSGroupCtl.Listen(
  83. pxy.ctx,
  84. pxy.cfg.LoadBalancer.Group,
  85. pxy.cfg.LoadBalancer.GroupKey,
  86. tmpRouteConfig,
  87. )
  88. }
  89. return pxy.rc.VhostHTTPSMuxer.Listen(pxy.ctx, &tmpRouteConfig)
  90. }