https.go 2.1 KB

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