gourmet icon indicating copy to clipboard operation
gourmet copied to clipboard

#1: attempt a fix

Open j4ng5y opened this issue 5 years ago • 2 comments

remove the need to read the file in first, append the logfile json contents to the file instead of re-writing it

j4ng5y avatar Oct 04 '19 16:10 j4ng5y

This won't work since the Connections JSON object is an array. Right now, this is what your changes do:

{
  "NetworkInterface": "eno1",
  "NetworkAddress": [
    "192.168.1.169/24",
    "fe80::b7e9:59c1:b3d9:6ba0/64"
  ],
  "Connections": null
}{
  "Connections": [
    {
      "Timestamp": "2019-10-07T19:37:39.082109227-04:00",
      "UID": 12935341172492116558,
      "SourceIP": "192.168.1.72",
      "SourcePort": 5353,
      "DestinationIP": "224.0.0.251",
      "DestinationPort": 5353,
      "TransportType": "udp",
      "Analyzers": {}
    }
  ]
}{
  "Connections": [
    {
      "Timestamp": "2019-10-07T19:37:39.082278705-04:00",
      "UID": 12935341172492116558,
      "SourceIP": "192.168.1.72",
      "SourcePort": 5353,
      "DestinationIP": "224.0.0.251",
      "DestinationPort": 5353,
      "TransportType": "udp",
      "Analyzers": {}
    }
  ]
}

The logic needs to be a bit more complex so that:

  1. If this is the first Connection we see, simply append it to the Connections object via Marshal/Unmarshaling the entire file.
  2. For 2 to n connections, os.Seek() into the file until we are just past the closing curly brace of the previous connection. Then, add a comma and append the new Connection. This will allow us to insert a new Connection into the existing JSON array without having to Unmarshal/Marshal the entire array.

I can finish this up if it's not clear what needs to be done. Thanks for your help!

spitfire55 avatar Oct 07 '19 23:10 spitfire55

Sorry, I guess I missed that it's an array. I can work on revising here shortly

j4ng5y avatar Oct 08 '19 16:10 j4ng5y