feat: build wahrwelt A/D toolkit
This commit is contained in:
1 parent
303438d206
commit
653924d8c6
564 files changed
+36310
-49998
No files matched your search
@@ -0,0 +1,80 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
linesPerSploitLimit = 100000
|
||||
)
|
||||
|
||||
func NewLogStorage(ctx context.Context, redisURL string) (*LogStorage, error) {
|
||||
opts, err := redis.ParseURL(redisURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("неверный адрес Redis: %w", err)
|
||||
}
|
||||
c := redis.NewClient(opts)
|
||||
if err := c.Ping(ctx).Err(); err != nil {
|
||||
return nil, fmt.Errorf("подключение к Redis: %w", err)
|
||||
}
|
||||
return &LogStorage{rdb: c}, nil
|
||||
}
|
||||
|
||||
type LogStorage struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
func (s *LogStorage) Add(ctx context.Context, lines []LogLine) error {
|
||||
if _, err := s.rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||
for _, line := range lines {
|
||||
key := getRedisStream(line.Exploit, line.Version)
|
||||
vals, err := line.DumpValues()
|
||||
if err != nil {
|
||||
return fmt.Errorf("сериализация строки журнала: %w", err)
|
||||
}
|
||||
args := redis.XAddArgs{
|
||||
Stream: key,
|
||||
MaxLen: linesPerSploitLimit,
|
||||
Approx: true,
|
||||
Values: vals,
|
||||
}
|
||||
if err := pipe.XAdd(ctx, &args).Err(); err != nil {
|
||||
return fmt.Errorf("добавление строки журнала: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("выполнение пакета команд Redis: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LogStorage) Get(ctx context.Context, opts GetOptions) ([]*LogLine, error) {
|
||||
key := getRedisStream(opts.Exploit, strconv.FormatInt(opts.Version, 10))
|
||||
res, err := s.rdb.XRange(ctx, key, "-", "+").Result()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("чтение потока %s: %w", key, err)
|
||||
}
|
||||
lines := make([]*LogLine, 0, len(res))
|
||||
for _, msg := range res {
|
||||
line, err := NewLogLineFromValues(msg.Values)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("разбор строки журнала из Redis: %w", err)
|
||||
}
|
||||
lines = append(lines, line)
|
||||
}
|
||||
return lines, nil
|
||||
}
|
||||
|
||||
type GetOptions struct {
|
||||
Exploit string
|
||||
Version int64
|
||||
}
|
||||
|
||||
func getRedisStream(exploit string, version string) string {
|
||||
return fmt.Sprintf("logs:%s:%s", exploit, version)
|
||||
}
|
||||
Reference in new issue
Block a user