Fix linter issues
This commit is contained in:
@@ -2,15 +2,15 @@ package deconnector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (d *Deconnector) handleDeconnect(clientConn net.Conn, connectReq *http.Request, upstreamURL *url.URL) {
|
||||
func (d *Deconnector) handleDeconnect(ctx context.Context, clientConn net.Conn, connectReq *http.Request) {
|
||||
// Tell client the tunnel is open
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 200 Connection established\r\n\r\n")
|
||||
_, _ = fmt.Fprintf(clientConn, "HTTP/1.1 200 Connection established\r\n\r\n")
|
||||
|
||||
// Read the real HTTP request the client sends through the tunnel
|
||||
innerReq, err := http.ReadRequest(bufio.NewReader(clientConn))
|
||||
@@ -30,5 +30,5 @@ func (d *Deconnector) handleDeconnect(clientConn net.Conn, connectReq *http.Requ
|
||||
WithField("url", innerReq.URL).
|
||||
Info("Handling de-CONNECT request")
|
||||
|
||||
d.forwardHTTP(clientConn, innerReq, upstreamURL)
|
||||
d.forwardHTTP(ctx, clientConn, innerReq)
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package deconnector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (d *Deconnector) forwardHTTP(clientConn net.Conn, req *http.Request, upstreamURL *url.URL) {
|
||||
upstreamConn, err := d.dialer.Dial()
|
||||
func (d *Deconnector) forwardHTTP(ctx context.Context, clientConn net.Conn, req *http.Request) {
|
||||
upstreamConn, err := d.dialer.Dial(ctx)
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("upstream dial failed")
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 502 Bad Gateway\r\n\r\n")
|
||||
@@ -18,6 +18,10 @@ func (d *Deconnector) forwardHTTP(clientConn net.Conn, req *http.Request, upstre
|
||||
}
|
||||
defer upstreamConn.Close()
|
||||
|
||||
if authHeader, ok := d.dialer.Auth(); ok {
|
||||
req.Header.Set("Proxy-Authorization", "Basic "+authHeader)
|
||||
}
|
||||
|
||||
if err := req.WriteProxy(upstreamConn); err != nil {
|
||||
d.app.Logger().WithError(err).Error("failed to write request")
|
||||
|
||||
@@ -33,5 +37,8 @@ func (d *Deconnector) forwardHTTP(clientConn net.Conn, req *http.Request, upstre
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
resp.Write(clientConn)
|
||||
err = resp.Write(clientConn)
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("failed to write response")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,18 +2,12 @@ package deconnector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (d *Deconnector) Handle(clientConn net.Conn) {
|
||||
upstreamURL, err := d.dialer.UpstreamURL()
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("failed to get upstream URL")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Deconnector) Handle(ctx context.Context, clientConn net.Conn) {
|
||||
defer clientConn.Close()
|
||||
|
||||
req, err := http.ReadRequest(bufio.NewReader(clientConn))
|
||||
@@ -26,9 +20,9 @@ func (d *Deconnector) Handle(clientConn net.Conn) {
|
||||
if req.Method == http.MethodConnect {
|
||||
_, port, _ := net.SplitHostPort(req.Host)
|
||||
if port == "443" {
|
||||
d.handleTunnel(clientConn, req.Host, upstreamURL)
|
||||
d.handleTunnel(ctx, clientConn, req.Host)
|
||||
} else {
|
||||
d.handleDeconnect(clientConn, req, upstreamURL)
|
||||
d.handleDeconnect(ctx, clientConn, req)
|
||||
}
|
||||
} else {
|
||||
req.RequestURI = ""
|
||||
@@ -42,6 +36,6 @@ func (d *Deconnector) Handle(clientConn net.Conn) {
|
||||
WithField("url", req.URL).
|
||||
Info("Forwarding HTTP request")
|
||||
|
||||
d.forwardHTTP(clientConn, req, upstreamURL)
|
||||
d.forwardHTTP(ctx, clientConn, req)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,18 @@ package deconnector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (d *Deconnector) handleTunnel(clientConn net.Conn, host string, upstreamURL *url.URL) {
|
||||
//nolint:funlen
|
||||
func (d *Deconnector) handleTunnel(ctx context.Context, clientConn net.Conn, host string) {
|
||||
d.app.Logger().WithField("host", host).Info("Handling CONNECT tunnel")
|
||||
|
||||
upstreamConn, err := d.dialer.Dial()
|
||||
upstreamConn, err := d.dialer.Dial(ctx)
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("upstream dial failed")
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 502 Bad Gateway\r\n\r\n")
|
||||
@@ -24,6 +25,10 @@ func (d *Deconnector) handleTunnel(clientConn net.Conn, host string, upstreamURL
|
||||
connectLine := fmt.Sprintf(
|
||||
"CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", host, host,
|
||||
)
|
||||
if authHeader, ok := d.dialer.Auth(); ok {
|
||||
connectLine += fmt.Sprintf("Proxy-Authorization: Basic %s\r\n", authHeader)
|
||||
}
|
||||
|
||||
fmt.Fprint(upstreamConn, connectLine)
|
||||
|
||||
resp, err := http.ReadResponse(bufio.NewReader(upstreamConn), nil)
|
||||
@@ -33,13 +38,40 @@ func (d *Deconnector) handleTunnel(clientConn net.Conn, host string, upstreamURL
|
||||
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 200 Connection established\r\n\r\n")
|
||||
|
||||
done := make(chan struct{}, 2)
|
||||
|
||||
go func() { io.Copy(upstreamConn, clientConn); done <- struct{}{} }()
|
||||
go func() { io.Copy(clientConn, upstreamConn); done <- struct{}{} }()
|
||||
go func() {
|
||||
defer func() {
|
||||
_ = upstreamConn.Close()
|
||||
_ = clientConn.Close()
|
||||
|
||||
done <- struct{}{}
|
||||
}()
|
||||
|
||||
if _, err := io.Copy(upstreamConn, clientConn); err != nil {
|
||||
d.app.Logger().
|
||||
WithError(err).
|
||||
Debug("client -> upstream copy stopped")
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
defer func() {
|
||||
_ = upstreamConn.Close()
|
||||
_ = clientConn.Close()
|
||||
|
||||
done <- struct{}{}
|
||||
}()
|
||||
|
||||
if _, err := io.Copy(clientConn, upstreamConn); err != nil {
|
||||
d.app.Logger().
|
||||
WithError(err).
|
||||
Debug("upstream -> client copy stopped")
|
||||
}
|
||||
}()
|
||||
|
||||
<-done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user