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

Commit

Permalink
Changed to endpoint selection
Browse files Browse the repository at this point in the history
Now you can use ws/endpoint to choose your end point.
  • Loading branch information
Jorropo committed Jun 13, 2019
1 parent 6efd965 commit 303a9e8
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 33 deletions.
35 changes: 30 additions & 5 deletions addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"net/url"
"strings"

ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
Expand All @@ -22,10 +23,11 @@ func (addr *Addr) Network() string {
}

// NewAddr creates a new Addr using the given host string
func NewAddr(host string) *Addr {
func NewAddr(host string, path string) *Addr {
return &Addr{
URL: &url.URL{
Host: host,
Path: path,
},
}
}
Expand All @@ -35,14 +37,32 @@ func ConvertWebsocketMultiaddrToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
if err != nil {
return nil, err
}
path, err := maddr.ValueForProtocol(WsProtocol.Code)
if err != nil {
return nil, err
}

return NewAddr(host), nil
return NewAddr(host, path), nil
}

var notWebsocketError = fmt.Errorf("not a websocket address")

func ParseWebsocketNetAddr(a net.Addr) (ma.Multiaddr, error) {
wsa, ok := a.(*Addr)
if !ok {
return nil, fmt.Errorf("not a websocket address")
return nil, notWebsocketError
}

pathl := strings.SplitN(wsa.Path, "/", 2)
path := ""
if len(pathl) == 1 {
path = wsa.Path
} else {
path = pathl[1]
}

if strings.Contains(path, "/") {
return nil, fmt.Errorf("Endpoint must be under root, not %s", path)
}

tcpaddr, err := net.ResolveTCPAddr("tcp", wsa.Host)
Expand All @@ -55,7 +75,7 @@ func ParseWebsocketNetAddr(a net.Addr) (ma.Multiaddr, error) {
return nil, err
}

wsma, err := ma.NewMultiaddr("/ws")
wsma, err := ma.NewMultiaddr("/ws/" + path)
if err != nil {
return nil, err
}
Expand All @@ -69,5 +89,10 @@ func parseMultiaddr(a ma.Multiaddr) (string, error) {
return "", err
}

return "ws://" + host, nil
endpoint, err := a.ValueForProtocol(WsProtocol.Code)
if err != nil {
return "", err
}

return "ws://" + host + "/" + endpoint, nil
}
43 changes: 33 additions & 10 deletions addrs_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package websocket

import (
"encoding/hex"
"net/url"
"testing"

ma "github.com/multiformats/go-multiaddr"
)

