|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "log" |
| 6 | + "net" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/tidwall/redcon" |
| 10 | +) |
| 11 | + |
| 12 | +type Handler struct { |
| 13 | + config Config |
| 14 | + netMode string |
| 15 | +} |
| 16 | + |
| 17 | +type HandlerContext struct { |
| 18 | + upstreamConn net.Conn |
| 19 | + username string |
| 20 | +} |
| 21 | + |
| 22 | +func NewHandler(config Config) *Handler { |
| 23 | + netMode := "tcp" |
| 24 | + if strings.HasPrefix(config.UpstreamRedis, "/") || strings.HasPrefix(config.UpstreamRedis, "@") { |
| 25 | + netMode = "unix" |
| 26 | + } |
| 27 | + return &Handler{ |
| 28 | + config: config, |
| 29 | + netMode: netMode, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (m *Handler) ServeRESP(conn redcon.Conn, cmd redcon.Command) { |
| 34 | + context, ok := conn.Context().(HandlerContext) |
| 35 | + if !ok || context.upstreamConn == nil { |
| 36 | + conn.Close() |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + upConn := context.upstreamConn |
| 41 | + command := strings.ToUpper(string(cmd.Args[0])) |
| 42 | + |
| 43 | + // Modify key (if applicable) |
| 44 | + if modCommands[command] && len(cmd.Args) > 1 { |
| 45 | + cmd.Args[1] = append([]byte(context.username+":"), cmd.Args[1]...) |
| 46 | + } |
| 47 | + |
| 48 | + // Construct RESP command |
| 49 | + request := buildRESPCommand(cmd.Args) |
| 50 | + |
| 51 | + // Send to Redis |
| 52 | + _, err := upConn.Write(request) |
| 53 | + if err != nil { |
| 54 | + log.Printf("Failed to send command: %v", err) |
| 55 | + conn.Close() |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // Read response from Redis |
| 60 | + response, err := bufio.NewReader(upConn).ReadBytes('\n') |
| 61 | + if err != nil { |
| 62 | + log.Printf("Failed to read response: %v", err) |
| 63 | + conn.Close() |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + // Send response back to client |
| 68 | + conn.WriteRaw(response) |
| 69 | +} |
| 70 | + |
| 71 | +func (m *Handler) AcceptConn(conn redcon.Conn) bool { |
| 72 | + redisConn, err := net.Dial(m.netMode, m.config.UpstreamRedis) |
| 73 | + if err != nil { |
| 74 | + log.Printf("Failed to connect to Redis: %v", err) |
| 75 | + return false |
| 76 | + } |
| 77 | + |
| 78 | + conn.SetContext(HandlerContext{ |
| 79 | + upstreamConn: redisConn, |
| 80 | + username: "default", // TODO: Fetch username dynamically |
| 81 | + }) |
| 82 | + return true |
| 83 | +} |
| 84 | + |
| 85 | +func (m *Handler) ClosedConn(conn redcon.Conn, err error) { |
| 86 | + context, ok := conn.Context().(HandlerContext) |
| 87 | + if ok && context.upstreamConn != nil { |
| 88 | + context.upstreamConn.Close() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func buildRESPCommand(args [][]byte) []byte { |
| 93 | + var sb strings.Builder |
| 94 | + sb.WriteString("*") |
| 95 | + sb.WriteString(strings.TrimSpace(string([]byte{byte(len(args) + '0')}))) |
| 96 | + sb.WriteString("\r\n") |
| 97 | + for _, arg := range args { |
| 98 | + sb.WriteString("$") |
| 99 | + sb.WriteString(strings.TrimSpace(string([]byte{byte(len(arg) + '0')}))) |
| 100 | + sb.WriteString("\r\n") |
| 101 | + sb.Write(arg) |
| 102 | + sb.WriteString("\r\n") |
| 103 | + } |
| 104 | + return []byte(sb.String()) |
| 105 | +} |
0 commit comments