From b804f09c42fad9847e94b9a2101a1bae0a413a6d Mon Sep 17 00:00:00 2001 From: Magdy AboKhedr Date: Tue, 7 Apr 2026 14:23:40 +0200 Subject: [PATCH] fix: parse SHIP messages correctly in live TCP and logtail sources --- internal/capture/source_logtail.go | 30 +++++++++++++++++++++++++----- internal/capture/source_tcp.go | 30 +++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/internal/capture/source_logtail.go b/internal/capture/source_logtail.go index dffe0a5..cbca86e 100644 --- a/internal/capture/source_logtail.go +++ b/internal/capture/source_logtail.go @@ -135,13 +135,13 @@ func (s *LogTailSource) processLine(line string, baseDate time.Time, emit func(* Timestamp: ts, Direction: dir, NormalizedJSON: json.RawMessage(normalized), - ShipMsgType: model.ShipMsgTypeData, } peerDevice := parser.ExtractPeerDevice(peer) spineMsg := s.parser.ParseSpineFromJSON(normalized) if spineMsg != nil { + msg.ShipMsgType = model.ShipMsgTypeData msg.SpinePayload = spineMsg.SpinePayload msg.CmdClassifier = spineMsg.CmdClassifier msg.FunctionSet = spineMsg.FunctionSet @@ -154,11 +154,31 @@ func (s *LogTailSource) processLine(line string, baseDate time.Time, emit func(* msg.FeatureSource = spineMsg.FeatureSource msg.FeatureDest = spineMsg.FeatureDest } else { - msg.ParseError = "could not parse SPINE datagram from log line" - if dir == model.DirectionOutgoing { - msg.DeviceDest = peerDevice + shipResult, err := s.parser.ParseShipFromJSON(normalized) + if err == nil { + msg.ShipMsgType = shipResult.ShipMsgType + msg.ShipPayload = shipResult.ShipPayload + if shipResult.Spine != nil { + msg.SpinePayload = shipResult.Spine.SpinePayload + msg.CmdClassifier = shipResult.Spine.CmdClassifier + msg.FunctionSet = shipResult.Spine.FunctionSet + msg.MsgCounter = shipResult.Spine.MsgCounter + msg.MsgCounterRef = shipResult.Spine.MsgCounterRef + msg.DeviceSource = shipResult.Spine.DeviceSource + msg.DeviceDest = shipResult.Spine.DeviceDest + msg.EntitySource = shipResult.Spine.EntitySource + msg.EntityDest = shipResult.Spine.EntityDest + msg.FeatureSource = shipResult.Spine.FeatureSource + msg.FeatureDest = shipResult.Spine.FeatureDest + } } else { - msg.DeviceSource = peerDevice + msg.ShipMsgType = model.ShipMsgTypeUnknown + msg.ParseError = "could not parse message: " + err.Error() + if dir == model.DirectionOutgoing { + msg.DeviceDest = peerDevice + } else { + msg.DeviceSource = peerDevice + } } } diff --git a/internal/capture/source_tcp.go b/internal/capture/source_tcp.go index f849b43..fd660c1 100644 --- a/internal/capture/source_tcp.go +++ b/internal/capture/source_tcp.go @@ -236,13 +236,13 @@ func (s *TCPSource) emitMessage(timeStr, direction, peer, jsonPayload string, ba Timestamp: ts, Direction: dir, NormalizedJSON: json.RawMessage(normalized), - ShipMsgType: model.ShipMsgTypeData, } peerDevice := parser.ExtractPeerDevice(peer) spineMsg := s.parser.ParseSpineFromJSON(normalized) if spineMsg != nil { + msg.ShipMsgType = model.ShipMsgTypeData msg.SpinePayload = spineMsg.SpinePayload msg.CmdClassifier = spineMsg.CmdClassifier msg.FunctionSet = spineMsg.FunctionSet @@ -255,11 +255,31 @@ func (s *TCPSource) emitMessage(timeStr, direction, peer, jsonPayload string, ba msg.FeatureSource = spineMsg.FeatureSource msg.FeatureDest = spineMsg.FeatureDest } else { - msg.ParseError = "could not parse SPINE datagram from log line" - if dir == model.DirectionOutgoing { - msg.DeviceDest = peerDevice + shipResult, err := s.parser.ParseShipFromJSON(normalized) + if err == nil { + msg.ShipMsgType = shipResult.ShipMsgType + msg.ShipPayload = shipResult.ShipPayload + if shipResult.Spine != nil { + msg.SpinePayload = shipResult.Spine.SpinePayload + msg.CmdClassifier = shipResult.Spine.CmdClassifier + msg.FunctionSet = shipResult.Spine.FunctionSet + msg.MsgCounter = shipResult.Spine.MsgCounter + msg.MsgCounterRef = shipResult.Spine.MsgCounterRef + msg.DeviceSource = shipResult.Spine.DeviceSource + msg.DeviceDest = shipResult.Spine.DeviceDest + msg.EntitySource = shipResult.Spine.EntitySource + msg.EntityDest = shipResult.Spine.EntityDest + msg.FeatureSource = shipResult.Spine.FeatureSource + msg.FeatureDest = shipResult.Spine.FeatureDest + } } else { - msg.DeviceSource = peerDevice + msg.ShipMsgType = model.ShipMsgTypeUnknown + msg.ParseError = "could not parse message: " + err.Error() + if dir == model.DirectionOutgoing { + msg.DeviceDest = peerDevice + } else { + msg.DeviceSource = peerDevice + } } }