Skip to content

Commit

Permalink
Resolve remote address for Tunnel (fail early)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Feb 22, 2022
1 parent d76b71c commit 25836fd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
6 changes: 3 additions & 3 deletions api/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ deps:
owner: googleapis
repository: googleapis
branch: main
commit: e98790c58c414f10952900c9efc42ed6
digest: b1-KBCXEzQLhDPo4oBwUxEZTNVylAY4drJ7oqz9x0wy6-Q=
create_time: 2022-02-19T15:03:38.153099Z
commit: bdfe2a6dbea44b2aadd202aef3b02139
digest: b1-fsLQE8NjJb-wMtT1cRL6GOXR5WX__qNZRUzlZUo0VBs=
create_time: 2022-02-20T15:03:24.321088Z
21 changes: 12 additions & 9 deletions tunnel_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package connect
import (
"context"
"errors"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
"io"
"net"
"sync"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)

// InboundTunnel represents an inbound tunnel.
Expand Down Expand Up @@ -71,6 +74,13 @@ func (s *TunnelService) Valid() error {
// Tunnel implements TunnelServiceServer.
// See the proto definition for more documentation.
func (s *TunnelService) Tunnel(stream TunnelService_TunnelServer) error {
// Get address of client
var remoteAddr net.Addr
if p, ok := peer.FromContext(stream.Context()); !ok {
return status.Error(codes.Internal, "could not resolve address of client")
} else {
remoteAddr = p.Addr
}
// Create inbound tunnel from stream
ctx, cancel := context.WithCancel(stream.Context())
defer cancel()
Expand All @@ -83,13 +93,6 @@ func (s *TunnelService) Tunnel(stream TunnelService_TunnelServer) error {
conn: func() TunnelConn {
// initialize tunnel conn once we need it
initConnOnce.Do(func() {
var remoteAddr net.Addr
p, ok := peer.FromContext(stream.Context())
if ok {
remoteAddr = p.Addr
} else {
remoteAddr = connectAddr("unknown")
}
r, w := tunnelServerRW(ctx, stream)
conn = newTunnelConn(s.LocalAddr, remoteAddr, r, w, closeFn)
})
Expand Down

0 comments on commit 25836fd

Please sign in to comment.