mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-09-01 16:48:19 +00:00
add decrypted channel msg to packet details
also caught a bug where we never set the packet as being decrypted even when it had been
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
+20
-1
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user