Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

feat: remove relay discovery and unspecified relay dialing #101

Merged
merged 2 commits into from
Apr 1, 2020
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
46 changes: 7 additions & 39 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package relay
import (
"context"
"fmt"
"math/rand"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
Expand All @@ -30,6 +29,13 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (*Conn, err
return nil, fmt.Errorf("%s is not a relay address", a)
}

if relayaddr == nil {
return nil, fmt.Errorf(
"can't dial a p2p-circuit without specifying a relay: %s",
a,
)
}

// Strip the /p2p-circuit prefix from the destaddr.
_, destaddr = ma.SplitFirst(destaddr)

Expand All @@ -38,11 +44,6 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (*Conn, err
dinfo.Addrs = append(dinfo.Addrs, destaddr)
}

if relayaddr == nil {
// unspecific relay address, try dialing using known hop relays
return r.tryDialRelays(ctx, *dinfo)
}

var rinfo *peer.AddrInfo
rinfo, err := peer.AddrInfoFromP2pAddr(relayaddr)
if err != nil {
Expand All @@ -51,36 +52,3 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (*Conn, err

return r.DialPeer(ctx, *rinfo, *dinfo)
}

func (r *Relay) tryDialRelays(ctx context.Context, dinfo peer.AddrInfo) (*Conn, error) {
var relays []peer.ID
r.mx.Lock()
for p := range r.relays {
relays = append(relays, p)
}
r.mx.Unlock()

// shuffle list of relays, avoid overloading a specific relay
for i := range relays {
j := rand.Intn(i + 1)
relays[i], relays[j] = relays[j], relays[i]
}

for _, relay := range relays {
if len(r.host.Network().ConnsToPeer(relay)) == 0 {
continue
}

rctx, cancel := context.WithTimeout(ctx, HopConnectTimeout)
c, err := r.DialPeer(rctx, peer.AddrInfo{ID: relay}, dinfo)
cancel()

if err == nil {
return c, nil
}

log.Debugf("error opening relay connection through %s: %s", dinfo.ID, err.Error())
}

return nil, fmt.Errorf("Failed to dial through %d known relay hosts", len(relays))
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UG
github.com/libp2p/go-flow-metrics v0.0.1 h1:0gxuFd2GuK7IIP5pKljLwps6TvcuYgvG7Atqi3INF5s=
github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8=
github.com/libp2p/go-flow-metrics v0.0.2/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM=
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
github.com/libp2p/go-libp2p-blankhost v0.1.4 h1:I96SWjR4rK9irDHcHq3XHN6hawCRTPUADzkJacgZLvk=
github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU=
Expand Down Expand Up @@ -287,6 +288,7 @@ go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.1 h1:8dP3SGL7MPB94crU3bEPplMPe83FI4EouesJUeFHv50=
go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA=
go.opencensus.io v0.22.2 h1:75k/FF0Q2YM8QYo07VPddOLBslDt1MZOdEslOHvmzAs=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
54 changes: 0 additions & 54 deletions notify.go

This file was deleted.

56 changes: 28 additions & 28 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -45,37 +44,50 @@ type Relay struct {
ctx context.Context
self peer.ID

active bool
hop bool
discovery bool
active bool
hop bool

incoming chan *Conn

relays map[peer.ID]struct{}
mx sync.Mutex

// atomic counters
streamCount int32
liveHopCount int32
}

// RelayOpts are options for configuring the relay transport.
type RelayOpt int
type RelayOpt func(*Relay) error

var (
// OptActive configures the relay transport to actively establish
// outbound connections on behalf of clients. You probably don't want to
// enable this unless you know what you're doing.
OptActive = RelayOpt(0)
OptActive RelayOpt = func(r *Relay) error {
r.active = true
return nil
}
// OptHop configures the relay transport to accept requests to relay
// traffic on behalf of third-parties. Unless OptActive is specified,
// this will only relay traffic between peers already connected to this
// node.
OptHop = RelayOpt(1)
// OptDiscovery configures this relay transport to discover new relays
// by probing every new peer. You almost _certainly_ don't want to
// enable this.
OptDiscovery = RelayOpt(2)
OptHop = func(r *Relay) error {
r.hop = true
return nil
}
// OptDiscovery is a no-op. It was introduced as a way to probe new
// peers to see if they were willing to act as a relays. However, in
// practice, it's useless. While it does test to see if these peers are
// relays, it doesn't (and can't), check to see if these peers are
// _active_ relays (i.e., will actively dial the target peer).
//
// This option may be re-enabled in the future but for now you shouldn't
// use it.
OptDiscovery = func(r *Relay) error {
log.Errorf(
"circuit.OptDiscovery is now a no-op: %s",
"dialing peers with a random relay is no longer supported",
)
return nil
}
)

type RelayError struct {
Expand All @@ -94,28 +106,16 @@ func NewRelay(ctx context.Context, h host.Host, upgrader *tptu.Upgrader, opts ..
ctx: ctx,
self: h.ID(),
incoming: make(chan *Conn),
relays: make(map[peer.ID]struct{}),
}

for _, opt := range opts {
switch opt {
case OptActive:
r.active = true
case OptHop:
r.hop = true
case OptDiscovery:
r.discovery = true
default:
return nil, fmt.Errorf("unrecognized option: %d", opt)
if err := opt(r); err != nil {
return nil, err
}
}

h.SetStreamHandler(ProtoID, r.handleNewStream)

if r.discovery {
h.Network().Notify(r.notifiee())
}

return r, nil
}

Expand Down
57 changes: 16 additions & 41 deletions relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestBasicRelay(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)
r1 := newTestRelay(t, ctx, hosts[0])

newTestRelay(t, ctx, hosts[1], OptHop)

Expand Down Expand Up @@ -143,7 +143,7 @@ func TestRelayReset(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)
r1 := newTestRelay(t, ctx, hosts[0])

newTestRelay(t, ctx, hosts[1], OptHop)

Expand Down Expand Up @@ -201,7 +201,7 @@ func TestBasicRelayDial(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)
r1 := newTestRelay(t, ctx, hosts[0])

_ = newTestRelay(t, ctx, hosts[1], OptHop)
r3 := newTestRelay(t, ctx, hosts[2])
Expand Down Expand Up @@ -263,13 +263,12 @@ func TestBasicRelayDial(t *testing.T) {
}
}

func TestUnspecificRelayDial(t *testing.T) {
func TestUnspecificRelayDialFails(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hosts := getNetHosts(t, ctx, 3)

r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)
r1 := newTestRelay(t, ctx, hosts[0])

newTestRelay(t, ctx, hosts[1], OptHop)

Expand All @@ -281,36 +280,22 @@ func TestUnspecificRelayDial(t *testing.T) {
time.Sleep(100 * time.Millisecond)

var (
conn1, conn2 net.Conn
done = make(chan struct{})
done = make(chan struct{})
)

defer func() {
cancel()
<-done
if conn1 != nil {
conn1.Close()
}
if conn2 != nil {
conn2.Close()
}
}()

msg := []byte("relay works!")
go func() {
defer close(done)
list := r3.Listener()

var err error
conn1, err = list.Accept()
if err != nil {
t.Error(err)
return
}

_, err = conn1.Write(msg)
if err != nil {
t.Error(err)
return
_, err = list.Accept()
if err == nil {
t.Error("should not have received relay connection")
}
}()

Expand All @@ -320,19 +305,9 @@ func TestUnspecificRelayDial(t *testing.T) {
defer rcancel()

var err error
conn2, err = r1.Dial(rctx, addr, hosts[2].ID())
if err != nil {
t.Fatal(err)
}

data := make([]byte, len(msg))
_, err = io.ReadFull(conn2, data)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(data, msg) {
t.Fatal("message was incorrect:", string(data))
_, err = r1.Dial(rctx, addr, hosts[2].ID())
if err == nil {
t.Fatal("expected dial with unspecified relay address to fail, even if we're connected to a relay")
}
}

Expand All @@ -347,7 +322,7 @@ func TestRelayThroughNonHop(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)
r1 := newTestRelay(t, ctx, hosts[0])

newTestRelay(t, ctx, hosts[1])

Expand Down Expand Up @@ -384,7 +359,7 @@ func TestRelayNoDestConnection(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)
r1 := newTestRelay(t, ctx, hosts[0])

newTestRelay(t, ctx, hosts[1], OptHop)

Expand Down Expand Up @@ -419,7 +394,7 @@ func TestActiveRelay(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)
r1 := newTestRelay(t, ctx, hosts[0])

newTestRelay(t, ctx, hosts[1], OptHop, OptActive)

Expand Down
Loading