2018-07-04 10:51:47 +00:00
|
|
|
// Package websocket implements a websocket based transport for go-libp2p.
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p-core/transport"
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
ws "github.com/gorilla/websocket"
|
2018-07-04 10:51:47 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2019-10-04 15:21:24 +00:00
|
|
|
mafmt "github.com/multiformats/go-multiaddr-fmt"
|
2018-07-04 10:51:47 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WsProtocol is the multiaddr protocol definition for this transport.
|
2019-10-04 15:21:24 +00:00
|
|
|
//
|
|
|
|
// Deprecated: use `ma.ProtocolWithCode(ma.P_WS)
|
|
|
|
var WsProtocol = ma.ProtocolWithCode(ma.P_WS)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// WsFmt is multiaddr formatter for WsProtocol
|
|
|
|
var WsFmt = mafmt.And(mafmt.TCP, mafmt.Base(WsProtocol.Code))
|
|
|
|
|
|
|
|
// WsCodec is the multiaddr-net codec definition for the websocket transport
|
|
|
|
var WsCodec = &manet.NetCodec{
|
|
|
|
NetAddrNetworks: []string{"websocket"},
|
|
|
|
ProtocolName: "ws",
|
|
|
|
ConvertMultiaddr: ConvertWebsocketMultiaddrToNetAddr,
|
|
|
|
ParseNetAddr: ParseWebsocketNetAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default gorilla upgrader
|
|
|
|
var upgrader = ws.Upgrader{
|
|
|
|
// Allow requests from *all* origins.
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
manet.RegisterNetCodec(WsCodec)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebsocketTransport is the actual go-libp2p transport
|
|
|
|
type WebsocketTransport struct {
|
|
|
|
Upgrader *tptu.Upgrader
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(u *tptu.Upgrader) *WebsocketTransport {
|
|
|
|
return &WebsocketTransport{u}
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
var _ transport.Transport = (*WebsocketTransport)(nil)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
func (t *WebsocketTransport) CanDial(a ma.Multiaddr) bool {
|
|
|
|
return WsFmt.Matches(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WebsocketTransport) Protocols() []int {
|
|
|
|
return []int{WsProtocol.Code}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WebsocketTransport) Proxy() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WebsocketTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) {
|
|
|
|
wsurl, err := parseMultiaddr(raddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wscon, _, err := ws.DefaultDialer.Dial(wsurl, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
mnc, err := manet.WrapNetConn(NewConn(wscon))
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
wscon.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return mnc, nil
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func (t *WebsocketTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (transport.CapableConn, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
macon, err := t.maDial(ctx, raddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t.Upgrader.UpgradeOutbound(ctx, t, macon, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WebsocketTransport) maListen(a ma.Multiaddr) (manet.Listener, error) {
|
|
|
|
lnet, lnaddr, err := manet.DialArgs(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nl, err := net.Listen(lnet, lnaddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse("http://" + nl.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
nl.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
malist, err := t.wrapListener(nl, u)
|
|
|
|
if err != nil {
|
|
|
|
nl.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go malist.serve()
|
|
|
|
|
|
|
|
return malist, nil
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func (t *WebsocketTransport) Listen(a ma.Multiaddr) (transport.Listener, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
malist, err := t.maListen(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t.Upgrader.UpgradeListener(t, malist), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WebsocketTransport) wrapListener(l net.Listener, origin *url.URL) (*listener, error) {
|
|
|
|
laddr, err := manet.FromNetAddr(l.Addr())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsma, err := ma.NewMultiaddr("/ws")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
laddr = laddr.Encapsulate(wsma)
|
|
|
|
|
|
|
|
return &listener{
|
|
|
|
laddr: laddr,
|
|
|
|
Listener: l,
|
|
|
|
incoming: make(chan *Conn),
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
}, nil
|
|
|
|
}
|