-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: Neil Alexander <[email protected]>
- Loading branch information
1 parent
da7ebde
commit 5ea16e6
Showing
5 changed files
with
181 additions
and
0 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,123 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/Arceliar/phony" | ||
"nhooyr.io/websocket" | ||
) | ||
|
||
type linkWS struct { | ||
phony.Inbox | ||
*links | ||
} | ||
|
||
type linkWSConn struct { | ||
net.Conn | ||
} | ||
|
||
type linkWSListener struct { | ||
ch chan *linkWSConn | ||
ctx context.Context | ||
httpServer *http.Server | ||
listener net.Listener | ||
} | ||
|
||
type wsServer struct { | ||
ch chan *linkWSConn | ||
ctx context.Context | ||
} | ||
|
||
func (l *linkWSListener) Accept() (net.Conn, error) { | ||
qs := <-l.ch | ||
if qs == nil { | ||
return nil, context.Canceled | ||
} | ||
return qs, nil | ||
} | ||
|
||
func (l *linkWSListener) Addr() net.Addr { | ||
return l.listener.Addr() | ||
} | ||
|
||
func (l *linkWSListener) Close() error { | ||
if err := l.httpServer.Shutdown(l.ctx); err != nil { | ||
return err | ||
} | ||
return l.listener.Close() | ||
} | ||
|
||
func (s *wsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/health" || r.URL.Path == "/healthz" { | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte("OK")) | ||
return | ||
} | ||
|
||
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{ | ||
Subprotocols: []string{"ygg-ws"}, | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if c.Subprotocol() != "ygg-ws" { | ||
c.Close(websocket.StatusPolicyViolation, "client must speak the ygg-ws subprotocol") | ||
return | ||
} | ||
|
||
s.ch <- &linkWSConn{ | ||
Conn: websocket.NetConn(s.ctx, c, websocket.MessageBinary), | ||
} | ||
} | ||
|
||
func (l *links) newLinkWS() *linkWS { | ||
lt := &linkWS{ | ||
links: l, | ||
} | ||
return lt | ||
} | ||
|
||
func (l *linkWS) 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 &linkWSConn{ | ||
Conn: websocket.NetConn(ctx, wsconn, websocket.MessageBinary), | ||
}, nil | ||
} | ||
|
||
func (l *linkWS) listen(ctx context.Context, url *url.URL, _ string) (net.Listener, error) { | ||
nl, err := net.Listen("tcp", url.Host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ch := make(chan *linkWSConn) | ||
|
||
httpServer := &http.Server{ | ||
Handler: &wsServer{ | ||
ch: ch, | ||
ctx: ctx, | ||
}, | ||
BaseContext: func(_ net.Listener) context.Context { return ctx }, | ||
ReadTimeout: time.Second * 10, | ||
WriteTimeout: time.Second * 10, | ||
} | ||
|
||
lwl := &linkWSListener{ | ||
ch: ch, | ||
ctx: ctx, | ||
httpServer: httpServer, | ||
listener: nl, | ||
} | ||
go lwl.httpServer.Serve(nl) // nolint:errcheck | ||
return lwl, 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,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") | ||
} |