-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay.go
38 lines (30 loc) · 809 Bytes
/
relay.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
package snet
import (
"io"
"net"
"sync"
"time"
)
// Relay copies between local and remote bidirectionally. Returns number of
// bytes copied from remote to local, from local to remote, and any error occurred.
// Borrowed from: https://github.com/shadowsocks/go-shadowsocks2
func Relay(local, remote net.Conn) error {
var err, err1 error
var wg sync.WaitGroup
delay := time.Second
// local = Dumper(local)
// remote = Dumper(remote)
wg.Add(1)
go func() {
defer wg.Done()
_, err1 = io.Copy(remote, local)
remote.SetDeadline(time.Now().Add(delay)) // wake up the other goroutine blocking on remote
}()
_, err = io.Copy(local, remote)
local.SetDeadline(time.Now().Add(delay)) // wake up the other goroutine blocking on local
wg.Wait()
if err1 != nil {
return err1
}
return err
}