mirror of
https://gitee.com/IrisVega/frp.git
synced 2024-11-01 22:31:29 +08:00
newhttp support BasicAuth
This commit is contained in:
parent
46358d466d
commit
4cc5ddc012
@ -194,10 +194,11 @@ type HttpProxy struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pxy *HttpProxy) Run() (err error) {
|
func (pxy *HttpProxy) Run() (err error) {
|
||||||
routeConfig := &vhost.VhostRouteConfig{
|
routeConfig := vhost.VhostRouteConfig{
|
||||||
RewriteHost: pxy.cfg.HostHeaderRewrite,
|
RewriteHost: pxy.cfg.HostHeaderRewrite,
|
||||||
Username: pxy.cfg.HttpUser,
|
Username: pxy.cfg.HttpUser,
|
||||||
Password: pxy.cfg.HttpPwd,
|
Password: pxy.cfg.HttpPwd,
|
||||||
|
CreateConnFn: pxy.GetRealConn,
|
||||||
}
|
}
|
||||||
|
|
||||||
locations := pxy.cfg.Locations
|
locations := pxy.cfg.Locations
|
||||||
@ -208,7 +209,7 @@ func (pxy *HttpProxy) Run() (err error) {
|
|||||||
routeConfig.Domain = domain
|
routeConfig.Domain = domain
|
||||||
for _, location := range locations {
|
for _, location := range locations {
|
||||||
routeConfig.Location = location
|
routeConfig.Location = location
|
||||||
err := pxy.ctl.svr.httpReverseProxy.Register(routeConfig.Domain, routeConfig.Location, routeConfig.RewriteHost, pxy.GetRealConn)
|
err := pxy.ctl.svr.httpReverseProxy.Register(routeConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -225,7 +226,7 @@ func (pxy *HttpProxy) Run() (err error) {
|
|||||||
routeConfig.Domain = pxy.cfg.SubDomain + "." + config.ServerCommonCfg.SubDomainHost
|
routeConfig.Domain = pxy.cfg.SubDomain + "." + config.ServerCommonCfg.SubDomainHost
|
||||||
for _, location := range locations {
|
for _, location := range locations {
|
||||||
routeConfig.Location = location
|
routeConfig.Location = location
|
||||||
err := pxy.ctl.svr.httpReverseProxy.Register(routeConfig.Domain, routeConfig.Location, routeConfig.RewriteHost, pxy.GetRealConn)
|
err := pxy.ctl.svr.httpReverseProxy.Register(routeConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
frpLog "github.com/fatedier/frp/utils/log"
|
frpLog "github.com/fatedier/frp/utils/log"
|
||||||
frpNet "github.com/fatedier/frp/utils/net"
|
|
||||||
"github.com/fatedier/frp/utils/pool"
|
"github.com/fatedier/frp/utils/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -47,13 +46,6 @@ func getHostFromAddr(addr string) (host string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateConnFunc func() (frpNet.Conn, error)
|
|
||||||
|
|
||||||
type ProxyOption struct {
|
|
||||||
RewriteHost string
|
|
||||||
DialFunc CreateConnFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
type HttpReverseProxy struct {
|
type HttpReverseProxy struct {
|
||||||
proxy *ReverseProxy
|
proxy *ReverseProxy
|
||||||
tr *http.Transport
|
tr *http.Transport
|
||||||
@ -94,18 +86,14 @@ func NewHttpReverseProxy() *HttpReverseProxy {
|
|||||||
return rp
|
return rp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *HttpReverseProxy) Register(domain string, location string, rewriteHost string, fn CreateConnFunc) error {
|
func (rp *HttpReverseProxy) Register(routeCfg VhostRouteConfig) error {
|
||||||
rp.cfgMu.Lock()
|
rp.cfgMu.Lock()
|
||||||
defer rp.cfgMu.Unlock()
|
defer rp.cfgMu.Unlock()
|
||||||
_, ok := rp.vhostRouter.Exist(domain, location)
|
_, ok := rp.vhostRouter.Exist(routeCfg.Domain, routeCfg.Location)
|
||||||
if ok {
|
if ok {
|
||||||
return ErrRouterConfigConflict
|
return ErrRouterConfigConflict
|
||||||
} else {
|
} else {
|
||||||
payload := &ProxyOption{
|
rp.vhostRouter.Add(routeCfg.Domain, routeCfg.Location, &routeCfg)
|
||||||
RewriteHost: rewriteHost,
|
|
||||||
DialFunc: fn,
|
|
||||||
}
|
|
||||||
rp.vhostRouter.Add(domain, location, payload)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -119,7 +107,7 @@ func (rp *HttpReverseProxy) UnRegister(domain string, location string) {
|
|||||||
func (rp *HttpReverseProxy) GetRealHost(domain string, location string) (host string) {
|
func (rp *HttpReverseProxy) GetRealHost(domain string, location string) (host string) {
|
||||||
vr, ok := rp.getVhost(domain, location)
|
vr, ok := rp.getVhost(domain, location)
|
||||||
if ok {
|
if ok {
|
||||||
host = vr.payload.(*ProxyOption).RewriteHost
|
host = vr.payload.(*VhostRouteConfig).RewriteHost
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -127,7 +115,7 @@ func (rp *HttpReverseProxy) GetRealHost(domain string, location string) (host st
|
|||||||
func (rp *HttpReverseProxy) CreateConnection(domain string, location string) (net.Conn, error) {
|
func (rp *HttpReverseProxy) CreateConnection(domain string, location string) (net.Conn, error) {
|
||||||
vr, ok := rp.getVhost(domain, location)
|
vr, ok := rp.getVhost(domain, location)
|
||||||
if ok {
|
if ok {
|
||||||
fn := vr.payload.(*ProxyOption).DialFunc
|
fn := vr.payload.(*VhostRouteConfig).CreateConnFn
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
return fn()
|
return fn()
|
||||||
}
|
}
|
||||||
@ -135,6 +123,18 @@ func (rp *HttpReverseProxy) CreateConnection(domain string, location string) (ne
|
|||||||
return nil, ErrNoDomain
|
return nil, ErrNoDomain
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rp *HttpReverseProxy) CheckAuth(domain, location, user, passwd string) bool {
|
||||||
|
vr, ok := rp.getVhost(domain, location)
|
||||||
|
if ok {
|
||||||
|
checkUser := vr.payload.(*VhostRouteConfig).Username
|
||||||
|
checkPasswd := vr.payload.(*VhostRouteConfig).Password
|
||||||
|
if (checkUser != "" || checkPasswd != "") && (checkUser != user || checkPasswd != passwd) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (rp *HttpReverseProxy) getVhost(domain string, location string) (vr *VhostRouter, ok bool) {
|
func (rp *HttpReverseProxy) getVhost(domain string, location string) (vr *VhostRouter, ok bool) {
|
||||||
rp.cfgMu.RLock()
|
rp.cfgMu.RLock()
|
||||||
defer rp.cfgMu.RUnlock()
|
defer rp.cfgMu.RUnlock()
|
||||||
@ -157,6 +157,14 @@ func (rp *HttpReverseProxy) getVhost(domain string, location string) (vr *VhostR
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rp *HttpReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
func (rp *HttpReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
domain := getHostFromAddr(req.Host)
|
||||||
|
location := req.URL.Path
|
||||||
|
user, passwd, _ := req.BasicAuth()
|
||||||
|
if !rp.CheckAuth(domain, location, user, passwd) {
|
||||||
|
rw.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||||||
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
rp.proxy.ServeHTTP(rw, req)
|
rp.proxy.ServeHTTP(rw, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,12 +50,16 @@ func NewVhostMuxer(listener frpNet.Listener, vhostFunc muxFunc, authFunc httpAut
|
|||||||
return mux, nil
|
return mux, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CreateConnFunc func() (frpNet.Conn, error)
|
||||||
|
|
||||||
type VhostRouteConfig struct {
|
type VhostRouteConfig struct {
|
||||||
Domain string
|
Domain string
|
||||||
Location string
|
Location string
|
||||||
RewriteHost string
|
RewriteHost string
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
|
|
||||||
|
CreateConnFn CreateConnFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// listen for a new domain name, if rewriteHost is not empty and rewriteFunc is not nil
|
// listen for a new domain name, if rewriteHost is not empty and rewriteFunc is not nil
|
||||||
|
Loading…
Reference in New Issue
Block a user