diff --git a/db/packets.go b/db/packets.go index 2816b1e..61e3e38 100644 --- a/db/packets.go +++ b/db/packets.go @@ -17,6 +17,7 @@ import ( "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/meshcore-go/meshcore-go" ) func (s *Store) UpsertPacket(ctx context.Context, p ingest.UpsertPacketParams) (bool, error) { @@ -50,6 +51,10 @@ func (s *Store) UpsertPacket(ctx context.Context, p ingest.UpsertPacketParams) ( return row.Inserted, nil } +func (s *Store) SetPacketDecrypted(ctx context.Context, hash []byte) error { + return s.q.SetPacketDecrypted(ctx, hash) +} + func (s *Store) ListPackets(ctx context.Context, payloadType, routeType int16, iatas []string, scope string, since, until time.Time, cursor int64, limit int32) (api.Page[api.PacketSummary], error) { var cursorTS pgtype.Timestamptz if cursor > 0 { @@ -120,6 +125,40 @@ func (s *Store) GetPacket(ctx context.Context, packetHash []byte) (*api.Packet, if err != nil { return nil, err } + if row.PayloadType == int16(meshcore.PayloadTypeGrpTxt) && row.CmSenderName != nil { + var base struct { + Type string `json:"type"` + Raw string `json:"raw"` + ChannelHash string `json:"channelHash"` + CipherMac string `json:"cipherMac"` + Ciphertext string `json:"ciphertext"` + CiphertextLength int `json:"ciphertextLength"` + Decrypted *struct { + Sender string `json:"sender"` + Content string `json:"content"` + SentAt int64 `json:"sentAt"` + } `json:"decrypted"` + } + if err := json.Unmarshal(row.ParsedPayload, &base); err == nil { + base.Decrypted = &struct { + Sender string `json:"sender"` + Content string `json:"content"` + SentAt int64 `json:"sentAt"` + }{ + Sender: *row.CmSenderName, + Content: func() string { + if row.CmContent != nil { + return *row.CmContent + } + return "" + }(), + SentAt: row.CmSentAt.Time.UnixMilli(), + } + if updated, err := json.Marshal(base); err == nil { + row.ParsedPayload = updated + } + } + } obsRows, err := s.q.ListObservationsForPacket(ctx, packetHash) if err != nil { return nil, err diff --git a/db/queries/queries.sql b/db/queries/queries.sql index 1d3a1b1..acee29f 100644 --- a/db/queries/queries.sql +++ b/db/queries/queries.sql @@ -232,10 +232,17 @@ ON CONFLICT (packet_hash) DO UPDATE SET RETURNING packet_hash, payload_type, payload_version, route_type, transport_codes_present, region_code, sub_region_code, origin_pubkey, raw_payload, raw_header, parsed_payload, decrypted, channel_hash, first_heard_at, last_heard_at, (xmax = 0) AS inserted; +-- name: SetPacketDecrypted :exec +UPDATE packets SET decrypted = true WHERE packet_hash = $1; + -- name: GetPacketByHash :one -SELECT p.*, ts.name AS scope_name +SELECT p.*, ts.name AS scope_name, + cm.sender_name AS cm_sender_name, + cm.content AS cm_content, + cm.sent_at AS cm_sent_at FROM packets p LEFT JOIN transport_scopes ts ON ts.id = p.scope_id +LEFT JOIN channel_messages cm ON cm.packet_hash = p.packet_hash WHERE p.packet_hash = $1; -- name: GetPacketObservationCount :one diff --git a/db/sqlc/queries.sql.go b/db/sqlc/queries.sql.go index ff36144..7927a2a 100644 --- a/db/sqlc/queries.sql.go +++ b/db/sqlc/queries.sql.go @@ -592,9 +592,13 @@ func (q *Queries) GetObserverTelemetry(ctx context.Context, arg GetObserverTelem } const getPacketByHash = `-- name: GetPacketByHash :one -SELECT p.packet_hash, p.payload_type, p.payload_version, p.route_type, p.transport_codes_present, p.region_code, p.sub_region_code, p.scope_id, p.origin_pubkey, p.raw_payload, p.raw_header, p.parsed_payload, p.decrypted, p.channel_hash, p.first_heard_at, p.last_heard_at, ts.name AS scope_name +SELECT p.packet_hash, p.payload_type, p.payload_version, p.route_type, p.transport_codes_present, p.region_code, p.sub_region_code, p.scope_id, p.origin_pubkey, p.raw_payload, p.raw_header, p.parsed_payload, p.decrypted, p.channel_hash, p.first_heard_at, p.last_heard_at, ts.name AS scope_name, + cm.sender_name AS cm_sender_name, + cm.content AS cm_content, + cm.sent_at AS cm_sent_at FROM packets p LEFT JOIN transport_scopes ts ON ts.id = p.scope_id +LEFT JOIN channel_messages cm ON cm.packet_hash = p.packet_hash WHERE p.packet_hash = $1 ` @@ -616,6 +620,9 @@ type GetPacketByHashRow struct { FirstHeardAt pgtype.Timestamptz `json:"first_heard_at"` LastHeardAt pgtype.Timestamptz `json:"last_heard_at"` ScopeName *string `json:"scope_name"` + CmSenderName *string `json:"cm_sender_name"` + CmContent *string `json:"cm_content"` + CmSentAt pgtype.Timestamptz `json:"cm_sent_at"` } func (q *Queries) GetPacketByHash(ctx context.Context, packetHash []byte) (GetPacketByHashRow, error) { @@ -639,6 +646,9 @@ func (q *Queries) GetPacketByHash(ctx context.Context, packetHash []byte) (GetPa &i.FirstHeardAt, &i.LastHeardAt, &i.ScopeName, + &i.CmSenderName, + &i.CmContent, + &i.CmSentAt, ) return i, err } @@ -2337,6 +2347,15 @@ func (q *Queries) SetNodeMultibyteTraces(ctx context.Context, id uuid.UUID) erro return err } +const setPacketDecrypted = `-- name: SetPacketDecrypted :exec +UPDATE packets SET decrypted = true WHERE packet_hash = $1 +` + +func (q *Queries) SetPacketDecrypted(ctx context.Context, packetHash []byte) error { + _, err := q.db.Exec(ctx, setPacketDecrypted, packetHash) + return err +} + const updateObserverStatus = `-- name: UpdateObserverStatus :one UPDATE observers SET display_name = COALESCE(NULLIF($2, ''), display_name), diff --git a/internal/ingest/ingest.go b/internal/ingest/ingest.go index b62886a..2981757 100644 --- a/internal/ingest/ingest.go +++ b/internal/ingest/ingest.go @@ -78,6 +78,9 @@ type DB interface { // UpsertPacket inserts or bumps the packets row. Returns (isNew, error). UpsertPacket(ctx context.Context, p UpsertPacketParams) (bool, error) + // SetPacketDecrypted marks a packet as decrypted in the DB + SetPacketDecrypted(ctx context.Context, hash []byte) error + // InsertObservation inserts a packet_observations row. // Returns (inserted, error); inserted=false means ON CONFLICT DO NOTHING fired. InsertObservation(ctx context.Context, o InsertObservationParams) (bool, error) diff --git a/internal/ingest/side_effects.go b/internal/ingest/side_effects.go index c95cc38..e8379f7 100644 --- a/internal/ingest/side_effects.go +++ b/internal/ingest/side_effects.go @@ -153,6 +153,9 @@ func (w *Worker) handlePayloadTypeSideEffects(ctx context.Context, packet *meshc } if newMsg { + if err := w.db.SetPacketDecrypted(ctx, packetHash[:]); err != nil { + log.Printf("ingest[%s]: failed to set packet decrypted: %v", w.cfg.BrokerName, err) + } evt := channelMessageEvent{ ChannelID: channelID, ChannelHash: hex.EncodeToString(channelHashBytes),