-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.go
486 lines (454 loc) · 13.1 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package kpx
import (
"container/list"
"fmt"
"github.com/txthinking/socks5"
"math"
"net"
"os"
"path"
"sync"
"sync/atomic"
"time"
"github.com/fsnotify/fsnotify"
"github.com/palantir/stacktrace"
)
type Proxy struct {
config atomic.Pointer[Config] // atomic
forceStop bool // not atomic - used only for get/set, no conditional update
newRequestId atomic.Int32 // atomic - used in each process
requestsCount atomic.Int32 // atomic - used in each connection
kerberos *KerberosStore // not atomic - used only for get/set, no conditional update - initialized once
lastModTime time.Time // not atomic - used only for get/set in one coroutine
lastLoadTime time.Time // not atomic - used only for get/set in one coroutine
loadCounter atomic.Int32 // atomic - used in each process to test if config has been updated
reloadEvent *ManualResetEvent //
fixWatchEvent *ManualResetEvent //
connPool map[string]*list.List // must be synced - used in each process
poolMutex sync.Mutex // atomic - used in each process
experimentalConnectionPools bool
// krbClients map[string]*KerberosClient //
// configPtr *unsafe.Pointer
}
func (p *Proxy) getConfig() *Config {
return p.config.Load()
}
func (p *Proxy) setConfig(config *Config) {
p.config.Store(config)
p.loadCounter.Add(1)
trace = config.conf.Trace
debug = config.conf.Debug
p.experimentalConnectionPools = config.conf.experimentalConnectionPools
//
features := ""
if config.conf.experimentalConnectionPools {
features += "," + EXPERIMENTAL_CONNETION_POOLS
}
if config.conf.experimentalHostsCache {
features += "," + EXPERIMENTAL_HOSTS_CACHE
}
if features != "" {
logInfo("[-] Experimental features: " + features[1:])
}
}
func (p *Proxy) init() error {
p.forceStop = false
// p.krbClients = make(map[string]*KerberosClient)
// p.configPtr = (*unsafe.Pointer)(unsafe.Pointer(&p.unsafeConfig))
p.reloadEvent = NewManualResetEvent(false)
p.fixWatchEvent = NewManualResetEvent(false)
p.connPool = map[string]*list.List{}
return nil
}
// Initial loading
func (p *Proxy) load() error {
// load config
if options.Config != "" {
file, err := os.Open(options.Config)
if err != nil {
return stacktrace.Propagate(err, "unable to open file")
}
defer func(file *os.File) {
_ = file.Close()
}(file)
stat, err := file.Stat()
if err != nil {
return stacktrace.Propagate(err, "unable to stat file")
}
p.lastModTime = stat.ModTime()
p.lastLoadTime = time.Now()
}
config, err := NewConfig(options.Config)
if err != nil {
return stacktrace.Propagate(err, "unable to create config")
}
p.setConfig(config)
// ask missing credentials
err = config.askCredentials()
if err != nil {
return stacktrace.Propagate(err, "unable to get credentials")
}
// initialize kerberos
k, err := NewKerberosStore(config)
if err != nil {
return stacktrace.Propagate(err, "unable to create kerberos store")
}
p.kerberos = k
// initialize kerberos clients
err = p.loadKerberos(config)
if err != nil {
return stacktrace.Propagate(err, "unable to create kerberos clients")
}
return nil
}
func (p *Proxy) loadKerberos(config *Config) error {
// initialize kerberos clients based on user/realm
for _, proxy := range config.conf.Proxies {
if *proxy.Type == ProxyKerberos && proxy.cred != nil && proxy.cred.isUsed {
if proxy.cred.isNative {
// try to log in with kerberos
err := NativeKerberos.SafeTryLogin()
if err != nil {
return stacktrace.Propagate(err, "unable to login to native os kerberos")
}
} else {
// try to log in with username/password
_, err := p.kerberos.safeTryLogin(*proxy.cred.Login, *proxy.Realm, *proxy.cred.Password, false)
if err != nil {
return stacktrace.Propagate(err, "unable to login to kerberos")
}
}
}
}
return nil
}
func (p *Proxy) watch1() {
if trace {
logInfo("start configuration reload task")
}
for {
select {
case <-p.reloadEvent.Channel():
case <-time.After(RELOAD_TEST_TIMEOUT * time.Second):
}
p.reloadEvent.Reset()
if trace {
logInfo("reload configuration")
}
p.reload()
p.fixWatchEvent.Signal()
}
}
func (p *Proxy) watch2() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
if trace {
logError("watcher error: %v", err)
}
return
}
if trace {
logInfo("start configuration watcher task")
}
timer := time.AfterFunc(math.MaxInt64, func() { p.reloadEvent.Signal() })
timer.Stop()
watchPath := path.Dir(options.Config)
_ = watcher.Add(watchPath)
for {
select {
case <-p.fixWatchEvent.Channel():
// update watcher's list
p.fixWatchEvent.Reset()
wl := watcher.WatchList()
if len(wl) != 1 || wl[0] != watchPath {
if trace {
logInfo("reconfigure watcher")
}
_ = watcher.Add(watchPath)
}
case e, ok := <-watcher.Errors:
// watcher error
if trace {
logInfo("watcher error: ok=%v %v", ok, e)
}
case e, ok := <-watcher.Events:
// watcher event
if trace {
logInfo("watcher event: ok=%v %v", ok, e)
}
if !ok {
continue
}
if path.Base(e.Name) == path.Base(options.Config) && (e.Has(fsnotify.Create) || e.Has(fsnotify.Write)) {
timer.Reset(100 * time.Millisecond)
}
}
}
}
func (p *Proxy) reload() {
stat, err := os.Stat(options.Config)
if err != nil {
return
}
oldConfig := p.getConfig()
if stat.ModTime() == p.lastModTime && time.Now().Before(p.lastLoadTime.Add(RELOAD_FORCE_TIMEOUT*time.Second)) && !oldConfig.needFastReload {
return
}
// test if we need to reload
newConfig, err := NewConfig(options.Config)
p.lastModTime = stat.ModTime()
p.lastLoadTime = time.Now()
if err != nil {
logInfo("[-] Error while reloading configuration: %s", err)
return
}
// test if we can hot-reload - no need for more credentials
for _, cred := range newConfig.conf.Credentials {
// start by copying old credentials
oldCred := oldConfig.conf.Credentials[*cred.name]
if oldCred != nil {
if cred.Login == nil {
cred.Login = oldCred.Login
}
if cred.Password == nil {
cred.Password = oldCred.Password
}
}
// then verify if it used it must have a login/password
if cred.isUsed && !cred.isNative {
if cred.Login == nil || cred.Password == nil {
logInfo("[-] Could not Hot-reload the configuration as it requires new credentials")
return
}
}
}
logInfo("[-] Hot-reload of the configuration succeeded")
// replace current config with the new one
p.setConfig(newConfig)
}
func (p *Proxy) run() error {
config := p.getConfig()
// start automatic exit
if options.Timeout > 0 {
go func() {
<-time.After(time.Duration(options.Timeout) * time.Second)
logDestroy()
os.Exit(0)
}()
logInfo("[-] Proxy will exit automatically in %v seconds", options.Timeout)
}
// start automatic pool vacuum
go func() {
for !p.stopped() {
<-time.After(time.Duration(POOL_CLOSE_TIMEOUT) * time.Second)
p.vacuumPool()
}
}()
// start http server
if config.conf.Port != 0 {
ln, err := net.Listen("tcp4", fmt.Sprint(config.conf.Bind, ":", config.conf.Port))
if err != nil {
return stacktrace.Propagate(err, "unable to listen on %s:%d", config.conf.Bind, config.conf.Port)
}
hostPort := ln.Addr().String()
logInfo("[-] Use %s as your http proxy or http://%s/proxy.pac as your proxy PAC url", hostPort, hostPort)
go func() {
for {
conn, err := ln.Accept()
if err != nil {
continue
}
ConfigureConn(conn)
if p.stopped() {
_ = conn.Close() // force closing client, ignore any error
break
}
if trace {
logInfo("new connection")
}
go func() {
c := p.requestsCount.Add(1)
if trace {
logInfo("connections count=%d", c)
}
NewProcess(p, conn).processHttp()
c = p.requestsCount.Add(-1)
if trace {
logInfo("connections count=%d", c)
}
}()
}
}()
}
// start socks5 server
if config.conf.SocksPort != 0 {
socks, err := socks5.NewClassicServer(fmt.Sprintf("%s:%d", config.conf.Bind, config.conf.SocksPort), config.conf.Bind, "", "", 0, 60)
if err != nil {
return stacktrace.Propagate(err, "unable to create socks server on %s:%d", config.conf.Bind, config.conf.SocksPort)
}
logInfo("[-] Use %s as your socks5 proxy and configure it to use remote dns - curl syntax is 'curl -x socks5h://%s' or 'curl --socks5-hostname %s'", socks.Addr, socks.Addr, socks.Addr)
err = socks.ListenAndServe(p)
if err != nil {
return stacktrace.Propagate(err, "unable to listen on %s:%d", config.conf.Bind, config.conf.SocksPort)
}
}
// wait forever, only exit() can stop or a previous return with an error
select {}
}
func (p *Proxy) TCPHandle(server *socks5.Server, conn *net.TCPConn, request *socks5.Request) error {
if request.Cmd != socks5.CmdConnect {
logInfo("[-] TCP socks proxy is not implemented for command %b", request.Cmd)
return nil
}
// return any address, not important as we are using connect?
a, addr, port, err := socks5.ParseAddress("127.0.0.1:12345")
if err != nil {
conn.Close()
return err
}
if a == socks5.ATYPDomain {
addr = addr[1:]
}
reply := socks5.NewReply(socks5.RepSuccess, a, addr, port)
if _, err := reply.WriteTo(conn); err != nil {
conn.Close()
return err
}
NewProcess(p, conn).processSocks(request)
return nil
}
func (p *Proxy) UDPHandle(server *socks5.Server, addr *net.UDPAddr, datagram *socks5.Datagram) error {
logInfo("[-] UDP socks proxy is not implemented")
return nil
}
func (p *Proxy) stop() {
p.forceStop = true
logDestroy()
os.Exit(1)
}
func (p *Proxy) stopped() bool {
return p.forceStop
}
// generate a new kerberos ticket, using a new client if not yet cached per realm/username/password
func (p *Proxy) generateKerberosNegotiate(username string, realm string, password string, protocol string, host string) (*string, error) {
if p.stopped() {
return nil, nil
}
token, err := p.kerberos.safeGetToken(username, realm, password, protocol, host)
if err != nil {
return nil, stacktrace.Propagate(err, "unable to get kerberos token")
}
auth := "Negotiate " + *token
return &auth, nil
}
func (p *Proxy) generateKerberosNative(protocol string, host string) (*string, error) {
if p.stopped() {
return nil, nil
}
token, err := NativeKerberos.SafeGetToken(protocol, host)
if err != nil {
return nil, stacktrace.Propagate(err, "unable to get kerberos token")
}
auth := "Negotiate " + *token
return &auth, nil
}
type PooledConnection struct {
conn *CloseAwareConn
timeout time.Time
reqId int32
}
type PooledConnectionInfo struct {
key string
conn *CloseAwareConn
reqId int32
}
func (p *Proxy) newPooledConn(dialer *net.Dialer, network string, proxy string, host string, context string, reqId int32) (bool, *PooledConnectionInfo, error) {
key := network + "/" + proxy + "/" + context + "/" + host
if p.experimentalConnectionPools {
p.poolMutex.Lock()
defer p.poolMutex.Unlock()
var items *list.List
if l, ok := p.connPool[key]; ok {
items = l
} else {
l = list.New()
p.connPool[key] = l
items = l
}
for {
item := items.Front()
if item == nil {
break
}
items.Remove(item)
pc := item.Value.(*PooledConnection)
if pc.timeout.After(time.Now()) {
if trace {
logInfo("(%d) reusing connection %d from pool", reqId, pc.reqId)
}
_ = pc.conn.SetDeadline(time.Time{})
pc.conn.Reset(reqId)
return true, &PooledConnectionInfo{key, pc.conn, pc.reqId}, nil
} else {
_ = pc.conn.Close()
}
if trace {
logInfo("(%d) removed old connection %d from pool", reqId, pc.reqId)
}
}
}
// create a new connection
c, err := NewCloseAwareConn(dialer, network, proxy, reqId)
return false, &PooledConnectionInfo{key, c, reqId}, err
}
func (p *Proxy) pushConnToPool(info *PooledConnectionInfo, reqId int32) {
if p.experimentalConnectionPools {
if trace {
logInfo("(%d) pushing connection %d to pool for later reuse", reqId, info.reqId)
}
p.poolMutex.Lock()
defer p.poolMutex.Unlock()
poolTimeout := time.Now().Add(POOL_CLOSE_TIMEOUT * time.Second)
closeTimeout := poolTimeout.Add(POOL_CLOSE_TIMEOUT_ADD * time.Second)
if err := info.conn.SetDeadline(closeTimeout); err == nil {
var items *list.List
if l, ok := p.connPool[info.key]; ok {
items = l
} else {
l = list.New()
p.connPool[info.key] = l
items = l
}
items.PushBack(&PooledConnection{conn: info.conn, timeout: poolTimeout, reqId: info.reqId})
}
}
}
func (p *Proxy) vacuumPool() {
if trace {
logInfo("deleting connections from pool")
}
p.poolMutex.Lock()
defer p.poolMutex.Unlock()
now := time.Now()
total := 0
count := 0
for key, items := range p.connPool {
var next *list.Element
for e := items.Front(); e != nil; e = next {
total++
next = e.Next()
pc := e.Value.(*PooledConnection)
if pc.timeout.After(now) {
count++
_ = pc.conn.Close()
items.Remove(e)
}
}
if items.Len() == 0 {
delete(p.connPool, key)
}
}
if trace {
logInfo("%d connections removed from pool, %d remaining", count, total-count)
}
}