Skip to content

Commit

Permalink
Merge pull request fatedier#182 from xuebing1110/dev
Browse files Browse the repository at this point in the history
[dev] http router by host and url
  • Loading branch information
fatedier authored Dec 24, 2016
2 parents 6b34ed4 + b1d1a7a commit 59a34b8
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 109 deletions.
11 changes: 9 additions & 2 deletions src/assets/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -34,10 +35,11 @@
</td>
<td><span ng-bind="x.type"></span></td>
<td><span ng-bind="x.listen_port"></span></td>
<td><span ng-bind="x.status"></span></td>
<td><span ng-bind="x.status_desc"></span></td>
<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>
Expand All @@ -63,7 +65,8 @@
listen_port: "<<< .ListenPort >>>",
current_conns: <<< .CurrentConns >>> ,
domains: [ <<< range.CustomDomains >>> "<<< . >>>", <<< end >>> ],
stat: "<<< .Status >>>",
locations: [ <<< range.Locations >>> "<<< . >>>", <<< end >>> ],
stat: "<<< .StatusDesc >>>",
use_encryption: "<<< .UseEncryption >>>",
use_gzip: "<<< .UseGzip >>>",
privilege_mode: "<<< .PrivilegeMode >>>",
Expand Down Expand Up @@ -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>";
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/frps/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ 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.ListenPort)
metric.SetProxyInfo(*s.ProxyServerConf)

// start proxy and listen for user connections, no block
err := s.Start(c)
Expand Down
34 changes: 22 additions & 12 deletions src/models/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@
package config

type BaseConf struct {
Name string
AuthToken string
Type string
UseEncryption bool
UseGzip bool
PrivilegeMode bool
PrivilegeToken string
PoolCount int64
HostHeaderRewrite string
HttpUserName string
HttpPassWord string
SubDomain string
Name string `json:"name"`
AuthToken string `json:"-"`
Type string `json:"type"`
UseEncryption bool `json:"use_encryption"`
UseGzip bool `json:"use_gzip"`
PrivilegeMode bool `json:"privilege_mode"`
PrivilegeToken string `json:"-"`
PoolCount int64 `json:"pool_count"`
HostHeaderRewrite string `json:"host_header_rewrite"`
HttpUserName string `json:"http_username"`
HttpPassWord string `json:"-"`
SubDomain string `json:"subdomain"`
}

type ProxyServerConf struct {
BaseConf
BindAddr string `json:"bind_addr"`
ListenPort int64 `json:"listen_port"`
CustomDomains []string `json:"custom_domains"`
Locations []string `json:"custom_locations"`

Status int64 `json:"status"`
}
32 changes: 9 additions & 23 deletions src/models/metric/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sync"
"time"

"github.com/fatedier/frp/src/models/config"
"github.com/fatedier/frp/src/models/consts"
)

Expand All @@ -29,15 +30,8 @@ var (
)

type ServerMetric struct {
Name string `json:"name"`
Type string `json:"type"`
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"`
config.ProxyServerConf
StatusDesc string `json:"status_desc"`

// statistics
CurrentConns int64 `json:"current_conns"`
Expand Down Expand Up @@ -110,24 +104,16 @@ func GetProxyMetrics(proxyName string) *ServerMetric {
}
}

func SetProxyInfo(proxyName string, proxyType, bindAddr string,
useEncryption, useGzip, privilegeMode bool, customDomains []string,
listenPort int64) {
func SetProxyInfo(p config.ProxyServerConf) {
smMutex.Lock()
info, ok := ServerMetricInfoMap[proxyName]
info, ok := ServerMetricInfoMap[p.Name]
if !ok {
info = &ServerMetric{}
info.Daily = make([]*DailyServerStats, 0)
}
info.Name = proxyName
info.Type = proxyType
info.UseEncryption = useEncryption
info.UseGzip = useGzip
info.PrivilegeMode = privilegeMode
info.BindAddr = bindAddr
info.ListenPort = listenPort
info.CustomDomains = customDomains
ServerMetricInfoMap[proxyName] = info

info.ProxyServerConf = p
ServerMetricInfoMap[p.Name] = info
smMutex.Unlock()
}

Expand All @@ -137,7 +123,7 @@ func SetStatus(proxyName string, status int64) {
smMutex.RUnlock()
if ok {
metric.mutex.Lock()
metric.Status = consts.StatusStr[status]
metric.StatusDesc = consts.StatusStr[status]
metric.mutex.Unlock()
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/models/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

//location
locStr, loc_ok := section["custom_location"]
if loc_ok {
proxyServer.Locations = strings.Split(locStr, ",")
} else {
proxyServer.Locations = []string{""}
}
} else if proxyServer.Type == "https" {
// for https
proxyServer.ListenPort = VhostHttpsPort
Expand All @@ -330,9 +338,8 @@ 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)
for _, p := range proxyServers {
metric.SetProxyInfo(*p.ProxyServerConf)
}
return proxyServers, nil
}
Expand Down Expand Up @@ -393,8 +400,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)
metric.SetProxyInfo(*s.ProxyServerConf)
s.Init()
return nil
}
Expand Down
39 changes: 16 additions & 23 deletions src/models/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ type Listener interface {
}

type ProxyServer struct {
config.BaseConf
BindAddr string
ListenPort int64
CustomDomains []string
*config.ProxyServerConf

Status int64
CtlConn *conn.Conn // control connection with frpc
Expand All @@ -54,8 +51,9 @@ type ProxyServer struct {
}

func NewProxyServer() (p *ProxyServer) {
psc := &config.ProxyServerConf{CustomDomains: make([]string, 0)}
p = &ProxyServer{
CustomDomains: make([]string, 0),
ProxyServerConf: psc,
}
return p
}
Expand Down Expand Up @@ -108,6 +106,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
}

Expand All @@ -130,27 +137,13 @@ 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
}
ls := VhostHttpMuxer.Listen(p.ProxyServerConf)
for _, l := range ls {
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
}
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)
if err != nil {
return err
}
ls := VhostHttpsMuxer.Listen(p.ProxyServerConf)
for _, l := range ls {
p.listeners = append(p.listeners, l)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/vhost/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions src/utils/vhost/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 107 additions & 0 deletions src/utils/vhost/router.go
Original file line number Diff line number Diff line change
@@ -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, found := r.RouterByDomain[domain]
if !found {
return
}

//can't support load balance,will to do
for _, vr = range vrs {
if strings.HasPrefix(url, vr.location) {
return vr, true
}
}

return
}
Loading

0 comments on commit 59a34b8

Please sign in to comment.