This commit is contained in:
fatedier
2016-12-25 01:53:23 +08:00
parent 59a34b81e0
commit eb4f779384
11 changed files with 180 additions and 156 deletions

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

@@ -15,26 +15,16 @@
package config
type BaseConf struct {
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"`
Name string
AuthToken string
Type string
UseEncryption bool
UseGzip bool
PrivilegeMode bool
PrivilegeToken string
PoolCount int64
HostHeaderRewrite string
HttpUserName string
HttpPassWord string
SubDomain string
}

View File

@@ -19,7 +19,6 @@ import (
"sync"
"time"
"github.com/fatedier/frp/src/models/config"
"github.com/fatedier/frp/src/models/consts"
)
@@ -30,8 +29,15 @@ var (
)
type ServerMetric struct {
config.ProxyServerConf
StatusDesc string `json:"status_desc"`
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"`
// statistics
CurrentConns int64 `json:"current_conns"`
@@ -104,16 +110,24 @@ func GetProxyMetrics(proxyName string) *ServerMetric {
}
}
func SetProxyInfo(p config.ProxyServerConf) {
func SetProxyInfo(proxyName string, proxyType, bindAddr string,
useEncryption, useGzip, privilegeMode bool, customDomains []string,
listenPort int64) {
smMutex.Lock()
info, ok := ServerMetricInfoMap[p.Name]
info, ok := ServerMetricInfoMap[proxyName]
if !ok {
info = &ServerMetric{}
info.Daily = make([]*DailyServerStats, 0)
}
info.ProxyServerConf = p
ServerMetricInfoMap[p.Name] = info
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
smMutex.Unlock()
}
@@ -123,7 +137,7 @@ func SetStatus(proxyName string, status int64) {
smMutex.RUnlock()
if ok {
metric.mutex.Lock()
metric.StatusDesc = consts.StatusStr[status]
metric.Status = consts.StatusStr[status]
metric.mutex.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

@@ -305,10 +305,10 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e
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, ",")
// locations
locations, ok := section["locations"]
if ok {
proxyServer.Locations = strings.Split(locations, ",")
} else {
proxyServer.Locations = []string{""}
}
@@ -338,8 +338,9 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e
}
// set metric statistics of all proxies
for _, p := range proxyServers {
metric.SetProxyInfo(*p.ProxyServerConf)
for name, p := range proxyServers {
metric.SetProxyInfo(name, p.Type, p.BindAddr, p.UseEncryption, p.UseGzip,
p.PrivilegeMode, p.CustomDomains, p.ListenPort)
}
return proxyServers, nil
}
@@ -400,7 +401,8 @@ func CreateProxy(s *ProxyServer) error {
}
}
ProxyServers[s.Name] = s
metric.SetProxyInfo(*s.ProxyServerConf)
metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip,
s.PrivilegeMode, s.CustomDomains, s.ListenPort)
s.Init()
return nil
}

View File

@@ -35,7 +35,11 @@ type Listener interface {
}
type ProxyServer struct {
*config.ProxyServerConf
config.BaseConf
BindAddr string
ListenPort int64
CustomDomains []string
Locations []string
Status int64
CtlConn *conn.Conn // control connection with frpc
@@ -51,9 +55,9 @@ type ProxyServer struct {
}
func NewProxyServer() (p *ProxyServer) {
psc := &config.ProxyServerConf{CustomDomains: make([]string, 0)}
p = &ProxyServer{
ProxyServerConf: psc,
CustomDomains: make([]string, 0),
Locations: make([]string, 0),
}
return p
}
@@ -75,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
@@ -137,13 +142,46 @@ func (p *ProxyServer) Start(c *conn.Conn) (err error) {
}
p.listeners = append(p.listeners, l)
} else if p.Type == "http" {
ls := VhostHttpMuxer.Listen(p.ProxyServerConf)
for _, l := range ls {
p.listeners = append(p.listeners, l)
for _, domain := range p.CustomDomains {
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)
}
}
}
if p.SubDomain != "" {
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)
}
}
}
} else if p.Type == "https" {
ls := VhostHttpsMuxer.Listen(p.ProxyServerConf)
for _, l := range ls {
for _, domain := range p.CustomDomains {
l, err := VhostHttpsMuxer.Listen(domain, "", p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}
p.listeners = append(p.listeners, l)
}
}