-
Notifications
You must be signed in to change notification settings - Fork 0
/
connforwarder.go
81 lines (69 loc) · 1.68 KB
/
connforwarder.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
74
75
76
77
78
79
80
81
package websocket
import (
"context"
"io"
"net"
"time"
)
type connectionForwarder struct {
io.ReadWriteCloser
shouldWait bool
delayedDialFinish context.Context
finishedDial context.CancelFunc
dialer DelayedDialerForwarded
}
func (c *connectionForwarder) Read(p []byte) (n int, err error) {
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.ReadWriteCloser == nil {
return 0, newError("unable to read delayed dial websocket connection as it do not exist")
}
}
return c.ReadWriteCloser.Read(p)
}
func (c *connectionForwarder) Write(p []byte) (n int, err error) {
if c.shouldWait {
var err error
c.ReadWriteCloser, err = c.dialer.Dial(p)
c.finishedDial()
if err != nil {
return 0, newError("Unable to proceed with delayed write").Base(err)
}
c.shouldWait = false
return len(p), nil
}
return c.ReadWriteCloser.Write(p)
}
func (c *connectionForwarder) Close() error {
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.ReadWriteCloser == nil {
return newError("unable to close delayed dial websocket connection as it do not exist")
}
}
return c.ReadWriteCloser.Close()
}
func (c connectionForwarder) LocalAddr() net.Addr {
return &net.UnixAddr{
Name: "not available",
Net: "",
}
}
func (c connectionForwarder) RemoteAddr() net.Addr {
return &net.UnixAddr{
Name: "not available",
Net: "",
}
}
func (c connectionForwarder) SetDeadline(t time.Time) error {
return nil
}
func (c connectionForwarder) SetReadDeadline(t time.Time) error {
return nil
}
func (c connectionForwarder) SetWriteDeadline(t time.Time) error {
return nil
}
type DelayedDialerForwarded interface {
Dial(earlyData []byte) (io.ReadWriteCloser, error)
}