| 12 | import {Loader} from "src/domain/loads/types" |
| 13 | |
| 14 | class BrimcapLoader implements Loader { |
| 15 | constructor(private ctx: LoadContext, private root: string) {} |
| 16 | |
| 17 | async when() { |
| 18 | const file = this.ctx.files[0] |
| 19 | return file && (await isPcap(file)) |
| 20 | } |
| 21 | |
| 22 | async run() { |
| 23 | if (this.ctx.files.length > 1) { |
| 24 | throw new Error("Only one PCAP can be loaded at a time") |
| 25 | } |
| 26 | this.ctx.setProgress(0) |
| 27 | await this.load(this.startPipeline()) |
| 28 | this.index() |
| 29 | configureZeekPool(this.ctx.poolId) |
| 30 | this.ctx.setProgress(1) |
| 31 | } |
| 32 | |
| 33 | startPipeline() { |
| 34 | return pipeline(fs.createReadStream(this.pcap), this.analyze(), (err) => { |
| 35 | if (err) { |
| 36 | this.ctx.abort() |
| 37 | this.ctx.addError(errorToString(err)) |
| 38 | } |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | analyze() { |
| 43 | const process = createAnalyzeProcess(this.ctx.signal) |
| 44 | const stream = createTransform(process) |
| 45 | const totalSize = fs.statSync(this.pcap).size |
| 46 | monitorAnalyzeProgress(process, ({type, ...status}) => { |
| 47 | switch (type) { |
| 48 | case "status": |
| 49 | this.ctx.setProgress(status.pcap_read_size / totalSize || 0) |
| 50 | this.ctx.onPoolChanged() |
| 51 | break |
| 52 | case "warning": |
| 53 | if (status.warning) this.ctx.addError(status.warning) |
| 54 | break |
| 55 | case "error": |
| 56 | if (status.error) stream.destroy(new Error(status.error)) |
| 57 | break |
| 58 | } |
| 59 | }) |
| 60 | return stream |
| 61 | } |
| 62 | |
| 63 | index() { |
| 64 | const cli = createCli() |
| 65 | try { |
| 66 | cli.index({root: this.root, pcap: this.pcap}) |
| 67 | } catch (e) { |
| 68 | console.error(e) |
| 69 | throw errors.pcapIngest(e) |
| 70 | } |
| 71 | } |
nothing calls this directly
no outgoing calls
no test coverage detected