-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1422 from libp2p/merge-websocket
move go-ws-transport here
- Loading branch information
Showing
11 changed files
with
1,027 additions
and
6 deletions.
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
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,5 @@ | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
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,19 @@ | ||
The MIT License (MIT) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,137 @@ | ||
package websocket | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/url" | ||
"strconv" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
manet "github.com/multiformats/go-multiaddr/net" | ||
) | ||
|
||
// Addr is an implementation of net.Addr for WebSocket. | ||
type Addr struct { | ||
*url.URL | ||
} | ||
|
||
var _ net.Addr = (*Addr)(nil) | ||
|
||
// Network returns the network type for a WebSocket, "websocket". | ||
func (addr *Addr) Network() string { | ||
return "websocket" | ||
} | ||
|
||
// NewAddr creates an Addr with `ws` scheme (insecure). | ||
// | ||
// Deprecated. Use NewAddrWithScheme. | ||
func NewAddr(host string) *Addr { | ||
// Older versions of the transport only supported insecure connections (i.e. | ||
// WS instead of WSS). Assume that is the case here. | ||
return NewAddrWithScheme(host, false) | ||
} | ||
|
||
// NewAddrWithScheme creates a new Addr using the given host string. isSecure | ||
// should be true for WSS connections and false for WS. | ||
func NewAddrWithScheme(host string, isSecure bool) *Addr { | ||
scheme := "ws" | ||
if isSecure { | ||
scheme = "wss" | ||
} | ||
return &Addr{ | ||
URL: &url.URL{ | ||
Scheme: scheme, | ||
Host: host, | ||
}, | ||
} | ||
} | ||
|
||
func ConvertWebsocketMultiaddrToNetAddr(maddr ma.Multiaddr) (net.Addr, error) { | ||
url, err := parseMultiaddr(maddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Addr{URL: url}, nil | ||
} | ||
|
||
func ParseWebsocketNetAddr(a net.Addr) (ma.Multiaddr, error) { | ||
wsa, ok := a.(*Addr) | ||
if !ok { | ||
return nil, fmt.Errorf("not a websocket address") | ||
} | ||
|
||
var ( | ||
tcpma ma.Multiaddr | ||
err error | ||
port int | ||
host = wsa.Hostname() | ||
) | ||
|
||
// Get the port | ||
if portStr := wsa.Port(); portStr != "" { | ||
port, err = strconv.Atoi(portStr) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse port '%q': %s", portStr, err) | ||
} | ||
} else { | ||
return nil, fmt.Errorf("invalid port in url: '%q'", wsa.URL) | ||
} | ||
|
||
// NOTE: Ignoring IPv6 zones... | ||
// Detect if host is IP address or DNS | ||
if ip := net.ParseIP(host); ip != nil { | ||
// Assume IP address | ||
tcpma, err = manet.FromNetAddr(&net.TCPAddr{ | ||
IP: ip, | ||
Port: port, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
// Assume DNS name | ||
tcpma, err = ma.NewMultiaddr(fmt.Sprintf("/dns/%s/tcp/%d", host, port)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
wsma, err := ma.NewMultiaddr("/" + wsa.Scheme) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return tcpma.Encapsulate(wsma), nil | ||
} | ||
|
||
func parseMultiaddr(maddr ma.Multiaddr) (*url.URL, error) { | ||
// Only look at the _last_ component. | ||
maddr, wscomponent := ma.SplitLast(maddr) | ||
if maddr == nil || wscomponent == nil { | ||
return nil, fmt.Errorf("websocket addrs need at least two components") | ||
} | ||
|
||
var scheme string | ||
switch wscomponent.Protocol().Code { | ||
case ma.P_WS: | ||
scheme = "ws" | ||
case ma.P_WSS: | ||
scheme = "wss" | ||
default: | ||
return nil, fmt.Errorf("not a websocket multiaddr") | ||
} | ||
|
||
network, host, err := manet.DialArgs(maddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch network { | ||
case "tcp", "tcp4", "tcp6": | ||
default: | ||
return nil, fmt.Errorf("unsupported websocket network %s", network) | ||
} | ||
return &url.URL{ | ||
Scheme: scheme, | ||
Host: host, | ||
}, nil | ||
} |
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,81 @@ | ||
package websocket | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func TestMultiaddrParsing(t *testing.T) { | ||
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555/ws") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
wsaddr, err := parseMultiaddr(addr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if wsaddr.String() != "ws://127.0.0.1:5555" { | ||
t.Fatalf("expected ws://127.0.0.1:5555, got %s", wsaddr) | ||
} | ||
} | ||
|
||
type httpAddr struct { | ||
*url.URL | ||
} | ||
|
||
func (addr *httpAddr) Network() string { | ||
return "http" | ||
} | ||
|
||
func TestParseWebsocketNetAddr(t *testing.T) { | ||
notWs := &httpAddr{&url.URL{Host: "http://127.0.0.1:1234"}} | ||
_, err := ParseWebsocketNetAddr(notWs) | ||
if err.Error() != "not a websocket address" { | ||
t.Fatalf("expect \"not a websocket address\", got \"%s\"", err) | ||
} | ||
|
||
wsAddr := NewAddrWithScheme("127.0.0.1:5555", false) | ||
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()) | ||
} | ||
} | ||
|
||
func TestConvertWebsocketMultiaddrToNetAddr(t *testing.T) { | ||
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555/ws") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
wsaddr, err := ConvertWebsocketMultiaddrToNetAddr(addr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if wsaddr.String() != "ws://127.0.0.1:5555" { | ||
t.Fatalf("expected ws://127.0.0.1:5555, got %s", wsaddr) | ||
} | ||
if wsaddr.Network() != "websocket" { | ||
t.Fatalf("expected network: \"websocket\", got \"%s\"", wsaddr.Network()) | ||
} | ||
} | ||
|
||
func TestListeningOnDNSAddr(t *testing.T) { | ||
ln, err := newListener(ma.StringCast("/dns/localhost/tcp/0/ws"), nil) | ||
require.NoError(t, err) | ||
addr := ln.Multiaddr() | ||
first, rest := ma.SplitFirst(addr) | ||
require.Equal(t, first.Protocol().Code, ma.P_DNS) | ||
require.Equal(t, first.Value(), "localhost") | ||
next, _ := ma.SplitFirst(rest) | ||
require.Equal(t, next.Protocol().Code, ma.P_TCP) | ||
require.NotEqual(t, next.Value(), "0") | ||
} |
Oops, something went wrong.