(conn net.Conn, reader *bufio.Reader)
| 34 | } |
| 35 | |
| 36 | func (s *Server) handleRedisConnection(conn net.Conn, reader *bufio.Reader) { |
| 37 | if s == nil || conn == nil { |
| 38 | return |
| 39 | } |
| 40 | if reader == nil { |
| 41 | reader = bufio.NewReader(conn) |
| 42 | } |
| 43 | |
| 44 | clientIP, localClient := resolveRemoteIP(conn.RemoteAddr()) |
| 45 | authed := false |
| 46 | writer := bufio.NewWriter(conn) |
| 47 | defer func() { |
| 48 | if errClose := conn.Close(); errClose != nil { |
| 49 | log.Errorf("redis connection close error: %v", errClose) |
| 50 | } |
| 51 | }() |
| 52 | |
| 53 | flush := func() bool { |
| 54 | if errFlush := writer.Flush(); errFlush != nil { |
| 55 | log.Errorf("redis protocol flush error: %v", errFlush) |
| 56 | return false |
| 57 | } |
| 58 | return true |
| 59 | } |
| 60 | |
| 61 | if s.cfg != nil && s.cfg.Home.Enabled { |
| 62 | _ = writeRedisError(writer, "ERR redis usage output disabled in home mode") |
| 63 | _ = writer.Flush() |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | for { |
| 68 | if !s.managementRoutesEnabled.Load() { |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | args, errRead := readRESPArray(reader) |
| 73 | if errRead != nil { |
| 74 | if !errors.Is(errRead, io.EOF) { |
| 75 | _ = writeRedisError(writer, "ERR "+errRead.Error()) |
| 76 | _ = writer.Flush() |
| 77 | } |
| 78 | return |
| 79 | } |
| 80 | if len(args) == 0 { |
| 81 | _ = writeRedisError(writer, "ERR empty command") |
| 82 | if !flush() { |
| 83 | return |
| 84 | } |
| 85 | continue |
| 86 | } |
| 87 | |
| 88 | cmd := strings.ToUpper(strings.TrimSpace(args[0])) |
| 89 | |
| 90 | if cmd != "AUTH" && !authed { |
| 91 | if s.mgmt != nil { |
| 92 | _, statusCode, errMsg := s.mgmt.AuthenticateManagementKey(clientIP, localClient, "") |
| 93 | if statusCode == http.StatusForbidden && strings.HasPrefix(errMsg, "IP banned due to too many failed attempts") { |
no test coverage detected