https.go 2.3 KB

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