-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
131 lines (109 loc) · 2.62 KB
/
handler.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
package main
import (
"io"
"io/ioutil"
"log"
"net"
"strings"
"github.com/tidwall/redcon"
)
type proxy struct {
phase phaseType
srcRedisAddr string
dstRedisAddr string
}
type proxyContext struct {
phase phaseType
src net.Conn
dst net.Conn
}
func (p *proxy) handler(conn redcon.Conn, cmd redcon.Command) {
ctx, ok := conn.Context().(*proxyContext)
if !ok {
return
}
command := strings.ToLower(string(cmd.Args[0]))
if _, isCmdReadOnly := readOnlyCmds[command]; isCmdReadOnly {
var readTarget net.Conn
if ctx.phase == WriteBothReadSrc {
readTarget = ctx.src
} else {
readTarget = ctx.dst
}
_, err := readTarget.Write(cmd.Raw)
if err != nil {
log.Printf("Write failed: %v", err)
}
return
}
if ctx.phase == WriteBothReadSrc || ctx.phase == WriteBothReadDst {
if _, err := ctx.src.Write(cmd.Raw); err != nil {
log.Printf("src.Write failed: %v", err)
}
}
if _, err := ctx.dst.Write(cmd.Raw); err != nil {
log.Printf("dst.Write failed: %v", err)
}
}
func (p *proxy) relayReplies(client io.Writer, server io.Reader) {
for {
_, err := io.Copy(client, server)
if err == io.EOF {
return
}
if checkNetOpError(err) != nil {
log.Printf("io.Copy error: %v", err)
return
}
}
}
// onAccept is called when a client connects. If the function
// returns true, connection is accepted.
func (p *proxy) onAccept(conn redcon.Conn) bool {
log.Printf("client connected: %s\n", conn.RemoteAddr())
src, err := net.Dial("tcp", p.srcRedisAddr)
if err != nil {
log.Printf("net.Dial(%s) failed: %v", p.srcRedisAddr, err)
return false
}
dst, err := net.Dial("tcp", p.dstRedisAddr)
if err != nil {
log.Printf("net.Dial(%s) failed: %v", p.dstRedisAddr, err)
src.Close()
return false
}
switch p.phase {
case WriteBothReadSrc:
go p.relayReplies(conn.NetConn(), src)
go p.relayReplies(ioutil.Discard, dst)
case WriteBothReadDst:
go p.relayReplies(conn.NetConn(), dst)
go p.relayReplies(ioutil.Discard, src)
case WriteDstReadDst:
go p.relayReplies(conn.NetConn(), dst)
}
conn.SetContext(&proxyContext{
src: src,
dst: dst,
phase: p.phase,
})
return true
}
// onClose is called when a client connection is disconnected.
func (p *proxy) onClose(conn redcon.Conn, err error) {
log.Printf("client disconnected: %s, err: %v\n", conn.RemoteAddr(), err)
ctx, ok := conn.Context().(*proxyContext)
if ok {
ctx.src.Close()
ctx.dst.Close()
}
}
func checkNetOpError(err error) error {
if err != nil {
netOpError, ok := err.(*net.OpError)
if ok && strings.HasSuffix(netOpError.Err.Error(), "use of closed network connection") {
return nil
}
}
return err
}