From 72c2044a8dd11728b180f6c7201b30b181b2cd54 Mon Sep 17 00:00:00 2001
From: NaitLee <nait@yozora.tech>
Date: Wed, 18 Dec 2024 15:57:53 +0800
Subject: [PATCH] fix: fix cookie redirect logic again

---
 main.go | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/main.go b/main.go
index c2ca092..bbedee9 100644
--- a/main.go
+++ b/main.go
@@ -67,16 +67,21 @@ func cmain(argc int, argv []string) string {
 		p := strings.Split(p1, "/")[1:]
 		seg := p[0]
 		if seg == "" {
+			w.Header().Add("Set-Cookie", "view=; Path=/; SameSite=Strict; HttpOnly")
+			w.Header().Add("Cache-Control", "no-store")
 			fileserver.ServeHTTP(w, r)
 			return
 		}
 		vc, err := r.Cookie("view")
-		if err == nil && seg != vc.Value {
+		if err == nil && seg != vc.Value && vc.Value != "" {
 			w.Header().Add("Location", "/"+vc.Value+p1)
 			w.WriteHeader(http.StatusFound)
 			return
 		}
 		if conf, ok := cmdmap[seg]; ok {
+			if err != nil || vc.Value == "" {
+				w.Header().Add("Set-Cookie", "view="+seg+"; Path=/; SameSite=Strict; HttpOnly")
+			}
 			proxy_path := conf.Proxy + "/" + strings.Join(p[1:], "/")
 			proxy(w, r, proxy_path)
 			return