Merge pull request #192 from fatedier/pr_182

http proxy support router by url
This commit is contained in:
fatedier 2016-12-24 12:41:20 -06:00 committed by GitHub
commit 6a6ccc5302
15 changed files with 207 additions and 48 deletions

View File

@ -77,5 +77,7 @@ local_ip = 127.0.0.1
local_port = 80
use_gzip = true
custom_domains = web03.yourdomain.com
# locations is only useful for http type
locations = /,/pic
host_header_rewrite = example.com
subdomain = dev

View File

@ -25,6 +25,7 @@
<th class="tab_info" ng-click="col='current_conns';desc=!desc">CurCon<i class="iconfont pull-right">&#xe66d;</i></th>
<th class="tab_info" ng-click="col='daily[daily.length-1].flow_out';desc=!desc">FlowOut<i class="iconfont pull-right">&#xe66d;</i></th>
<th class="tab_info" ng-click="col='daily[daily.length-1].flow_in';desc=!desc">FlowIn<i class="iconfont pull-right">&#xe66d;</i></th>
<th class="tab_info" ng-click="col='daily[daily.length-1].total_accept_conns';desc=!desc">TotalAcceptConns<i class="iconfont pull-right">&#xe66d;</i></th>
</tr>
</thead>
<tbody id="tab_body">
@ -38,6 +39,7 @@
<td><span ng-bind="x.current_conns"></span></td>
<td><span ng-bind="x.daily[x.daily.length-1].flow_out"></span></td>
<td><span ng-bind="x.daily[x.daily.length-1].flow_in"></span></td>
<td><span ng-bind="x.daily[x.daily.length-1].total_accept_conns"></span></td>
</tr>
</tbody>
</table>
@ -63,7 +65,8 @@
listen_port: "<<< .ListenPort >>>",
current_conns: <<< .CurrentConns >>> ,
domains: [ <<< range.CustomDomains >>> "<<< . >>>", <<< end >>> ],
stat: "<<< .Status >>>",
locations: [ <<< range.Locations >>> "<<< . >>>", <<< end >>> ],
stat: "<<< .Status>>>",
use_encryption: "<<< .UseEncryption >>>",
use_gzip: "<<< .UseGzip >>>",
privilege_mode: "<<< .PrivilegeMode >>>",
@ -222,6 +225,10 @@
newrow += "<tr class='info_detail'><td colspan='4'>Domains</td><td colspan='4'>" +
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'>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>";

File diff suppressed because one or more lines are too long

View File

@ -159,6 +159,7 @@ func loginToServer(cli *client.ProxyClient) (c *conn.Conn, err error) {
privilegeKey := pcrypto.GetAuthKey(cli.Name + client.PrivilegeToken + fmt.Sprintf("%d", nowTime))
req.RemotePort = cli.RemotePort
req.CustomDomains = cli.CustomDomains
req.Locations = cli.Locations
req.PrivilegeKey = privilegeKey
} else {
authKey := pcrypto.GetAuthKey(cli.Name + cli.AuthToken + fmt.Sprintf("%d", nowTime))

View File

@ -297,6 +297,7 @@ func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
}
// set infomations from frpc
s.BindAddr = server.BindAddr
s.UseEncryption = req.UseEncryption
s.UseGzip = req.UseGzip
s.HostHeaderRewrite = req.HostHeaderRewrite
@ -332,7 +333,7 @@ func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
}
// 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.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip, s.PrivilegeMode, s.CustomDomains, s.Locations, s.ListenPort)
// start proxy and listen for user connections, no block
err := s.Start(c)

View File

