Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mcamou committed Nov 26, 2024
1 parent c65ec1a commit ded3f47
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pkg/network/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const proxyProtocol = "/connect-proxy/1.0.0"
func NewProxy(host host.Host, listenAddr string, listenPort uint16, targetPort uint16) (*Proxy, error) {
addr := net.ParseIP(listenAddr)
if addr == nil {
return nil, fmt.Errorf("Invalid Listen Address: %s", listenAddr)
return nil, fmt.Errorf("invalid Listen Address: %s", listenAddr)
}

Check warning on line 48 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L44-L48

Added lines #L44 - L48 were not covered by tests

return &Proxy{host, addr, listenPort, targetPort}, nil

Check warning on line 50 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L50

Added line #L50 was not covered by tests
Expand Down Expand Up @@ -74,17 +74,17 @@ func (p *Proxy) handleTunnel(ctx context.Context, w http.ResponseWriter, req *ht
}

Check warning on line 74 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L69-L74

Added lines #L69 - L74 were not covered by tests

parts := strings.Split(req.URL.Host, ":")
peerId, err := peer.IDFromBytes([]byte(parts[0]))
peerID, err := peer.IDFromBytes([]byte(parts[0]))
if err != nil {
http.Error(w, fmt.Sprintf("Invalid peerID '%s'", parts[0]), http.StatusBadRequest)
logrus.Errorf("Invalid PeerID '%s' in host '%s'", parts[0], req.Host)
return
}

Check warning on line 82 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L76-L82

Added lines #L76 - L82 were not covered by tests

dest_stream, err := p.host.NewStream(ctx, peerId, proxyProtocol)
destStream, err := p.host.NewStream(ctx, peerID, proxyProtocol)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
logrus.Errorf("Error while creating stream to peer %s: %v", peerId, err)
logrus.Errorf("Error while creating stream to peer %s: %v", peerID, err)
return
}

Check warning on line 89 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L84-L89

Added lines #L84 - L89 were not covered by tests

Expand All @@ -95,7 +95,7 @@ func (p *Proxy) handleTunnel(ctx context.Context, w http.ResponseWriter, req *ht
return
}

Check warning on line 96 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L91-L96

Added lines #L91 - L96 were not covered by tests

client_conn, _, err := hijacker.Hijack()
clientConn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
logrus.Errorf("Error while hijacking: %v", err)
Expand All @@ -104,19 +104,26 @@ func (p *Proxy) handleTunnel(ctx context.Context, w http.ResponseWriter, req *ht

w.WriteHeader(http.StatusOK)

go transfer(dest_stream, client_conn)
go transfer(client_conn, dest_stream)
go transfer(destStream, clientConn)
go transfer(clientConn, destStream)

Check warning on line 108 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L105-L108

Added lines #L105 - L108 were not covered by tests
}

func transfer(destination io.WriteCloser, source io.ReadCloser) {
defer destination.Close()
defer source.Close()
defer closeStream(source)
defer closeStream(destination)

if _, err := io.Copy(destination, source); err != nil {
logrus.Errorf("Error during transfer: %v", err)
}

Check warning on line 117 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L111-L117

Added lines #L111 - L117 were not covered by tests
}

func closeStream(s io.Closer) {
err := s.Close()
if err != nil {
logrus.Errorf("Error closing stream: %v", err)
}

Check warning on line 124 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L120-L124

Added lines #L120 - L124 were not covered by tests
}

func (p *Proxy) Start(ctx context.Context) {
p.host.SetStreamHandler(proxyProtocol, p.streamHandler)

Expand All @@ -127,7 +134,7 @@ func (p *Proxy) Start(ctx context.Context) {
go p.handleTunnel(ctx, w, req)
}),

Check warning on line 135 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L127-L135

Added lines #L127 - L135 were not covered by tests
// Disable HTTP/2.
// TODO Is this even necessary, since we're not even doing HTTPS?
// TODO Is this necessary, since we're not doing HTTPS?
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}

Expand Down

0 comments on commit ded3f47

Please sign in to comment.