From 39ada8ec7ab399e8dae702ca34cd182eac60692a Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 21 May 2020 05:09:49 -0600 Subject: [PATCH] working out some kinks in the WSWrap --- mplexer/cmd/telebit/telebit.go | 4 ++- mplexer/packer/listener.go | 12 ++++----- mplexer/packer/listener_test.go | 44 ++------------------------------- mplexer/packer/wsjunk_test.go | 43 ++++++++++++++++++++++++++++++++ mplexer/packer/wswrap.go | 33 +++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 50 deletions(-) create mode 100644 mplexer/packer/wsjunk_test.go diff --git a/mplexer/cmd/telebit/telebit.go b/mplexer/cmd/telebit/telebit.go index c4651b7..22dcfb3 100644 --- a/mplexer/cmd/telebit/telebit.go +++ b/mplexer/cmd/telebit/telebit.go @@ -72,7 +72,9 @@ func main() { //mux.HandleTLS("*", mux.TerminateTLS(mux)) mux.ForwardTCP("*", "localhost:3000", 120*time.Second) // TODO set failure - log.Fatal("Closed server: ", packer.ListenAndServe(wsconn, mux)) + + wsw := packer.NewWSWrap(wsconn) + log.Fatal("Closed server: ", packer.ListenAndServe(wsw, mux)) } func getToken(secret string) (token string, err error) { diff --git a/mplexer/packer/listener.go b/mplexer/packer/listener.go index 59fb8e4..f2eca0e 100644 --- a/mplexer/packer/listener.go +++ b/mplexer/packer/listener.go @@ -22,13 +22,11 @@ type Listener struct { } // Listen creates a new Listener and sets it up to receive and distribute connections. -func Listen(wsconn WSConn) *Listener { +func Listen(wsw *WSWrap) *Listener { ctx := context.TODO() - // Wrap the websocket and feed it into the Encoder and Decoder - wsw := &WSWrap{wsconn: wsconn, tmpr: nil} + // Feed the socket into the Encoder and Decoder listener := &Listener{ - //wsconn: wsconn, wsw: wsw, incoming: make(chan *Conn, 1), // buffer ever so slightly close: make(chan struct{}), @@ -42,7 +40,7 @@ func Listen(wsconn WSConn) *Listener { go func() { err := listener.encoder.Run() fmt.Printf("encoder stopped entirely: %q", err) - wsw.wsconn.Close() + wsw.Close() }() // Decode the stream as it comes in @@ -62,8 +60,8 @@ func Listen(wsconn WSConn) *Listener { } // ListenAndServe listens on a websocket and handles the incomming net.Conn-like connections with a Handler -func ListenAndServe(wsconn WSConn, mux Handler) error { - listener := Listen(wsconn) +func ListenAndServe(wsw *WSWrap, mux Handler) error { + listener := Listen(wsw) return Serve(listener, mux) } diff --git a/mplexer/packer/listener_test.go b/mplexer/packer/listener_test.go index 792aa61..3d76437 100644 --- a/mplexer/packer/listener_test.go +++ b/mplexer/packer/listener_test.go @@ -3,61 +3,21 @@ package packer import ( "errors" "io" - "net" "testing" - "time" ) func TestDialServer(t *testing.T) { // TODO replace the websocket connection with a mock server //ctx := context.Background() - wsconn := &WSTestConn{ - rwt: &RWTest{}, - } + wsw := &WSWrap{} mux := NewRouteMux() - t.Fatal(ListenAndServe(wsconn, mux)) + t.Fatal(ListenAndServe(wsw, mux)) } var ErrNoImpl error = errors.New("not implemented") -// WSTestConn is a fake websocket connection -type WSTestConn struct { - closed bool - rwt *RWTest -} - -func (wst *WSTestConn) NextReader() (messageType int, r io.Reader, err error) { - return 0, nil, ErrNoImpl -} -func (wst *WSTestConn) NextWriter(messageType int) (io.WriteCloser, error) { - return nil, ErrNoImpl -} -func (wst *WSTestConn) WriteControl(messageType int, data []byte, deadline time.Time) error { - if wst.closed { - return io.EOF - } - return nil -} -func (wst *WSTestConn) WriteMessage(messageType int, data []byte) error { - if wst.closed { - return io.EOF - } - return nil -} -func (wst *WSTestConn) SetReadDeadline(t time.Time) error { - return ErrNoImpl -} -func (wst *WSTestConn) Close() error { - wst.closed = true - return nil -} -func (wst *WSTestConn) RemoteAddr() net.Addr { - addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8443") - return addr -} - // RWTest is a fake buffer type RWTest struct { closed bool diff --git a/mplexer/packer/wsjunk_test.go b/mplexer/packer/wsjunk_test.go new file mode 100644 index 0000000..1a844e0 --- /dev/null +++ b/mplexer/packer/wsjunk_test.go @@ -0,0 +1,43 @@ +package packer + +import ( + "io" + "net" + "time" +) + +// WSTestConn is a fake websocket connection +type WSTestConn struct { + closed bool + rwt *RWTest +} + +func (wst *WSTestConn) NextReader() (messageType int, r io.Reader, err error) { + return 0, nil, ErrNoImpl +} +func (wst *WSTestConn) NextWriter(messageType int) (io.WriteCloser, error) { + return nil, ErrNoImpl +} +func (wst *WSTestConn) WriteControl(messageType int, data []byte, deadline time.Time) error { + if wst.closed { + return io.EOF + } + return nil +} +func (wst *WSTestConn) WriteMessage(messageType int, data []byte) error { + if wst.closed { + return io.EOF + } + return nil +} +func (wst *WSTestConn) SetReadDeadline(t time.Time) error { + return ErrNoImpl +} +func (wst *WSTestConn) Close() error { + wst.closed = true + return nil +} +func (wst *WSTestConn) RemoteAddr() net.Addr { + addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8443") + return addr +} diff --git a/mplexer/packer/wswrap.go b/mplexer/packer/wswrap.go index 02ce5a9..92fc1b4 100644 --- a/mplexer/packer/wswrap.go +++ b/mplexer/packer/wswrap.go @@ -31,6 +31,14 @@ type WSConn interface { // LocalAddr() net.Addr } +// NewWSWrap allocates a new websocket connection wrapper +func NewWSWrap(wsconn WSConn) *WSWrap { + return &WSWrap{ + wsconn: wsconn, + tmpr: nil, + } +} + func (wsw *WSWrap) Read(b []byte) (int, error) { if nil == wsw.tmpr { _, msgr, err := wsw.wsconn.NextReader() @@ -86,3 +94,28 @@ func (wsw *WSWrap) Close() error { _ = wsw.wsconn.Close() return err } + +// LocalAddr returns the local network address. +func (wsw *WSWrap) LocalAddr() *Addr { + panic("not implemented") +} + +// RemoteAddr returns the remote network address. +func (wsw *WSWrap) RemoteAddr() *Addr { + panic("not implemented") +} + +// SetDeadline sets the read and write deadlines associated +func (wsw *WSWrap) SetDeadline(t time.Time) error { + panic("not implemented") +} + +// SetReadDeadline sets the deadline for future Read calls +func (wsw *WSWrap) SetReadDeadline(t time.Time) error { + panic("not implemented") +} + +// SetWriteDeadline sets the deadline for future Write calls +func (wsw *WSWrap) SetWriteDeadline(t time.Time) error { + panic("not implemented") +}