forked from eycorsican/go-tun2socks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
192 lines (167 loc) · 3.96 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
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
package socks
import (
"io"
"net"
"strconv"
"sync"
"time"
"golang.org/x/net/proxy"
"github.com/eycorsican/go-tun2socks/common/dns"
"github.com/eycorsican/go-tun2socks/common/log"
"github.com/eycorsican/go-tun2socks/common/lsof"
"github.com/eycorsican/go-tun2socks/common/stats"
"github.com/eycorsican/go-tun2socks/core"
)
type tcpHandler struct {
sync.Mutex
proxyHost string
proxyPort uint16
fakeDns dns.FakeDns
sessionStater stats.SessionStater
}
func NewTCPHandler(proxyHost string, proxyPort uint16, fakeDns dns.FakeDns, sessionStater stats.SessionStater) core.TCPConnHandler {
return &tcpHandler{
proxyHost: proxyHost,
proxyPort: proxyPort,
fakeDns: fakeDns,
sessionStater: sessionStater,
}
}
type direction byte
const (
dirUplink direction = iota
dirDownlink
)
func statsCopy(dst io.Writer, src io.Reader, sess *stats.Session, dir direction) (written int64, err error) {
buf := make([]byte, 32*1024)
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
switch dir {
case dirUplink:
sess.AddUploadBytes(int64(nw))
case dirDownlink:
sess.AddDownloadBytes(int64(nw))
default:
}
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return written, err
}
type duplexConn interface {
net.Conn
CloseRead() error
CloseWrite() error
}
func (h *tcpHandler) relay(lhs, rhs net.Conn, sess *stats.Session) {
var err error
upCh := make(chan struct{})
cls := func(dir direction, interrupt bool) {
lhsDConn, lhsOk := lhs.(duplexConn)
rhsDConn, rhsOk := rhs.(duplexConn)
if !interrupt && lhsOk && rhsOk {
switch dir {
case dirUplink:
lhsDConn.CloseRead()
rhsDConn.CloseWrite()
case dirDownlink:
lhsDConn.CloseWrite()
rhsDConn.CloseRead()
default:
panic("unexpected direction")
}
} else {
lhs.Close()
rhs.Close()
}
}
// Uplink
go func() {
if h.sessionStater != nil && sess != nil {
_, err = statsCopy(rhs, lhs, sess, dirUplink)
} else {
_, err = io.Copy(rhs, lhs)
}
if err != nil {
cls(dirUplink, true) // interrupt the conn if the error is not nil (not EOF)
} else {
cls(dirUplink, false) // half close uplink direction of the TCP conn if possible
}
upCh <- struct{}{}
}()
// Downlink
if h.sessionStater != nil && sess != nil {
_, err = statsCopy(lhs, rhs, sess, dirDownlink)
} else {
_, err = io.Copy(lhs, rhs)
}
if err != nil {
cls(dirDownlink, true)
} else {
cls(dirDownlink, false)
}
<-upCh // Wait for uplink done.
if h.sessionStater != nil {
h.sessionStater.RemoveSession(lhs)
}
}
func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error {
dialer, err := proxy.SOCKS5("tcp", core.ParseTCPAddr(h.proxyHost, h.proxyPort).String(), nil, nil)
if err != nil {
return err
}
// Replace with a domain name if target address IP is a fake IP.
var targetHost string
if h.fakeDns != nil && h.fakeDns.IsFakeIP(target.IP) {
targetHost = h.fakeDns.QueryDomain(target.IP)
} else {
targetHost = target.IP.String()
}
dest := net.JoinHostPort(targetHost, strconv.Itoa(target.Port))
c, err := dialer.Dial(target.Network(), dest)
if err != nil {
return err
}
var process string
var sess *stats.Session
if h.sessionStater != nil {
// Get name of the process.
localHost, localPortStr, _ := net.SplitHostPort(conn.LocalAddr().String())
localPortInt, _ := strconv.Atoi(localPortStr)
process, err = lsof.GetCommandNameBySocket(target.Network(), localHost, uint16(localPortInt))
if err != nil {
process = "unknown process"
}
sess = &stats.Session{
process,
target.Network(),
conn.LocalAddr().String(),
dest,
0,
0,
time.Now(),
}
h.sessionStater.AddSession(conn, sess)
}
go h.relay(conn, c, sess)
log.Access(process, "proxy", target.Network(), conn.LocalAddr().String(), dest)
return nil
}