Skip to content

Commit

Permalink
chore: made things a little more go-like
Browse files Browse the repository at this point in the history
  • Loading branch information
howech committed Oct 18, 2024
1 parent 8fafd03 commit 903a8c6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
Binary file added go/gpg-bridge
Binary file not shown.
24 changes: 16 additions & 8 deletions go/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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),
Expand Down Expand Up @@ -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
}
}
26 changes: 18 additions & 8 deletions go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,29 @@ 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 {
log.Println(err)
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
}
}
}

Expand All @@ -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() {
Expand Down

0 comments on commit 903a8c6

Please sign in to comment.