aboutsummaryrefslogtreecommitdiff
path: root/xP/xP.go
diff options
context:
space:
mode:
Diffstat (limited to 'xP/xP.go')
-rw-r--r--xP/xP.go83
1 files changed, 67 insertions, 16 deletions
diff --git a/xP/xP.go b/xP/xP.go
index ba63fe9..7e0c386 100644
--- a/xP/xP.go
+++ b/xP/xP.go
@@ -1,4 +1,4 @@
-// Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
+// Copyright (c) 2022 - 2025, Přemysl Eric Janouch <p@janouch.name>
// SPDX-License-Identifier: 0BSD
package main
@@ -6,12 +6,16 @@ package main
import (
"bufio"
"context"
+ "crypto/sha1"
+ "embed"
"encoding/binary"
+ "encoding/hex"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
+ "io/fs"
"log"
"net"
"net/http"
@@ -23,7 +27,12 @@ import (
)
var (
- debug = flag.Bool("debug", false, "enable debug output")
+ debug = flag.Bool("debug", false, "enable debug output")
+ webRoot = flag.String("webroot", "", "override bundled web resources")
+
+ //go:embed public/*
+ webResources embed.FS
+ webResourcesHash string
addressBind string
addressConnect string
@@ -75,12 +84,12 @@ func relayMakeReceiver(ctx context.Context, conn net.Conn) <-chan []byte {
go func() {
defer close(p)
for {
- j := relayReadFrame(r)
- if j == nil {
+ b := relayReadFrame(r)
+ if b == nil {
return
}
select {
- case p <- j:
+ case p <- b:
case <-ctx.Done():
return
}
@@ -145,8 +154,7 @@ func clientWriteError(ctx context.Context, ws *websocket.Conn, err error) bool {
b, ok := (&RelayEventMessage{
EventSeq: 0,
Data: RelayEventData{
- Interface: RelayEventDataError{
- Event: RelayEventError,
+ Variant: &RelayEventDataError{
CommandSeq: 0,
Error: err.Error(),
},
@@ -241,21 +249,20 @@ func handleWS(w http.ResponseWriter, r *http.Request) {
// -----------------------------------------------------------------------------
-var staticHandler = http.FileServer(http.Dir("."))
-
var page = template.Must(template.New("/").Parse(`<!DOCTYPE html>
<html>
<head>
<title>xP</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
+ <base href="{{ .Root }}/">
<link rel="stylesheet" href="xP.css" />
</head>
<body>
<script src="mithril.js">
</script>
<script>
- let proxy = '{{ . }}'
+ let proxy = '{{ .Proxy }}'
</script>
<script type="module" src="xP.js">
</script>
@@ -263,20 +270,49 @@ var page = template.Must(template.New("/").Parse(`<!DOCTYPE html>
</html>`))
func handleDefault(w http.ResponseWriter, r *http.Request) {
- if r.URL.Path != "/" {
- staticHandler.ServeHTTP(w, r)
- return
- }
-
wsURI := addressWS
if wsURI == "" {
wsURI = fmt.Sprintf("ws://%s/ws", r.Host)
}
- if err := page.Execute(w, wsURI); err != nil {
+
+ args := struct {
+ Root string
+ Proxy string
+ }{
+ Root: webResourcesHash,
+ Proxy: wsURI,
+ }
+ if err := page.Execute(w, &args); err != nil {
log.Println("Template execution failed: " + err.Error())
}
}
+func hashFS(root fs.FS) []byte {
+ hasher := sha1.New()
+ callback := func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ }
+
+ // Note that this can be fooled.
+ fmt.Fprintln(hasher, path)
+
+ if !d.IsDir() {
+ file, err := root.Open(path)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+ io.Copy(hasher, file)
+ }
+ return nil
+ }
+ if err := fs.WalkDir(root, ".", callback); err != nil {
+ log.Fatalln(err)
+ }
+ return hasher.Sum(nil)
+}
+
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
@@ -295,6 +331,21 @@ func main() {
addressWS = flag.Arg(2)
}
+ subResources, err := fs.Sub(webResources, "public")
+ if err != nil {
+ log.Fatalln(err)
+ }
+ if *webRoot != "" {
+ subResources = os.DirFS(*webRoot)
+ }
+
+ // The simplest way of ensuring that web browsers don't use
+ // stale cached copies of our files.
+ webResourcesHash = hex.EncodeToString(hashFS(subResources))
+ http.Handle("/"+webResourcesHash+"/",
+ http.StripPrefix("/"+webResourcesHash+"/",
+ http.FileServerFS(subResources)))
+
http.Handle("/ws", http.HandlerFunc(handleWS))
http.Handle("/", http.HandlerFunc(handleDefault))