added server.md scaffolding
This commit is contained in:
parent
88f414e0b6
commit
32f3e6fb86
10
Makefile
10
Makefile
@ -1,7 +1,7 @@
|
|||||||
weave:
|
weave:
|
||||||
srcweave --formatter srcweave-format --weave docs/ ReadMe.go.md security/Authentication.md Dependencies.md
|
srcweave --formatter srcweave-format --weave docs/ ReadMe.md security/Authentication.md EnvironmentVariables.md Dependencies.md Server.md
|
||||||
tangle:
|
tangle:
|
||||||
srcweave --formatter srcweave-format --tangle smartkeyboard/ ReadMe.go.md security/Authentication.md EnvironmentVariables.md Dependencies.md
|
srcweave --formatter srcweave-format --tangle smartkeyboard/ ReadMe.md security/Authentication.md EnvironmentVariables.md Dependencies.md Server.md
|
||||||
clean:
|
clean:
|
||||||
rm -rf docs
|
rm -rf docs
|
||||||
find smartkeyboard/ -type f -not -name "*_test.go" -delete
|
find smartkeyboard/ -type f -not -name "*_test.go" -delete
|
||||||
@ -9,8 +9,8 @@ clean:
|
|||||||
rm go.sum
|
rm go.sum
|
||||||
|
|
||||||
test: tangle
|
test: tangle
|
||||||
-go mod init voidnet.tech/m/v2
|
-cd smartkeyboard && go mod init keyboard.voidnet.tech
|
||||||
go mod tidy
|
-cd smartkeyboard && go mod tidy
|
||||||
go test -v ./...
|
-cd smartkeyboard && go test -v ./...
|
||||||
|
|
||||||
all: weave tangle
|
all: weave tangle
|
13
ReadMe.md
13
ReadMe.md
@ -45,20 +45,24 @@ A smart keyboard could, for example, be used for the following:
|
|||||||
|
|
||||||
First, we call
|
First, we call
|
||||||
|
|
||||||
|
``` go
|
||||||
|
|
||||||
--- entrypoint
|
--- entrypoint
|
||||||
|
|
||||||
func main(){
|
func main(){
|
||||||
fmt.Println("Hello, World!")
|
server.StartServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
--- /main.go
|
--- /main.go
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@{set network bind globals}
|
import(
|
||||||
|
"keyboard.voidnet.tech/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@{entrypoint}
|
@{entrypoint}
|
||||||
|
|
||||||
@ -75,4 +79,5 @@ First, we call
|
|||||||
var string tcpBindPort
|
var string tcpBindPort
|
||||||
var bool tcpBindPortExists
|
var bool tcpBindPortExists
|
||||||
|
|
||||||
---
|
---
|
||||||
|
```
|
103
Server.md
Normal file
103
Server.md
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
# 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) {
|
||||||
|
kb, err := keybd_event.NewKeyBonding()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Print("upgrade:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
|
for {
|
||||||
|
_, message, err := c.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("read:", err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Printf("recv: %s", message)
|
||||||
|
message_string := string(message)
|
||||||
|
for _, char := range message_string {
|
||||||
|
// make sure that the char is a number
|
||||||
|
number, err := strconv.Atoi(string(char))
|
||||||
|
if err == nil {
|
||||||
|
kb.SetKeys(number)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// sendkeys
|
||||||
|
//kb.SetKeys(30, 32, 33)
|
||||||
|
go kb.Launching()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartServer() {
|
||||||
|
|
||||||
|
// create http server on unix socket
|
||||||
|
@{create listener}
|
||||||
|
http.HandleFunc("/sendkeys", clientConnected)
|
||||||
|
http.Serve(listener, nil)
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
```
|
||||||
|
|
||||||
|
--- /server/server.go
|
||||||
|
package server
|
||||||
|
|
||||||
|
import(
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"net/http"
|
||||||
|
"log"
|
||||||
|
@{gorilla/websocket import string}
|
||||||
|
@{keybd_event import string}
|
||||||
|
)
|
||||||
|
|
||||||
|
var listener net.Listener
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{} // use default options
|
||||||
|
|
||||||
|
|
||||||
|
@{start websocket server}
|
||||||
|
---
|
Loading…
Reference in New Issue
Block a user