forked from Mxmilu666/frp
support the configure of url in "http" section
This commit is contained in:
@@ -45,6 +45,8 @@ func GetHttpRequestInfo(c *conn.Conn) (_ net.Conn, _ map[string]string, err erro
|
||||
// hostName
|
||||
tmpArr := strings.Split(request.Host, ":")
|
||||
reqInfoMap["Host"] = tmpArr[0]
|
||||
reqInfoMap["Path"] = request.URL.Path
|
||||
reqInfoMap["Scheme"] = request.URL.Scheme
|
||||
|
||||
// Authorization
|
||||
authStr := request.Header.Get("Authorization")
|
||||
|
||||
@@ -186,5 +186,6 @@ func GetHttpsHostname(c *conn.Conn) (sc net.Conn, _ map[string]string, err error
|
||||
return sc, reqInfoMap, err
|
||||
}
|
||||
reqInfoMap["Host"] = host
|
||||
reqInfoMap["Scheme"] = "https"
|
||||
return sc, reqInfoMap, nil
|
||||
}
|
||||
|
||||
107
src/utils/vhost/router.go
Normal file
107
src/utils/vhost/router.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package vhost
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type VhostRouters struct {
|
||||
RouterByDomain map[string][]*VhostRouter
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
type VhostRouter struct {
|
||||
name string
|
||||
domain string
|
||||
location string
|
||||
listener *Listener
|
||||
}
|
||||
|
||||
func NewVhostRouters() *VhostRouters {
|
||||
return &VhostRouters{
|
||||
RouterByDomain: make(map[string][]*VhostRouter),
|
||||
}
|
||||
}
|
||||
|
||||
//sort by location
|
||||
type ByLocation []*VhostRouter
|
||||
|
||||
func (a ByLocation) Len() int {
|
||||
return len(a)
|
||||
}
|
||||
func (a ByLocation) Swap(i, j int) {
|
||||
a[i], a[j] = a[j], a[i]
|
||||
}
|
||||
func (a ByLocation) Less(i, j int) bool {
|
||||
return strings.Compare(a[i].location, a[j].location) < 0
|
||||
}
|
||||
|
||||
func (r *VhostRouters) add(name, domain string, locations []string, l *Listener) {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
vrs, found := r.RouterByDomain[domain]
|
||||
if !found {
|
||||
vrs = make([]*VhostRouter, 0)
|
||||
}
|
||||
|
||||
for _, loc := range locations {
|
||||
vr := &VhostRouter{
|
||||
name: name,
|
||||
domain: domain,
|
||||
location: loc,
|
||||
listener: l,
|
||||
}
|
||||
vrs = append(vrs, vr)
|
||||
}
|
||||
|
||||
sort.Reverse(ByLocation(vrs))
|
||||
r.RouterByDomain[domain] = vrs
|
||||
}
|
||||
|
||||
func (r *VhostRouters) del(l *Listener) {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
vrs, found := r.RouterByDomain[l.domain]
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
|
||||
for i, vr := range vrs {
|
||||
if vr.listener == l {
|
||||
if len(vrs) > i+1 {
|
||||
r.RouterByDomain[l.domain] = append(vrs[:i], vrs[i+1:]...)
|
||||
} else {
|
||||
r.RouterByDomain[l.domain] = vrs[:i]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *VhostRouters) get(rname string) (vr *VhostRouter, exist bool) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
var domain, url string
|
||||
tmparray := strings.SplitN(rname, ":", 2)
|
||||
if len(tmparray) == 2 {
|
||||
domain = tmparray[0]
|
||||
url = tmparray[1]
|
||||
}
|
||||
|
||||
vrs, exist := r.RouterByDomain[domain]
|
||||
if !exist {
|
||||
return
|
||||
}
|
||||
|
||||
//can't support load balance,will to do
|
||||
for _, vr = range vrs {
|
||||
if strings.HasPrefix(url, vr.location) {
|
||||
return vr, true
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -21,7 +21,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fatedier/frp/src/models/config"
|
||||
"github.com/fatedier/frp/src/utils/conn"
|
||||
"github.com/fatedier/frp/src/utils/log"
|
||||
)
|
||||
|
||||
type muxFunc func(*conn.Conn) (net.Conn, map[string]string, error)
|
||||
@@ -29,71 +31,77 @@ type httpAuthFunc func(*conn.Conn, string, string, string) (bool, error)
|
||||
type hostRewriteFunc func(*conn.Conn, string) (net.Conn, error)
|
||||
|
||||
type VhostMuxer struct {
|
||||
listener *conn.Listener
|
||||
timeout time.Duration
|
||||
vhostFunc muxFunc
|
||||
authFunc httpAuthFunc
|
||||
rewriteFunc hostRewriteFunc
|
||||
registryMap map[string]*Listener
|
||||
mutex sync.RWMutex
|
||||
listener *conn.Listener
|
||||
timeout time.Duration
|
||||
vhostFunc muxFunc
|
||||
authFunc httpAuthFunc
|
||||
rewriteFunc hostRewriteFunc
|
||||
registryRouter *VhostRouters
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewVhostMuxer(listener *conn.Listener, vhostFunc muxFunc, authFunc httpAuthFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
|
||||
mux = &VhostMuxer{
|
||||
listener: listener,
|
||||
timeout: timeout,
|
||||
vhostFunc: vhostFunc,
|
||||
authFunc: authFunc,
|
||||
rewriteFunc: rewriteFunc,
|
||||
registryMap: make(map[string]*Listener),
|
||||
listener: listener,
|
||||
timeout: timeout,
|
||||
vhostFunc: vhostFunc,
|
||||
authFunc: authFunc,
|
||||
rewriteFunc: rewriteFunc,
|
||||
registryRouter: NewVhostRouters(),
|
||||
}
|
||||
go mux.run()
|
||||
return mux, nil
|
||||
}
|
||||
|
||||
// listen for a new domain name, if rewriteHost is not empty and rewriteFunc is not nil, then rewrite the host header to rewriteHost
|
||||
func (v *VhostMuxer) Listen(name string, rewriteHost, userName, passWord string) (l *Listener, err error) {
|
||||
func (v *VhostMuxer) Listen(p *config.ProxyServerConf) (ls []*Listener) {
|
||||
v.mutex.Lock()
|
||||
defer v.mutex.Unlock()
|
||||
if _, exist := v.registryMap[name]; exist {
|
||||
return nil, fmt.Errorf("domain name %s is already bound", name)
|
||||
}
|
||||
|
||||
l = &Listener{
|
||||
name: name,
|
||||
rewriteHost: rewriteHost,
|
||||
userName: userName,
|
||||
passWord: passWord,
|
||||
mux: v,
|
||||
accept: make(chan *conn.Conn),
|
||||
ls = make([]*Listener, 0)
|
||||
for _, domain := range p.CustomDomains {
|
||||
l := &Listener{
|
||||
name: p.Name,
|
||||
domain: domain,
|
||||
locations: p.Locations,
|
||||
rewriteHost: p.HostHeaderRewrite,
|
||||
userName: p.HttpUserName,
|
||||
passWord: p.HttpPassWord,
|
||||
mux: v,
|
||||
accept: make(chan *conn.Conn),
|
||||
}
|
||||
v.registryRouter.add(p.Name, domain, p.Locations, l)
|
||||
ls = append(ls, l)
|
||||
}
|
||||
v.registryMap[name] = l
|
||||
return l, nil
|
||||
return ls
|
||||
}
|
||||
|
||||
func (v *VhostMuxer) getListener(name string) (l *Listener, exist bool) {
|
||||
v.mutex.RLock()
|
||||
defer v.mutex.RUnlock()
|
||||
// first we check the full hostname
|
||||
// if not exist, then check the wildcard_domain such as *.example.com
|
||||
l, exist = v.registryMap[name]
|
||||
if exist {
|
||||
return l, exist
|
||||
|
||||
// // first we check the full hostname
|
||||
vr, found := v.registryRouter.get(name)
|
||||
if found {
|
||||
return vr.listener, true
|
||||
}
|
||||
|
||||
//if not exist, then check the wildcard_domain such as *.example.com
|
||||
domainSplit := strings.Split(name, ".")
|
||||
if len(domainSplit) < 3 {
|
||||
log.Warn("can't found the router for %s", name)
|
||||
return l, false
|
||||
}
|
||||
domainSplit[0] = "*"
|
||||
name = strings.Join(domainSplit, ".")
|
||||
l, exist = v.registryMap[name]
|
||||
return l, exist
|
||||
}
|
||||
|
||||
func (v *VhostMuxer) unRegister(name string) {
|
||||
v.mutex.Lock()
|
||||
defer v.mutex.Unlock()
|
||||
delete(v.registryMap, name)
|
||||
vr, found = v.registryRouter.get(name)
|
||||
if !found {
|
||||
log.Warn("can't found the router for %s", name)
|
||||
return
|
||||
}
|
||||
|
||||
return vr.listener, true
|
||||
}
|
||||
|
||||
func (v *VhostMuxer) run() {
|
||||
@@ -120,6 +128,9 @@ func (v *VhostMuxer) handle(c *conn.Conn) {
|
||||
|
||||
name := strings.ToLower(reqInfoMap["Host"])
|
||||
// get listener by hostname
|
||||
if reqInfoMap["Scheme"] == "http" {
|
||||
name = name + ":" + reqInfoMap["Path"]
|
||||
}
|
||||
l, ok := v.getListener(name)
|
||||
if !ok {
|
||||
c.Close()
|
||||
@@ -150,6 +161,8 @@ func (v *VhostMuxer) handle(c *conn.Conn) {
|
||||
|
||||
type Listener struct {
|
||||
name string
|
||||
domain string
|
||||
locations []string
|
||||
rewriteHost string
|
||||
userName string
|
||||
passWord string
|
||||
@@ -177,7 +190,7 @@ func (l *Listener) Accept() (*conn.Conn, error) {
|
||||
}
|
||||
|
||||
func (l *Listener) Close() error {
|
||||
l.mux.unRegister(l.name)
|
||||
l.mux.registryRouter.del(l)
|
||||
close(l.accept)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user