@ -35,6 +35,7 @@ type ProxyClient struct {
RemotePort int64
CustomDomains []string
Locations []string
udpTunnel *conn.Conn
once sync.Once

View File

@ -227,6 +227,14 @@ func LoadConf(confFile string) (err error) {
if !ok && proxyClient.SubDomain == "" {
return fmt.Errorf("Parse conf error: proxy [%s] custom_domains and subdomain should set at least one of them when type is http", proxyClient.Name)
}
// locations
locations, ok := section["locations"]
if ok {
proxyClient.Locations = strings.Split(locations, ",")
} else {
proxyClient.Locations = []string{""}
}
} else if proxyClient.Type == "https" {
// custom_domains
domainStr, ok := section["custom_domains"]

View File

@ -34,6 +34,7 @@ type ServerMetric struct {
BindAddr string `json:"bind_addr"`
ListenPort int64 `json:"listen_port"`
CustomDomains []string `json:"custom_domains"`
Locations []string `json:"locations"`
Status string `json:"status"`
UseEncryption bool `json:"use_encryption"`
UseGzip bool `json:"use_gzip"`
@ -112,7 +113,7 @@ func GetProxyMetrics(proxyName string) *ServerMetric {
func SetProxyInfo(proxyName string, proxyType, bindAddr string,
useEncryption, useGzip, privilegeMode bool, customDomains []string,
listenPort int64) {
locations []string, listenPort int64) {
smMutex.Lock()
info, ok := ServerMetricInfoMap[proxyName]
if !ok {
@ -127,6 +128,7 @@ func SetProxyInfo(proxyName string, proxyType, bindAddr string,
info.BindAddr = bindAddr
info.ListenPort = listenPort
info.CustomDomains = customDomains
info.Locations = locations
ServerMetricInfoMap[proxyName] = info
smMutex.Unlock()
}

View File

@ -34,6 +34,7 @@ type ControlReq struct {
ProxyType string `json:"proxy_type"`
RemotePort int64 `json:"remote_port"`
CustomDomains []string `json:"custom_domains, omitempty"`
Locations []string `json:"locations"`
HostHeaderRewrite string `json:"host_header_rewrite"`
HttpUserName string `json:"http_username"`
HttpPassWord string `json:"http_password"`

View File

@ -304,6 +304,14 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e
} else {
return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type is http", proxyServer.Name)
}
// locations
locations, ok := section["locations"]
if ok {
proxyServer.Locations = strings.Split(locations, ",")
} else {
proxyServer.Locations = []string{""}
}
} else if proxyServer.Type == "https" {
// for https
proxyServer.ListenPort = VhostHttpsPort
@ -332,7 +340,7 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e
// set metric statistics of all proxies
for name, p := range proxyServers {
metric.SetProxyInfo(name, p.Type, p.BindAddr, p.UseEncryption, p.UseGzip,
p.PrivilegeMode, p.CustomDomains, p.ListenPort)
p.PrivilegeMode, p.CustomDomains, p.Locations, p.ListenPort)
}
return proxyServers, nil
}
@ -394,7 +402,7 @@ func CreateProxy(s *ProxyServer) error {
}
ProxyServers[s.Name] = s
metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip,
s.PrivilegeMode, s.CustomDomains, s.ListenPort)
s.PrivilegeMode, s.CustomDomains, s.Locations, s.ListenPort)
s.Init()
return nil
}

View File

@ -39,6 +39,7 @@ type ProxyServer struct {
BindAddr string
ListenPort int64
CustomDomains []string
Locations []string
Status int64
CtlConn *conn.Conn // control connection with frpc
@ -56,6 +57,7 @@ type ProxyServer struct {
func NewProxyServer() (p *ProxyServer) {
p = &ProxyServer{
CustomDomains: make([]string, 0),
Locations: make([]string, 0),
}
return p
}
@ -77,6 +79,7 @@ func NewProxyServerFromCtlMsg(req *msg.ControlReq) (p *ProxyServer) {
p.ListenPort = VhostHttpsPort
}
p.CustomDomains = req.CustomDomains
p.Locations = req.Locations
p.HostHeaderRewrite = req.HostHeaderRewrite
p.HttpUserName = req.HttpUserName
p.HttpPassWord = req.HttpPassWord
@ -108,6 +111,15 @@ func (p *ProxyServer) Compare(p2 *ProxyServer) bool {
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
}
@ -131,23 +143,42 @@ func (p *ProxyServer) Start(c *conn.Conn) (err error) {
p.listeners = append(p.listeners, l)
} else if p.Type == "http" {
for _, domain := range p.CustomDomains {
l, err := VhostHttpMuxer.Listen(domain, p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
if len(p.Locations) == 0 {
l, err := VhostHttpMuxer.Listen(domain, "", p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}
p.listeners = append(p.listeners, l)
} else {
for _, location := range p.Locations {
l, err := VhostHttpMuxer.Listen(domain, location, p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}
p.listeners = append(p.listeners, l)
}
}
p.listeners = append(p.listeners, l)
}
if p.SubDomain != "" {
l, err := VhostHttpMuxer.Listen(p.SubDomain, p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
if len(p.Locations) == 0 {
l, err := VhostHttpMuxer.Listen(p.SubDomain, "", p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}
p.listeners = append(p.listeners, l)
} else {
for _, location := range p.Locations {
l, err := VhostHttpMuxer.Listen(p.SubDomain, location, p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}
p.listeners = append(p.listeners, l)
}
}
p.listeners = append(p.listeners, l)
}
} else if p.Type == "https" {
for _, domain := range p.CustomDomains {
l, err := VhostHttpsMuxer.Listen(domain, p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
l, err := VhostHttpsMuxer.Listen(domain, "", p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}

View File

@ -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")

View File

@ -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
}

96
src/utils/vhost/router.go Normal file
View File

@ -0,0 +1,96 @@
package vhost
import (
"sort"
"strings"
"sync"
)
type VhostRouters struct {
RouterByDomain map[string][]*VhostRouter
mutex sync.RWMutex
}
type VhostRouter struct {
domain string
location string
listener *Listener
}
func NewVhostRouters() *VhostRouters {
return &VhostRouters{
RouterByDomain: make(map[string][]*VhostRouter),
}
}
func (r *VhostRouters) Add(domain, location string, l *Listener) {
r.mutex.Lock()
defer r.mutex.Unlock()
vrs, found := r.RouterByDomain[domain]
if !found {
vrs = make([]*VhostRouter, 0)
}
vr := &VhostRouter{
domain: domain,
location: location,
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.name]
if !found {
return
}
for i, vr := range vrs {
if vr.listener == l {
if len(vrs) > i+1 {
r.RouterByDomain[l.name] = append(vrs[:i], vrs[i+1:]...)
} else {
r.RouterByDomain[l.name] = vrs[:i]
}
}
}
}
func (r *VhostRouters) Get(host, path string) (vr *VhostRouter, exist bool) {
r.mutex.RLock()
defer r.mutex.RUnlock()
vrs, found := r.RouterByDomain[host]
if !found {
return
}
//can't support load balance,will to do
for _, vr = range vrs {
if strings.HasPrefix(path, vr.location) {
return vr, true
}
}
return
}
//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
}

View File

@ -29,35 +29,32 @@ 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(name, location, rewriteHost, userName, passWord string) (l *Listener, err error) {
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,
@ -67,33 +64,34 @@ func (v *VhostMuxer) Listen(name string, rewriteHost, userName, passWord string)
mux: v,
accept: make(chan *conn.Conn),
}
v.registryMap[name] = l
v.registryRouter.Add(name, location, l)
return l, nil
}
func (v *VhostMuxer) getListener(name string) (l *Listener, exist bool) {
func (v *VhostMuxer) getListener(name, path 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
vr, found := v.registryRouter.Get(name, path)
if found {
return vr.listener, true
}
domainSplit := strings.Split(name, ".")
if len(domainSplit) < 3 {
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, path)
if !found {
return
}
return vr.listener, true
}
func (v *VhostMuxer) run() {
@ -119,8 +117,8 @@ func (v *VhostMuxer) handle(c *conn.Conn) {
}
name := strings.ToLower(reqInfoMap["Host"])
// get listener by hostname
l, ok := v.getListener(name)
path := strings.ToLower(reqInfoMap["Path"])
l, ok := v.getListener(name, path)
if !ok {
c.Close()
return
@ -177,7 +175,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
}