From 485594497c67f2748f84953d7596d92eb6f534cf Mon Sep 17 00:00:00 2001 From: wood chen Date: Mon, 14 Oct 2024 12:19:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9D=A5=E6=BA=90=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E6=97=A5=E5=BF=97=E5=8F=8A=E9=94=99=E8=AF=AF=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index feef580..47ac09f 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "math/rand" "net" "net/http" + "net/url" "os" "path/filepath" "strings" @@ -196,11 +197,23 @@ func getCSVContent(path string) (*URLSelector, error) { func handleAPIRequest(w http.ResponseWriter, r *http.Request) { start := time.Now() realIP := getRealIP(r) + referer := r.Referer() + + // 获取来源域名 + var sourceDomain string + if referer != "" { + if parsedURL, err := url.Parse(referer); err == nil { + sourceDomain = parsedURL.Hostname() + } + } + if sourceDomain == "" { + sourceDomain = "direct" + } if time.Since(lastFetchTime) > cacheDuration { if err := loadCSVPaths(); err != nil { http.Error(w, "Failed to load CSV paths", http.StatusInternalServerError) - log.Println("Error loading CSV paths:", err) + log.Printf("Error loading CSV paths: %v", err) return } } @@ -228,7 +241,7 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) { selector, err := getCSVContent(csvPath) if err != nil { http.Error(w, "Failed to fetch CSV content", http.StatusInternalServerError) - log.Println("Error fetching CSV content:", err) + log.Printf("Error fetching CSV content: %v", err) return } @@ -240,8 +253,8 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) { randomURL := selector.GetRandomURL() duration := time.Since(start) - log.Printf("Request: %s %s from %s - Duration: %v - Redirecting to: %s\n", - r.Method, r.URL.Path, realIP, duration, randomURL) + log.Printf("Request: %s %s from %s - Source: %s - Duration: %v - Redirecting to: %s", + r.Method, r.URL.Path, realIP, sourceDomain, duration, randomURL) http.Redirect(w, r, randomURL, http.StatusFound) }