fix: use http cookie for subpath proxying, referer header is firefox only

This commit is contained in:
Konuri Maki 2024-12-18 15:31:54 +08:00
parent 5d06b7b2dd
commit 24bae2ac48
4 changed files with 14 additions and 26 deletions

1
entemu/.gitignore vendored
View file

@ -11,3 +11,4 @@ _fresh/
node_modules/ node_modules/
vendor/ vendor/
.npmrc

View file

@ -19,15 +19,6 @@ button {
background: none; background: none;
border: 1px solid var(--fore); border: 1px solid var(--fore);
border-radius: 4px; border-radius: 4px;
/*
font: inherit;
font-size: 1rem;
border: none;
padding: 4px;
background-color: var(--back);
border-radius: 1em;
width: 2.2rem;
*/
} }
button, [role="button"] { button, [role="button"] {
cursor: pointer; cursor: pointer;

View file

@ -11,7 +11,7 @@ body {
margin: 1em auto; margin: 1em auto;
width: 21cm; width: 21cm;
max-width: 100%; max-width: 100%;
padding: 8px 0; padding: 0 8px;
line-height: 1.5; line-height: 1.5;
background-color: var(--back); background-color: var(--back);
color: var(--fore); color: var(--fore);

28
main.go
View file

@ -64,24 +64,20 @@ func cmain(argc int, argv []string) string {
cmdmap := map[string]JunkConf{} cmdmap := map[string]JunkConf{}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
p1 := r.URL.Path p1 := r.URL.Path
if p1 == "/" {
fileserver.ServeHTTP(w, r)
return
}
p := strings.Split(p1, "/")[1:] p := strings.Split(p1, "/")[1:]
seg := p[0] seg := p[0]
if ref := r.Header.Get("Referer"); ref != "" { vc, err := r.Cookie("view")
rurl, err := url.Parse(ref) if err != nil {
rp := strings.Split(rurl.Path, "/")[1:] w.Header().Add("Set-Cookie", "view="+seg+"; Path=/; SameSite=Strict; HttpOnly")
if rurl.Path != "/" { w.Header().Add("Location", p1)
rseg := rp[0] w.WriteHeader(http.StatusFound)
if err == nil && seg != rseg && (len(p) < 2 || p[1] != rseg) { return
p1 = "/" + rseg + r.URL.Path }
w.Header().Set("Location", p1) v := vc.Value
w.WriteHeader(http.StatusFound) if seg != v {
return w.Header().Add("Location", "/"+v+p1)
} w.WriteHeader(http.StatusFound)
} return
} }
if conf, ok := cmdmap[seg]; ok { if conf, ok := cmdmap[seg]; ok {
proxy_path := conf.Proxy + "/" + strings.Join(p[1:], "/") proxy_path := conf.Proxy + "/" + strings.Join(p[1:], "/")