-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
203 lines (179 loc) · 4.82 KB
/
connection.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package websocket
import (
"context"
"io"
"net"
"time"
"github.com/gorilla/websocket"
"github.com/bmclab-git/v2ray-core/v5/common/buf"
"github.com/bmclab-git/v2ray-core/v5/common/errors"
"github.com/bmclab-git/v2ray-core/v5/common/serial"
)
var _ buf.Writer = (*connection)(nil)
// connection is a wrapper for net.Conn over WebSocket connection.
type connection struct {
conn *websocket.Conn
reader io.Reader
remoteAddr net.Addr
shouldWait bool
delayedDialFinish context.Context
finishedDial context.CancelFunc
dialer DelayedDialer
}
type DelayedDialer interface {
Dial(earlyData []byte) (*websocket.Conn, error)
}
func newConnection(conn *websocket.Conn, remoteAddr net.Addr) *connection {
return &connection{
conn: conn,
remoteAddr: remoteAddr,
}
}
func newConnectionWithEarlyData(conn *websocket.Conn, remoteAddr net.Addr, earlyData io.Reader) *connection {
return &connection{
conn: conn,
remoteAddr: remoteAddr,
reader: earlyData,
}
}
func newConnectionWithDelayedDial(dialer DelayedDialer) *connection {
delayedDialContext, cancelFunc := context.WithCancel(context.Background())
return &connection{
shouldWait: true,
delayedDialFinish: delayedDialContext,
finishedDial: cancelFunc,
dialer: dialer,
}
}
func newRelayedConnectionWithDelayedDial(dialer DelayedDialerForwarded) *connectionForwarder {
delayedDialContext, cancelFunc := context.WithCancel(context.Background())
return &connectionForwarder{
shouldWait: true,
delayedDialFinish: delayedDialContext,
finishedDial: cancelFunc,
dialer: dialer,
}
}
func newRelayedConnection(conn io.ReadWriteCloser) *connectionForwarder {
return &connectionForwarder{
ReadWriteCloser: conn,
shouldWait: false,
}
}
// Read implements net.Conn.Read()
func (c *connection) Read(b []byte) (int, error) {
for {
reader, err := c.getReader()
if err != nil {
return 0, err
}
nBytes, err := reader.Read(b)
if errors.Cause(err) == io.EOF {
c.reader = nil
continue
}
return nBytes, err
}
}
func (c *connection) getReader() (io.Reader, error) {
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
return nil, newError("unable to read delayed dial websocket connection as it do not exist")
}
}
if c.reader != nil {
return c.reader, nil
}
_, reader, err := c.conn.NextReader()
if err != nil {
return nil, err
}
c.reader = reader
return reader, nil
}
// Write implements io.Writer.
func (c *connection) Write(b []byte) (int, error) {
if c.shouldWait {
var err error
c.conn, err = c.dialer.Dial(b)
c.finishedDial()
if err != nil {
return 0, newError("Unable to proceed with delayed write").Base(err)
}
c.remoteAddr = c.conn.RemoteAddr()
c.shouldWait = false
return len(b), nil
}
if err := c.conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
return 0, err
}
return len(b), nil
}
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
mb = buf.Compact(mb)
mb, err := buf.WriteMultiBuffer(c, mb)
buf.ReleaseMulti(mb)
return err
}
func (c *connection) Close() error {
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
return newError("unable to close delayed dial websocket connection as it do not exist")
}
}
var errors []interface{}
if err := c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
errors = append(errors, err)
}
if err := c.conn.Close(); err != nil {
errors = append(errors, err)
}
if len(errors) > 0 {
return newError("failed to close connection").Base(newError(serial.Concat(errors...)))
}
return nil
}
func (c *connection) LocalAddr() net.Addr {
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
newError("websocket transport is not materialized when LocalAddr() is called").AtWarning().WriteToLog()
return &net.UnixAddr{
Name: "@placeholder",
Net: "unix",
}
}
}
return c.conn.LocalAddr()
}
func (c *connection) RemoteAddr() net.Addr {
return c.remoteAddr
}
func (c *connection) SetDeadline(t time.Time) error {
if err := c.SetReadDeadline(t); err != nil {
return err
}
return c.SetWriteDeadline(t)
}
func (c *connection) SetReadDeadline(t time.Time) error {
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
newError("websocket transport is not materialized when SetReadDeadline() is called").AtWarning().WriteToLog()
return nil
}
}
return c.conn.SetReadDeadline(t)
}
func (c *connection) SetWriteDeadline(t time.Time) error {
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
newError("websocket transport is not materialized when SetWriteDeadline() is called").AtWarning().WriteToLog()
return nil
}
}
return c.conn.SetWriteDeadline(t)
}