forked from jpillora/go-tcp-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
183 lines (161 loc) · 3.84 KB
/
proxy.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
package proxy
import (
"io"
"net"
)
// Proxy - Manages a Proxy connection, piping data between local and remote.
type Proxy struct {
sentBytes uint64
receivedBytes uint64
laddr, r1addr, r2addr *net.TCPAddr
lconn, r1conn, r2conn io.ReadWriteCloser
erred bool
errsig chan bool
// this is to hold a copy of the local socket data to be sent to second remote
buffSecondRemote chan []byte
// Settings
Nagles bool
KeepAlive bool
Log Logger
OutputHex bool
}
// New - Create a new Proxy instance. Takes over local connection passed in,
// and closes it when finished.
func New(lconn *net.TCPConn, laddr, r1addr *net.TCPAddr, r2addr *net.TCPAddr) *Proxy {
return &Proxy{
lconn: lconn,
laddr: laddr,
r1addr: r1addr,
r2addr: r2addr,
erred: false,
errsig: make(chan bool),
buffSecondRemote: make(chan []byte, 0xffff),
Log: NullLogger{},
}
}
type setNoDelayer interface {
SetNoDelay(bool) error
}
type setKeepAlive interface {
SetKeepAlive(bool) error
}
// Start - open connection to remote and start proxying data.
func (p *Proxy) Start() {
defer p.lconn.Close()
var err error
//connect to first remote
p.r1conn, err = net.DialTCP("tcp", nil, p.r1addr)
if err != nil {
p.Log.Warn("Remote connection failed: %s", err)
return
}
defer p.r1conn.Close()
//connect to second remote
p.r2conn, err = net.DialTCP("tcp", nil, p.r2addr)
if err != nil {
p.Log.Warn("Second Remote connection failed: %s", err)
return
}
defer p.r2conn.Close()
//nagles?
if p.Nagles {
if conn, ok := p.lconn.(setNoDelayer); ok {
conn.SetNoDelay(true)
}
if conn, ok := p.r1conn.(setNoDelayer); ok {
conn.SetNoDelay(true)
}
if conn, ok := p.r2conn.(setNoDelayer); ok {
conn.SetNoDelay(true)
}
}
//KeepAlive?
if p.KeepAlive {
if conn, ok := p.lconn.(setKeepAlive); ok {
conn.SetKeepAlive(true)
}
if conn, ok := p.r1conn.(setKeepAlive); ok {
conn.SetKeepAlive(true)
}
if conn, ok := p.r2conn.(setKeepAlive); ok {
conn.SetKeepAlive(true)
}
}
//display both ends
p.Log.Info("Opened %s >>> %s", p.laddr.String(), p.r1addr.String())
//bidirectional copy to first remote
go p.pipe(p.lconn, p.r1conn)
go p.pipe(p.r1conn, p.lconn)
// close the buffered channel meant for second remote
defer close(p.buffSecondRemote)
// one way copy to second remote
go p.pipeToSecondRemote(p.r2conn)
//wait for close...
<-p.errsig
p.Log.Info("Closed (%d bytes sent, %d bytes recieved)", p.sentBytes, p.receivedBytes)
}
func (p *Proxy) err(s string, err error) {
if p.erred {
return
}
if err != io.EOF {
p.Log.Warn(s, err)
}
p.errsig <- true
p.erred = true
}
func (p *Proxy) pipe(src, dst io.ReadWriter) {
islocal := src == p.lconn
var dataDirection string
if islocal {
dataDirection = ">>> %d bytes sent%s"
} else {
dataDirection = "<<< %d bytes recieved%s"
}
var byteFormat string
if p.OutputHex {
byteFormat = "%x"
} else {
byteFormat = "%s"
}
//directional copy (64k buffer)
buff := make([]byte, 0xffff)
for {
n, err := src.Read(buff)
if err != nil {
p.err("Read failed '%s'\n", err)
return
}
b := buff[:n]
//show output
p.Log.Debug(dataDirection, n, "")
p.Log.Trace(byteFormat, b)
// send a copy of the socket data to the channel meant for the second remote
go func() {
if islocal {
buff1 := buff[:n]
p.buffSecondRemote <- buff1
}
}()
//write out result
n, err = dst.Write(b)
if err != nil {
p.err("Write failed '%s'\n", err)
return
}
if islocal {
p.sentBytes += uint64(n)
} else {
p.receivedBytes += uint64(n)
}
}
}
func (p *Proxy) pipeToSecondRemote(dst io.ReadWriter) {
for d := range p.buffSecondRemote {
_, err := dst.Write(d)
if err != nil {
p.err("Write failed to second remote '%s'\n", err)
return
}
}
}