func TestMultiaddrParsing(t *testing.T) {
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555/ws")
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555/ws/libp2pEndpoint")
if err != nil {
t.Fatal(err)
}
Expand All @@ -17,8 +18,8 @@ func TestMultiaddrParsing(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if wsaddr != "ws://127.0.0.1:5555" {
t.Fatalf("expected ws://127.0.0.1:5555, got %s", wsaddr)
if wsaddr != "ws://127.0.0.1:5555/libp2pEndpoint" {
t.Fatalf("expected ws://127.0.0.1:5555/libp2pEndpoint, got %s", wsaddr)
}
}

Expand All @@ -31,25 +32,25 @@ func (addr *httpAddr) Network() string {
}

func TestParseWebsocketNetAddr(t *testing.T) {
notWs := &httpAddr{&url.URL{Host: "http://127.0.0.1:1234"}}
notWs := &httpAddr{&url.URL{Host: "http://127.0.0.1:1234/libp2pEndpoint"}}
_, err := ParseWebsocketNetAddr(notWs)
if err.Error() != "not a websocket address" {
t.Fatalf("expect \"not a websocket address\", got \"%s\"", err)
}

wsAddr := NewAddr("127.0.0.1:5555")
wsAddr := NewAddr("127.0.0.1:5555", "libp2pEndpoint")
parsed, err := ParseWebsocketNetAddr(wsAddr)
if err != nil {
t.Fatal(err)
}

if parsed.String() != "/ip4/127.0.0.1/tcp/5555/ws" {
t.Fatalf("expected \"/ip4/127.0.0.1/tcp/5555/ws\", got \"%s\"", parsed.String())
if parsed.String() != "/ip4/127.0.0.1/tcp/5555/ws/libp2pEndpoint" {
t.Fatalf("expected \"/ip4/127.0.0.1/tcp/5555/ws/libp2pEndpoint\", got \"%s\"", parsed.String())
}
}

func TestConvertWebsocketMultiaddrToNetAddr(t *testing.T) {
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555/ws")
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555/ws/libp2pEndpoint")
if err != nil {
t.Fatal(err)
}
Expand All @@ -58,10 +59,32 @@ func TestConvertWebsocketMultiaddrToNetAddr(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if wsaddr.String() != "//127.0.0.1:5555" {
t.Fatalf("expected //127.0.0.1:5555, got %s", wsaddr)
if wsaddr.String() != "//127.0.0.1:5555/libp2pEndpoint" {
t.Fatalf("expected //127.0.0.1:5555/libp2pEndpoint, got %s", wsaddr)
}
if wsaddr.Network() != "websocket" {
t.Fatalf("expected network: \"websocket\", got \"%s\"", wsaddr.Network())
}
}

func TestTranscoder(t *testing.T) {
bytes, err := WsTranscoder.StringToBytes("libp2pEndpoint")
if err != nil {
t.Fatal(err)
}

str, err := WsTranscoder.BytesToString(bytes)
if err != nil {
t.Fatal(err)
}

if str != "libp2pEndpoint" {
t.Fatalf("excepted \"libp2pEndpoint\" but got \"%s\"", str)
}

// Now testing with a /
bytes, err = WsTranscoder.StringToBytes("libp2p/endpoint")
if bytes != nil || err == nil {
t.Fatalf("endpoint with a / shouldn't works, but here got : %s", hex.Dump(bytes))
}
}
8 changes: 5 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Conn struct {
DefaultMessageType int
reader io.Reader
closeOnce sync.Once
Path string
}

func (c *Conn) Read(b []byte) (int, error) {
Expand Down Expand Up @@ -97,11 +98,11 @@ func (c *Conn) Close() error {
}

func (c *Conn) LocalAddr() net.Addr {
return NewAddr(c.Conn.LocalAddr().String())
return NewAddr(c.Conn.LocalAddr().String(), c.Path)
}

func (c *Conn) RemoteAddr() net.Addr {
return NewAddr(c.Conn.RemoteAddr().String())
return NewAddr(c.Conn.RemoteAddr().String(), c.Path)
}

func (c *Conn) SetDeadline(t time.Time) error {
Expand All @@ -121,9 +122,10 @@ func (c *Conn) SetWriteDeadline(t time.Time) error {
}

// NewConn creates a Conn given a regular gorilla/websocket Conn.
func NewConn(raw *ws.Conn) *Conn {
func NewConn(raw *ws.Conn, path string) *Conn {
return &Conn{
Conn: raw,
DefaultMessageType: ws.BinaryMessage,
Path: path,
}
}
103 changes: 103 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,109 @@ require (
github.com/libp2p/go-libp2p-testing v0.0.3
github.com/libp2p/go-libp2p-transport-upgrader v0.1.1
github.com/multiformats/go-multiaddr v0.0.4
github.com/multiformats/go-multiaddr-fmt v0.0.1
github.com/multiformats/go-multiaddr-net v0.0.1
github.com/whyrusleeping/mafmt v1.2.8
)

replace github.com/libp2p/go-addr-util => ../go-addr-util

replace github.com/libp2p/go-buffer-pool => ../go-buffer-pool

replace github.com/libp2p/go-conn-security-multistream => ../go-conn-security-multistream

replace github.com/libp2p/go-flow-metrics => ../go-flow-metrics

replace github.com/libp2p/go-libp2p => ../go-libp2p

replace github.com/libp2p/go-libp2p-autonat => ../go-libp2p-autonat

replace github.com/libp2p/go-libp2p-autonat-svc => ../go-libp2p-autonat-svc

replace github.com/libp2p/go-libp2p-blankhost => ../go-libp2p-blankhost

replace github.com/libp2p/go-libp2p-circuit => ../go-libp2p-circuit

replace github.com/libp2p/go-libp2p-connmgr => ../go-libp2p-connmgr

replace github.com/libp2p/go-libp2p-consensus => ../go-libp2p-consensus

replace github.com/libp2p/go-libp2p-core => ../go-libp2p-core

replace github.com/libp2p/go-libp2p-daemon => ../go-libp2p-daemon

replace github.com/libp2p/go-libp2p-discovery => ../go-libp2p-discovery

replace github.com/libp2p/go-libp2p-examples => ../go-libp2p-examples

replace github.com/libp2p/go-libp2p-gorpc => ../go-libp2p-gorpc

replace github.com/libp2p/go-libp2p-kad-dht => ../go-libp2p-kad-dht

replace github.com/libp2p/go-libp2p-kbucket => ../go-libp2p-kbucket

replace github.com/libp2p/go-libp2p-loggables => ../go-libp2p-loggables

replace github.com/libp2p/go-libp2p-mplex => ../go-libp2p-mplex

replace github.com/libp2p/go-libp2p-nat => ../go-libp2p-nat

replace github.com/libp2p/go-libp2p-netutil => ../go-libp2p-netutil

replace github.com/libp2p/go-libp2p-peerstore => ../go-libp2p-peerstore

replace github.com/libp2p/go-libp2p-pnet => ../go-libp2p-pnet

replace github.com/libp2p/go-libp2p-pubsub => ../go-libp2p-pubsub

replace github.com/libp2p/go-libp2p-pubsub-router => ../go-libp2p-pubsub-router

replace github.com/libp2p/go-libp2p-quic-transport => ../go-libp2p-quic-transport

replace github.com/libp2p/go-libp2p-raft => ../go-libp2p-raft

replace github.com/libp2p/go-libp2p-record => ../go-libp2p-record

replace github.com/libp2p/go-libp2p-routing-helpers => ../go-libp2p-routing-helpers

replace github.com/libp2p/go-libp2p-secio => ../go-libp2p-secio

replace github.com/libp2p/go-libp2p-swarm => ../go-libp2p-swarm

replace github.com/libp2p/go-libp2p-testing => ../go-libp2p-testing

replace github.com/libp2p/go-libp2p-tls => ../go-libp2p-tls

replace github.com/libp2p/go-libp2p-transport-upgrader => ../go-libp2p-transport-upgrader

replace github.com/libp2p/go-libp2p-yamux => ../go-libp2p-yamux

replace github.com/libp2p/go-maddr-filter => ../go-maddr-filter

replace github.com/libp2p/go-mplex => ../go-mplex

replace github.com/libp2p/go-msgio => ../go-msgio

replace github.com/libp2p/go-multiaddr => ../go-multiaddr

replace github.com/libp2p/go-multiaddr-dns => ../go-multiaddr-dns

replace github.com/libp2p/go-multiaddr-fmt => ../go-multiaddr-fmt

replace github.com/libp2p/go-multiaddr-net => ../go-multiaddr-net

replace github.com/libp2p/go-multistream => ../go-multistream

replace github.com/libp2p/go-nat => ../go-nat

replace github.com/libp2p/go-reuseport => ../go-reuseport

replace github.com/libp2p/go-reuseport-transport => ../go-reuseport-transport

replace github.com/libp2p/go-sockaddr => ../go-sockaddr

replace github.com/libp2p/go-stream-muxer-multistream => ../go-stream-muxer-multistream

replace github.com/libp2p/go-tcp-transport => ../go-tcp-transport

replace github.com/libp2p/go-yamux => ../go-yamux

0 comments on commit 303a9e8

Please sign in to comment.