2023-07-17 05:02:29 +02:00
|
|
|
package browserbiometrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
2024-03-15 15:59:35 +01:00
|
|
|
"fmt"
|
2023-07-17 05:02:29 +02:00
|
|
|
"os"
|
|
|
|
"unsafe"
|
|
|
|
|
2024-05-04 01:06:24 +02:00
|
|
|
"github.com/quexten/goldwarden/cli/browserbiometrics/logging"
|
2023-07-17 05:02:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const bufferSize = 8192 * 8
|
|
|
|
|
|
|
|
var nativeEndian binary.ByteOrder
|
|
|
|
|
|
|
|
func setupCommunication() {
|
2024-04-05 21:06:23 +02:00
|
|
|
// determine native endianness
|
2023-07-17 05:02:29 +02:00
|
|
|
var one int16 = 1
|
|
|
|
b := (*byte)(unsafe.Pointer(&one))
|
|
|
|
if *b == 0 {
|
|
|
|
nativeEndian = binary.BigEndian
|
|
|
|
} else {
|
|
|
|
nativeEndian = binary.LittleEndian
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 15:59:35 +01:00
|
|
|
func dataToBytes(msg SendMessage) ([]byte, error) {
|
2023-07-17 05:02:29 +02:00
|
|
|
byteMsg, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
2024-03-15 15:59:35 +01:00
|
|
|
return nil, fmt.Errorf("unable to marshal OutgoingMessage struct to slice of bytes: %w", err)
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
2024-03-15 15:59:35 +01:00
|
|
|
return byteMsg, nil
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
|
|
|
|
2024-03-15 15:59:35 +01:00
|
|
|
func writeMessageLength(msg []byte) error {
|
2023-07-17 05:02:29 +02:00
|
|
|
err := binary.Write(os.Stdout, nativeEndian, uint32(len(msg)))
|
|
|
|
if err != nil {
|
2024-03-15 15:59:35 +01:00
|
|
|
return fmt.Errorf("unable to write message length to stdout: %w", err)
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
2024-03-15 15:59:35 +01:00
|
|
|
return nil
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
|
|
|
|
2024-03-15 15:59:35 +01:00
|
|
|
func readMessageLength(msg []byte) (int, error) {
|
2024-04-29 19:49:57 +02:00
|
|
|
var length int32
|
2023-07-17 05:02:29 +02:00
|
|
|
buf := bytes.NewBuffer(msg)
|
|
|
|
err := binary.Read(buf, nativeEndian, &length)
|
|
|
|
if err != nil {
|
2024-03-15 15:59:35 +01:00
|
|
|
return 0, fmt.Errorf("Unable to read bytes representing message length: %w", err)
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
2024-04-29 19:49:57 +02:00
|
|
|
return int(length), nil
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
|
|
|
|
2024-03-15 15:59:35 +01:00
|
|
|
func send(msg SendMessage) error {
|
|
|
|
byteMsg, err := dataToBytes(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-22 15:29:58 +01:00
|
|
|
logging.Debugf("[SENSITIVE] Sending message: " + string(byteMsg))
|
2024-03-15 15:59:35 +01:00
|
|
|
err = writeMessageLength(byteMsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-17 05:02:29 +02:00
|
|
|
|
|
|
|
var msgBuf bytes.Buffer
|
2024-03-15 15:59:35 +01:00
|
|
|
_, err = msgBuf.Write(byteMsg)
|
2023-07-17 05:02:29 +02:00
|
|
|
if err != nil {
|
2024-03-15 15:59:35 +01:00
|
|
|
return err
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = msgBuf.WriteTo(os.Stdout)
|
2024-03-15 15:59:35 +01:00
|
|
|
return err
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|