Skip to content

Commit

Permalink
feat: add AUTH command
Browse files Browse the repository at this point in the history
  • Loading branch information
andrelcunha committed Nov 20, 2024
1 parent a1107cc commit a87592f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
9 changes: 7 additions & 2 deletions cmd/redis-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ func main() {
// Set up AOF
aofChan := make(chan string)

// Start the server
// Set up the store
s := store.NewStore(aofChan)
srv := server.NewServer(s)

// Set up configuration
config := server.NewConfig()

// Start the server
srv := server.NewServer(s, config)

//Load snapshot on startup
if err := persistence.LoadSnapshot(s, "snapshot.gob"); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package server

type Config struct {
Port string
Password string
}

func NewConfig() *Config {
return &Config{
Port: "6379",
Password: "guest",
}
}
42 changes: 38 additions & 4 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import (
"net"
"strconv"
"strings"
"sync"
"time"

"com.github.andrelcunha.go-redis-clone/pkg/store"
)

// Server represents a TCP server
type Server struct {
store *store.Store
store *store.Store
config *Config
mu sync.Mutex
authenticatedConnections map[net.Conn]bool
}

// NewServer creates a new server
func NewServer(s *store.Store) *Server {
return &Server{store: s}
func NewServer(store *store.Store, config *Config) *Server {
return &Server{
store: store,
config: config,
authenticatedConnections: make(map[net.Conn]bool),
}
}

// Start starts the server
Expand Down Expand Up @@ -62,8 +70,28 @@ func (s *Server) handleCommand(conn net.Conn, cmd string) {
return
}

//check authentication
if !s.isAuthenticates(conn) && parts[0] != "AUTH" {
fmt.Fprintln(conn, "NOAUTH Authentication required.")
return
}

switch parts[0] {

case "AUTH":
if len(parts) != 2 {
fmt.Fprintln(conn, "ERR wrong number of arguments for 'AUTH' command")
return
}
if parts[1] == s.config.Password {
s.mu.Lock()
s.authenticatedConnections[conn] = true
s.mu.Unlock()
fmt.Fprintln(conn, "OK")
} else {
fmt.Fprintln(conn, "ERR invalid password")
}

case "SET":
if len(parts) != 3 {
fmt.Fprintln(conn, "ERR wrong number of arguments for 'SET' command")
Expand Down Expand Up @@ -159,6 +187,12 @@ func (s *Server) handleCommand(conn net.Conn, cmd string) {

default:
fmt.Fprintln(conn, "ERR unknown command '"+parts[0]+"'")
fmt.Fprintln(conn, "Available commands: SET, GET, DEL, EXISTS, SETNX, EXPIRE, INCR, DECR")
fmt.Fprintln(conn, "Available commands: AUTH, SET, GET, DEL, EXISTS, SETNX, EXPIRE, INCR, DECR")
}
}

func (s *Server) isAuthenticates(conn net.Conn) bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.authenticatedConnections[conn]
}

0 comments on commit a87592f

Please sign in to comment.