This commit is contained in:
wood 2024-09-12 17:06:18 +08:00
parent 8287d9f726
commit 7ee5c10ea3

59
main.go
View File

@ -30,22 +30,16 @@ var (
) )
func main() { func main() {
// 初始化随机数生成器
source := rand.NewSource(time.Now().UnixNano()) source := rand.NewSource(time.Now().UnixNano())
rng = rand.New(source) rng = rand.New(source)
// 配置日志
setupLogging() setupLogging()
// 加载初始的 CSV 路径配置
if err := loadCSVPaths(); err != nil { if err := loadCSVPaths(); err != nil {
log.Fatal("Failed to load CSV paths:", err) log.Fatal("Failed to load CSV paths:", err)
} }
// 设置路由 http.HandleFunc("/", handleRequest)
http.Handle("/", http.FileServer(http.Dir("./public")))
http.HandleFunc("/pic/", logRequest(handleDynamicRequest))
http.HandleFunc("/video/", logRequest(handleDynamicRequest))
log.Printf("Listening on %s...\n", port) log.Printf("Listening on %s...\n", port)
if err := http.ListenAndServe(port, nil); err != nil { if err := http.ListenAndServe(port, nil); err != nil {
@ -83,27 +77,6 @@ func getRealIP(r *http.Request) string {
return ip return ip
} }
func logRequest(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
handler(w, r)
duration := time.Since(start)
realIP := getRealIP(r)
proto := r.Header.Get("X-Forwarded-Proto")
if proto == "" {
proto = "http"
}
host := r.Header.Get("X-Forwarded-Host")
if host == "" {
host = r.Host
}
log.Printf("Request: %s %s://%s%s from %s - Duration: %v\n",
r.Method, proto, host, r.URL.Path, realIP, duration)
}
}
func loadCSVPaths() error { func loadCSVPaths() error {
jsonPath := filepath.Join("public", "url.json") jsonPath := filepath.Join("public", "url.json")
log.Printf("Attempting to read file: %s", jsonPath) log.Printf("Attempting to read file: %s", jsonPath)
@ -132,7 +105,6 @@ func getCSVContent(path string) ([]string, error) {
content, exists := csvCache[path] content, exists := csvCache[path]
mu.RUnlock() mu.RUnlock()
if exists { if exists {
log.Printf("CSV content for %s found in cache\n", path)
return content, nil return content, nil
} }
@ -157,13 +129,12 @@ func getCSVContent(path string) ([]string, error) {
csvCache[path] = fileArray csvCache[path] = fileArray
mu.Unlock() mu.Unlock()
log.Printf("CSV content for %s fetched and cached\n", path)
return fileArray, nil return fileArray, nil
} }
func handleDynamicRequest(w http.ResponseWriter, r *http.Request) { func handleRequest(w http.ResponseWriter, r *http.Request) {
start := time.Now()
realIP := getRealIP(r) realIP := getRealIP(r)
log.Printf("Handling request from IP: %s\n", realIP)
if time.Since(lastFetchTime) > cacheDuration { if time.Since(lastFetchTime) > cacheDuration {
if err := loadCSVPaths(); err != nil { if err := loadCSVPaths(); err != nil {
@ -175,8 +146,9 @@ func handleDynamicRequest(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/") path := strings.TrimPrefix(r.URL.Path, "/")
pathSegments := strings.Split(path, "/") pathSegments := strings.Split(path, "/")
if len(pathSegments) < 2 { if len(pathSegments) < 2 {
http.NotFound(w, r) http.ServeFile(w, r, filepath.Join("public", "index.html"))
return return
} }
@ -199,19 +171,16 @@ func handleDynamicRequest(w http.ResponseWriter, r *http.Request) {
return return
} }
if len(fileArray) == 0 {
http.Error(w, "No content available", http.StatusNotFound)
return
}
randomURL := fileArray[rng.Intn(len(fileArray))] randomURL := fileArray[rng.Intn(len(fileArray))]
proto := r.Header.Get("X-Forwarded-Proto") duration := time.Since(start)
if proto == "" { log.Printf("Request: %s %s from %s - Duration: %v - Redirecting to: %s\n",
proto = "http" r.Method, r.URL.Path, realIP, duration, randomURL)
}
host := r.Header.Get("X-Forwarded-Host")
if host == "" {
host = r.Host
}
redirectURL := fmt.Sprintf("%s://%s%s", proto, host, randomURL) http.Redirect(w, r, randomURL, http.StatusFound)
log.Printf("Redirecting to %s\n", redirectURL)
http.Redirect(w, r, redirectURL, http.StatusFound)
} }