From d772b82e946da76b2fadc95e160a685d46b288b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thilo=20Karra=C3=9F?= Date: Mon, 20 Feb 2023 11:55:50 +0100 Subject: [PATCH] migrate to go module; ignore more irrelevant code at the wasm side --- addr.go | 2 +- client_conn.go | 23 +++++++++++------------ doc.go | 5 ++--- frame.go | 8 +++++--- go.mod | 7 +++++++ go.sum | 15 +++++++++++++++ listener.go | 20 +++++++++++--------- server_conn.go | 14 ++++++++------ 8 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/addr.go b/addr.go index 1a0eca8..9326d39 100644 --- a/addr.go +++ b/addr.go @@ -1,4 +1,4 @@ -package zocket /* import "udico.de/dreem/zocket" */ +package zocket // Addr represents a (the) WebSocket end point address. // Since WebSocket connections are upgraded HTTP Connections, diff --git a/client_conn.go b/client_conn.go index 87ff3c9..25cc970 100644 --- a/client_conn.go +++ b/client_conn.go @@ -1,6 +1,6 @@ -// +build js, wasm +//go:build js && wasm -package zocket /* import "udico.de/dreem/zocket" */ +package zocket import ( "context" @@ -17,12 +17,12 @@ type handler func(this js.Value, args []js.Value) interface{} // ClientConnection is a TCP like Connection between the wasm and a server. type ClientConnection struct { - M sync.Mutex - ws js.Value - conn chan struct{} - in chan []byte + M sync.Mutex + ws js.Value + conn chan struct{} + in chan []byte onMessage handler - buffered []byte + buffered []byte } func makeHandler(c chan []byte) handler { @@ -43,8 +43,8 @@ func makeHandler(c chan []byte) handler { func Dial(ctx context.Context, target string) (net.Conn, error) { fmt.Printf("dialing %v\n", target) ret := ClientConnection{ - ws: js.Global().Get("WebSocket").New(target), - in: make(chan []byte), + ws: js.Global().Get("WebSocket").New(target), + in: make(chan []byte), conn: make(chan struct{}), } ret.ws.Set("binaryType", "arraybuffer") @@ -80,7 +80,6 @@ func Dial(ctx context.Context, target string) (net.Conn, error) { return ret, nil } - // Read gets some bytes from a ws frame. Blocks. func (c ClientConnection) Read(b []byte) (int, error) { fmt.Println("client_read") @@ -138,7 +137,7 @@ func (c ClientConnection) Close() error { // dummy and make underlying layers think they're using a tcp conection. func (c ClientConnection) LocalAddr() net.Addr { fmt.Println("DBG: call LocalAddr() on ClientConnection") - return Addr { + return Addr{ network: "tcp", address: "0.0.0.0", } @@ -147,7 +146,7 @@ func (c ClientConnection) LocalAddr() net.Addr { // RemoteAddr returns the adress of the remote endpoint. func (c ClientConnection) RemoteAddr() net.Addr { fmt.Println("DBG: call RemoteAddr() on ClientConnection") - return Addr { + return Addr{ network: "tcp", // TODO: Use the real servers adress somehow (or none at all? :think:) address: "127.0.0.1", diff --git a/doc.go b/doc.go index 0709bc1..3ae0f4b 100644 --- a/doc.go +++ b/doc.go @@ -1,3 +1,2 @@ -// Zocket transparently tunnels a TCP connection through a WebSocket layer from within WebAssembly. - -package zocket /* import "udico.de/dreem/zocket" */ +// Package zocket transparently tunnels a TCP connection through a WebSocket layer from within WebAssembly. +package zocket diff --git a/frame.go b/frame.go index 4c3410f..1f766e6 100644 --- a/frame.go +++ b/frame.go @@ -1,4 +1,6 @@ -package zocket /* import "udico.de/dreem/zocket" */ +//go:build !js && !wasm + +package zocket import ( "encoding/binary" @@ -19,7 +21,7 @@ const ( ) /* - * + * Frame */ type Frame struct { Fin bool @@ -158,4 +160,4 @@ func (f *Frame) WriteTo(conn net.Conn) error { l += n } return nil -} \ No newline at end of file +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..314778e --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module udico.de/dreem/zocket + +go 1.20 + +require github.com/sirupsen/logrus v1.9.0 + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ed65537 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/listener.go b/listener.go index cc75a91..de31557 100644 --- a/listener.go +++ b/listener.go @@ -1,4 +1,6 @@ -package zocket /* import "udico.de/dreem/zocket" */ +//go:build !js || !wasm + +package zocket import ( "bufio" @@ -48,14 +50,14 @@ func (l Listener) Accept() (net.Conn, error) { // Any blocked Accept operations will be unblocked and return errors. func (l Listener) Close() error { select { - case _, ok := <-l.cConn: - // either the channel closed or we just read a connection from it. - // we discard the connection, since we're about to close the channel anyways - if !ok { - return errors.New("channel already closed") - } - default: - // something blocks. the channel is alive + case _, ok := <-l.cConn: + // either the channel closed or we just read a connection from it. + // we discard the connection, since we're about to close the channel anyways + if !ok { + return errors.New("channel already closed") + } + default: + // something blocks. the channel is alive } close(l.cConn) return nil diff --git a/server_conn.go b/server_conn.go index 40dabb3..f9962f3 100644 --- a/server_conn.go +++ b/server_conn.go @@ -1,4 +1,6 @@ -package zocket /* import "udico.de/dreem/zocket" */ +//go:build !js || !wasm + +package zocket import ( "bytes" @@ -11,7 +13,7 @@ import ( // ServerConnection is the serverside part of the ws abstraction type ServerConnection struct { - conn net.Conn // The underlying REAL connection. + conn net.Conn // The underlying REAL connection. buffered []byte } @@ -30,13 +32,13 @@ func (c ServerConnection) Read(b []byte) (int, error) { } return len(b), nil } - buf := &bytes.Buffer{}// - datloop: + buf := &bytes.Buffer{} // +datloop: for { tFrame, err := ReadFrame(c.conn) if err != nil { - return 0, err - } + return 0, err + } switch tFrame.Opcode { case FrameType_Ping: // send pong