From 4865319105a7eb29fd35b6fdd1b9b5e601b95884 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Wed, 26 Sep 2018 18:04:41 +0200 Subject: [PATCH] Change transport address to not include /p2p part --- conn.go | 12 +++++++----- dial.go | 16 ++++++++-------- listen.go | 7 +------ relay_test.go | 14 ++++---------- 4 files changed, 20 insertions(+), 29 deletions(-) diff --git a/conn.go b/conn.go index b0afbd0..df39f11 100644 --- a/conn.go +++ b/conn.go @@ -35,12 +35,14 @@ func (c *Conn) RemoteAddr() net.Addr { } } +// TODO: is it okay to cast c.Conn().RemotePeer() into a multiaddr? might be "user input" func (c *Conn) RemoteMultiaddr() ma.Multiaddr { - a, err := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s/p2p-circuit/ipfs/%s", c.Conn().RemotePeer().Pretty(), c.remote.ID.Pretty())) - if err != nil { - panic(err) - } - return a + proto := ma.ProtocolWithCode(ma.P_P2P).Name + peerid := c.Conn().RemotePeer().Pretty() + p2paddr := ma.StringCast(fmt.Sprintf("/%s/%s", proto, peerid)) + + circaddr := ma.Cast(ma.CodeToVarint(P_CIRCUIT)) + return p2paddr.Encapsulate(circaddr) } func (c *Conn) LocalMultiaddr() ma.Multiaddr { diff --git a/dial.go b/dial.go index 3f021fa..ecfd23f 100644 --- a/dial.go +++ b/dial.go @@ -12,20 +12,20 @@ import ( ) func (d *RelayTransport) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (tpt.Conn, error) { - c, err := d.Relay().Dial(ctx, a) + c, err := d.Relay().Dial(ctx, a, p) if err != nil { return nil, err } return d.upgrader.UpgradeOutbound(ctx, d, c, p) } -func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr) (*Conn, error) { +func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (*Conn, error) { if !r.Matches(a) { return nil, fmt.Errorf("%s is not a relay address", a) } parts := ma.Split(a) - spl, _ := ma.NewMultiaddr("/p2p-circuit") + spl := ma.Cast(ma.CodeToVarint(P_CIRCUIT)) var relayaddr, destaddr ma.Multiaddr for i, p := range parts { @@ -36,9 +36,9 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr) (*Conn, error) { } } - dinfo, err := pstore.InfoFromP2pAddr(destaddr) - if err != nil { - return nil, err + dinfo := &pstore.PeerInfo{ID: p, Addrs: []ma.Multiaddr{}} + if len(destaddr.Bytes()) > 0 { + dinfo.Addrs = append(dinfo.Addrs, destaddr) } if len(relayaddr.Bytes()) == 0 { @@ -47,9 +47,9 @@ func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr) (*Conn, error) { } var rinfo *pstore.PeerInfo - rinfo, err = pstore.InfoFromP2pAddr(relayaddr) + rinfo, err := pstore.InfoFromP2pAddr(relayaddr) if err != nil { - return nil, err + return nil, fmt.Errorf("error parsing multiaddr '%s': %s", relayaddr.String(), err) } return r.DialPeer(ctx, *rinfo, *dinfo) diff --git a/listen.go b/listen.go index ee1f92e..0b1885b 100644 --- a/listen.go +++ b/listen.go @@ -1,7 +1,6 @@ package relay import ( - "fmt" "net" pb "github.com/libp2p/go-libp2p-circuit/pb" @@ -50,11 +49,7 @@ func (l *RelayListener) Addr() net.Addr { } func (l *RelayListener) Multiaddr() ma.Multiaddr { - a, err := ma.NewMultiaddr(fmt.Sprintf("/p2p-circuit/ipfs/%s", l.self.Pretty())) - if err != nil { - panic(err) - } - return a + return ma.Cast(ma.CodeToVarint(P_CIRCUIT)) } func (l *RelayListener) Close() error { diff --git a/relay_test.go b/relay_test.go index 04feb6c..6fdc1d6 100644 --- a/relay_test.go +++ b/relay_test.go @@ -204,15 +204,12 @@ func TestBasicRelayDial(t *testing.T) { con.Close() }() - addr, err := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s/p2p-circuit/ipfs/%s", hosts[1].ID().Pretty(), hosts[2].ID().Pretty())) - if err != nil { - t.Fatal(err) - } + addr := ma.StringCast(fmt.Sprintf("/ipfs/%s/p2p-circuit", hosts[1].ID().Pretty())) rctx, rcancel := context.WithTimeout(ctx, time.Second) defer rcancel() - con, err := r1.Dial(rctx, addr) + con, err := r1.Dial(rctx, addr, hosts[2].ID()) if err != nil { t.Fatal(err) } @@ -262,15 +259,12 @@ func TestUnspecificRelayDial(t *testing.T) { con.Close() }() - addr, err := ma.NewMultiaddr(fmt.Sprintf("/p2p-circuit/ipfs/%s", hosts[2].ID().Pretty())) - if err != nil { - t.Fatal(err) - } + addr := ma.StringCast(fmt.Sprintf("/p2p-circuit")) rctx, rcancel := context.WithTimeout(ctx, time.Second) defer rcancel() - con, err := r1.Dial(rctx, addr) + con, err := r1.Dial(rctx, addr, hosts[2].ID()) if err != nil { t.Fatal(err) }