2022-09-13 17:28:40 +00:00
# Keyboard socket server
The server has two jobs, to authenticate and to accept a stream of key presses from the client.
2022-12-27 23:05:32 +00:00
For efficiency and security we support use of a unix socket, but tcp can be used instead. In the case of TCP, the server will listen on 127.1 by default but can be configured to listen on a different address and port. In any case, it is highly recommended to run the server behind a reverse proxy supporting HTTPS such as nginx or caddy.
2022-09-13 17:28:40 +00:00
# Picking a socket type and setting the listener
``` go
--- create listener
@{unixSocketPath} // gets unixSocketPath from environment, unixSocketPathExists defines if it exists
@{TCPBindAddress} // gets tcpBindAddress from environment, tcpBindAddressExists defines if it exists
@{TCPBindPort} // gets tcpBindPort from environment, tcpBindPortExists defines if it exists
if unixSocketPathExists {
listener, _ = net.Listen("unix", unixSocketPath)
} else{
if tcpBindAddressExists & & tcpBindPortExists {
2023-01-02 07:24:35 +00:00
2022-09-13 17:28:40 +00:00
listener, _ = net.Listen("tcp", tcpBindAddress + ":" + tcpBindPort)
} else {
listener, _ = net.Listen("tcp", "127.0.0.1:8080")
}
}
---
```
2022-12-27 23:05:32 +00:00
## HTTP API endpoints
2022-09-13 17:28:40 +00:00
2022-12-27 23:05:32 +00:00
2023-01-02 07:24:35 +00:00
``` go
2022-12-27 23:05:32 +00:00
--- start http server
2022-09-13 17:28:40 +00:00
func StartServer() {
@{create listener}
2023-01-02 07:24:35 +00:00
fmt.Println("Listening on", listener.Addr())
2022-09-13 17:28:40 +00:00
http.HandleFunc("/sendkeys", clientConnected)
2022-12-31 06:34:03 +00:00
//http.HandleFunc("/activewindow", )
2022-09-13 17:28:40 +00:00
http.Serve(listener, nil)
}
---
2022-09-16 05:42:29 +00:00
2022-09-13 17:28:40 +00:00
--- /server/server.go
package server
import(
"net"
2022-09-16 05:42:29 +00:00
"time"
2022-09-13 17:28:40 +00:00
"os"
"net/http"
2023-01-02 07:24:35 +00:00
"fmt"
2023-01-22 03:28:56 +00:00
"strings"
2022-09-13 17:28:40 +00:00
"log"
2022-09-16 05:42:29 +00:00
"keyboard.voidnet.tech/auth"
2022-09-13 17:28:40 +00:00
@{gorilla/websocket import string}
2022-09-16 05:42:29 +00:00
@{sendkeys import string}
2022-09-13 17:28:40 +00:00
)
var listener net.Listener
var upgrader = websocket.Upgrader{} // use default options
2022-12-27 23:05:32 +00:00
@{streaming keyboard input}
2022-12-31 06:34:03 +00:00
@{start http server}
2022-09-16 05:42:29 +00:00
---
```