forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirect.go
268 lines (222 loc) · 4.84 KB
/
direct.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package ray
import (
"context"
"io"
"sync"
"time"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/platform"
"v2ray.com/core/common/signal"
)
type Option func(*directRay)
type addInt64 interface {
Add(int64) int64
}
func WithUplinkStatCounter(c addInt64) Option {
return func(s *directRay) {
s.Input.onDataSize = append(s.Input.onDataSize, func(delta uint64) {
c.Add(int64(delta))
})
}
}
func WithDownlinkStatCounter(c addInt64) Option {
return func(s *directRay) {
s.Output.onDataSize = append(s.Output.onDataSize, func(delta uint64) {
c.Add(int64(delta))
})
}
}
// New creates a new Ray for direct traffic transport.
func New(ctx context.Context, opts ...Option) Ray {
r := &directRay{
Input: NewStream(ctx),
Output: NewStream(ctx),
}
for _, opt := range opts {
opt(r)
}
return r
}
type directRay struct {
Input *Stream
Output *Stream
}
func (v *directRay) OutboundInput() InputStream {
return v.Input
}
func (v *directRay) OutboundOutput() OutputStream {
return v.Output
}
func (v *directRay) InboundInput() OutputStream {
return v.Input
}
func (v *directRay) InboundOutput() InputStream {
return v.Output
}
var streamSizeLimit uint64 = 10 * 1024 * 1024
func init() {
const raySizeEnvKey = "v2ray.ray.buffer.size"
size := platform.EnvFlag{
Name: raySizeEnvKey,
AltName: platform.NormalizeEnvName(raySizeEnvKey),
}.GetValueAsInt(10)
streamSizeLimit = uint64(size) * 1024 * 1024
}
// Stream is a sequential container for data in bytes.
type Stream struct {
access sync.RWMutex
data buf.MultiBuffer
size uint64
ctx context.Context
readSignal *signal.Notifier
writeSignal *signal.Notifier
onDataSize []func(uint64)
close bool
err bool
}
// NewStream creates a new Stream.
func NewStream(ctx context.Context) *Stream {
s := &Stream{
ctx: ctx,
readSignal: signal.NewNotifier(),
writeSignal: signal.NewNotifier(),
size: 0,
}
return s
}
func (s *Stream) getData() (buf.MultiBuffer, error) {
s.access.Lock()
defer s.access.Unlock()
if s.data != nil {
mb := s.data
s.data = nil
s.size = 0
return mb, nil
}
if s.err {
return nil, io.ErrClosedPipe
}
if s.close {
return nil, io.EOF
}
return nil, nil
}
// Peek fills in the given buffer with data from head of the Stream.
func (s *Stream) Peek(b *buf.Buffer) {
s.access.RLock()
defer s.access.RUnlock()
common.Must(b.Reset(func(data []byte) (int, error) {
return s.data.Copy(data), nil
}))
}
// ReadMultiBuffer reads data from the Stream.
func (s *Stream) ReadMultiBuffer() (buf.MultiBuffer, error) {
for {
mb, err := s.getData()
if err != nil {
return nil, err
}
if mb != nil {
s.readSignal.Signal()
return mb, nil
}
select {
case <-s.ctx.Done():
return nil, s.ctx.Err()
case <-s.writeSignal.Wait():
}
}
}
// ReadTimeout reads from the Stream with a specified timeout.
func (s *Stream) ReadTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
for {
mb, err := s.getData()
if err != nil {
return nil, err
}
if mb != nil {
s.readSignal.Signal()
return mb, nil
}
select {
case <-s.ctx.Done():
return nil, s.ctx.Err()
case <-time.After(timeout):
return nil, buf.ErrReadTimeout
case <-s.writeSignal.Wait():
}
}
}
// Size returns the number of bytes hold in the Stream.
func (s *Stream) Size() uint64 {
s.access.RLock()
defer s.access.RUnlock()
return s.size
}
// waitForStreamSize waits until the Stream has room for more data, or any error happens.
func (s *Stream) waitForStreamSize() error {
if streamSizeLimit == 0 {
return nil
}
for s.Size() >= streamSizeLimit {
select {
case <-s.ctx.Done():
return s.ctx.Err()
case <-s.readSignal.Wait():
if s.err || s.close {
return io.ErrClosedPipe
}
}
}
return nil
}
// WriteMultiBuffer writes more data into the Stream.
func (s *Stream) WriteMultiBuffer(data buf.MultiBuffer) error {
if data.IsEmpty() {
return nil
}
if err := s.waitForStreamSize(); err != nil {
data.Release()
return err
}
s.access.Lock()
defer s.access.Unlock()
if s.err || s.close {
data.Release()
return io.ErrClosedPipe
}
if s.data == nil {
s.data = buf.NewMultiBufferCap(128)
}
dataSize := uint64(data.Len())
for _, f := range s.onDataSize {
f(dataSize)
}
s.data.AppendMulti(data)
s.size += dataSize
s.writeSignal.Signal()
return nil
}
// Close closes the stream for writing. Read() still works until EOF.
func (s *Stream) Close() error {
s.access.Lock()
s.close = true
s.readSignal.Signal()
s.writeSignal.Signal()
s.access.Unlock()
return nil
}
// CloseError closes the Stream with error. Read() will return an error afterwards.
func (s *Stream) CloseError() {
s.access.Lock()
s.err = true
if s.data != nil {
s.data.Release()
s.data = nil
s.size = 0
}
s.access.Unlock()
s.readSignal.Signal()
s.writeSignal.Signal()
}