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