gosmartkeyboard/docs/Server.html

153 lines
5.9 KiB
HTML
Raw Permalink Normal View History

2023-03-07 22:29:26 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>GoSmartKeyboard</title>
<link rel="stylesheet" href="google-code-prettify/prettify.css">
<link rel="stylesheet" href="styles/prettify-theme.css">
<script defer src="google-code-prettify/prettify.js"></script>
<script defer src="google-code-prettify/run_prettify.js"></script>
<link rel="stylesheet" href="styles/main.css">
</head>
<!-- Generated by srcweave https://github.com/justinmeiners/srcweave -->
<h1>Keyboard socket server<a id="c13"></a></h1>
<p>The server has two jobs, to authenticate and to accept a stream of key presses from the client.</p>
<p>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.</p>
<h1>Server Entrypoint<a id="c14"></a></h1>
<p>Before main execution, both the server and client check for a version command line argument. If it is present, the program will print the version and exit.</p>
<p>First, we make sure a token is provisioned. In the future we will use the system keyring.</p>
<p>Then we can start the web server and listen for websocket connections.</p>
<div class="code-block">
<span class="block-header">
<strong class="block-title"><em><a id="entrypoint-block-45" href="#entrypoint-block-45">entrypoint</a></em></strong></span>
<pre class="prettyprint"><code class=""> func main(){
<em class="block-link nocode" title="Plumbing.html"><a href="Plumbing.html#handle-version-command-block-42">@{handle version command}</a></em>
tokenBase64, _ := auth.ProvisionToken()
if len(tokenBase64) &gt; 0 {
fmt.Println("This is your authentication token, it will only be shown once: " + tokenBase64)
}
server.StartServer()
}
</code></pre>
<p class="block-usages"><small>Used by <a href="#-server-main.go-block-47" title="/server/main.go">1</a> </small></p></div>
<div class="code-block">
<span class="block-header">
<strong class="block-title"><em><a id="-server-main.go-block-47" href="#-server-main.go-block-47">/server/main.go</a></em></strong></span>
<pre class="prettyprint"><code class=""> package main
import(
"fmt"
"os"
"keyboard.voidnet.tech/server"
"keyboard.voidnet.tech/auth"
)
<em class="block-link nocode"><a href="#entrypoint-block-45">@{entrypoint}</a></em>
</code></pre>
</div>
<h1>Picking a socket type and setting the listener<a id="c15"></a></h1>
<div class="code-block">
<span class="block-header">
<strong class="block-title"><em><a id="create-listener-block-49" href="#create-listener-block-49">create listener</a></em></strong></span>
<pre class="prettyprint"><code class=""><em class="block-link nocode" title="EnvironmentVariables.html"><a href="EnvironmentVariables.html#unixsocketpath-block-22">@{unixSocketPath}</a></em> // gets unixSocketPath from environment, unixSocketPathExists defines if it exists
<em class="block-link nocode" title="EnvironmentVariables.html"><a href="EnvironmentVariables.html#tcpbindaddress-block-24">@{TCPBindAddress}</a></em> // gets tcpBindAddress from environment, tcpBindAddressExists defines if it exists
<em class="block-link nocode" title="EnvironmentVariables.html"><a href="EnvironmentVariables.html#tcpbindport-block-26">@{TCPBindPort}</a></em> // gets tcpBindPort from environment, tcpBindPortExists defines if it exists
if unixSocketPathExists {
listener, _ = net.Listen("unix", unixSocketPath)
} else{
if tcpBindAddressExists &amp;&amp; tcpBindPortExists {
listener, _ = net.Listen("tcp", tcpBindAddress + ":" + tcpBindPort)
} else {
listener, _ = net.Listen("tcp", "127.0.0.1:8080")
}
}
</code></pre>
<p class="block-usages"><small>Used by <a href="#start-http-server-block-51" title="start http server">1</a> </small></p></div>
<h2>1. HTTP API endpoints<a id="s15:0"></a></h2>
<div class="code-block">
<span class="block-header">
<strong class="block-title"><em><a id="start-http-server-block-51" href="#start-http-server-block-51">start http server</a></em></strong></span>
<pre class="prettyprint"><code class="">func StartServer() {
<em class="block-link nocode"><a href="#create-listener-block-49">@{create listener}</a></em>
fmt.Println("Listening on", listener.Addr())
http.HandleFunc("/sendkeys", clientConnected)
//http.HandleFunc("/activewindow", )
http.Serve(listener, nil)
}
</code></pre>
<p class="block-usages"><small>Used by <a href="#-server-server-server.go-block-53" title="/server/server/server.go">1</a> </small></p></div>
<div class="code-block">
<span class="block-header">
<strong class="block-title"><em><a id="-server-server-server.go-block-53" href="#-server-server-server.go-block-53">/server/server/server.go</a></em></strong></span>
<pre class="prettyprint"><code class="">package server
import(
"net"
"time"
"os"
"os/exec"
"strings"
"regexp"
"net/http"
"fmt"
"log"
"keyboard.voidnet.tech/auth"
<em class="block-link nocode" title="Dependencies.html"><a href="Dependencies.html#gorilla-websocket-import-string-block-11">@{gorilla/websocket import string}</a></em>
<em class="block-link nocode" title="Dependencies.html"><a href="Dependencies.html#keylogger-import-string-block-9">@{keylogger import string}</a></em>
)
var listener net.Listener
var upgrader = websocket.Upgrader{} // use default options
<em class="block-link nocode" title="Streaming.html"><a href="Streaming.html#streaming-keyboard-input-block-56">@{streaming keyboard input}</a></em>
<em class="block-link nocode"><a href="#start-http-server-block-51">@{start http server}</a></em>
</code></pre>
</div>
</body>
</html>