-
Notifications
You must be signed in to change notification settings - Fork 8
/
tunnel.go
46 lines (41 loc) · 852 Bytes
/
tunnel.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
package httpproxy
import (
"context"
"io"
)
// tunnel create tunnels for two io.ReadWriteCloser
func tunnel(ctx context.Context, c1, c2 io.ReadWriteCloser, buf1, buf2 []byte) error {
ctx, cancel := context.WithCancel(ctx)
var errs tunnelErr
go func() {
_, errs[0] = io.CopyBuffer(c1, c2, buf1)
cancel()
}()
go func() {
_, errs[1] = io.CopyBuffer(c2, c1, buf2)
cancel()
}()
<-ctx.Done()
errs[2] = c1.Close()
errs[3] = c2.Close()
errs[4] = ctx.Err()
if errs[4] == context.Canceled {
errs[4] = nil
}
return errs.FirstError()
}
type tunnelErr [5]error
func (t tunnelErr) FirstError() error {
for _, err := range t {
if err != nil {
return err
}
}
return nil
}
// BytesPool is an interface for getting and returning temporary
// bytes for use by io.CopyBuffer.
type BytesPool interface {
Get() []byte
Put([]byte)
}