Skip to content

Commit

Permalink
Compress data for transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Mar 15, 2024
1 parent 3e4bd53 commit a0e5e0f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
61 changes: 45 additions & 16 deletions cmd/witnessctl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
package main

import (
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"time"

"github.com/cheggaaa/pb/v3"
Expand Down Expand Up @@ -80,24 +81,52 @@ func (d Device) hab() error {
}

func (d Device) getLogMessages(cmd byte) (string, error) {
b := strings.Builder{}
req := &api.LogMessagesRequest{}
rsp := &api.LogMessagesResponse{More: true}
for rsp.More {
rb, _ := proto.Marshal(req)
buf, err := d.u2f.Command(cmd, rb)
if err != nil {
return "", err
r, w := io.Pipe()
defer r.Close()

errC := make(chan error, 1)
// Kick off a goroutine to fetch chunks of log and pipe it into the
// decompressor.
go func() {
// Signal that there's no more compressed data.
defer w.Close()
defer close(errC)

req := &api.LogMessagesRequest{}
rsp := &api.LogMessagesResponse{More: true}
for rsp.More {
rb, _ := proto.Marshal(req)
buf, err := d.u2f.Command(cmd, rb)
if err != nil {
errC <- err
return
}
if err := proto.Unmarshal(buf, rsp); err != nil {
errC <- err
return
}
w.Write(rsp.GetPayload())
req.Continue = true

// Don't overload the HID endpoint
time.Sleep(10 * time.Millisecond)
}
if err := proto.Unmarshal(buf, rsp); err != nil {
return "", err
}
b.Write(rsp.GetPayload())
req.Continue = true
time.Sleep(10 * time.Millisecond)
}()

gz, err := gzip.NewReader(r)
if err != nil {
log.Printf("Failed to create gzip reader: %v", err)
return "", err
}
gz.Close()

// Grab the decompressed logs, and return
s, err := io.ReadAll(gz)
if err != nil {
return "", err
}

return b.String(), nil
return string(s), <-errC
}

func (d Device) consoleLogs() (string, error) {
Expand Down
18 changes: 15 additions & 3 deletions trusted_os/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package main

import (
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"errors"
Expand Down Expand Up @@ -168,9 +170,19 @@ func (ctl *controlInterface) handleLogsRequest(r []byte, l func() []byte) (res [
if !req.Continue {
log.Printf("Grabbing log messages...")
logs := l()
log.Printf("Found %d bytes of log messages to send", len(logs))
ctl.logBuffer = make([]byte, len(logs))
copy(ctl.logBuffer, logs)
ll := len(logs)
b := &bytes.Buffer{}
gz := gzip.NewWriter(b)
if _, err := gz.Write(logs); err != nil {
log.Printf("Failed to gzip logs: %v", err)
}
if err := gz.Close(); err != nil {
log.Printf("Failed to close gzip writer: %v", err)

}
logs = nil
ctl.logBuffer = b.Bytes()
log.Printf("Compressed %d bytes of log messages to %d send", ll, len(ctl.logBuffer))
}
ret := &api.LogMessagesResponse{}
if l := len(ctl.logBuffer); l > maxChunkSize {
Expand Down

0 comments on commit a0e5e0f

Please sign in to comment.