-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
82 lines (72 loc) · 1.66 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
package main
import (
"cmp"
"embed"
"io/fs"
"log/slog"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path"
"time"
cacheProxy "github.com/meerkat-dashboard/meerkat/proxy"
)
//go:embed site
var siteFS embed.FS
var (
listenAddr = cmp.Or(os.Getenv("LISTEN_ADDRESS"), ":8080")
distsHost = cmp.Or(os.Getenv("DISTS_HOST"), "https://syncthing-apt.s3.fr-par.scw.cloud")
)
func main() {
subFS, _ := fs.Sub(fs.FS(siteFS), "site")
site := http.FS(subFS)
http.Handle("/", http.FileServer(site))
proxy, err := newCachingProxy(distsHost, 5*time.Minute)
if err != nil {
slog.Error("failed to construct proxy", "error", err)
os.Exit(2)
}
filtered := validateFilename(proxy, []string{
"InRelease",
"InRelease.gz",
"Release",
"Release.gz",
"Release.gpg",
"Release.gpg.gz",
"Packages",
"Packages.gz",
"*.deb",
})
http.Handle("/dists/", filtered)
if err := http.ListenAndServe(listenAddr, nil); err != nil {
slog.Error("failed to listen", "error", err)
}
}
func validateFilename(next http.Handler, names []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
name := path.Base(req.URL.Path)
for _, valid := range names {
if ok, _ := path.Match(valid, name); ok {
next.ServeHTTP(w, req)
return
}
}
http.NotFound(w, req)
})
}
func newCachingProxy(next string, cacheTime time.Duration) (http.Handler, error) {
remote, err := url.Parse(next)
if err != nil {
return nil, err
}
rev := &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(remote)
},
}
cp := &cacheProxy.Proxy{}
cp.Next = rev
rev.ModifyResponse = cp.StoreOKResponse(cacheTime)
return cp, nil
}