-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.go
228 lines (188 loc) · 4.77 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package tcpproxy
import (
"log"
"net"
"sync"
"time"
)
// Proxy is a TCP proxy that proxies all incoming connections to a remote address.
type Proxy struct {
// enable debug log
DebugLog bool
// listen address for incoming connections
listenAddress string
// proxy incoming connections to here
remoteAddress string
remote *remote
// save state in this file to survive restarts
StatePath string
// listen http on this address for management interface
MgmtListenAddr string
// seconds to wait before killing open connections on remote address switch
GracePeriod time.Duration
// time to wait when connecting the server
ConnectTimeout time.Duration
// tcp keepalive period
Keepalive time.Duration
// holds open connections
conns sync.Map
// when should we resolve again?
ResolvePeriod time.Duration
// channel is closed when proxy is ready to accept connections
Ready chan struct{}
// listens RemoteAddress for incoming connections
proxyListener net.Listener
// listens HTTP requests for management interface
mgmtListener net.Listener
// for waiting active connections on Shutdown
wg sync.WaitGroup
// will be closed when Shutdown is called
shutdown chan struct{}
m sync.Mutex
}
// NewProxy returns a new Proxy server.
func NewProxy(listenAddress, remoteAddress string) *Proxy {
return &Proxy{
listenAddress: listenAddress,
remoteAddress: remoteAddress,
GracePeriod: 10 * time.Second,
ConnectTimeout: 10 * time.Second,
Keepalive: 60 * time.Second,
ResolvePeriod: 10 * time.Second,
Ready: make(chan struct{}),
shutdown: make(chan struct{}),
}
}
// Run the proxy by listening the address and accepting incoming connections.
func (p *Proxy) Run() {
p.remote = newRemote(p.remoteAddress, p.ResolvePeriod)
p.loadState()
var err error
p.proxyListener, err = net.Listen("tcp", p.listenAddress)
if err != nil {
log.Fatalln("cannot listen address:", err)
}
if p.MgmtListenAddr != "" {
p.mgmtListener, err = net.Listen("tcp", p.MgmtListenAddr)
if err != nil {
log.Fatalln("cannot listen address:", err)
}
go p.serveMgmt()
}
close(p.Ready)
for {
conn, err := p.proxyListener.Accept()
if err != nil {
select {
case <-p.shutdown:
return
default:
}
log.Println("cannot accept inbound connection:", err)
continue
}
p.wg.Add(1)
go p.handleConn(conn)
}
}
// Shutdown the proxy gracefully.
func (p *Proxy) Shutdown() error {
close(p.shutdown)
err := p.proxyListener.Close()
if err != nil {
return err
}
if p.mgmtListener != nil {
err = p.mgmtListener.Close()
if err != nil {
return err
}
}
p.wg.Wait()
return nil
}
func (p *Proxy) handleConn(in net.Conn) {
debugln("connected", in.RemoteAddr())
c := newProxyConn(in)
p.conns.Store(c, nil)
p.proxyConn(c)
p.conns.Delete(c)
in.Close()
debugln("disconnected", in.RemoteAddr())
p.wg.Done()
}
func (p *Proxy) proxyConn(c *proxyConn) {
setKeepAlive(c.in, p.Keepalive)
err := p.connectRemote(c)
if err != nil {
log.Println("cannot connect remote address:", err)
return
}
defer c.out.Close()
setKeepAlive(c.out, p.Keepalive)
<-c.copyStream()
}
func (p *Proxy) connectRemote(c *proxyConn) error {
connectAddr, err := p.remote.getIP()
if err != nil {
return err
}
for {
rconn, err := net.DialTimeout("tcp", connectAddr, p.ConnectTimeout)
if err != nil {
return err
}
addr, err := p.remote.getIP()
if err != nil {
return err
}
if addr != connectAddr {
// Remote address has changed while we are connecting to it.
// If changed, close the current remote connection and connect to new address.
rconn.Close()
connectAddr = addr
continue
}
c.Lock()
c.out = rconn
c.Unlock()
return nil
}
}
// GetRemoteAddress returns the current remote address.
func (p *Proxy) GetRemoteAddress() string {
return p.remote.getAddr()
}
// SetRemoteAddress sets the remote address.
// When remote address has been changed, after p.GracePeriod duration,
// all previous connections are killed if they are still alive.
func (p *Proxy) SetRemoteAddress(newAddr string) {
log.Println("changing remote address to", newAddr)
log.Println("old remote address was", p.remote.getAddr())
p.m.Lock()
defer p.m.Unlock()
p.remote.setAddr(newAddr)
p.saveState()
log.Println("remote address has been changed to", newAddr)
go p.killOldConns()
}
func (p *Proxy) killOldConns() {
log.Println("waiting for open connections to shutdown gracefully for", p.GracePeriod)
time.Sleep(p.GracePeriod)
addr := p.remote.getAddr()
var count int
handleConn := func(key, value interface{}) bool {
c := key.(*proxyConn)
c.Lock()
if c.out != nil {
if c.out.RemoteAddr().String() != addr {
c.out.Close()
count++
}
}
c.Unlock()
return true
}
p.conns.Range(handleConn)
log.Println("killed", count, "old connections")
}