diff options
author | Mike Crute <mike@crute.us> | 2023-07-29 12:15:13 -0700 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-07-29 12:15:13 -0700 |
commit | 4e995f9e6c3adc43a361b6fa9b976d25378f1594 (patch) | |
tree | 862642149583fa4ad662edfe0b31a7d65b8e302e /proxy | |
parent | fea07831eadd35532055ec16fc43b0cde56a54b1 (diff) | |
download | websocket_proxy-4e995f9e6c3adc43a361b6fa9b976d25378f1594.tar.bz2 websocket_proxy-4e995f9e6c3adc43a361b6fa9b976d25378f1594.tar.xz websocket_proxy-4e995f9e6c3adc43a361b6fa9b976d25378f1594.zip |
Initial import of rewrite
Diffstat (limited to 'proxy')
-rw-r--r-- | proxy/copier.go | 8 | ||||
-rw-r--r-- | proxy/ws_read_writer.go | 39 |
2 files changed, 47 insertions, 0 deletions
diff --git a/proxy/copier.go b/proxy/copier.go new file mode 100644 index 0000000..e10cba6 --- /dev/null +++ b/proxy/copier.go | |||
@@ -0,0 +1,8 @@ | |||
1 | package proxy | ||
2 | |||
3 | import "io" | ||
4 | |||
5 | func CopyWithErrors(dst io.Writer, src io.Reader, errc chan<- error) { | ||
6 | _, err := io.Copy(dst, src) | ||
7 | errc <- err | ||
8 | } | ||
diff --git a/proxy/ws_read_writer.go b/proxy/ws_read_writer.go new file mode 100644 index 0000000..b064019 --- /dev/null +++ b/proxy/ws_read_writer.go | |||
@@ -0,0 +1,39 @@ | |||
1 | package proxy | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | |||
6 | "github.com/gorilla/websocket" | ||
7 | ) | ||
8 | |||
9 | type WebsocketReadWriter struct { | ||
10 | W *websocket.Conn | ||
11 | } | ||
12 | |||
13 | func (w *WebsocketReadWriter) Read(p []byte) (int, error) { | ||
14 | t, m, err := w.W.ReadMessage() | ||
15 | if err != nil { | ||
16 | return 0, fmt.Errorf("error reading websocket: %w", err) | ||
17 | } | ||
18 | if t != websocket.BinaryMessage { | ||
19 | return 0, fmt.Errorf("error reading websocket: only binary messages are supported") | ||
20 | } | ||
21 | if cap(p) < len(m) { | ||
22 | return 0, fmt.Errorf("error reading websocket: outputbuffer too short for input") | ||
23 | } | ||
24 | if i := copy(p, m); i != len(m) { | ||
25 | return 0, fmt.Errorf("error reading websocket: copy failed %d of %d", i, len(m)) | ||
26 | } | ||
27 | return len(m), nil | ||
28 | } | ||
29 | |||
30 | func (w *WebsocketReadWriter) Write(p []byte) (int, error) { | ||
31 | if err := w.W.WriteMessage(websocket.BinaryMessage, p); err != nil { | ||
32 | return 0, fmt.Errorf("error writing websocket: %w", err) | ||
33 | } | ||
34 | return len(p), nil | ||
35 | } | ||
36 | |||
37 | func (w *WebsocketReadWriter) Close() error { | ||
38 | return w.W.Close() | ||
39 | } | ||