mirror of https://github.com/dirtbags/moth.git
Working cache support
This commit is contained in:
parent
0e54a6f9f5
commit
95bcc860e0
|
@ -10,7 +10,9 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -265,15 +267,16 @@ func (ctx *Instance) dehydrateHandler(w http.ResponseWriter, req *http.Request)
|
||||||
|
|
||||||
zipWriter := zip.NewWriter( buf )
|
zipWriter := zip.NewWriter( buf )
|
||||||
|
|
||||||
|
// Package up important JSON endpoints
|
||||||
writeZipContents(zipWriter, fmt.Sprintf("%s/%s", zipBaseDir, "puzzles.json"), ctx.jPuzzleList)
|
writeZipContents(zipWriter, fmt.Sprintf("%s/%s", zipBaseDir, "puzzles.json"), ctx.jPuzzleList)
|
||||||
writeZipContents(zipWriter, fmt.Sprintf("%s/%s", zipBaseDir, "points.json"), ctx.jPointsLog)
|
writeZipContents(zipWriter, fmt.Sprintf("%s/%s", zipBaseDir, "points.json"), ctx.jPointsLog)
|
||||||
|
|
||||||
|
// Package up files for currently-unlocked puzzles in categories
|
||||||
for category_name, category := range ctx.categories {
|
for category_name, category := range ctx.categories {
|
||||||
for _, file := range category.zf.File {
|
for _, file := range category.zf.File {
|
||||||
parts := strings.Split(file.Name, "/")
|
parts := strings.Split(file.Name, "/")
|
||||||
|
|
||||||
if (parts[0] == "content") {
|
if (parts[0] == "content") {
|
||||||
fmt.Printf("Inspecting %s/%s\n", category_name, file.Name)
|
|
||||||
local_buf := new(bytes.Buffer)
|
local_buf := new(bytes.Buffer)
|
||||||
fh, _ := file.Open()
|
fh, _ := file.Open()
|
||||||
defer fh.Close()
|
defer fh.Close()
|
||||||
|
@ -283,17 +286,21 @@ func (ctx *Instance) dehydrateHandler(w http.ResponseWriter, req *http.Request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pack up the theme files
|
||||||
|
theme_root_re := regexp.MustCompile(fmt.Sprintf("^%s", ctx.ThemeDir))
|
||||||
filepath.Walk(ctx.ThemeDir, func (path string, info os.FileInfo, err error) error {
|
filepath.Walk(ctx.ThemeDir, func (path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if ! info.IsDir() {
|
|
||||||
fmt.Printf("Walking %s\n", path)
|
if ! info.IsDir() { // Only package up files
|
||||||
local_buf := new(bytes.Buffer)
|
local_buf := new(bytes.Buffer)
|
||||||
fh, _ := os.Open(path)
|
fh, _ := os.Open(path)
|
||||||
defer fh.Close()
|
defer fh.Close()
|
||||||
local_buf.ReadFrom(fh)
|
local_buf.ReadFrom(fh)
|
||||||
writeZipContents(zipWriter, fmt.Sprintf("%s/%s", zipBaseDir, path), local_buf.Bytes())
|
localized_path := theme_root_re.ReplaceAllLiteralString( path, "")
|
||||||
|
fmt.Printf("Localized path is %s\n", localized_path)
|
||||||
|
writeZipContents(zipWriter, fmt.Sprintf("%s/%s", zipBaseDir, localized_path), local_buf.Bytes())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -305,7 +312,44 @@ func (ctx *Instance) dehydrateHandler(w http.ResponseWriter, req *http.Request)
|
||||||
w.Write(buf.Bytes())
|
w.Write(buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx *Instance) manifestHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
manifest := make([]string, 0)
|
||||||
|
manifest = append(manifest, "puzzles.json")
|
||||||
|
manifest = append(manifest, "points.json")
|
||||||
|
|
||||||
|
// Pack up the theme files
|
||||||
|
theme_root_re := regexp.MustCompile(fmt.Sprintf("^%s/", ctx.ThemeDir))
|
||||||
|
filepath.Walk(ctx.ThemeDir, func (path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ! info.IsDir() { // Only package up files
|
||||||
|
localized_path := theme_root_re.ReplaceAllLiteralString( path, "")
|
||||||
|
manifest = append(manifest, localized_path)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
// Package up files for currently-unlocked puzzles in categories
|
||||||
|
for category_name, category := range ctx.categories {
|
||||||
|
for _, file := range category.zf.File {
|
||||||
|
parts := strings.Split(file.Name, "/")
|
||||||
|
|
||||||
|
if (parts[0] == "content") {
|
||||||
|
manifest = append(manifest, path.Join("content", category_name, path.Join(parts[1:]...)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
manifest_json, _ := json.Marshal(manifest)
|
||||||
|
w.Write(manifest_json)
|
||||||
|
}
|
||||||
|
|
||||||
func writeZipContents(z *zip.Writer, path string, contents []byte) error {
|
func writeZipContents(z *zip.Writer, path string, contents []byte) error {
|
||||||
|
path = filepath.Clean(path)
|
||||||
|
fmt.Printf("Writing contents to %s\n", path)
|
||||||
f, err := z.Create(path)
|
f, err := z.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -357,5 +401,6 @@ func (ctx *Instance) BindHandlers() {
|
||||||
ctx.mux.HandleFunc(ctx.Base+"/content/", ctx.contentHandler)
|
ctx.mux.HandleFunc(ctx.Base+"/content/", ctx.contentHandler)
|
||||||
ctx.mux.HandleFunc(ctx.Base+"/puzzles.json", ctx.puzzlesHandler)
|
ctx.mux.HandleFunc(ctx.Base+"/puzzles.json", ctx.puzzlesHandler)
|
||||||
ctx.mux.HandleFunc(ctx.Base+"/points.json", ctx.pointsHandler)
|
ctx.mux.HandleFunc(ctx.Base+"/points.json", ctx.pointsHandler)
|
||||||
|
ctx.mux.HandleFunc(ctx.Base+"/state_manifest.json", ctx.manifestHandler)
|
||||||
ctx.mux.HandleFunc(ctx.Base+"/dehydrate", ctx.dehydrateHandler)
|
ctx.mux.HandleFunc(ctx.Base+"/dehydrate", ctx.dehydrateHandler)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,9 @@
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width">
|
||||||
<link rel="stylesheet" href="basic.css">
|
<link rel="stylesheet" href="basic.css">
|
||||||
|
<script src="moth-pwa.js" type="text/javascript"></script>
|
||||||
<script src="moth.js"></script>
|
<script src="moth.js"></script>
|
||||||
|
<link rel="manifest" href="manifest.json">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1 id="title">MOTH</h1>
|
<h1 id="title">MOTH</h1>
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"name": "Monarch of the Hill",
|
||||||
|
"short_name": "MOTH",
|
||||||
|
"start_url": ".",
|
||||||
|
"display": "standalone",
|
||||||
|
"background_color": "#fff",
|
||||||
|
"theme_color": "#ECB",
|
||||||
|
"description": "The MOTH CTF engine"
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
function pwa_init() {
|
||||||
|
console.log("Starting service worker");
|
||||||
|
if ('serviceWorker' in navigator) {
|
||||||
|
navigator.serviceWorker.register("./sw.js").then(function(reg) {
|
||||||
|
console.log("Successfully registered service worker", reg);
|
||||||
|
}).catch(function(err) {
|
||||||
|
console.warn("Error while registering service worker", err);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log("Service workers not supported. Some offline functionality may not work");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (document.readyState === "loading") {
|
||||||
|
document.addEventListener("DOMContentLoaded", pwa_init);
|
||||||
|
} else {
|
||||||
|
pwa_init();
|
||||||
|
}
|
|
@ -78,12 +78,7 @@ function renderPuzzles(obj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function heartbeat(teamId) {
|
function heartbeat(teamId) {
|
||||||
let fd = new FormData()
|
fetch("puzzles.json?id=" + teamId)
|
||||||
fd.append("id", teamId)
|
|
||||||
fetch("puzzles.json", {
|
|
||||||
method: "POST",
|
|
||||||
body: fd,
|
|
||||||
})
|
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
if (resp.ok) {
|
if (resp.ok) {
|
||||||
resp.json()
|
resp.json()
|
||||||
|
@ -110,6 +105,68 @@ function showPuzzles(teamId) {
|
||||||
document.getElementById("puzzles").appendChild(spinner)
|
document.getElementById("puzzles").appendChild(spinner)
|
||||||
heartbeat(teamId)
|
heartbeat(teamId)
|
||||||
setInterval(e => { heartbeat(teamId) }, 40000)
|
setInterval(e => { heartbeat(teamId) }, 40000)
|
||||||
|
|
||||||
|
let cacher = document.createElement("li");
|
||||||
|
let cache_button = document.createElement("a");
|
||||||
|
cache_button.innerText = "Cache";
|
||||||
|
cache_button.title = "Cache an offine copy of current content";
|
||||||
|
cache_button.href = "#";
|
||||||
|
cache_button.addEventListener("click", async function() {
|
||||||
|
toast("Caching all currently-open content");
|
||||||
|
await fetchAll(teamId);
|
||||||
|
toast("Done caching content");
|
||||||
|
});
|
||||||
|
cacher.appendChild(cache_button);
|
||||||
|
document.getElementsByTagName("nav")[0].getElementsByTagName("ul")[0].appendChild(cacher);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchAll(teamId) {
|
||||||
|
let headers = new Headers();
|
||||||
|
headers.append("pragma", "no-cache");
|
||||||
|
headers.append("cache-control", "no-cache");
|
||||||
|
requests = [];
|
||||||
|
|
||||||
|
requests.push( fetch("state_manifest.json?id=" + teamId, {headers: headers})
|
||||||
|
.then( resp => {
|
||||||
|
if (resp.ok) {
|
||||||
|
resp.json()
|
||||||
|
.then( contents => {
|
||||||
|
console.log("Processing manifest");
|
||||||
|
for (let resource of contents) {
|
||||||
|
if (resource == "puzzles.json") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
fetch(resource, {headers: headers})
|
||||||
|
.then( e => {
|
||||||
|
console.log("Fetched " + resource);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
let resp = await fetch("puzzles.json?id=" + teamId, {headers: headers});
|
||||||
|
|
||||||
|
if (resp.ok) {
|
||||||
|
let categories = await resp.json();
|
||||||
|
let cat_names = Object.keys(categories)
|
||||||
|
cat_names.sort()
|
||||||
|
for (let cat_name of cat_names) {
|
||||||
|
if (cat_name.startsWith("__")) {
|
||||||
|
// Skip metadata
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let puzzles = categories[cat_name];
|
||||||
|
for (let puzzle of puzzles) {
|
||||||
|
let url = "puzzle.html?cat=" + cat_name + "&points=" + puzzle[0] + "&pid=" + puzzle[1];
|
||||||
|
requests.push( fetch(url, {headers: headers})
|
||||||
|
.then( e => {
|
||||||
|
console.log("Fetched " + url);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Promise.all(requests);
|
||||||
}
|
}
|
||||||
|
|
||||||
function login(e) {
|
function login(e) {
|
||||||
|
@ -165,3 +222,4 @@ if (document.readyState === "loading") {
|
||||||
} else {
|
} else {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
<link rel="stylesheet" href="basic.css">
|
<link rel="stylesheet" href="basic.css">
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width">
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
|
<script src="moth-pwa.js" type="text/javascript"></script>
|
||||||
<script src="puzzle.js"></script>
|
<script src="puzzle.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
<title>Scoreboard</title>
|
<title>Scoreboard</title>
|
||||||
<link rel="stylesheet" href="basic.css">
|
<link rel="stylesheet" href="basic.css">
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width">
|
||||||
|
<script src="moth-pwa.js" type="text/javascript"></script>
|
||||||
<script>
|
<script>
|
||||||
function update(state) {
|
function update(state) {
|
||||||
let element = document.getElementById("scoreboard");
|
let element = document.getElementById("scoreboard");
|
||||||
|
@ -111,8 +112,8 @@ function once() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
let base = window.location.href.replace("scoreboard.html", "")
|
let base = window.location.href.replace("scoreboard.html", "");
|
||||||
document.querySelector("#location").textContent = base
|
document.querySelector("#location").textContent = base;
|
||||||
|
|
||||||
setInterval(once, 60000);
|
setInterval(once, 60000);
|
||||||
once();
|
once();
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
var cacheName = "moth:v1"
|
||||||
|
var content = [
|
||||||
|
"index.html",
|
||||||
|
"basic.css",
|
||||||
|
"puzzle.js",
|
||||||
|
"puzzle.html",
|
||||||
|
"scoreboard.html",
|
||||||
|
"moth.js",
|
||||||
|
"sw.js",
|
||||||
|
"points.json",
|
||||||
|
];
|
||||||
|
|
||||||
|
self.addEventListener("install", function(e) {
|
||||||
|
e.waitUntil(
|
||||||
|
caches.open(cacheName).then(function(cache) {
|
||||||
|
return cache.addAll(content).then(
|
||||||
|
function() {
|
||||||
|
self.skipWaiting();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
/* Attempt to fetch live resources, first, then fall back to cache */
|
||||||
|
self.addEventListener('fetch', function(event) {
|
||||||
|
let cache_used = false;
|
||||||
|
|
||||||
|
event.respondWith(
|
||||||
|
fetch(event.request)
|
||||||
|
.catch(function(evt) {
|
||||||
|
console.log("Falling back to cache for " + event.request.url);
|
||||||
|
cache_used = true;
|
||||||
|
return caches.match(event.request, {ignoreSearch: true});
|
||||||
|
}).then(function(res) {
|
||||||
|
if (res && res.ok) {
|
||||||
|
let res_clone = res.clone();
|
||||||
|
if (! cache_used ) {
|
||||||
|
caches.open(cacheName).then(function(cache) {
|
||||||
|
cache.put(event.request, res_clone);
|
||||||
|
console.log("Storing " + event.request.url + " in cache");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
} else {
|
||||||
|
console.log("Failed to retrieve resource");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
Loading…
Reference in New Issue