Called when a stream is finished. It moves the stream from open_streams to closed_streams, prints output, and dumps the file
(self, key, message='')
| 234 | return pkt |
| 235 | |
| 236 | def __closeStream(self, key, message=''): |
| 237 | """ |
| 238 | Called when a stream is finished. It moves the stream from |
| 239 | open_streams to closed_streams, prints output, and dumps the file |
| 240 | """ |
| 241 | theStream = self.open_streams[key] |
| 242 | if not theStream['filename']: |
| 243 | message = "INCOMPLETE -- missing filename" |
| 244 | else: |
| 245 | theStream['filename'] = theStream['filename'].decode('utf-8', "backslashreplace") |
| 246 | |
| 247 | # Rebuild the file from the individual blocks |
| 248 | rebuiltFile = b'' |
| 249 | for i in sorted(theStream['filedata'].keys()): |
| 250 | rebuiltFile += theStream['filedata'][i] |
| 251 | |
| 252 | # if we're reading, swap the client and server IP so the output better |
| 253 | # shows who requested the connection |
| 254 | if theStream['readwrite'] == 'read': |
| 255 | ipsNports = (key[2], key[3], key[0], key[1]) |
| 256 | else: |
| 257 | ipsNports = key |
| 258 | |
| 259 | # print out information about the stream |
| 260 | msg = "{:5} {} ({} bytes) {}".format( |
| 261 | theStream['readwrite'], |
| 262 | theStream['filename'], |
| 263 | len(rebuiltFile), |
| 264 | message) |
| 265 | self.write(msg, ts=theStream['timestamp'], sip=ipsNports[0], |
| 266 | sport=ipsNports[1], dip=ipsNports[2], dport=ipsNports[3], |
| 267 | readwrite=theStream['readwrite'], filename=theStream['filename']) |
| 268 | |
| 269 | # dump the file, if that's what the user wants |
| 270 | if self.rip and len(rebuiltFile) > 0: |
| 271 | outpath = dshell.util.gen_local_filename(self.outdir, theStream['filename']) |
| 272 | outfile = open(outpath, 'wb') |
| 273 | outfile.write(rebuiltFile) |
| 274 | outfile.close() |
| 275 | |
| 276 | # remove the stream from the list of open streams |
| 277 | self.closed_streams.append(( |
| 278 | key, |
| 279 | self.open_streams[key]['closed_connection'] |
| 280 | )) |
| 281 | del(self.open_streams[key]) |
| 282 |
no test coverage detected