57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
// Package websocket implements a websocket based transport for go-libp2p.
|
|
package websocket
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
"github.com/libp2p/go-libp2p-core/transport"
|
|
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
mafmt "github.com/multiformats/go-multiaddr-fmt"
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
|
)
|
|
|
|
// WsFmt is multiaddr formatter for WsProtocol
|
|
var WsFmt = mafmt.And(mafmt.TCP, mafmt.Base(ma.P_WS))
|
|
|
|
// This is _not_ WsFmt because we want the transport to stick to dialing fully
|
|
// resolved addresses.
|
|
var dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP), mafmt.Base(ma.P_WS))
|
|
|
|
func init() {
|
|
manet.RegisterFromNetAddr(ParseWebsocketNetAddr, "websocket")
|
|
manet.RegisterToNetAddr(ConvertWebsocketMultiaddrToNetAddr, "ws")
|
|
}
|
|
|
|
var _ transport.Transport = (*WebsocketTransport)(nil)
|
|
|
|
// WebsocketTransport is the actual go-libp2p transport
|
|
type WebsocketTransport struct {
|
|
Upgrader *tptu.Upgrader
|
|
}
|
|
|
|
func New(u *tptu.Upgrader) *WebsocketTransport {
|
|
return &WebsocketTransport{u}
|
|
}
|
|
|
|
func (t *WebsocketTransport) CanDial(a ma.Multiaddr) bool {
|
|
return dialMatcher.Matches(a)
|
|
}
|
|
|
|
func (t *WebsocketTransport) Protocols() []int {
|
|
return []int{ma.ProtocolWithCode(ma.P_WS).Code}
|
|
}
|
|
|
|
func (t *WebsocketTransport) Proxy() bool {
|
|
return false
|
|
}
|
|
|
|
func (t *WebsocketTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (transport.CapableConn, error) {
|
|
macon, err := t.maDial(ctx, raddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return t.Upgrader.UpgradeOutbound(ctx, t, macon, p)
|
|
}
|