-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
208 lines (194 loc) · 5.37 KB
/
repo.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package debanator
import (
"bytes"
"fmt"
"io"
"net/http"
"path"
"time"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/go-chi/chi/v5"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"pault.ag/go/debian/control"
"pault.ag/go/debian/dependency"
"pault.ag/go/debian/hashio"
"pault.ag/go/debian/version"
)
const FILESPREFIX = "pool"
type hashedFile struct {
buf []byte
md5Hash control.MD5FileHash
sha1Hash control.SHA1FileHash
sha256Hash control.SHA256FileHash
}
type Repo struct {
// The prefix to serving http paths to the files provided by this package.
// This is needed so that we can give absolute paths in Package files.
filePrefix string
be Backend
packages []LogicalPackage
cache map[string]hashedFile
release []byte
}
func NewRepoFromBackend(backend Backend, filePrefix string) Repo {
return Repo{
be: backend,
cache: make(map[string]hashedFile),
filePrefix: filePrefix,
}
}
func (r *Repo) GetArches() []dependency.Arch {
arches := make(map[dependency.Arch]struct{})
for _, lp := range r.packages {
for arch := range lp.Arches {
arches[arch] = struct{}{}
}
}
return maps.Keys(arches)
}
// Find the latest versions of all packages for the given arch
func (r *Repo) GetPackagesForArch(a dependency.Arch) []control.BinaryIndex {
out := []control.BinaryIndex{}
for _, p := range r.packages {
if versions, ok := p.Arches[a]; ok {
var latest version.Version
for v := range versions {
if version.Compare(v, latest) > 0 {
latest = v
}
}
out = append(out, p.Arches[a][latest])
}
}
return out
}
func (r *Repo) makePackagesFileForArch(arch dependency.Arch) error {
var b bytes.Buffer
w, hashers, err := hashio.NewHasherWriters([]string{"md5", "sha256", "sha1"}, &b)
enc, _ := control.NewEncoder(w)
for _, d := range r.GetPackagesForArch(arch) {
if err = enc.Encode(d); err != nil {
return fmt.Errorf("encoding package %s: %w", d.Package, err)
}
}
fname := fmt.Sprintf("main/binary-%s/Packages", arch)
hashes := make(map[string]control.FileHash)
for _, h := range hashers {
hashes[h.Name()] = control.FileHashFromHasher(fname, *h)
}
r.cache[fname] = hashedFile{
buf: b.Bytes(),
sha256Hash: control.SHA256FileHash{
FileHash: hashes["sha256"],
},
sha1Hash: control.SHA1FileHash{
FileHash: hashes["sha1"]},
md5Hash: control.MD5FileHash{FileHash: hashes["md5"]},
}
return nil
}
// Generate and cache all the Package/Repo files
func (r *Repo) GenerateFiles() error {
for _, arch := range r.GetArches() {
if err := r.makePackagesFileForArch(arch); err != nil {
return fmt.Errorf("generating files for arch %s: %w", arch, err)
}
}
r.makeRelease()
return nil
}
func (r *Repo) makeRelease() {
var rel bytes.Buffer
enc, _ := control.NewEncoder(&rel)
const dateFmt = "Mon, 02 Jan 2006 15:04:05 MST"
var md5s []control.MD5FileHash
var sha1s []control.SHA1FileHash
var sha256s []control.SHA256FileHash
for _, f := range r.cache {
md5s = append(md5s, f.md5Hash)
sha1s = append(sha1s, f.sha1Hash)
sha256s = append(sha256s, f.sha256Hash)
}
if err := enc.Encode(Release{
Suite: "stable",
Architectures: r.GetArches(),
Components: "main",
Date: time.Now().UTC().Format(dateFmt),
MD5Sum: md5s,
SHA1: sha1s,
SHA256: sha256s,
}); err != nil {
log.Fatal(err)
}
r.release = rel.Bytes()
return
}
// Handle a deb/apt repository http request
func (r *Repo) GetHandler(keyring *crypto.KeyRing) http.Handler {
router := chi.NewRouter()
router.Get("/Release", func(w http.ResponseWriter, req *http.Request) {
if _, err := w.Write(r.release); err != nil {
log.Fatal(err)
}
})
router.Get("/Release.gpg", func(w http.ResponseWriter, req *http.Request) {
msg := crypto.NewPlainMessage(r.release)
sig, err := keyring.SignDetached(msg)
if err != nil {
log.Fatal(err)
}
sigStr, err := sig.GetArmored()
if err != nil {
log.Fatal(err)
}
io.WriteString(w, sigStr)
})
router.Get("/main/{arch}/Packages", func(w http.ResponseWriter, req *http.Request) {
h, ok := r.cache[fmt.Sprintf("main/%s/Packages", chi.URLParam(req, "arch"))]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
_, err := w.Write(h.buf)
if err != nil {
log.Error(err)
}
})
router.Get(fmt.Sprintf("/%s/*", FILESPREFIX), r.be.ServeFiles(r.filePrefix).ServeHTTP)
return router
}
func (r *Repo) Populate() error {
packs := make(map[string]LogicalPackage)
files := Unwrap(r.be.GetFiles())
for _, f := range files {
rd := Unwrap(f.GetReader())
bi, err := BinaryIndexFromDeb(rd, path.Join(r.filePrefix, FILESPREFIX, f.GetName()))
if err != nil {
return fmt.Errorf("processing deb file: %w", err)
}
packageName := bi.Package
if _, ok := packs[packageName]; !ok {
packs[packageName] = LogicalPackage{
Name: packageName,
Arches: make(map[dependency.Arch]map[version.Version]control.BinaryIndex),
}
}
pack := packs[packageName]
if _, ok := pack.Arches[bi.Architecture]; !ok {
pack.Arches[bi.Architecture] = make(map[version.Version]control.BinaryIndex)
}
arch := pack.Arches[bi.Architecture]
if _, ok := arch[bi.Version]; !ok {
arch[bi.Version] = *bi
} else {
log.WithFields(log.Fields{
"package": packageName,
"arch": arch,
"version": bi.Version.String(),
}).Warn("Duplicate package/arch/version found, ignoring...")
}
}
r.packages = maps.Values(packs)
return nil
}