-
Notifications
You must be signed in to change notification settings - Fork 10
/
tcp.go
63 lines (52 loc) · 1.33 KB
/
tcp.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
package main
import (
"fmt"
"io"
"log"
"net"
"time"
"github.com/shadowsocks-server/shadowsocks-legendsock/socks"
)
func tcpRemote(instance *Instance, cipher func(net.Conn) net.Conn) {
socket, err := net.Listen("tcp", fmt.Sprintf(":%d", instance.Port))
if err != nil {
log.Printf("Failed to listen TCP on %d: %v", instance.Port, err)
return
}
defer socket.Close()
instance.TCPSocket = socket
for instance.Started {
client, err := socket.Accept()
if err != nil {
continue
}
go tcpHandle(instance, client, cipher)
}
}
func tcpHandle(instance *Instance, client net.Conn, cipher func(net.Conn) net.Conn) {
defer client.Close()
client.(*net.TCPConn).SetKeepAlive(true)
client = cipher(client)
target, err := socks.ReadAddr(client)
if err != nil {
return
}
remote, err := net.Dial("tcp", target.String())
if err != nil {
return
}
defer remote.Close()
tcpRelay(instance, client, remote)
}
func tcpRelay(instance *Instance, left, right net.Conn) {
go func() {
size, _ := io.CopyBuffer(right, left, make([]byte, 4096))
instance.Bandwidth.IncreaseUpload(uint64(size))
right.SetDeadline(time.Now())
left.SetDeadline(time.Now())
}()
size, _ := io.CopyBuffer(left, right, make([]byte, 4096))
instance.Bandwidth.IncreaseDownload(uint64(size))
right.SetDeadline(time.Now())
left.SetDeadline(time.Now())
}