-
Notifications
You must be signed in to change notification settings - Fork 0
/
localConn.go
73 lines (63 loc) · 1.6 KB
/
localConn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"fmt"
"io"
"net"
)
// LocalConn represents a local tcp connection
type LocalConn struct {
conn net.Conn
localListener net.Listener
port string
bytesReceived uint32
bytesSent uint32
reader io.Reader
writer io.Writer
}
// LocalConnOption is the configuration option for LocalConn
// used in the constructor.
type LocalConnOption func(conn *LocalConn)
func NewLocalConn(ctx context.Context, opts ...LocalConnOption) (*LocalConn, error) {
lc := &LocalConn{}
for _, opt := range opts {
opt(lc)
}
// for testing
// TODO: Add local port checking so you don't need to be explicit about
// what port you're looking for, also _maybe_ add ipv6?
localListener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", lc.port))
if err != nil {
return nil, err
}
lc.localListener = localListener
return lc, nil
}
func WithLocalConnPort(port string) LocalConnOption {
return func(conn *LocalConn) {
conn.port = port
}
}
func WithLocalConnWriter(writer io.Writer) LocalConnOption {
return func(conn *LocalConn) {
conn.writer = writer
}
}
func WithLocalConnReader(reader io.Reader) LocalConnOption {
return func(conn *LocalConn) {
conn.reader = reader
}
}
// Accept is blocking, only start accepting once we can confirm that
// the websocket connection is valid
func (lc *LocalConn) Accept() error {
c, err := lc.localListener.Accept()
lc.conn = c
return err
}
func (lc *LocalConn) Read(buf []byte) (n int, err error) {
return lc.conn.Read(buf)
}
func (lc *LocalConn) Write(buf []byte) (n int, err error) {
return lc.conn.Write(buf)
}