diff --git a/go/gpg-bridge b/go/gpg-bridge new file mode 100755 index 0000000..c0a76e5 Binary files /dev/null and b/go/gpg-bridge differ diff --git a/go/gpg.go b/go/gpg.go index a5e8b51..9a8f5fb 100644 --- a/go/gpg.go +++ b/go/gpg.go @@ -32,12 +32,15 @@ type websocket_command struct { Fingerprint string `json: "fingerprint,omitempty"` } -func gpg_sign_message(message string, fingerprint string) websocket_result { +func gpg_sign_message(message string, fingerprint string, results chan <- websocket_result) { decoded, _ := base64.StdEncoding.DecodeString(message) tempfile, _ := os.CreateTemp("", "message-*") tempfile.Write(decoded) defer os.Remove(tempfile.Name()) + results <- websocket_result{ + Communication: "Signing process started. Please touch your Yubikey.", + } command := exec.Command( "gpg", @@ -58,13 +61,14 @@ func gpg_sign_message(message string, fingerprint string) websocket_result { result, err := command.Output() if err != nil { - return websocket_result{ + results <- websocket_result{ Communication: "Signing failed", Error: buf.String(), } + return } - return websocket_result{ + results <- websocket_result{ Communication: "Message has been signed successfully.", Message: message, Signature: string(result), @@ -135,25 +139,29 @@ func gpg_getkeys() websocket_result { //} -func process_command(data []byte) websocket_result { +func process_command(data []byte, results chan<- websocket_result) { var command websocket_command err := json.Unmarshal(data, &command) if err != nil { - return websocket_result{ + results <- websocket_result{ Communication: "Invalid payload.", } + return } switch command.Command { case "sign": - return gpg_sign_message(command.Message, command.Fingerprint) + gpg_sign_message(command.Message, command.Fingerprint, results) + return case "getkeys": - return gpg_getkeys() + results <- gpg_getkeys() + return default: - return websocket_result{ + results <- websocket_result{ Communication: "Unknown command.", } + return } } diff --git a/go/main.go b/go/main.go index 62c5b0a..6746995 100644 --- a/go/main.go +++ b/go/main.go @@ -18,7 +18,17 @@ var upgrader = websocket.Upgrader{ WriteBufferSize: 1024, } -func reader(conn *websocket.Conn) { +func writer(conn *websocket.Conn, results <-chan websocket_result) { + for result := range results { + response_json, _ := json.Marshal(result) + if err := conn.WriteMessage(websocket.TextMessage, response_json); err != nil { + log.Println(err) + } + } +} + + +func reader(conn *websocket.Conn, results chan<- websocket_result) { for { messageType, message, err := conn.ReadMessage() if err != nil { @@ -26,13 +36,11 @@ func reader(conn *websocket.Conn) { return } - response := process_command(message) - response_json, _ := json.Marshal(response) + if messageType == websocket.TextMessage { + process_command(message, results) + close(results) + } - if err := conn.WriteMessage(messageType, response_json); err != nil { - log.Println(err) - return - } } } @@ -47,7 +55,9 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { // helpful log statement to show connections log.Println("Client Connected") - reader(ws) + results := make(chan websocket_result, 5) + go writer(ws, results) + reader(ws, results) } func setupRoutes() {