-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (103 loc) · 3.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"crypto/rand"
"fmt"
"log"
"math/big"
"net/http"
"os"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/joho/godotenv"
)
type Server struct {
gh *GitHubClient
}
func main() {
err := godotenv.Load()
if err != nil {
log.Println("Error loading .env file")
}
r := chi.NewRouter()
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
log.Println("GITHUB_TOKEN is required")
os.Exit(1)
}
server := Server{
gh: NewGitHubClient(nil, GithubClientConfig{
Token: token,
OrgRepo: "1mgr/images-mirror",
Org: "1mgr",
Timeout: 30 * time.Second,
CheckInterval: 1 * time.Second,
}),
}
r.Get("/*", server.MirrorImageHandler)
http.ListenAndServe(":8080", r)
}
func (server *Server) MirrorImageHandler(w http.ResponseWriter, req *http.Request) {
query := req.URL.Path
sourceIP := req.Header.Get("CF-Connecting-IP")
if sourceIP == "" {
sourceIP = req.RemoteAddr
}
log.Printf("Received request: query=%s, sourceIP=%s\n", query, sourceIP)
userAgent := strings.ToLower(req.Header.Get("User-Agent"))
if !strings.Contains(userAgent, "curl") && !strings.Contains(userAgent, "wget") {
http.Redirect(w, req, "https://github.com/1mgr/image-mirrors", http.StatusFound)
return
}
image := strings.TrimPrefix(req.URL.Path, "/")
if image == "" {
httpError(w, 400, "image not passed")
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Expose-Headers", "Content-Type")
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
writeLine(w, fmt.Sprintf("⬇️ Received request to mirror image: %s", image))
if !isValidImage(image) {
httpError(w, http.StatusBadRequest, fmt.Sprintf("❌ Invalid image: %s. Image name must be valid docker hub image with version tag. For example: postgres:16", image))
return
}
if !imageExists(image) {
httpError(w, http.StatusBadRequest, fmt.Sprintf("❌ Image not found in dockerhub: %s", image))
return
}
if mirrored, lastMirrorSince := server.gh.IsImageAlreadyMirrored(image); mirrored && lastMirrorSince < time.Hour*12 {
writeLine(w, fmt.Sprintf("✅ Image already mirrored. Last mirror was %s ago", lastMirrorSince))
} else {
log.Printf("Image %s is not mirrored yet\n %s", image, lastMirrorSince)
id := randID()
err := server.gh.LaunchGithubAction(image, id)
if err != nil {
httpError(w, http.StatusInternalServerError, fmt.Sprintf("❌ Failed to launch GitHub action: %v", err))
return
}
writeLine(w, "🚀 Launched github action workflow")
if err := server.gh.FollowWorkflowRun(makeStatusWriter(w), id); err != nil {
httpError(w, http.StatusInternalServerError, fmt.Sprintf("❌ Failed to follow workflow run: %v", err))
return
}
}
_, remainder, tag := splitDockerImageParts(image)
imageToPull := "ghcr.io/1mgr/" + shortenRemainder(remainder) + ":" + tag
writeLine(w, "ℹ️ Pull your image from the mirror:")
writeLine(w, "docker pull "+imageToPull)
}
func randID() string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, 8)
for i := range b {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
panic(err)
}
b[i] = letters[num.Int64()]
}
return string(b)
}