Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag relay hops in relay implementations #1188

Merged
merged 4 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions p2p/protocol/circuitv1/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const (
ConnectTimeout = 30 * time.Second
HandshakeTimeout = time.Minute

relayHopTag = "relay-v1-hop"
relayHopTagValue = 2

maxMessageSize = 4096
)

Expand Down Expand Up @@ -158,15 +161,15 @@ func (r *Relay) handleHopStream(s network.Stream, msg *pb.CircuitRelay) {
}

r.active++
r.conns[src.ID]++
r.conns[dest.ID]++
r.addConn(src.ID)
r.addConn(src.ID)
r.mx.Unlock()

cleanup := func() {
r.mx.Lock()
r.active--
r.conns[src.ID]--
r.conns[dest.ID]--
r.rmConn(src.ID)
r.rmConn(dest.ID)
r.mx.Unlock()
}

Expand Down Expand Up @@ -262,6 +265,26 @@ func (r *Relay) handleHopStream(s network.Stream, msg *pb.CircuitRelay) {
go r.relayConn(bs, s, dest.ID, src.ID, done)
}

func (r *Relay) addConn(p peer.ID) {
conns := r.conns[p]
conns++
r.conns[p] = conns
vyzo marked this conversation as resolved.
Show resolved Hide resolved
if conns == 1 {
r.host.ConnManager().TagPeer(p, relayHopTag, relayHopTagValue)
}
}

func (r *Relay) rmConn(p peer.ID) {
conns := r.conns[p]
conns--
if conns > 0 {
r.conns[p] = conns
} else {
delete(r.conns, p)
r.host.ConnManager().UntagPeer(p, relayHopTag)
}
}

func (r *Relay) relayConn(src, dest network.Stream, srcID, destID peer.ID, done func()) {
defer done()

Expand Down
33 changes: 28 additions & 5 deletions p2p/protocol/circuitv2/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (
ConnectTimeout = 30 * time.Second
HandshakeTimeout = time.Minute

relayHopTag = "relay-v2-hop"
relayHopTagValue = 2

maxMessageSize = 4096
)

Expand Down Expand Up @@ -215,23 +218,23 @@ func (r *Relay) handleConnect(s network.Stream, msg *pbv2.HopMessage) {
r.handleError(s, pbv2.Status_RESOURCE_LIMIT_EXCEEDED)
return
}
r.conns[src]++

destConns := r.conns[dest.ID]
if destConns >= r.rc.MaxCircuits {
r.conns[src]--
r.mx.Unlock()
log.Debugf("refusing connection from %s to %s; too many connecitons to %s", src, dest.ID, dest.ID)
r.handleError(s, pbv2.Status_RESOURCE_LIMIT_EXCEEDED)
return
}
r.conns[dest.ID]++

r.addConn(src)
r.addConn(dest.ID)
r.mx.Unlock()

cleanup := func() {
r.mx.Lock()
r.conns[src]--
r.conns[dest.ID]--
r.rmConn(src)
r.rmConn(dest.ID)
r.mx.Unlock()
}

Expand Down Expand Up @@ -339,6 +342,26 @@ func (r *Relay) handleConnect(s network.Stream, msg *pbv2.HopMessage) {
}
}

func (r *Relay) addConn(p peer.ID) {
conns := r.conns[p]
conns++
r.conns[p] = conns
if conns == 1 {
r.host.ConnManager().TagPeer(p, relayHopTag, relayHopTagValue)
}
}

func (r *Relay) rmConn(p peer.ID) {
conns := r.conns[p]
conns--
if conns > 0 {
r.conns[p] = conns
} else {
delete(r.conns, p)
r.host.ConnManager().UntagPeer(p, relayHopTag)
}
}

func (r *Relay) relayLimited(src, dest network.Stream, srcID, destID peer.ID, limit int64, done func()) {
defer done()

Expand Down