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

return the full address from conn.RemoteMultiaddr #80

Merged
merged 3 commits into from
Sep 18, 2019
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
15 changes: 9 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ func (c *Conn) untagHop() {

// TODO: is it okay to cast c.Conn().RemotePeer() into a multiaddr? might be "user input"
func (c *Conn) RemoteMultiaddr() ma.Multiaddr {
proto := ma.ProtocolWithCode(ma.P_P2P).Name
peerid := c.stream.Conn().RemotePeer().Pretty()
p2paddr := ma.StringCast(fmt.Sprintf("/%s/%s", proto, peerid))

circaddr := ma.Cast(ma.CodeToVarint(P_CIRCUIT))
return p2paddr.Encapsulate(circaddr)
// TODO: We should be able to do this directly without converting to/from a string.
relayAddr, err := ma.NewComponent(
ma.ProtocolWithCode(ma.P_P2P).Name,
c.stream.Conn().RemotePeer().Pretty(),
)
if err != nil {
panic(err)
}
return ma.Join(c.stream.Conn().RemoteMultiaddr(), relayAddr, circuitAddr)
}

func (c *Conn) LocalMultiaddr() ma.Multiaddr {
Expand Down
25 changes: 11 additions & 14 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,25 @@ func (d *RelayTransport) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (t
}

func (r *Relay) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (*Conn, error) {
if !r.Matches(a) {
// split /a/p2p-circuit/b into (/a, /p2p-circuit/b)
relayaddr, destaddr := ma.SplitFunc(a, func(c ma.Component) bool {
return c.Protocol().Code == P_CIRCUIT
})

// If the address contained no /p2p-circuit part, the second part is nil.
if destaddr == nil {
return nil, fmt.Errorf("%s is not a relay address", a)
}
parts := ma.Split(a)

spl := ma.Cast(ma.CodeToVarint(P_CIRCUIT))

var relayaddr, destaddr ma.Multiaddr
for i, p := range parts {
if p.Equal(spl) {
relayaddr = ma.Join(parts[:i]...)
destaddr = ma.Join(parts[i+1:]...)
break
}
}
// Strip the /p2p-circuit prefix from the destaddr.
_, destaddr = ma.SplitFirst(destaddr)

dinfo := &peer.AddrInfo{ID: p, Addrs: []ma.Multiaddr{}}
if len(destaddr.Bytes()) > 0 {
if destaddr != nil {
dinfo.Addrs = append(dinfo.Addrs, destaddr)
}

if len(relayaddr.Bytes()) == 0 {
if relayaddr == nil {
// unspecific relay address, try dialing using known hop relays
return r.tryDialRelays(ctx, *dinfo)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ require (
github.com/multiformats/go-multiaddr v0.0.4
github.com/multiformats/go-multiaddr-net v0.0.1
)

go 1.12
2 changes: 1 addition & 1 deletion listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (l *RelayListener) Addr() net.Addr {
}

func (l *RelayListener) Multiaddr() ma.Multiaddr {
return ma.Cast(ma.CodeToVarint(P_CIRCUIT))
return circuitAddr
}

func (l *RelayListener) Close() error {
Expand Down
2 changes: 1 addition & 1 deletion relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ func TestRelayReset(t *testing.T) {

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

hosts := getNetHosts(t, ctx, 3)

Expand All @@ -213,6 +212,7 @@ func TestBasicRelayDial(t *testing.T) {
)

defer func() {
cancel()
<-done
if conn1 != nil {
conn1.Close()
Expand Down
3 changes: 3 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ var Protocol = ma.Protocol{
VCode: ma.CodeToVarint(P_CIRCUIT),
}

var circuitAddr ma.Multiaddr

func init() {
ma.AddProtocol(Protocol)
circuitAddr = ma.Cast(Protocol.VCode)
}

var _ transport.Transport = (*RelayTransport)(nil)
Expand Down