add proxying cookies in websocket proxy
fix bug in http proxy when receive 0 addresses
This commit is contained in:
1 parent
6ec99e8d1f
commit
0cf2b61ce3
2 files changed
+22
-5
No files matched your search
@@ -23,6 +23,10 @@ func NewProxy(skipAuth bool) *httpProxy {
|
||||
}
|
||||
|
||||
func (p *httpProxy) Consumer(addressList []structure.AddressConfiguration) bool {
|
||||
if len(addressList) == 0 {
|
||||
p.client = nil
|
||||
return true
|
||||
}
|
||||
addresses := make([]string, len(addressList))
|
||||
for key, addr := range addressList {
|
||||
addresses[key] = addr.GetAddress()
|
||||
@@ -35,7 +39,8 @@ func (p *httpProxy) Consumer(addressList []structure.AddressConfiguration) bool
|
||||
}
|
||||
|
||||
func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse {
|
||||
if p.client == nil {
|
||||
client := p.client
|
||||
if client == nil {
|
||||
msg := "client undefined"
|
||||
log.Error(log_code.ErrorClientHttp, msg)
|
||||
utils.WriteError(ctx, msg, codes.Internal, nil)
|
||||
@@ -53,7 +58,7 @@ func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.P
|
||||
req.Header.Add("X-Forwarded-For", addr)
|
||||
}
|
||||
|
||||
err := p.client.Do(req, res)
|
||||
err := client.Do(req, res)
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorClientHttp, err)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"isp-gate-service/utils"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -70,6 +72,8 @@ func (p *websocketProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) dom
|
||||
|
||||
reqHeaders := fasthttp.RequestHeader{}
|
||||
ctx.Request.Header.CopyTo(&reqHeaders)
|
||||
reqHost := string(ctx.Request.URI().Host())
|
||||
reqScheme := string(ctx.Request.URI().Scheme())
|
||||
|
||||
if addr, _, err := net.SplitHostPort(ctx.RemoteAddr().String()); err == nil {
|
||||
reqHeaders.Add("X-Forwarded-For", addr)
|
||||
@@ -85,7 +89,7 @@ func (p *websocketProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) dom
|
||||
}
|
||||
|
||||
addr := addrs.Get()
|
||||
url := "ws://" + addr.GetAddress() + "/" + path
|
||||
urlAddr := "ws://" + addr.GetAddress() + "/" + path
|
||||
header := http.Header{}
|
||||
|
||||
reqHeaders.VisitAll(func(key, value []byte) {
|
||||
@@ -95,14 +99,22 @@ func (p *websocketProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) dom
|
||||
}
|
||||
})
|
||||
|
||||
outgoingConn, _, err := outgoingDialer.Dial(url, header)
|
||||
cookie := make([]*http.Cookie, 0)
|
||||
reqHeaders.VisitAllCookie(func(key, value []byte) {
|
||||
cookie = append(cookie, &http.Cookie{Domain: reqHost, Name: string(key), Value: string(value)})
|
||||
})
|
||||
cookies := &cookiejar.Jar{}
|
||||
cookies.SetCookies(&url.URL{Host: reqHost, Scheme: reqScheme}, cookie)
|
||||
outgoingDialer.Jar = cookies
|
||||
|
||||
outgoingConn, _, err := outgoingDialer.Dial(urlAddr, header)
|
||||
if err == nil {
|
||||
go func() {
|
||||
_ = p.proxyConn(outgoingConn, incomingConn)
|
||||
}()
|
||||
_ = p.proxyConn(incomingConn, outgoingConn)
|
||||
} else {
|
||||
log.Errorf(log_code.ErrorWebsocketProxy, "unable to connect to service %s: %v", url, err)
|
||||
log.Errorf(log_code.ErrorWebsocketProxy, "unable to connect to service %s: %v", urlAddr, err)
|
||||
_ = incomingConn.Close()
|
||||
}
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user