Compare commits

...

2 Commits

Author SHA1 Message Date
Kevin F
6779f87be3 Add FIFO mode for client 2023-01-21 21:28:56 -06:00
Kevin F
718bba4702 Interpret \n as Enter (will need to adjust for Winshit) 2023-01-21 21:27:59 -06:00
5 changed files with 60 additions and 8 deletions

View File

@ -7,12 +7,20 @@ When GoSmartKeyboard is started in client mode, it does the following:
3. Connect to the server.
4 Send the auth token to the server.
5. If the server responds with "authenticated", we start reading keys from stdin and sending them to the server until EOF.
6. If KEYBOARD_FIFO is specified as an environment variable, we read from the path specified there instead as a named pipe.
``` go
--- handle client command
if len(os.Args) > 1 && os.Args[1] == "connect" {
@{start client}
@{get client fifo input file from environment}
@{setup client}
if clientFifioInputFileExists {
@{start client with fifo}
os.Exit(0)
}
@{start client with stdin}
os.Exit(0)
}
@ -26,7 +34,7 @@ The base64 authentication token is loaded from the environment variable `KEYBOAR
``` go
--- start client
--- setup client
@{load connection URL from second CLI argument}
@{get authTokenInput from environment}
@ -80,14 +88,40 @@ if !strings.HasPrefix(connectionURL, "ws://") && !strings.HasPrefix(connectionUR
```
## Sending keys
## Sending keys from a named pipe
``` go
--- start client with fifo
for {
input, err := ioutil.ReadFile(clientFifoInputFile)
if err != nil {
log.Fatal(err)
}
if len(input) > 0 {
fmt.Println("send" + strings.Replace(string(input), " ", "space", 10))
err = client.WriteMessage(websocket.TextMessage, input)
if err != nil {
log.Fatal("write:", err)
}
}
}
---
```
## Sending keys from stdin
We read keys from stdin and send them to the server until we get EOF
``` go
--- start client +=
--- start client with stdin
reader := bufio.NewReader(os.Stdin)
for {

View File

@ -24,6 +24,14 @@ authTokenFile, authTokenFileIsSet := os.LookupEnv("KEYBOARD_AUTH_TOKEN_FILE")
---
## Client fifo
--- get client fifo input file from environment
clientFifoInputFile, clientFifioInputFileExists := os.LookupEnv("KEYBOARD_FIFO")
---
## HTTP Bind Settings

View File

@ -88,6 +88,7 @@ Then we can start the web server and listen for websocket connections.
"io"
"strings"
"bufio"
"io/ioutil"
"log"
@{gorilla/websocket import string}
"keyboard.voidnet.tech/server"

View File

@ -62,6 +62,7 @@ import(
"os"
"net/http"
"fmt"
"strings"
"log"
"keyboard.voidnet.tech/auth"
@{gorilla/websocket import string}

View File

@ -31,7 +31,7 @@ func clientConnected(w http.ResponseWriter, r *http.Request) {
}
c.WriteMessage(websocket.TextMessage, []byte("authenticated"))
var parts []string
for {
time.Sleep(25 * time.Millisecond)
_, message, err := c.ReadMessage()
@ -44,9 +44,17 @@ func clientConnected(w http.ResponseWriter, r *http.Request) {
if message_string == "" {
message_string = "\n"
}
err = keyboard.Type(message_string)
if err != nil {
log.Println("type:", err)
parts = strings.Split(message_string, "\n")
for _, part := range parts {
err = keyboard.Type(part)
if err != nil {
log.Println("type:", err)
}
if len(parts) > 1 {
keyboard.Enter()
}
}
}
}