-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy.go
110 lines (100 loc) · 2.19 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
package stadis
import (
"io"
"log"
"net"
"sync"
"time"
)
type proxyServer struct {
mu sync.RWMutex
clientName string
proxyName string
proxyPort string
originAddr string
listener net.Listener
}
func newProxyServer(clientName, proxyName, proxyPort, originAddr string) (ps *proxyServer, err error) {
ps = new(proxyServer)
ps.clientName = clientName
ps.proxyName = proxyName
ps.proxyPort = proxyPort
ps.originAddr = originAddr
ps.listener, err = net.Listen("tcp", "localhost:"+ps.proxyPort)
if err != nil {
log.Println("failed to listen proxy", err)
return
}
err = Cli.ServerStarted(ps.proxyName, ps.proxyPort)
if err != nil {
log.Println("at newProxyServer", err)
return
}
return
}
func (ps *proxyServer) serve() {
for {
downstream, err := ps.listener.Accept()
if err != nil {
return
}
originConn, err := net.DialTimeout("tcp", ps.originAddr, time.Second)
if err != nil {
log.Println(err)
downstream.Close()
return
}
clientPort := remotePort(downstream)
err = Cli.ClientConnected(ps.getClientName(), clientPort)
if err != nil {
log.Printf("%v, clinetPort:%s\n", err, clientPort)
return
}
//the delay and failing happens on this upstream conn.
upstream, err := newConnection(originConn, clientPort, ps.proxyPort)
if err != nil {
log.Println(err)
downstream.Close()
originConn.Close()
return
}
go handleCopy(downstream, upstream)
}
}
func (ps *proxyServer) close() (err error) {
ps.listener.Close()
err = Cli.ServerStopped(ps.proxyName, ps.proxyPort)
return
}
func (ps *proxyServer) setClientName(clientName string) {
ps.mu.Lock()
ps.clientName = clientName
ps.mu.Unlock()
}
func (ps *proxyServer) getClientName() (clientName string) {
ps.mu.RLock()
clientName = ps.clientName
ps.mu.RUnlock()
return
}
func handleCopy(downstream, upstream net.Conn) {
done := make(chan bool)
go func() {
n, err := io.Copy(downstream, upstream)
if err != nil {
log.Println(n, err)
}
done <- true
}()
n, err := io.Copy(upstream, downstream)
if err != nil {
log.Println(n, err)
}
<-done
upstream.Close()
downstream.Close()
err = Cli.ClientDisconnected(remotePort(downstream))
if err != nil {
log.Println(err)
}
}