-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
executable file
·116 lines (90 loc) · 2.6 KB
/
server.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
package main
import (
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"github.com/bvisonl/redis-seer/redis"
)
func startServer() {
listen, err := net.Listen("tcp4", ":"+strconv.Itoa(seerConfig.Port))
if err != nil {
log.Fatalf("Unable to start server on port %d. Error: %s", seerConfig.Port, err)
os.Exit(1)
}
defer listen.Close()
log.Printf("Listening on :%d", seerConfig.Port)
for {
proxyConnection, err := listen.Accept()
if err != nil {
log.Fatalln(err)
continue
}
go initProxy(proxyConnection)
}
}
func initProxy(proxyConnection net.Conn) {
defer proxyConnection.Close()
// Create a new connection with all redis servers
redisConnections := make(map[string]net.Conn, 0)
// Establish connection with all the servers
for key, server := range seerConfig.Servers {
if server.Enabled == false {
continue
}
redisConnection, err := net.Dial("tcp4", server.Host+":"+strconv.Itoa(server.Port))
redisConnections[key] = redisConnection
if err != nil {
log.Printf("An error occurred connection to server: %s. Error: %s\r\n", key, err)
continue
}
defer redisConnection.Close()
}
// Start listening for requests and proxying them
proxy(proxyConnection, redisConnections)
}
func proxy(proxyConnection net.Conn, redisConnections map[string]net.Conn) {
proxyReader := redis.NewReader(proxyConnection)
// Start reading data
for {
proxyData, err := proxyReader.ReadObject()
if err != nil {
if err == io.EOF {
continue
}
fmt.Printf("Error reading data from client. Error: %s\r\n", err)
break
}
target, err := redis.GetTarget(proxyData)
if err != nil {
proxyConnection.Write([]byte("-Error " + err.Error() + "\r\n"))
continue
}
SelectServer:
selectedRedis, err := selectServer(target)
if err != nil {
proxyConnection.Write([]byte("-Error " + err.Error() + "\r\n"))
continue
}
// Send data to the selected redis
if redisConnections[selectedRedis] == nil {
redisConnections[selectedRedis], err = net.Dial("tcp4", seerConfig.Servers[selectedRedis].Host+":"+strconv.Itoa(seerConfig.Servers[selectedRedis].Port))
defer redisConnections[selectedRedis].Close()
if redisConnections[selectedRedis] == nil {
goto SelectServer
}
}
(redisConnections[selectedRedis]).Write(proxyData)
// Get data from selected redis
redisReader := redis.NewReader((redisConnections[selectedRedis]))
redisData, err := redisReader.ReadObject()
if err != nil {
fmt.Printf("Error reading data from Redis %s. Error: %s\r\n", selectedRedis, err)
continue
}
// Relay response from redis back to the client
proxyConnection.Write(redisData)
}
}