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,72 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
|
||||
logspb "github.com/c4t-but-s4d/neo/v2/proto/go/logs"
|
||||
)
|
||||
|
||||
func NewLogLineFromValues(vals map[string]interface{}) (*LogLine, error) {
|
||||
var line LogLine
|
||||
if err := mapstructure.Decode(vals, &line); err != nil {
|
||||
return nil, fmt.Errorf("разбор структуры строки журнала: %w", err)
|
||||
}
|
||||
return &line, nil
|
||||
}
|
||||
|
||||
func NewLogLineFromProto(p *logspb.LogLine) *LogLine {
|
||||
return &LogLine{
|
||||
Exploit: p.Exploit,
|
||||
Version: strconv.FormatInt(p.Version, 10),
|
||||
Message: p.Message,
|
||||
Level: p.Level,
|
||||
Team: p.Team,
|
||||
}
|
||||
}
|
||||
|
||||
type LogLine struct {
|
||||
Exploit string `mapstructure:"exploit"`
|
||||
Version string `mapstructure:"version"`
|
||||
Message string `mapstructure:"message"`
|
||||
Level string `mapstructure:"level"`
|
||||
Team string `mapstructure:"team"`
|
||||
}
|
||||
|
||||
func (l *LogLine) String() string {
|
||||
return fmt.Sprintf("Line(%s.v%s)", l.Exploit, l.Version)
|
||||
}
|
||||
|
||||
func (l *LogLine) DumpValues() (map[string]interface{}, error) {
|
||||
res := make(map[string]interface{})
|
||||
if err := mapstructure.Decode(l, &res); err != nil {
|
||||
return nil, fmt.Errorf("кодирование структуры строки журнала: %w", err)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (l *LogLine) EstimateSize() int {
|
||||
const (
|
||||
estNum = 5
|
||||
estDenom = 4
|
||||
structSize = 8 * 5
|
||||
)
|
||||
sizeEst := structSize + len(l.Exploit) + len(l.Version) + len(l.Message) + len(l.Level) + len(l.Team)
|
||||
return sizeEst * estNum / estDenom
|
||||
}
|
||||
|
||||
func (l *LogLine) ToProto() (*logspb.LogLine, error) {
|
||||
version, err := strconv.ParseInt(l.Version, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("преобразование версии %v: %w", l.Version, err)
|
||||
}
|
||||
return &logspb.LogLine{
|
||||
Exploit: l.Exploit,
|
||||
Version: version,
|
||||
Message: l.Message,
|
||||
Level: l.Level,
|
||||
Team: l.Team,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
"github.com/c4t-but-s4d/neo/v2/internal/server/common"
|
||||
"github.com/c4t-but-s4d/neo/v2/internal/server/utils"
|
||||
"github.com/c4t-but-s4d/neo/v2/pkg/gstream"
|
||||
logspb "github.com/c4t-but-s4d/neo/v2/proto/go/logs"
|
||||
)
|
||||
|
||||
const (
|
||||
logLinesBatchSize = 100
|
||||
maxMsgSize = 4 * 1024 * 1024
|
||||
)
|
||||
|
||||
func New(storage *LogStorage) *Server {
|
||||
return &Server{
|
||||
LoggingServer: common.NewLoggingServer("logs"),
|
||||
|
||||
storage: storage,
|
||||
}
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
logspb.UnimplementedServiceServer
|
||||
common.LoggingServer
|
||||
|
||||
storage *LogStorage
|
||||
}
|
||||
|
||||
func (s *Server) AddLogLines(ctx context.Context, lines *logspb.AddLogLinesRequest) (*emptypb.Empty, error) {
|
||||
s.GetMethodLogger(ctx).Infof("New request with %d lines", len(lines.Lines))
|
||||
|
||||
decoded := make([]LogLine, 0, len(lines.Lines))
|
||||
for _, line := range lines.Lines {
|
||||
decoded = append(decoded, *NewLogLineFromProto(line))
|
||||
}
|
||||
if err := s.storage.Add(ctx, decoded); err != nil {
|
||||
return nil, utils.WrapErrorf(codes.Internal, "добавление строк журнала: %v", err)
|
||||
}
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (s *Server) SearchLogLines(req *logspb.SearchLogLinesRequest, stream logspb.Service_SearchLogLinesServer) error {
|
||||
s.LogRequest(stream.Context(), req)
|
||||
|
||||
opts := GetOptions{
|
||||
Exploit: req.Exploit,
|
||||
Version: req.Version,
|
||||
}
|
||||
lines, err := s.storage.Get(stream.Context(), opts)
|
||||
if err != nil {
|
||||
return utils.WrapErrorf(codes.Internal, "поиск строк журнала: %v", err)
|
||||
}
|
||||
|
||||
cache := gstream.NewDynamicSizeCache[*LogLine, logspb.SearchLogLinesResponse](
|
||||
stream,
|
||||
maxMsgSize,
|
||||
func(lines []*LogLine) (*logspb.SearchLogLinesResponse, error) {
|
||||
resp := &logspb.SearchLogLinesResponse{
|
||||
Lines: make([]*logspb.LogLine, 0, len(lines)),
|
||||
}
|
||||
for _, line := range lines {
|
||||
protoLine, err := line.ToProto()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("преобразование строки журнала: %w", err)
|
||||
}
|
||||
resp.Lines = append(resp.Lines, protoLine)
|
||||
}
|
||||
return resp, nil
|
||||
},
|
||||
)
|
||||
|
||||
for i, line := range lines {
|
||||
if err := cache.Queue(line); err != nil {
|
||||
return utils.WrapErrorf(codes.Internal, "добавление строки журнала в очередь: %v", err)
|
||||
}
|
||||
if (i-1+logLinesBatchSize)%logLinesBatchSize == 0 {
|
||||
if err := cache.Flush(); err != nil {
|
||||
return utils.WrapErrorf(codes.Internal, "отправка накопленных строк: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := cache.Flush(); err != nil {
|
||||
return utils.WrapErrorf(codes.Internal, "отправка последних строк: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -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