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.
|
|
|
|
|
|
|
|
For efficiency and security we support use of a unix socket, but tcp can be used instead
|
|
|
|
|
|
|
|
# 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 {
|
|
|
|
listener, _ = net.Listen("tcp", tcpBindAddress + ":" + tcpBindPort)
|
|
|
|
} else {
|
|
|
|
listener, _ = net.Listen("tcp", "127.0.0.1:8080")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
---
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
# Starting the server
|
|
|
|
|
|
|
|
``` go
|
|
|
|
--- start websocket server
|
|
|
|
|
|
|
|
func clientConnected(w http.ResponseWriter, r *http.Request) {
|
2022-09-16 05:42:29 +00:00
|
|
|
keyboard, err := sendkeys.NewKBWrapWithOptions(sendkeys.Noisy)
|
2022-09-13 17:28:40 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("upgrade:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2022-09-16 05:42:29 +00:00
|
|
|
// get auth token
|
|
|
|
_, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if auth.CheckAuthToken(string(message)) != nil {
|
|
|
|
log.Println("invalid token")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.WriteMessage(websocket.TextMessage, []byte("authenticated"))
|
|
|
|
|
|
|
|
|
2022-09-13 17:28:40 +00:00
|
|
|
for {
|
2022-09-16 05:42:29 +00:00
|
|
|
time.Sleep(25 * time.Millisecond)
|
2022-09-13 17:28:40 +00:00
|
|
|
_, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Printf("recv: %s", message)
|
|
|
|
message_string := string(message)
|
2022-09-16 05:42:29 +00:00
|
|
|
err = keyboard.Type(message_string)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("type:", err)
|
|
|
|
break
|
2022-09-13 17:28:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartServer() {
|
|
|
|
|
|
|
|
// create http server on unix socket
|
|
|
|
@{create listener}
|
|
|
|
http.HandleFunc("/sendkeys", clientConnected)
|
|
|
|
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"
|
|
|
|
"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
|
|
|
|
|
|
|
|
|
|
|
|
@{start websocket server}
|
2022-09-16 05:42:29 +00:00
|
|
|
---
|
|
|
|
```
|