Create sessions on boot requests instead of websocket handshakes
This commit is contained in:
parent
da87dccb53
commit
90785aba20
File diff suppressed because one or more lines are too long
@ -1,9 +1,6 @@
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
|
||||
// This entrypoint gets inlined in the index page cached by service workers
|
||||
// and is responsible for fetching the data we would otherwise embed
|
||||
|
||||
window.__env__ = fetch('/data', {
|
||||
window.__init__ = fetch('/init', {
|
||||
credentials: 'same-origin'
|
||||
}).then(res => {
|
||||
if (res.ok) {
|
||||
|
@ -70,11 +70,8 @@ function loadState({ store }, env) {
|
||||
});
|
||||
}
|
||||
|
||||
export default function initialState(ctx) {
|
||||
if (window.__env__) {
|
||||
window.__env__.then(env => loadState(ctx, env));
|
||||
} else {
|
||||
const env = JSON.parse(document.getElementById('env').innerHTML);
|
||||
loadState(ctx, env);
|
||||
}
|
||||
export default async function initialState(ctx) {
|
||||
const env = await window.__init__;
|
||||
ctx.socket.connect();
|
||||
loadState(ctx, env);
|
||||
}
|
||||
|
@ -14,8 +14,6 @@ export default class Socket {
|
||||
});
|
||||
this.handlers = [];
|
||||
this.connected = false;
|
||||
|
||||
this.connect();
|
||||
}
|
||||
|
||||
connect() {
|
||||
|
@ -1,6 +1,4 @@
|
||||
<%! data *indexData, cssPath string, inlineScript string, scripts []string, sw bool %>
|
||||
|
||||
<%% import "github.com/mailru/easyjson" %%>
|
||||
<%! cssPath string, inlineScript string, scripts []string %>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
@ -13,9 +11,7 @@
|
||||
<title>Dispatch</title>
|
||||
<meta name="description" content="Web-based IRC client.">
|
||||
|
||||
<% if sw { %>
|
||||
<link rel="preload" href="/data" as="fetch" crossorigin>
|
||||
<% } %>
|
||||
<link rel="preload" href="/init" as="fetch" crossorigin>
|
||||
|
||||
<script>
|
||||
<%== inlineScript %>
|
||||
@ -37,10 +33,6 @@
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
<% if data != nil { %>
|
||||
<script id="env" type="application/json"><% easyjson.MarshalToWriter(data, w) %></script>
|
||||
<% } %>
|
||||
|
||||
<% for _, script := range scripts { %>
|
||||
<script src="<%== script %>"></script>
|
||||
<% } %>
|
||||
|
@ -4,15 +4,10 @@
|
||||
package server
|
||||
import (
|
||||
"io"
|
||||
"github.com/mailru/easyjson"
|
||||
)
|
||||
|
||||
func IndexTemplate(w io.Writer, data *indexData, cssPath string, inlineScript string, scripts []string, sw bool) error {
|
||||
io.WriteString(w, "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta name=\"theme-color\" content=\"#f0f0f0\"><title>Dispatch</title><meta name=\"description\" content=\"Web-based IRC client.\">")
|
||||
if sw {
|
||||
io.WriteString(w, "<link rel=\"preload\" href=\"/data\" as=\"fetch\" crossorigin>")
|
||||
}
|
||||
io.WriteString(w, "<script>")
|
||||
func IndexTemplate(w io.Writer, cssPath string, inlineScript string, scripts []string) error {
|
||||
io.WriteString(w, "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta name=\"theme-color\" content=\"#f0f0f0\"><title>Dispatch</title><meta name=\"description\" content=\"Web-based IRC client.\"><link rel=\"preload\" href=\"/init\" as=\"fetch\" crossorigin><script>")
|
||||
io.WriteString(w, inlineScript )
|
||||
io.WriteString(w, "</script><link rel=\"preload\" href=\"/font/fontello.woff2?48901973\" as=\"font\" type=\"font/woff2\" crossorigin><link rel=\"preload\" href=\"/font/RobotoMono-Regular.woff2\" as=\"font\" type=\"font/woff2\" crossorigin><link rel=\"preload\" href=\"/font/Montserrat-Regular.woff2\" as=\"font\" type=\"font/woff2\" crossorigin><link rel=\"preload\" href=\"/font/Montserrat-Bold.woff2\" as=\"font\" type=\"font/woff2\" crossorigin><link rel=\"preload\" href=\"/font/RobotoMono-Bold.woff2\" as=\"font\" type=\"font/woff2\" crossorigin>")
|
||||
if cssPath != "" {
|
||||
@ -21,11 +16,6 @@ io.WriteString(w, cssPath )
|
||||
io.WriteString(w, "\" rel=\"stylesheet\">")
|
||||
}
|
||||
io.WriteString(w, "<link rel=\"manifest\" href=\"/manifest.json\"></head><body><div id=\"root\"></div>")
|
||||
if data != nil {
|
||||
io.WriteString(w, "<script id=\"env\" type=\"application/json\">")
|
||||
easyjson.MarshalToWriter(data, w)
|
||||
io.WriteString(w, "</script>")
|
||||
}
|
||||
for _, script := range scripts {
|
||||
io.WriteString(w, "<script src=\"")
|
||||
io.WriteString(w, script )
|
||||
|
@ -47,13 +47,11 @@ func newH2PushAsset(name string) h2PushAsset {
|
||||
var (
|
||||
files []*File
|
||||
|
||||
indexStylesheet string
|
||||
indexScripts []string
|
||||
inlineScript string
|
||||
inlineScriptSha256 string
|
||||
inlineScriptSW string
|
||||
inlineScriptSWSha256 string
|
||||
serviceWorker []byte
|
||||
indexStylesheet string
|
||||
indexScripts []string
|
||||
inlineScript string
|
||||
inlineScriptSha256 string
|
||||
serviceWorker []byte
|
||||
|
||||
h2PushAssets []h2PushAsset
|
||||
h2PushCookieValue string
|
||||
@ -82,17 +80,12 @@ func (d *Dispatch) initFileServer() {
|
||||
bootloader := decompressedAsset(findAssetName("boot*.js"))
|
||||
runtime := decompressedAsset(findAssetName("runtime*.js"))
|
||||
|
||||
inlineScript = string(runtime)
|
||||
inlineScriptSW = string(bootloader) + string(runtime)
|
||||
inlineScript = string(bootloader) + string(runtime)
|
||||
|
||||
hash := sha256.New()
|
||||
hash.Write(runtime)
|
||||
inlineScriptSha256 = base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
||||
|
||||
hash.Reset()
|
||||
hash.Write(bootloader)
|
||||
hash.Write(runtime)
|
||||
inlineScriptSWSha256 = base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
||||
inlineScriptSha256 = base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
||||
|
||||
indexStylesheet = findAssetName("main*.css")
|
||||
indexScripts = []string{
|
||||
@ -148,15 +141,15 @@ func (d *Dispatch) initFileServer() {
|
||||
|
||||
serviceWorker = decompressedAsset("sw.js")
|
||||
hash.Reset()
|
||||
IndexTemplate(hash, nil, indexStylesheet, inlineScriptSW, indexScripts, true)
|
||||
IndexTemplate(hash, indexStylesheet, inlineScript, indexScripts)
|
||||
indexHash := base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
||||
|
||||
serviceWorker = append(serviceWorker, []byte(`
|
||||
workbox.precaching.precacheAndRoute([{
|
||||
revision: '`+indexHash+`',
|
||||
url: '/?sw'
|
||||
url: '/'
|
||||
}]);
|
||||
workbox.routing.registerNavigationRoute('/?sw');`)...)
|
||||
workbox.routing.registerNavigationRoute('/');`)...)
|
||||
|
||||
if viper.GetBool("https.hsts.enabled") && viper.GetBool("https.enabled") {
|
||||
hstsHeader = "max-age=" + viper.GetString("https.hsts.max_age")
|
||||
@ -283,8 +276,6 @@ func (d *Dispatch) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
_, sw := r.URL.Query()["sw"]
|
||||
|
||||
if cspEnabled {
|
||||
var wsSrc string
|
||||
if r.TLS != nil {
|
||||
@ -293,14 +284,9 @@ func (d *Dispatch) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||
wsSrc = "ws://" + r.Host
|
||||
}
|
||||
|
||||
inlineSha := inlineScriptSha256
|
||||
if sw {
|
||||
inlineSha = inlineScriptSWSha256
|
||||
}
|
||||
|
||||
csp := []string{
|
||||
"default-src 'none'",
|
||||
"script-src 'self' 'sha256-" + inlineSha + "'",
|
||||
"script-src 'self' 'sha256-" + inlineScriptSha256 + "'",
|
||||
"style-src 'self' 'unsafe-inline'",
|
||||
"font-src 'self'",
|
||||
"img-src 'self'",
|
||||
@ -323,21 +309,14 @@ func (d *Dispatch) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Strict-Transport-Security", hstsHeader)
|
||||
}
|
||||
|
||||
var data *indexData
|
||||
inline := inlineScriptSW
|
||||
if !sw {
|
||||
data = getIndexData(r, r.URL.EscapedPath(), d.handleAuth(w, r, false, true))
|
||||
inline = inlineScript
|
||||
}
|
||||
|
||||
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
gzw := getGzipWriter(w)
|
||||
IndexTemplate(gzw, data, indexStylesheet, inline, indexScripts, sw)
|
||||
IndexTemplate(gzw, indexStylesheet, inlineScript, indexScripts)
|
||||
putGzipWriter(gzw)
|
||||
} else {
|
||||
IndexTemplate(w, data, indexStylesheet, inline, indexScripts, sw)
|
||||
IndexTemplate(w, indexStylesheet, inlineScript, indexScripts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,7 +172,7 @@ func (d *Dispatch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
state := d.handleAuth(w, r, true, true)
|
||||
state := d.handleAuth(w, r, false, false)
|
||||
if state == nil {
|
||||
log.Println("[Auth] No state")
|
||||
fail(w, http.StatusInternalServerError)
|
||||
@ -180,8 +180,8 @@ func (d *Dispatch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
d.upgradeWS(w, r, state)
|
||||
} else if strings.HasPrefix(r.URL.Path, "/data") {
|
||||
state := d.handleAuth(w, r, false, false)
|
||||
} else if strings.HasPrefix(r.URL.Path, "/init") {
|
||||
state := d.handleAuth(w, r, true, true)
|
||||
data := getIndexData(r, "/", state)
|
||||
|
||||
writeJSON(w, r, data)
|
||||
|
Loading…
Reference in New Issue
Block a user