http.go 5.0 KB
Newer Older
F
fatedier 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright 2019 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package proxy

import (
	"io"
F
fatedier 已提交
19
	"net"
F
fatedier 已提交
20 21
	"strings"

F
fatedier 已提交
22 23 24 25
	"github.com/fatedier/frp/pkg/config"
	frpNet "github.com/fatedier/frp/pkg/util/net"
	"github.com/fatedier/frp/pkg/util/util"
	"github.com/fatedier/frp/pkg/util/vhost"
26
	"github.com/fatedier/frp/server/metrics"
F
fatedier 已提交
27 28 29 30

	frpIo "github.com/fatedier/golib/io"
)

F
fatedier 已提交
31
type HTTPProxy struct {
F
go vet  
fatedier 已提交
32
	*BaseProxy
F
fatedier 已提交
33
	cfg *config.HTTPProxyConf
F
fatedier 已提交
34 35 36 37

	closeFuncs []func()
}

F
fatedier 已提交
38
func (pxy *HTTPProxy) Run() (remoteAddr string, err error) {
F
fatedier 已提交
39
	xl := pxy.xl
F
fatedier 已提交
40
	routeConfig := vhost.RouteConfig{
F
fatedier 已提交
41 42
		RewriteHost:  pxy.cfg.HostHeaderRewrite,
		Headers:      pxy.cfg.Headers,
F
fatedier 已提交
43 44
		Username:     pxy.cfg.HTTPUser,
		Password:     pxy.cfg.HTTPPwd,
F
fatedier 已提交
45 46 47 48 49 50 51 52
		CreateConnFn: pxy.GetRealConn,
	}

	locations := pxy.cfg.Locations
	if len(locations) == 0 {
		locations = []string{""}
	}

F
fatedier 已提交
53 54 55 56 57 58
	defer func() {
		if err != nil {
			pxy.Close()
		}
	}()

F
fatedier 已提交
59 60
	addrs := make([]string, 0)
	for _, domain := range pxy.cfg.CustomDomains {
F
fatedier 已提交
61 62 63 64
		if domain == "" {
			continue
		}

F
fatedier 已提交
65 66 67 68 69
		routeConfig.Domain = domain
		for _, location := range locations {
			routeConfig.Location = location
			tmpDomain := routeConfig.Domain
			tmpLocation := routeConfig.Location
F
fatedier 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82

			// handle group
			if pxy.cfg.Group != "" {
				err = pxy.rc.HTTPGroupCtl.Register(pxy.name, pxy.cfg.Group, pxy.cfg.GroupKey, routeConfig)
				if err != nil {
					return
				}

				pxy.closeFuncs = append(pxy.closeFuncs, func() {
					pxy.rc.HTTPGroupCtl.UnRegister(pxy.name, pxy.cfg.Group, tmpDomain, tmpLocation)
				})
			} else {
				// no group
F
fatedier 已提交
83
				err = pxy.rc.HTTPReverseProxy.Register(routeConfig)
F
fatedier 已提交
84 85 86 87
				if err != nil {
					return
				}
				pxy.closeFuncs = append(pxy.closeFuncs, func() {
F
fatedier 已提交
88
					pxy.rc.HTTPReverseProxy.UnRegister(tmpDomain, tmpLocation)
F
fatedier 已提交
89 90
				})
			}
F
fatedier 已提交
91
			addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(pxy.serverCfg.VhostHTTPPort)))
F
fatedier 已提交
92
			xl.Info("http proxy listen for host [%s] location [%s] group [%s]", routeConfig.Domain, routeConfig.Location, pxy.cfg.Group)
F
fatedier 已提交
93 94 95 96
		}
	}

	if pxy.cfg.SubDomain != "" {
97
		routeConfig.Domain = pxy.cfg.SubDomain + "." + pxy.serverCfg.SubDomainHost
F
fatedier 已提交
98 99 100 101
		for _, location := range locations {
			routeConfig.Location = location
			tmpDomain := routeConfig.Domain
			tmpLocation := routeConfig.Location
F
fatedier 已提交
102 103 104 105 106 107 108 109 110 111 112 113

			// handle group
			if pxy.cfg.Group != "" {
				err = pxy.rc.HTTPGroupCtl.Register(pxy.name, pxy.cfg.Group, pxy.cfg.GroupKey, routeConfig)
				if err != nil {
					return
				}

				pxy.closeFuncs = append(pxy.closeFuncs, func() {
					pxy.rc.HTTPGroupCtl.UnRegister(pxy.name, pxy.cfg.Group, tmpDomain, tmpLocation)
				})
			} else {
F
fatedier 已提交
114
				err = pxy.rc.HTTPReverseProxy.Register(routeConfig)
F
fatedier 已提交
115 116 117 118
				if err != nil {
					return
				}
				pxy.closeFuncs = append(pxy.closeFuncs, func() {
F
fatedier 已提交
119
					pxy.rc.HTTPReverseProxy.UnRegister(tmpDomain, tmpLocation)
F
fatedier 已提交
120 121
				})
			}
F
fatedier 已提交
122
			addrs = append(addrs, util.CanonicalAddr(tmpDomain, pxy.serverCfg.VhostHTTPPort))
F
fatedier 已提交
123

F
fatedier 已提交
124
			xl.Info("http proxy listen for host [%s] location [%s] group [%s]", routeConfig.Domain, routeConfig.Location, pxy.cfg.Group)
F
fatedier 已提交
125 126 127 128 129 130
		}
	}
	remoteAddr = strings.Join(addrs, ",")
	return
}

