From 24bae2ac482e15d2d010e24ebc075246172331ac Mon Sep 17 00:00:00 2001
From: NaitLee <nait@yozora.tech>
Date: Wed, 18 Dec 2024 15:31:54 +0800
Subject: [PATCH] fix: use http cookie for subpath proxying, referer header is
 firefox only

---
 entemu/.gitignore        |  1 +
 entemu/static/styles.css |  9 ---------
 junk.css                 |  2 +-
 main.go                  | 28 ++++++++++++----------------
 4 files changed, 14 insertions(+), 26 deletions(-)

diff --git a/entemu/.gitignore b/entemu/.gitignore
index 0735100..df586b6 100644
--- a/entemu/.gitignore
+++ b/entemu/.gitignore
@@ -11,3 +11,4 @@ _fresh/
 node_modules/
 vendor/
 
+.npmrc
diff --git a/entemu/static/styles.css b/entemu/static/styles.css
index 392d399..f529ad1 100644
--- a/entemu/static/styles.css
+++ b/entemu/static/styles.css
@@ -19,15 +19,6 @@ button {
   background: none;
   border: 1px solid var(--fore);
   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"] {
   cursor: pointer;
diff --git a/junk.css b/junk.css
index 20789a6..2b48376 100644
--- a/junk.css
+++ b/junk.css
@@ -11,7 +11,7 @@ body {
     margin: 1em auto;
     width: 21cm;
     max-width: 100%;
-    padding: 8px 0;
+    padding: 0 8px;
     line-height: 1.5;
     background-color: var(--back);
     color: var(--fore);
diff --git a/main.go b/main.go
index b3a29d9..7d54b55 100644
--- a/main.go
+++ b/main.go
@@ -64,24 +64,20 @@ func cmain(argc int, argv []string) string {
 	cmdmap := map[string]JunkConf{}
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 		p1 := r.URL.Path
-		if p1 == "/" {
-			fileserver.ServeHTTP(w, r)
-			return
-		}
 		p := strings.Split(p1, "/")[1:]
 		seg := p[0]
-		if ref := r.Header.Get("Referer"); ref != "" {
-			rurl, err := url.Parse(ref)
-			rp := strings.Split(rurl.Path, "/")[1:]
-			if rurl.Path != "/" {
-				rseg := rp[0]
-				if err == nil && seg != rseg && (len(p) < 2 || p[1] != rseg) {
-					p1 = "/" + rseg + r.URL.Path
-					w.Header().Set("Location", p1)
-					w.WriteHeader(http.StatusFound)
-					return
-				}
-			}
+		vc, err := r.Cookie("view")
+		if err != nil {
+			w.Header().Add("Set-Cookie", "view="+seg+"; Path=/; SameSite=Strict; HttpOnly")
+			w.Header().Add("Location", p1)
+			w.WriteHeader(http.StatusFound)
+			return
+		}
+		v := vc.Value
+		if seg != v {
+			w.Header().Add("Location", "/"+v+p1)
+			w.WriteHeader(http.StatusFound)
+			return
 		}
 		if conf, ok := cmdmap[seg]; ok {
 			proxy_path := conf.Proxy + "/" + strings.Join(p[1:], "/")