Make sure that closing one direction closes the other, too. (#159)

* Make sure that closing one direction closes the other, too.

* Pacify linter.
This commit is contained in:
Christian Speckner 2025-01-31 17:09:16 +01:00 committed by GitHub
parent 47cd451c80
commit 7bb1be2d20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 57 deletions

26
http.go
View file

@ -10,8 +10,6 @@ import (
"net"
"net/http"
"strings"
"github.com/sourcegraph/conc"
)
const proxyAuthHeaderKey = "Proxy-Authorization"
@ -32,7 +30,7 @@ func (s *HTTPServer) authenticate(req *http.Request) (int, error) {
auth := req.Header.Get(proxyAuthHeaderKey)
if auth == "" {
return http.StatusProxyAuthRequired, fmt.Errorf(http.StatusText(http.StatusProxyAuthRequired))
return http.StatusProxyAuthRequired, fmt.Errorf("%s", http.StatusText(http.StatusProxyAuthRequired))
}
enc := strings.TrimPrefix(auth, "Basic ")
@ -131,17 +129,19 @@ func (s *HTTPServer) serve(conn net.Conn) {
log.Println("dial proxy failed: peer nil")
return
}
go func() {
wg := conc.NewWaitGroup()
wg.Go(func() {
_, err = io.Copy(conn, peer)
_ = conn.Close()
})
wg.Go(func() {
_, err = io.Copy(peer, conn)
_ = peer.Close()
})
wg.Wait()
defer conn.Close()
defer peer.Close()
_, _ = io.Copy(conn, peer)
}()
go func() {
defer conn.Close()
defer peer.Close()
_, _ = io.Copy(peer, conn)
}()
}