This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
WIP: Add support for new Protocol and Multiaddr format #26
Open
albrow
wants to merge
1
commit into
libp2p:webrtc-signal-protocol
Choose a base branch
from
albrow:generic-signal-protocol
base: webrtc-signal-protocol
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package libp2pwebrtcdirect | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
peer "github.com/libp2p/go-libp2p-peer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
manet "github.com/multiformats/go-multiaddr-net" | ||
) | ||
|
||
func newMultiaddrFromNetAddr(netAddr net.Addr, peerID peer.ID) (ma.Multiaddr, error) { | ||
tcpMa, err := manet.FromNetAddr(netAddr) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed create ma: %v", err) | ||
} | ||
httpMa := tcpMa.Encapsulate(httpma) | ||
webrtcMa, err := ma.NewMultiaddr("/p2p-webrtc/" + peer.IDB58Encode(peerID)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
maAddr := httpMa.Encapsulate(webrtcMa) | ||
return maAddr, nil | ||
} | ||
|
||
func getPeerIDFromMultiAddr(addr ma.Multiaddr) (peer.ID, error) { | ||
idString, err := addr.ValueForProtocol(protoCode) | ||
if err != nil { | ||
return "", err | ||
} | ||
return peer.IDB58Decode(idString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package libp2pwebrtcdirect | ||
|
||
import ( | ||
"fmt" | ||
|
||
peer "github.com/libp2p/go-libp2p-peer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func init() { | ||
if err := ma.AddProtocol(Protocol); err != nil { | ||
panic(fmt.Errorf("error registering p2p-webrtc protocol: %s", err)) | ||
} | ||
} | ||
|
||
const protoCode = 0x1992 | ||
|
||
var Protocol = ma.Protocol{ | ||
Name: "p2p-webrtc", | ||
Code: protoCode, | ||
VCode: ma.CodeToVarint(protoCode), | ||
Size: -1, | ||
Transcoder: Transcoder, | ||
} | ||
|
||
var Transcoder ma.Transcoder = transcoder{} | ||
|
||
// transcoder handles encoding/decoding peer ids as base58. | ||
type transcoder struct { | ||
strtobyte func(string) ([]byte, error) | ||
bytetostr func([]byte) (string, error) | ||
validbyte func([]byte) error | ||
} | ||
|
||
func (t transcoder) StringToBytes(s string) ([]byte, error) { | ||
id, err := peer.IDB58Decode(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []byte(id), nil | ||
} | ||
|
||
func (t transcoder) BytesToString(b []byte) (string, error) { | ||
id, err := peer.IDFromBytes(b) | ||
if err != nil { | ||
return "", err | ||
} | ||
return peer.IDB58Encode(id), nil | ||
} | ||
|
||
func (t transcoder) ValidateBytes(b []byte) error { | ||
_, err := peer.IDFromBytes(b) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,19 @@ import ( | |
|
||
peer "github.com/libp2p/go-libp2p-peer" | ||
tpt "github.com/libp2p/go-libp2p-transport" | ||
transport "github.com/libp2p/go-libp2p-transport" | ||
smux "github.com/libp2p/go-stream-muxer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
mafmt "github.com/multiformats/go-multiaddr-fmt" | ||
"github.com/pions/webrtc" | ||
) | ||
|
||
// Ensure that our Transport implements the correct interface. | ||
var _ transport.Transport = &Transport{} | ||
|
||
// Fmt is the Multiaddress format for WebRTC | ||
var Fmt = mafmt.And(mafmt.HTTP, mafmt.Base(Protocol.Code)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, we're only supporting signaling over HTTP. In the future it should be possible to support many different transports. Hopefully we can rely on existing code in libp2p to set up a connection (likely a |
||
|
||
// Transport is the WebRTC transport. | ||
type Transport struct { | ||
webrtcOptions webrtc.Configuration | ||
|
@@ -39,7 +46,7 @@ func NewTransport(webrtcOptions webrtc.Configuration, muxer smux.Transport) *Tra | |
// CanDial returns true if this transport believes it can dial the given | ||
// multiaddr. | ||
func (t *Transport) CanDial(addr ma.Multiaddr) bool { | ||
return mafmt.WebRTCDirect.Matches(addr) | ||
return Fmt.Matches(addr) | ||
} | ||
|
||
// Dial dials the peer at the remote address. | ||
|
@@ -48,11 +55,12 @@ func (t *Transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp | |
return nil, fmt.Errorf("can't dial address %s", raddr) | ||
} | ||
|
||
// TODO: Check that the peer id in raddr is equal to p. | ||
|
||
cfg, err := newConnConfig(t, raddr, false) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get dial args: %v", err) | ||
} | ||
|
||
cfg.remoteID = p | ||
|
||
conn, err := dial(ctx, cfg) | ||
|
@@ -73,6 +81,11 @@ func (t *Transport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) { | |
if err != nil { | ||
return nil, fmt.Errorf("failed to get dial args: %v", err) | ||
} | ||
localID, err := getPeerIDFromMultiAddr(laddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.localID = localID | ||
|
||
l, err := newListener(cfg) | ||
if err != nil { | ||
|
@@ -84,14 +97,14 @@ func (t *Transport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) { | |
|
||
// Protocols returns the list of terminal protocols this transport can dial. | ||
func (t *Transport) Protocols() []int { | ||
return []int{ma.P_P2P_WEBRTC_DIRECT} | ||
return []int{protoCode} | ||
} | ||
|
||
// Proxy always returns false for the TCP transport. | ||
// Proxy always returns false for the WebRTC transport. | ||
func (t *Transport) Proxy() bool { | ||
return false | ||
} | ||
|
||
func (t *Transport) String() string { | ||
return "p2p-webrtc-direct" | ||
return "p2p-webrtc" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ import ( | |
|
||
var log = logging.Logger("webrtcdirect-tpt") | ||
|
||
var webrtcma, _ = ma.NewMultiaddr("/p2p-webrtc-direct") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the new Multiaddress format requires a peer id after |
||
var httpma, _ = ma.NewMultiaddr("/http") | ||
|
||
var _ tpt.Transport = &Transport{} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was seeing some errors about a checksum mismatch. Manually changing this line fixed them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this can be fixed (dependency removed entirely) by dropping the examples.