Implement websocket (ws:// and wss://) links (#1152)

ws:// can be listened and dialed
wss:// is a convenience link for ws:// that supports dialing to ws://
peer.

---------

Signed-off-by: Vasyl Gello <vasek.gello@gmail.com>
Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
This commit is contained in:
Vasyl Gello 2024-07-23 21:58:11 +00:00 committed by GitHub
parent da7ebde828
commit 5ea16e63a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 181 additions and 0 deletions

43
src/core/link_wss.go Normal file
View file

@ -0,0 +1,43 @@
package core
import (
"context"
"fmt"
"net"
"net/url"
"github.com/Arceliar/phony"
"nhooyr.io/websocket"
)
type linkWSS struct {
phony.Inbox
*links
}
type linkWSSConn struct {
net.Conn
}
func (l *links) newLinkWSS() *linkWSS {
lwss := &linkWSS{
links: l,
}
return lwss
}
func (l *linkWSS) dial(ctx context.Context, url *url.URL, info linkInfo, options linkOptions) (net.Conn, error) {
wsconn, _, err := websocket.Dial(ctx, url.String(), &websocket.DialOptions{
Subprotocols: []string{"ygg-ws"},
})
if err != nil {
return nil, err
}
return &linkWSSConn{
Conn: websocket.NetConn(ctx, wsconn, websocket.MessageBinary),
}, nil
}
func (l *linkWSS) listen(ctx context.Context, url *url.URL, _ string) (net.Listener, error) {
return nil, fmt.Errorf("WSS listener not supported, use WS listener behind reverse proxy instead")
}