Adds Makefile, simpler concurrency

DecodingLayerParser
Jean-Louis Huynen 2019-01-29 16:06:23 +01:00
parent 6837969a68
commit 8741db8154
2 changed files with 31 additions and 31 deletions

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
arm5l: d4-tlsf.go
env GOOS=linux GOARCH=arm GOARM=5 go build -o d4-tlsf-arm5l d4-tlsf.go
amd64l: d4-tlsf.go
env GOOS=linux GOARCH=amd64 go build -o d4-tlsf-amd64l d4-tlsf.go

View File

@ -57,7 +57,6 @@ var fname = flag.String("r", "", "Filename to read from, overrides -i")
var outCerts = flag.String("w", "", "Folder to write certificates into") var outCerts = flag.String("w", "", "Folder to write certificates into")
var outJSON = flag.String("j", "", "Folder to write certificates into, stdin if not set") var outJSON = flag.String("j", "", "Folder to write certificates into, stdin if not set")
var jobQ chan TLSSession var jobQ chan TLSSession
var cancelC chan string
var memprofile = flag.String("memprofile", "", "Write memory profile") var memprofile = flag.String("memprofile", "", "Write memory profile")
@ -509,8 +508,9 @@ func main() {
cancelC := make(chan string) cancelC := make(chan string)
// We start a worker to send the processed TLS connection the outside world // We start a worker to send the processed TLS connection the outside world
var wg sync.WaitGroup var w sync.WaitGroup
go processCompletedSession(jobQ, cancelC, &wg) w.Add(1)
go processCompletedSession(jobQ, &w)
for packet := range source.Packets() { for packet := range source.Packets() {
count++ count++
@ -582,17 +582,21 @@ func main() {
assembler.FlushAll() assembler.FlushAll()
streamFactory.WaitGoRoutines() streamFactory.WaitGoRoutines()
wg.Wait()
// All systems gone
// We close the processing queue
close(jobQ)
w.Wait()
} }
func processCompletedSession(jobQ <-chan TLSSession, cancelC <-chan string, wg *sync.WaitGroup) { func processCompletedSession(jobQ <-chan TLSSession, w *sync.WaitGroup) {
for { for {
select { tlss, more := <-jobQ
case <-cancelC: if more {
output(tlss)
} else {
w.Done()
return return
case tlss := <-jobQ:
wg.Add(1)
output(tlss, wg)
} }
} }
} }
@ -607,7 +611,7 @@ func queueSession(t TLSSession) bool {
} }
} }
func output(t TLSSession, wg *sync.WaitGroup) { func output(t TLSSession) {
jsonRecord, _ := json.MarshalIndent(t.record, "", " ") jsonRecord, _ := json.MarshalIndent(t.record, "", " ")
@ -615,15 +619,10 @@ func output(t TLSSession, wg *sync.WaitGroup) {
if *outCerts != "" { if *outCerts != "" {
if _, err := os.Stat(fmt.Sprintf("./%s", *outCerts)); !os.IsNotExist(err) { if _, err := os.Stat(fmt.Sprintf("./%s", *outCerts)); !os.IsNotExist(err) {
for _, cert := range t.record.Certificates { for _, cert := range t.record.Certificates {
go func(cert *x509.Certificate) { err := ioutil.WriteFile(fmt.Sprintf("./%s/%s.crt", *outCerts, t.record.Timestamp.Format(time.RFC3339)), cert.Raw, 0644)
wg.Add(1) if err != nil {
err := ioutil.WriteFile(fmt.Sprintf("./%s/%s.crt", *outCerts, t.record.Timestamp.Format(time.RFC3339)), cert.Raw, 0644) panic("Could not write to file.")
if err != nil { }
panic("Could not write to file.")
} else {
wg.Done()
}
}(cert)
} }
} else { } else {
panic(fmt.Sprintf("./%s does not exist", *outCerts)) panic(fmt.Sprintf("./%s does not exist", *outCerts))
@ -633,24 +632,21 @@ func output(t TLSSession, wg *sync.WaitGroup) {
// If an output folder was specified for json files // If an output folder was specified for json files
if *outJSON != "" { if *outJSON != "" {
if _, err := os.Stat(fmt.Sprintf("./%s", *outJSON)); !os.IsNotExist(err) { if _, err := os.Stat(fmt.Sprintf("./%s", *outJSON)); !os.IsNotExist(err) {
go func() { err := ioutil.WriteFile(fmt.Sprintf("./%s/%s.json", *outJSON, t.record.Timestamp.Format(time.RFC3339)), jsonRecord, 0644)
wg.Add(1) if err != nil {
err := ioutil.WriteFile(fmt.Sprintf("./%s/%s.json", *outJSON, t.record.Timestamp.Format(time.RFC3339)), jsonRecord, 0644) panic("Could not write to file.")
if err != nil { }
panic("Could not write to file.")
} else {
wg.Done()
}
}()
} else { } else {
panic(fmt.Sprintf("./%s does not exist", *outJSON)) panic(fmt.Sprintf("./%s does not exist", *outJSON))
} }
// If not folder specidied, we output to stdout // If not folder specidied, we output to stdout
} else { } else {
r := bytes.NewReader(jsonRecord) r := bytes.NewReader(jsonRecord)
io.Copy(os.Stdout, r) _, err := io.Copy(os.Stdout, r)
if err != nil {
panic("Could not write to stdout.")
}
} }
Debug(t.String()) Debug(t.String())
wg.Done()
} }