-
Notifications
You must be signed in to change notification settings - Fork 43
/
proxy.go
63 lines (53 loc) · 867 Bytes
/
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
package cracker
import (
"net"
"sync"
"time"
)
type proxyConn struct {
remote net.Conn
uuid string
close chan struct{}
heart chan struct{}
sync.Mutex
hasClosed bool
}
func newProxyConn(remote net.Conn, uuid string) *proxyConn {
return &proxyConn{remote: remote, uuid: uuid,
close: make(chan struct{}),
heart: make(chan struct{}),
}
}
func (pc *proxyConn) Close() {
pc.Lock()
pc.hasClosed = true
pc.Unlock()
select {
case pc.close <- struct{}{}:
default:
}
}
func (pc *proxyConn) IsClosed() bool {
pc.Lock()
defer pc.Unlock()
return pc.hasClosed
}
func (pc *proxyConn) Heart() {
select {
case pc.heart <- struct{}{}:
default:
}
}
func (pc *proxyConn) Do() {
defer pc.remote.Close()
for {
select {
case <-time.After(time.Second * heartTTL):
return
case <-pc.close:
return
case <-pc.heart:
continue
}
}
}