https.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/g"
  18. "github.com/fatedier/frp/models/config"
  19. "github.com/fatedier/frp/utils/util"
  20. "github.com/fatedier/frp/utils/vhost"
  21. )
  22. type HttpsProxy struct {
  23. *BaseProxy
  24. cfg *config.HttpsProxyConf
  25. }
  26. func (pxy *HttpsProxy) Run() (remoteAddr string, err error) {
  27. routeConfig := &vhost.VhostRouteConfig{}
  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(routeConfig)
  40. if errRet != nil {
  41. err = errRet
  42. return
  43. }
  44. l.AddLogPrefix(pxy.name)
  45. pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
  46. pxy.listeners = append(pxy.listeners, l)
  47. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, g.GlbServerCfg.VhostHttpsPort))
  48. }
  49. if pxy.cfg.SubDomain != "" {
  50. routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
  51. l, errRet := pxy.rc.VhostHttpsMuxer.Listen(routeConfig)
  52. if errRet != nil {
  53. err = errRet
  54. return
  55. }
  56. l.AddLogPrefix(pxy.name)
  57. pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
  58. pxy.listeners = append(pxy.listeners, l)
  59. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(g.GlbServerCfg.VhostHttpsPort)))
  60. }
  61. pxy.startListenHandler(pxy, HandleUserTcpConnection)
  62. remoteAddr = strings.Join(addrs, ",")
  63. return
  64. }
  65. func (pxy *HttpsProxy) GetConf() config.ProxyConf {
  66. return pxy.cfg
  67. }
  68. func (pxy *HttpsProxy) Close() {
  69. pxy.BaseProxy.Close()
  70. }