F
fatedier 已提交
131
func (pxy *HTTPProxy) GetConf() config.ProxyConf {
F
fatedier 已提交
132 133 134
	return pxy.cfg
}

F
fatedier 已提交
135
func (pxy *HTTPProxy) GetRealConn(remoteAddr string) (workConn net.Conn, err error) {
F
fatedier 已提交
136
	xl := pxy.xl
F
fatedier 已提交
137 138
	rAddr, errRet := net.ResolveTCPAddr("tcp", remoteAddr)
	if errRet != nil {
F
fatedier 已提交
139
		xl.Warn("resolve TCP addr [%s] error: %v", remoteAddr, errRet)
F
fatedier 已提交
140 141 142 143
		// we do not return error here since remoteAddr is not necessary for proxies without proxy protocol enabled
	}

	tmpConn, errRet := pxy.GetWorkConnFromPool(rAddr, nil)
F
fatedier 已提交
144 145 146 147 148 149 150
	if errRet != nil {
		err = errRet
		return
	}

	var rwc io.ReadWriteCloser = tmpConn
	if pxy.cfg.UseEncryption {
151
		rwc, err = frpIo.WithEncryption(rwc, []byte(pxy.serverCfg.Token))
F
fatedier 已提交
152
		if err != nil {
F
fatedier 已提交
153
			xl.Error("create encryption stream error: %v", err)
F
fatedier 已提交
154 155 156 157 158 159 160 161
			return
		}
	}
	if pxy.cfg.UseCompression {
		rwc = frpIo.WithCompression(rwc)
	}
	workConn = frpNet.WrapReadWriteCloserToConn(rwc, tmpConn)
	workConn = frpNet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
162
	metrics.Server.OpenConnection(pxy.GetName(), pxy.GetConf().GetBaseInfo().ProxyType)
F
fatedier 已提交
163 164 165
	return
}

F
fatedier 已提交
166
func (pxy *HTTPProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
F
fatedier 已提交
167
	name := pxy.GetName()
168 169 170 171
	proxyType := pxy.GetConf().GetBaseInfo().ProxyType
	metrics.Server.CloseConnection(name, proxyType)
	metrics.Server.AddTrafficIn(name, proxyType, totalWrite)
	metrics.Server.AddTrafficOut(name, proxyType, totalRead)
F
fatedier 已提交
172 173
}

F
fatedier 已提交
174
func (pxy *HTTPProxy) Close() {
F
fatedier 已提交
175 176 177 178 179
	pxy.BaseProxy.Close()
	for _, closeFn := range pxy.closeFuncs {
		closeFn()
	}
}