Skip to content

Commit 23e96ea

Browse files
committed
feat: log requests, redirect if browser
1 parent 59f5cba commit 23e96ea

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"crypto/rand"
55
"fmt"
6+
"log"
67
"math/big"
78
"net/http"
89
"os"
@@ -21,13 +22,13 @@ func main() {
2122

2223
err := godotenv.Load()
2324
if err != nil {
24-
fmt.Println("Error loading .env file")
25+
log.Println("Error loading .env file")
2526
}
2627
r := chi.NewRouter()
2728

2829
token := os.Getenv("GITHUB_TOKEN")
2930
if token == "" {
30-
fmt.Println("GITHUB_TOKEN is required")
31+
log.Println("GITHUB_TOKEN is required")
3132
os.Exit(1)
3233
}
3334

@@ -46,6 +47,16 @@ func main() {
4647
}
4748

4849
func (server *Server) MirrorImageHandler(w http.ResponseWriter, req *http.Request) {
50+
query := req.URL.Path
51+
sourceIP := req.RemoteAddr
52+
log.Printf("Received request: query=%s, sourceIP=%s\n", query, sourceIP)
53+
54+
userAgent := strings.ToLower(req.Header.Get("User-Agent"))
55+
if !strings.Contains(userAgent, "curl") && !strings.Contains(userAgent, "wget") {
56+
http.Redirect(w, req, "https://github.com/1mgr/image-mirrors", http.StatusFound)
57+
return
58+
}
59+
4960
image := strings.TrimPrefix(req.URL.Path, "/")
5061
if image == "" {
5162
httpError(w, 400, "image not passed")

0 commit comments

Comments
 (0)