Added KeepAlive config in tcp listener.

Implemented delete user functionality from ACL.
This commit is contained in:
Kelvin Clement Mwinuka
2023-12-18 05:07:47 +08:00
parent 501a435157
commit 29b7d808eb
3 changed files with 72 additions and 24 deletions

View File

@@ -156,13 +156,23 @@ func (server *Server) handleConnection(ctx context.Context, conn net.Conn) {
for { for {
message, err := utils.ReadMessage(connRW) message, err := utils.ReadMessage(connRW)
if err != nil && err == io.EOF {
break
}
if err != nil { if err != nil {
if err == io.EOF {
// Connection closed
break
}
if err, ok := err.(net.Error); ok && err.Timeout() {
// Connection timeout
fmt.Println(err)
break
}
if err, ok := err.(tls.RecordHeaderError); ok {
// TLS verification error
fmt.Println(err)
break
}
fmt.Println(err) fmt.Println(err)
continue break
} }
if cmd, err := utils.Decode(message); err != nil { if cmd, err := utils.Decode(message); err != nil {
@@ -264,7 +274,21 @@ func (server *Server) handleConnection(ctx context.Context, conn net.Conn) {
func (server *Server) StartTCP(ctx context.Context) { func (server *Server) StartTCP(ctx context.Context) {
conf := server.config conf := server.config
var listener net.Listener
listenConfig := net.ListenConfig{
KeepAlive: 200 * time.Millisecond,
}
listener, err := listenConfig.Listen(ctx, "tcp", fmt.Sprintf("%s:%d", conf.BindAddr, conf.Port))
if err != nil {
log.Fatal(err)
}
if !conf.TLS {
// TCP
fmt.Printf("Starting TCP server at Address %s, Port %d...\n", conf.BindAddr, conf.Port)
}
if conf.TLS { if conf.TLS {
// TLS // TLS
@@ -274,23 +298,9 @@ func (server *Server) StartTCP(ctx context.Context) {
log.Fatal(err) log.Fatal(err)
} }
if l, err := tls.Listen("tcp", fmt.Sprintf("%s:%d", conf.BindAddr, conf.Port), &tls.Config{ listener = tls.NewListener(listener, &tls.Config{
Certificates: []tls.Certificate{cer}, Certificates: []tls.Certificate{cer},
}); err != nil { })
log.Fatal(err)
} else {
listener = l
}
}
if !conf.TLS {
// TCP
fmt.Printf("Starting TCP server at Address %s, Port %d...\n", conf.BindAddr, conf.Port)
if l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", conf.BindAddr, conf.Port)); err != nil {
log.Fatal(err)
} else {
listener = l
}
} }
// Listen to connection // Listen to connection

View File

@@ -12,6 +12,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"time"
) )
type Password struct { type Password struct {
@@ -133,6 +134,37 @@ func (acl *ACL) RegisterConnection(conn *net.Conn) {
} }
} }
func (acl *ACL) DeleteUser(usernames []string) error {
var user User
for _, username := range usernames {
if username == "default" {
// Skip default user
continue
}
// Extract the user
for _, u := range acl.Users {
if username == u.Username {
user = u
}
}
// Skip if the current username was not found in the ACL
if username != user.Username {
continue
}
// Terminate every connection attached to this user
for connRef, connection := range acl.Connections {
if connection.User.Username == user.Username {
(*connRef).SetReadDeadline(time.Now().Add(-1 * time.Second))
}
}
// Delete the user from the ACL
acl.Users = utils.Filter(acl.Users, func(u User) bool {
return u.Username != user.Username
})
}
return nil
}
func (acl *ACL) AuthenticateConnection(conn *net.Conn, cmd []string) error { func (acl *ACL) AuthenticateConnection(conn *net.Conn, cmd []string) error {
var passwords []Password var passwords []Password
var user User var user User

View File

@@ -237,7 +237,13 @@ func (p Plugin) handleSetUser(ctx context.Context, cmd []string, server utils.Se
} }
func (p Plugin) handleDelUser(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) { func (p Plugin) handleDelUser(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) {
return nil, errors.New("ACL DELUSER not implemented") if len(cmd) < 3 {
return nil, errors.New(utils.WRONG_ARGS_RESPONSE)
}
if err := p.acl.DeleteUser(cmd[2:]); err != nil {
return nil, err
}
return []byte(utils.OK_RESPONSE), nil
} }
func (p Plugin) handleWhoAmI(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) { func (p Plugin) handleWhoAmI(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {
@@ -395,7 +401,7 @@ func NewModule(acl *ACL) Plugin {
{ {
Command: "deluser", Command: "deluser",
Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory}, Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory},
Description: "(ACL DELUSER) Deletes users and terminates their connections", Description: "(ACL DELUSER) Deletes users and terminates their connections. Cannot delete default user",
Sync: true, Sync: true,
KeyExtractionFunc: func(cmd []string) ([]string, error) { KeyExtractionFunc: func(cmd []string) ([]string, error) {
return []string{}, nil return []string{}, nil