Merge 228d1af8d4
into b8a28e945c
This commit is contained in:
commit
095cba8540
@ -63,7 +63,8 @@
|
|||||||
listen_port: "<<< .ListenPort >>>",
|
listen_port: "<<< .ListenPort >>>",
|
||||||
current_conns: <<< .CurrentConns >>> ,
|
current_conns: <<< .CurrentConns >>> ,
|
||||||
domains: [ <<< range.CustomDomains >>> "<<< . >>>", <<< end >>> ],
|
domains: [ <<< range.CustomDomains >>> "<<< . >>>", <<< end >>> ],
|
||||||
stat: "<<< .Status >>>",
|
locations: [ <<< range.Locations >>> "<<< . >>>", <<< end >>> ],
|
||||||
|
stat: "<<< .StatusDesc >>>",
|
||||||
use_encryption: "<<< .UseEncryption >>>",
|
use_encryption: "<<< .UseEncryption >>>",
|
||||||
use_gzip: "<<< .UseGzip >>>",
|
use_gzip: "<<< .UseGzip >>>",
|
||||||
privilege_mode: "<<< .PrivilegeMode >>>",
|
privilege_mode: "<<< .PrivilegeMode >>>",
|
||||||
@ -222,6 +223,10 @@
|
|||||||
newrow += "<tr class='info_detail'><td colspan='4'>Domains</td><td colspan='4'>" +
|
newrow += "<tr class='info_detail'><td colspan='4'>Domains</td><td colspan='4'>" +
|
||||||
alldata[index].domains[domainindex] + "</td><tr>";
|
alldata[index].domains[domainindex] + "</td><tr>";
|
||||||
}
|
}
|
||||||
|
for (var locindex in alldata[index].locations) {
|
||||||
|
newrow += "<tr class='info_detail'><td colspan='4'>Locations</td><td colspan='4'>" +
|
||||||
|
alldata[index].locations[locindex] + "</td><tr>";
|
||||||
|
}
|
||||||
newrow += "<tr class='info_detail'><td colspan='4'>Ip</td><td colspan='4'>" + alldata[index].bind_addr + "</td><tr>";
|
newrow += "<tr class='info_detail'><td colspan='4'>Ip</td><td colspan='4'>" + alldata[index].bind_addr + "</td><tr>";
|
||||||
newrow += "<tr class='info_detail'><td colspan='4'>Status</td><td colspan='4'>" + alldata[index].stat + "</td><tr>";
|
newrow += "<tr class='info_detail'><td colspan='4'>Status</td><td colspan='4'>" + alldata[index].stat + "</td><tr>";
|
||||||
newrow += "<tr class='info_detail'><td colspan='4'>Encryption</td><td colspan='4'>" + alldata[index].use_encryption + "</td><tr>";
|
newrow += "<tr class='info_detail'><td colspan='4'>Encryption</td><td colspan='4'>" + alldata[index].use_encryption + "</td><tr>";
|
||||||
|
@ -302,7 +302,7 @@ func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update metric's proxy status
|
// update metric's proxy status
|
||||||
metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip, s.PrivilegeMode, s.CustomDomains, s.ListenPort)
|
metric.SetProxyInfo(*s.ProxyServerConf)
|
||||||
|
|
||||||
// start proxy and listen for user connections, no block
|
// start proxy and listen for user connections, no block
|
||||||
err := s.Start(c)
|
err := s.Start(c)
|
||||||
|
@ -151,6 +151,7 @@ func main() {
|
|||||||
log.Error("Create vhost http listener error, %v", err)
|
log.Error("Create vhost http listener error, %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
server.VhostHttpMuxer, err = vhost.NewHttpMuxer(vhostListener, 30*time.Second)
|
server.VhostHttpMuxer, err = vhost.NewHttpMuxer(vhostListener, 30*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Create vhost httpMuxer error, %v", err)
|
log.Error("Create vhost httpMuxer error, %v", err)
|
||||||
|
@ -15,13 +15,23 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
type BaseConf struct {
|
type BaseConf struct {
|
||||||
Name string
|
Name string `json:"name"`
|
||||||
AuthToken string
|
AuthToken string `json:"-"`
|
||||||
Type string
|
Type string `json:"type"`
|
||||||
UseEncryption bool
|
UseEncryption bool `json:"use_encryption"`
|
||||||
UseGzip bool
|
UseGzip bool `json:"use_gzip"`
|
||||||
PrivilegeMode bool
|
PrivilegeMode bool `json:"privilege_mode"`
|
||||||
PrivilegeToken string
|
PrivilegeToken string `json:"-"`
|
||||||
PoolCount int64
|
PoolCount int64 `json:"pool_count"`
|
||||||
HostHeaderRewrite string
|
HostHeaderRewrite string `json:"host_header_rewrite"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProxyServerConf struct {
|
||||||
|
BaseConf
|
||||||
|
BindAddr string `json:"bind_addr"`
|
||||||
|
ListenPort int64 `json:"bind_port"`
|
||||||
|
CustomDomains []string `json:"custom_domains"`
|
||||||
|
Locations []string `json:"custom_locations"`
|
||||||
|
|
||||||
|
Status int64 `json:"status"`
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fatedier/frp/src/models/config"
|
||||||
"github.com/fatedier/frp/src/models/consts"
|
"github.com/fatedier/frp/src/models/consts"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,15 +30,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ServerMetric struct {
|
type ServerMetric struct {
|
||||||
Name string `json:"name"`
|
config.ProxyServerConf
|
||||||
Type string `json:"type"`
|
StatusDesc string `json:"status_desc"`
|
||||||
BindAddr string `json:"bind_addr"`
|
|
||||||
ListenPort int64 `json:"listen_port"`
|
|
||||||
CustomDomains []string `json:"custom_domains"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
UseEncryption bool `json:"use_encryption"`
|
|
||||||
UseGzip bool `json:"use_gzip"`
|
|
||||||
PrivilegeMode bool `json:"privilege_mode"`
|
|
||||||
|
|
||||||
// statistics
|
// statistics
|
||||||
CurrentConns int64 `json:"current_conns"`
|
CurrentConns int64 `json:"current_conns"`
|
||||||
@ -110,24 +104,16 @@ func GetProxyMetrics(proxyName string) *ServerMetric {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetProxyInfo(proxyName string, proxyType, bindAddr string,
|
func SetProxyInfo(p config.ProxyServerConf) {
|
||||||
useEncryption, useGzip, privilegeMode bool, customDomains []string,
|
|
||||||
listenPort int64) {
|
|
||||||
smMutex.Lock()
|
smMutex.Lock()
|
||||||
info, ok := ServerMetricInfoMap[proxyName]
|
info, ok := ServerMetricInfoMap[p.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
info = &ServerMetric{}
|
info = &ServerMetric{}
|
||||||
info.Daily = make([]*DailyServerStats, 0)
|
info.Daily = make([]*DailyServerStats, 0)
|
||||||
}
|
}
|
||||||
info.Name = proxyName
|
|
||||||
info.Type = proxyType
|
info.ProxyServerConf = p
|
||||||
info.UseEncryption = useEncryption
|
ServerMetricInfoMap[p.Name] = info
|
||||||
info.UseGzip = useGzip
|
|
||||||
info.PrivilegeMode = privilegeMode
|
|
||||||
info.BindAddr = bindAddr
|
|
||||||
info.ListenPort = listenPort
|
|
||||||
info.CustomDomains = customDomains
|
|
||||||
ServerMetricInfoMap[proxyName] = info
|
|
||||||
smMutex.Unlock()
|
smMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +123,7 @@ func SetStatus(proxyName string, status int64) {
|
|||||||
smMutex.RUnlock()
|
smMutex.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
metric.mutex.Lock()
|
metric.mutex.Lock()
|
||||||
metric.Status = consts.StatusStr[status]
|
metric.StatusDesc = consts.StatusStr[status]
|
||||||
metric.mutex.Unlock()
|
metric.mutex.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,6 +272,12 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e
|
|||||||
} else {
|
} else {
|
||||||
return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyServer.Name)
|
return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyServer.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//location
|
||||||
|
locStr, loc_ok := section["custom_location"]
|
||||||
|
if loc_ok {
|
||||||
|
proxyServer.Locations = strings.Split(locStr, ",")
|
||||||
|
}
|
||||||
} else if proxyServer.Type == "https" {
|
} else if proxyServer.Type == "https" {
|
||||||
// for https
|
// for https
|
||||||
proxyServer.ListenPort = VhostHttpsPort
|
proxyServer.ListenPort = VhostHttpsPort
|
||||||
@ -294,9 +300,8 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set metric statistics of all proxies
|
// set metric statistics of all proxies
|
||||||
for name, p := range proxyServers {
|
for _, p := range proxyServers {
|
||||||
metric.SetProxyInfo(name, p.Type, p.BindAddr, p.UseEncryption, p.UseGzip,
|
metric.SetProxyInfo(*p.ProxyServerConf)
|
||||||
p.PrivilegeMode, p.CustomDomains, p.ListenPort)
|
|
||||||
}
|
}
|
||||||
return proxyServers, nil
|
return proxyServers, nil
|
||||||
}
|
}
|
||||||
@ -357,8 +362,7 @@ func CreateProxy(s *ProxyServer) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ProxyServers[s.Name] = s
|
ProxyServers[s.Name] = s
|
||||||
metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip,
|
metric.SetProxyInfo(*s.ProxyServerConf)
|
||||||
s.PrivilegeMode, s.CustomDomains, s.ListenPort)
|
|
||||||
s.Init()
|
s.Init()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -33,13 +33,9 @@ type Listener interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ProxyServer struct {
|
type ProxyServer struct {
|
||||||
config.BaseConf
|
*config.ProxyServerConf
|
||||||
BindAddr string
|
|
||||||
ListenPort int64
|
|
||||||
CustomDomains []string
|
|
||||||
|
|
||||||
Status int64
|
CtlConn *conn.Conn `json:"-"` // control connection with frpc
|
||||||
CtlConn *conn.Conn // control connection with frpc
|
|
||||||
listeners []Listener // accept new connection from remote users
|
listeners []Listener // accept new connection from remote users
|
||||||
ctlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel
|
ctlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel
|
||||||
workConnChan chan *conn.Conn // get new work conns from control goroutine
|
workConnChan chan *conn.Conn // get new work conns from control goroutine
|
||||||
@ -48,8 +44,9 @@ type ProxyServer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewProxyServer() (p *ProxyServer) {
|
func NewProxyServer() (p *ProxyServer) {
|
||||||
|
psc := &config.ProxyServerConf{CustomDomains: make([]string, 0)}
|
||||||
p = &ProxyServer{
|
p = &ProxyServer{
|
||||||
CustomDomains: make([]string, 0),
|
ProxyServerConf: psc,
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
@ -99,6 +96,14 @@ func (p *ProxyServer) Compare(p2 *ProxyServer) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(p.Locations) != len(p2.Locations) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i, _ := range p.Locations {
|
||||||
|
if p.Locations[i] != p2.Locations[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,19 +126,13 @@ func (p *ProxyServer) Start(c *conn.Conn) (err error) {
|
|||||||
}
|
}
|
||||||
p.listeners = append(p.listeners, l)
|
p.listeners = append(p.listeners, l)
|
||||||
} else if p.Type == "http" {
|
} else if p.Type == "http" {
|
||||||
for _, domain := range p.CustomDomains {
|
ls := VhostHttpMuxer.ListenByRouter(p.Name, p.CustomDomains, p.Locations, p.HostHeaderRewrite)
|
||||||
l, err := VhostHttpMuxer.Listen(domain, p.HostHeaderRewrite)
|
for _, l := range ls {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.listeners = append(p.listeners, l)
|
p.listeners = append(p.listeners, l)
|
||||||
}
|
}
|
||||||
} else if p.Type == "https" {
|
} else if p.Type == "https" {
|
||||||
for _, domain := range p.CustomDomains {
|
for _, domain := range p.CustomDomains {
|
||||||
l, err := VhostHttpsMuxer.Listen(domain, p.HostHeaderRewrite)
|
l := VhostHttpsMuxer.Listen(p.Name, domain, p.HostHeaderRewrite)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.listeners = append(p.listeners, l)
|
p.listeners = append(p.listeners, l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,8 @@ func GetHttpHostname(c *conn.Conn) (_ net.Conn, routerName string, err error) {
|
|||||||
return sc, "", err
|
return sc, "", err
|
||||||
}
|
}
|
||||||
tmpArr := strings.Split(request.Host, ":")
|
tmpArr := strings.Split(request.Host, ":")
|
||||||
routerName = tmpArr[0]
|
//routerName = tmpArr[0]
|
||||||
|
routerName = tmpArr[0] + ":" + request.URL.Path
|
||||||
request.Body.Close()
|
request.Body.Close()
|
||||||
return sc, routerName, nil
|
return sc, routerName, 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
|
||||||
|
}
|
@ -19,66 +19,94 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
//"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fatedier/frp/src/utils/conn"
|
"github.com/fatedier/frp/src/utils/conn"
|
||||||
|
"github.com/fatedier/frp/src/utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type muxFunc func(*conn.Conn) (net.Conn, string, error)
|
type muxFunc func(*conn.Conn) (net.Conn, string, error)
|
||||||
type hostRewriteFunc func(*conn.Conn, string) (net.Conn, error)
|
type hostRewriteFunc func(*conn.Conn, string) (net.Conn, error)
|
||||||
|
|
||||||
type VhostMuxer struct {
|
type VhostMuxer struct {
|
||||||
listener *conn.Listener
|
listener *conn.Listener
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
vhostFunc muxFunc
|
vhostFunc muxFunc
|
||||||
rewriteFunc hostRewriteFunc
|
rewriteFunc hostRewriteFunc
|
||||||
registryMap map[string]*Listener
|
registryRouter *VhostRouters
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVhostMuxer(listener *conn.Listener, vhostFunc muxFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
|
func NewVhostMuxer(listener *conn.Listener, vhostFunc muxFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
|
||||||
mux = &VhostMuxer{
|
mux = &VhostMuxer{
|
||||||
listener: listener,
|
listener: listener,
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
vhostFunc: vhostFunc,
|
vhostFunc: vhostFunc,
|
||||||
rewriteFunc: rewriteFunc,
|
rewriteFunc: rewriteFunc,
|
||||||
registryMap: make(map[string]*Listener),
|
registryRouter: NewVhostRouters(),
|
||||||
}
|
}
|
||||||
go mux.run()
|
go mux.run()
|
||||||
return mux, nil
|
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
|
// 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 string) (l *Listener, err error) {
|
func (v *VhostMuxer) Listen(name, domain string, rewriteHost string) (l *Listener) {
|
||||||
v.mutex.Lock()
|
v.mutex.Lock()
|
||||||
defer v.mutex.Unlock()
|
defer v.mutex.Unlock()
|
||||||
if _, exist := v.registryMap[name]; exist {
|
|
||||||
return nil, fmt.Errorf("domain name %s is already bound", name)
|
locations := []string{""}
|
||||||
}
|
|
||||||
|
|
||||||
l = &Listener{
|
l = &Listener{
|
||||||
name: name,
|
name: name,
|
||||||
|
domain: domain,
|
||||||
|
locations: locations,
|
||||||
rewriteHost: rewriteHost,
|
rewriteHost: rewriteHost,
|
||||||
mux: v,
|
mux: v,
|
||||||
accept: make(chan *conn.Conn),
|
accept: make(chan *conn.Conn),
|
||||||
}
|
}
|
||||||
v.registryMap[name] = l
|
|
||||||
return l, nil
|
v.registryRouter.add(name, domain, locations, l)
|
||||||
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *VhostMuxer) getListener(name string) (l *Listener, exist bool) {
|
func (v *VhostMuxer) ListenByRouter(name string, domains []string, locations []string, rewriteHost string) (ls []*Listener) {
|
||||||
v.mutex.RLock()
|
|
||||||
defer v.mutex.RUnlock()
|
|
||||||
l, exist = v.registryMap[name]
|
|
||||||
return l, exist
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *VhostMuxer) unRegister(name string) {
|
|
||||||
v.mutex.Lock()
|
v.mutex.Lock()
|
||||||
defer v.mutex.Unlock()
|
defer v.mutex.Unlock()
|
||||||
delete(v.registryMap, name)
|
|
||||||
|
ls = make([]*Listener, 0)
|
||||||
|
for _, domain := range domains {
|
||||||
|
l := &Listener{
|
||||||
|
name: name,
|
||||||
|
domain: domain,
|
||||||
|
locations: locations,
|
||||||
|
rewriteHost: rewriteHost,
|
||||||
|
mux: v,
|
||||||
|
accept: make(chan *conn.Conn),
|
||||||
|
}
|
||||||
|
v.registryRouter.add(name, domain, locations, l)
|
||||||
|
ls = append(ls, l)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ls
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VhostMuxer) getListener(rname string) (l *Listener, exist bool) {
|
||||||
|
v.mutex.RLock()
|
||||||
|
defer v.mutex.RUnlock()
|
||||||
|
|
||||||
|
var frcname string
|
||||||
|
vr, found := v.registryRouter.get(rname)
|
||||||
|
if found {
|
||||||
|
frcname = vr.name
|
||||||
|
} else {
|
||||||
|
log.Warn("can't found the router for %s", rname)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("get frcname %s for %s", frcname, rname)
|
||||||
|
return vr.listener, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *VhostMuxer) run() {
|
func (v *VhostMuxer) run() {
|
||||||
@ -101,32 +129,38 @@ func (v *VhostMuxer) handle(c *conn.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
name = strings.ToLower(name)
|
//name = strings.ToLower(name)
|
||||||
l, ok := v.getListener(name)
|
l, ok := v.getListener(name)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = sConn.SetDeadline(time.Time{}); err != nil {
|
if err = sConn.SetDeadline(time.Time{}); err != nil {
|
||||||
|
log.Error("set dead line err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.SetTcpConn(sConn)
|
c.SetTcpConn(sConn)
|
||||||
|
|
||||||
|
log.Debug("handle request: %s", c.GetRemoteAddr())
|
||||||
l.accept <- c
|
l.accept <- c
|
||||||
}
|
}
|
||||||
|
|
||||||
type Listener struct {
|
type Listener struct {
|
||||||
name string
|
name string
|
||||||
|
domain string
|
||||||
|
locations []string
|
||||||
rewriteHost string
|
rewriteHost string
|
||||||
mux *VhostMuxer // for closing VhostMuxer
|
mux *VhostMuxer // for closing VhostMuxer
|
||||||
accept chan *conn.Conn
|
accept chan *conn.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Listener) Accept() (*conn.Conn, error) {
|
func (l *Listener) Accept() (*conn.Conn, error) {
|
||||||
|
log.Debug("[%s][%s] now to accept ...", l.name, l.domain)
|
||||||
conn, ok := <-l.accept
|
conn, ok := <-l.accept
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("Listener closed")
|
return nil, fmt.Errorf("Listener closed")
|
||||||
}
|
}
|
||||||
|
log.Debug("[%s][%s] accept something ...", l.name, l.domain)
|
||||||
|
|
||||||
// if rewriteFunc is exist and rewriteHost is set
|
// if rewriteFunc is exist and rewriteHost is set
|
||||||
// rewrite http requests with a modified host header
|
// rewrite http requests with a modified host header
|
||||||
@ -141,7 +175,7 @@ func (l *Listener) Accept() (*conn.Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Listener) Close() error {
|
func (l *Listener) Close() error {
|
||||||
l.mux.unRegister(l.name)
|
l.mux.registryRouter.del(l)
|
||||||
close(l.accept)
|
close(l.accept)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user