Skip to content

Commit

Permalink
Merge pull request #3 from libp2p/feat/relay/v0.1.0
Browse files Browse the repository at this point in the history
Implement circuit/relay/0.1.0 spec
  • Loading branch information
vyzo authored Jul 20, 2017
2 parents ec6366c + 1536c8a commit 42132ad
Show file tree
Hide file tree
Showing 14 changed files with 847 additions and 356 deletions.
15 changes: 8 additions & 7 deletions p2p/protocol/internal/circuitv1-deprecated/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
iconn "github.com/libp2p/go-libp2p-interface-conn"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
tpt "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)

type Conn struct {
inet.Stream
remoteMaddr ma.Multiaddr
remotePeer peer.ID
remote pstore.PeerInfo
transport tpt.Transport
}

var _ iconn.Conn = (*Conn)(nil)
Expand All @@ -37,12 +38,12 @@ func (n *NetAddr) String() string {
func (c *Conn) RemoteAddr() net.Addr {
return &NetAddr{
Relay: c.Conn().RemotePeer().Pretty(),
Remote: c.remotePeer.String(),
Remote: c.remote.ID.Pretty(),
}
}

func (c *Conn) RemoteMultiaddr() ma.Multiaddr {
a, err := ma.NewMultiaddr(fmt.Sprintf("%s/ipfs/%s/p2p-circuit/%s", c.remoteMaddr, c.remotePeer.Pretty(), c.Conn().RemotePeer()))
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)
}
Expand All @@ -63,15 +64,15 @@ func (c *Conn) LocalAddr() net.Addr {
}

func (c *Conn) Transport() tpt.Transport {
panic("does anyone really call this?")
return c.transport
}

func (c *Conn) LocalPeer() peer.ID {
return c.Conn().LocalPeer()
}

func (c *Conn) RemotePeer() peer.ID {
return c.remotePeer
return c.remote.ID
}

func (c *Conn) LocalPrivateKey() ic.PrivKey {
Expand All @@ -83,5 +84,5 @@ func (c *Conn) RemotePublicKey() ic.PubKey {
}

func (c *Conn) ID() string {
return "TODO: relay conn ID"
return iconn.ID(c)
}
27 changes: 14 additions & 13 deletions p2p/protocol/internal/circuitv1-deprecated/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ import (
"context"
"fmt"

peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
tpt "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr"
)

type Dialer Relay
var _ tpt.Dialer = (*RelayDialer)(nil)

func (d *Dialer) Relay() *Relay {
type RelayDialer Relay

func (d *RelayDialer) Relay() *Relay {
return (*Relay)(d)
}

func (r *Relay) Dialer() *Dialer {
return (*Dialer)(r)
func (r *Relay) Dialer() *RelayDialer {
return (*RelayDialer)(r)
}

func (d *RelayDialer) Dial(a ma.Multiaddr) (tpt.Conn, error) {
return d.DialContext(d.ctx, a)
}

func (d *Dialer) DialPeer(ctx context.Context, p peer.ID, a ma.Multiaddr) (tpt.Conn, error) {
func (d *RelayDialer) DialContext(ctx context.Context, a ma.Multiaddr) (tpt.Conn, error) {
if !d.Matches(a) {
return nil, fmt.Errorf("%s is not a relay address", a)
}
Expand All @@ -42,19 +47,15 @@ func (d *Dialer) DialPeer(ctx context.Context, p peer.ID, a ma.Multiaddr) (tpt.C
return nil, err
}

destp2p, err := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", p.Pretty()))
dinfo, err := pstore.InfoFromP2pAddr(destaddr)
if err != nil {
return nil, err
}

destp2p = destaddr.Encapsulate(destp2p)

d.Relay().host.Peerstore().AddAddrs(rinfo.ID, rinfo.Addrs, pstore.TempAddrTTL)

return d.Relay().Dial(ctx, rinfo.ID, destp2p)
return d.Relay().DialPeer(ctx, *rinfo, *dinfo)
}

func (d *Dialer) Matches(a ma.Multiaddr) bool {
func (d *RelayDialer) Matches(a ma.Multiaddr) bool {
_, err := a.ValueForProtocol(P_CIRCUIT)
return err == nil
}
44 changes: 26 additions & 18 deletions p2p/protocol/internal/circuitv1-deprecated/listen.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package relay

import (
"fmt"
"net"

pb "github.com/libp2p/go-libp2p-circuit/pb"

peer "github.com/libp2p/go-libp2p-peer"
tpt "github.com/libp2p/go-libp2p-transport"
filter "github.com/libp2p/go-maddr-filter"
Expand All @@ -17,42 +20,47 @@ func (l *RelayListener) Relay() *Relay {
return (*Relay)(l)
}

func (r *Relay) Listener() (tpt.Listener, error) {
return (*RelayListener)(r), nil
}

func (r *Relay) Matches(a ma.Multiaddr) bool {
return false
func (r *Relay) Listener() *RelayListener {
return (*RelayListener)(r)
}

func (l *RelayListener) Accept() (tpt.Conn, error) {
ctx := l.Relay().ctx
select {
case c := <-l.incoming:
log.Infof("accepted relay connection: %s", c.ID())
s := RelayStatus{
Code: StatusOK,
Message: "OK",
}
if err := s.WriteTo(c); err != nil {
err := l.Relay().writeResponse(c.Stream, pb.CircuitRelay_SUCCESS)
if err != nil {
log.Debugf("error writing relay response: %s", err.Error())
// this won't prevent the other side from continuing to write
// TODO fully close the stream when Reset is implemented
c.Stream.Close()
return nil, err
}

log.Infof("accepted relay connection: %s", c.ID())

return c, nil
case <-ctx.Done():
return nil, ctx.Err()
case <-l.ctx.Done():
return nil, l.ctx.Err()
}
}

func (l *RelayListener) Addr() net.Addr {
panic("oh no")
return &NetAddr{
Relay: "any",
Remote: "any",
}
}

func (l *RelayListener) Multiaddr() ma.Multiaddr {
panic("oh no")
a, err := ma.NewMultiaddr(fmt.Sprintf("/p2p-circuit/ipfs/%s", l.self.Pretty()))
if err != nil {
panic(err)
}
return a
}

func (l *RelayListener) LocalPeer() peer.ID {
return l.Relay().self
return l.self
}

func (l *RelayListener) SetAddrFilters(f *filter.Filters) {
Expand Down
10 changes: 10 additions & 0 deletions p2p/protocol/internal/circuitv1-deprecated/pb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PB = $(wildcard *.proto)
GO = $(PB:.proto=.pb.go)

all: $(GO)

%.pb.go: %.proto
protoc --gogo_out=. $<

clean:
rm *.pb.go
Loading

0 comments on commit 42132ad

Please sign in to comment.