From 713c2b0f0443202e884cf762996edb8a7979282b Mon Sep 17 00:00:00 2001 From: 45Tatami Date: Fri, 31 Dec 2021 15:50:08 +0100 Subject: [PATCH] Add native application --- native_application/go.mod | 3 ++ native_application/main.go | 93 ++++++++++++++++++++++++++++++++++++++ native_inserter.json | 7 +++ 3 files changed, 103 insertions(+) create mode 100644 native_application/go.mod create mode 100644 native_application/main.go create mode 100644 native_inserter.json diff --git a/native_application/go.mod b/native_application/go.mod new file mode 100644 index 0000000..eb006f3 --- /dev/null +++ b/native_application/go.mod @@ -0,0 +1,3 @@ +module gorecv + +go 1.17 diff --git a/native_application/main.go b/native_application/main.go new file mode 100644 index 0000000..5729031 --- /dev/null +++ b/native_application/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "encoding/binary" + "encoding/json" + "fmt" + "io" + "log" + "net" +) + +type Message struct { + Body string `json:"body"` +} + +const MSG_SIZE_LIMIT = 1000 * 1000 // Unclear if 1MiB or 1MB, using lower bound +const LISTEN_INTERFACE = ":30501" + +func main() { + log.Println("Listening on", LISTEN_INTERFACE) + + l, err := net.Listen("tcp", LISTEN_INTERFACE) + if err != nil { + log.Fatal(err) + } + defer l.Close() + + msg_pipe := make(chan string) + + // Output routine + go func() { + for { + msg, err := json.Marshal(Message{<-msg_pipe}) + if err != nil { + log.Println("Error encoding message:", err) + continue + } + + msg_len := len(msg) + if msg_len >= MSG_SIZE_LIMIT { + log.Println("Message over webextension limit. Discarding") + continue + } + + len_buf := make([]byte, 4) + binary.LittleEndian.PutUint32(len_buf, uint32(msg_len)) + + log.Println(len_buf, string(msg)) + fmt.Print(string(len_buf)) + fmt.Print(string(msg)) + } + }() + + // Accept connections + for { + conn, err := l.Accept() + if err != nil { + log.Println("Error accepting conn:", err) + } + go conn_hndlr(conn, msg_pipe) + } +} + +func conn_hndlr(c net.Conn, ch chan<- string) { + defer c.Close() + log.Println("Connection opened:", c) + for { + msg, err := read_msg(c) + if err != nil { + break + } + ch <- msg + } + log.Println("Closing connection:", c) +} + +func read_msg(c net.Conn) (string, error) { + len_buf := make([]byte, 4) + + if _, err := io.ReadFull(c, len_buf); err != nil { + log.Println("Len read error:", err) + return "", err + } + + msg_len := binary.LittleEndian.Uint32(len_buf) + msg_buf := make([]byte, msg_len) + if _, err := io.ReadFull(c, msg_buf); err != nil { + log.Println("Msg read error:", err) + return "", err + } + + return string(msg_buf), nil +} diff --git a/native_inserter.json b/native_inserter.json new file mode 100644 index 0000000..e46f5f1 --- /dev/null +++ b/native_inserter.json @@ -0,0 +1,7 @@ +{ + "name": "native_inserter", + "description": "Example host for native messaging", + "path": "/usr/local/bin/gorecv", + "type": "stdio", + "allowed_extensions": [ "@native-inserter" ] +}