2 GoCheese -- Python private package repository and caching proxy
3 Copyright (C) 2019 Sergey Matveev <stargrave@stargrave.org>
4 2019 Elena Balakhonova <balakhonova_e@riseup.net>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // Python private package repository and caching proxy
48 "golang.org/x/crypto/blake2b"
49 "golang.org/x/net/netutil"
53 HTMLBegin = `<!DOCTYPE html>
56 <title>Links for %s</title>
60 HTMLEnd = " </body>\n</html>\n"
61 HTMLElement = " <a href=\"%s\"%s>%s</a><br/>\n"
62 InternalFlag = ".internal"
65 Warranty = `This program is free software: you can redistribute it and/or modify
66 it under the terms of the GNU General Public License as published by
67 the Free Software Foundation, version 3 of the License.
69 This program is distributed in the hope that it will be useful,
70 but WITHOUT ANY WARRANTY; without even the implied warranty of
71 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72 GNU General Public License for more details.
74 You should have received a copy of the GNU General Public License
75 along with this program. If not, see <http://www.gnu.org/licenses/>.`
79 pkgPyPI = regexp.MustCompile(`^.*<a href="([^"]+)"[^>]*>(.+)</a><br/>.*$`)
80 normalizationRe = regexp.MustCompilePOSIX("[-_.]+")
82 HashAlgoSHA256 = "sha256"
83 HashAlgoBLAKE2b256 = "blake2_256"
84 HashAlgoSHA512 = "sha512"
86 knownHashAlgos []string = []string{
93 root = flag.String("root", "./packages", "Path to packages directory")
94 bind = flag.String("bind", "[::]:8080", "Address to bind to")
95 tlsCert = flag.String("tls-cert", "", "Path to TLS X.509 certificate")
96 tlsKey = flag.String("tls-key", "", "Path to TLS X.509 private key")
97 norefreshURLPath = flag.String("norefresh", "/norefresh/", "Non-refreshing URL path")
98 refreshURLPath = flag.String("refresh", "/simple/", "Auto-refreshing URL path")
99 gpgUpdateURLPath = flag.String("gpgupdate", "/gpgupdate/", "GPG forceful refreshing URL path")
100 pypiURL = flag.String("pypi", "https://pypi.org/simple/", "Upstream PyPI URL")
101 passwdPath = flag.String("passwd", "passwd", "Path to file with authenticators")
102 passwdCheck = flag.Bool("passwd-check", false, "Test the -passwd file for syntax errors and exit")
103 fsck = flag.Bool("fsck", false, "Check integrity of all packages")
104 maxClients = flag.Int("maxclients", 128, "Maximal amount of simultaneous clients")
105 version = flag.Bool("version", false, "Print version information")
106 warranty = flag.Bool("warranty", false, "Print warranty information")
108 Version string = "UNKNOWN"
110 pypiURLParsed *url.URL
113 func mkdirForPkg(w http.ResponseWriter, r *http.Request, dir string) bool {
114 path := filepath.Join(*root, dir)
115 if _, err := os.Stat(path); os.IsNotExist(err) {
116 if err = os.Mkdir(path, os.FileMode(0777)); err != nil {
117 http.Error(w, err.Error(), http.StatusInternalServerError)
120 log.Println(r.RemoteAddr, "mkdir", dir)
125 func blake2b256New() hash.Hash {
126 h, err := blake2b.New256(nil)
134 w http.ResponseWriter,
140 if _, err := os.Stat(filepath.Join(*root, dir, InternalFlag)); err == nil {
143 resp, err := http.Get(*pypiURL + dir + "/")
145 http.Error(w, err.Error(), http.StatusBadGateway)
148 body, err := ioutil.ReadAll(resp.Body)
151 http.Error(w, err.Error(), http.StatusBadGateway)
154 if !mkdirForPkg(w, r, dir) {
157 dirPath := filepath.Join(*root, dir)
158 for _, lineRaw := range bytes.Split(body, []byte("\n")) {
159 submatches := pkgPyPI.FindStringSubmatch(string(lineRaw))
160 if len(submatches) == 0 {
164 filename := submatches[2]
165 pkgURL, err := url.Parse(uri)
167 http.Error(w, err.Error(), http.StatusBadGateway)
171 if pkgURL.Fragment == "" {
172 log.Println(r.RemoteAddr, "pypi", filename, "no digest provided")
173 http.Error(w, "no digest provided", http.StatusBadGateway)
176 digestInfo := strings.Split(pkgURL.Fragment, "=")
177 if len(digestInfo) == 1 {
178 // Ancient non PEP-0503 PyPIs, assume MD5
179 digestInfo = []string{"md5", digestInfo[0]}
180 } else if len(digestInfo) != 2 {
181 log.Println(r.RemoteAddr, "pypi", filename, "invalid digest provided")
182 http.Error(w, "invalid digest provided", http.StatusBadGateway)
185 digest, err := hex.DecodeString(digestInfo[1])
187 http.Error(w, err.Error(), http.StatusBadGateway)
190 hashAlgo := digestInfo[0]
191 var hasherNew func() hash.Hash
198 hasherNew = sha256.New
199 hashSize = sha256.Size
201 hasherNew = sha512.New
202 hashSize = sha512.Size
203 case HashAlgoBLAKE2b256:
204 hasherNew = blake2b256New
205 hashSize = blake2b.Size256
208 r.RemoteAddr, "pypi", filename,
209 "unknown digest algorithm", hashAlgo,
211 http.Error(w, "unknown digest algorithm", http.StatusBadGateway)
214 if len(digest) != hashSize {
215 log.Println(r.RemoteAddr, "pypi", filename, "invalid digest length")
216 http.Error(w, "invalid digest length", http.StatusBadGateway)
221 if pkgURL.Host == "" {
222 uri = pypiURLParsed.ResolveReference(pkgURL).String()
224 uri = pkgURL.String()
227 path := filepath.Join(dirPath, filename)
228 if filename == filenameGet {
230 // Skip heavy remote call, when shutting down
231 http.Error(w, "shutting down", http.StatusInternalServerError)
234 log.Println(r.RemoteAddr, "pypi download", filename)
235 resp, err = http.Get(uri)
237 log.Println(r.RemoteAddr, "pypi download error:", err.Error())
238 http.Error(w, err.Error(), http.StatusBadGateway)
241 defer resp.Body.Close()
242 hasher := hasherNew()
243 hasherSHA256 := sha256.New()
244 dst, err := TempFile(dirPath)
246 http.Error(w, err.Error(), http.StatusInternalServerError)
249 dstBuf := bufio.NewWriter(dst)
250 wrs := []io.Writer{hasher, dstBuf}
251 if hashAlgo != HashAlgoSHA256 {
252 wrs = append(wrs, hasherSHA256)
254 wr := io.MultiWriter(wrs...)
255 if _, err = io.Copy(wr, resp.Body); err != nil {
256 os.Remove(dst.Name())
258 http.Error(w, err.Error(), http.StatusInternalServerError)
261 if err = dstBuf.Flush(); err != nil {
262 os.Remove(dst.Name())
264 http.Error(w, err.Error(), http.StatusInternalServerError)
267 if bytes.Compare(hasher.Sum(nil), digest) != 0 {
268 log.Println(r.RemoteAddr, "pypi", filename, "digest mismatch")
269 os.Remove(dst.Name())
271 http.Error(w, "digest mismatch", http.StatusBadGateway)
274 if err = dst.Sync(); err != nil {
275 os.Remove(dst.Name())
277 http.Error(w, err.Error(), http.StatusInternalServerError)
280 if err = dst.Close(); err != nil {
281 http.Error(w, err.Error(), http.StatusInternalServerError)
284 if err = os.Rename(dst.Name(), path); err != nil {
285 http.Error(w, err.Error(), http.StatusInternalServerError)
288 if err = DirSync(dirPath); err != nil {
289 http.Error(w, err.Error(), http.StatusInternalServerError)
292 if hashAlgo != HashAlgoSHA256 {
293 hashAlgo = HashAlgoSHA256
294 digest = hasherSHA256.Sum(nil)
295 for _, algo := range knownHashAlgos[1:] {
296 os.Remove(path + "." + algo)
300 if filename == filenameGet || gpgUpdate {
301 if _, err = os.Stat(path); err != nil {
304 resp, err := http.Get(uri + GPGSigExt)
308 if resp.StatusCode != http.StatusOK {
312 sig, err := ioutil.ReadAll(resp.Body)
317 if !bytes.HasPrefix(sig, []byte("-----BEGIN PGP SIGNATURE-----")) {
318 log.Println(r.RemoteAddr, "pypi non PGP signature", filename)
321 if err = WriteFileSync(dirPath, path+GPGSigExt, sig); err != nil {
322 http.Error(w, err.Error(), http.StatusInternalServerError)
325 log.Println(r.RemoteAddr, "pypi downloaded signature", filename)
328 path = path + "." + hashAlgo
329 _, err = os.Stat(path)
333 if !os.IsNotExist(err) {
334 http.Error(w, err.Error(), http.StatusInternalServerError)
337 log.Println(r.RemoteAddr, "pypi touch", filename)
338 if err = WriteFileSync(dirPath, path, digest); err != nil {
339 http.Error(w, err.Error(), http.StatusInternalServerError)
346 func listRoot(w http.ResponseWriter, r *http.Request) {
347 files, err := ioutil.ReadDir(*root)
349 http.Error(w, err.Error(), http.StatusInternalServerError)
352 var result bytes.Buffer
353 result.WriteString(fmt.Sprintf(HTMLBegin, "root"))
354 for _, file := range files {
355 if file.Mode().IsDir() {
356 result.WriteString(fmt.Sprintf(
358 *refreshURLPath+file.Name()+"/",
363 result.WriteString(HTMLEnd)
364 w.Write(result.Bytes())
368 w http.ResponseWriter,
374 dirPath := filepath.Join(*root, dir)
376 if !refreshDir(w, r, dir, "", gpgUpdate) {
379 } else if _, err := os.Stat(dirPath); os.IsNotExist(err) && !refreshDir(w, r, dir, "", false) {
382 fis, err := ioutil.ReadDir(dirPath)
384 http.Error(w, err.Error(), http.StatusInternalServerError)
387 files := make(map[string]struct{}, len(fis)/2)
388 for _, fi := range fis {
389 files[fi.Name()] = struct{}{}
391 var result bytes.Buffer
392 result.WriteString(fmt.Sprintf(HTMLBegin, dir))
393 for _, algo := range knownHashAlgos {
394 for fn, _ := range files {
396 // Skip expensive I/O when shutting down
397 http.Error(w, "shutting down", http.StatusInternalServerError)
400 if !strings.HasSuffix(fn, "."+algo) {
404 digest, err := ioutil.ReadFile(filepath.Join(dirPath, fn))
406 http.Error(w, err.Error(), http.StatusInternalServerError)
409 fnClean := strings.TrimSuffix(fn, "."+algo)
410 delete(files, fnClean)
412 if _, err = os.Stat(filepath.Join(dirPath, fnClean+GPGSigExt)); err == nil {
413 gpgSigAttr = " data-gpg-sig=true"
414 delete(files, fnClean+GPGSigExt)
416 result.WriteString(fmt.Sprintf(
418 strings.Join([]string{
419 *refreshURLPath, dir, "/", fnClean,
420 "#", algo, "=", hex.EncodeToString(digest),
427 result.WriteString(HTMLEnd)
428 w.Write(result.Bytes())
431 func servePkg(w http.ResponseWriter, r *http.Request, dir, filename string) {
432 log.Println(r.RemoteAddr, "get", filename)
433 path := filepath.Join(*root, dir, filename)
434 if _, err := os.Stat(path); os.IsNotExist(err) {
435 if !refreshDir(w, r, dir, filename, false) {
439 http.ServeFile(w, r, path)
442 func serveUpload(w http.ResponseWriter, r *http.Request) {
444 username, password, ok := r.BasicAuth()
446 log.Println(r.RemoteAddr, "unauthenticated", username)
447 http.Error(w, "unauthenticated", http.StatusUnauthorized)
450 auther, ok := passwords[username]
451 if !ok || !auther.Auth(password) {
452 log.Println(r.RemoteAddr, "unauthenticated", username)
453 http.Error(w, "unauthenticated", http.StatusUnauthorized)
459 if err = r.ParseMultipartForm(1 << 20); err != nil {
460 http.Error(w, err.Error(), http.StatusBadRequest)
463 pkgNames, exists := r.MultipartForm.Value["name"]
464 if !exists || len(pkgNames) != 1 {
465 http.Error(w, "single name is expected in request", http.StatusBadRequest)
468 pkgName := normalizationRe.ReplaceAllString(pkgNames[0], "-")
469 dirPath := filepath.Join(*root, pkgName)
470 var digestExpected []byte
471 if digestExpectedHex, exists := r.MultipartForm.Value["sha256_digest"]; exists {
472 digestExpected, err = hex.DecodeString(digestExpectedHex[0])
474 http.Error(w, "bad sha256_digest: "+err.Error(), http.StatusBadRequest)
478 gpgSigsExpected := make(map[string]struct{})
480 // Checking is it internal package
481 if _, err = os.Stat(filepath.Join(dirPath, InternalFlag)); err != nil {
482 log.Println(r.RemoteAddr, "non-internal package", pkgName)
483 http.Error(w, "unknown internal package", http.StatusUnauthorized)
487 for _, file := range r.MultipartForm.File["content"] {
488 filename := file.Filename
489 gpgSigsExpected[filename+GPGSigExt] = struct{}{}
490 log.Println(r.RemoteAddr, "put", filename, "by", username)
491 path := filepath.Join(dirPath, filename)
492 if _, err = os.Stat(path); err == nil {
493 log.Println(r.RemoteAddr, "already exists", filename)
494 http.Error(w, "already exists", http.StatusBadRequest)
497 if !mkdirForPkg(w, r, pkgName) {
500 src, err := file.Open()
503 http.Error(w, err.Error(), http.StatusInternalServerError)
506 dst, err := TempFile(dirPath)
508 http.Error(w, err.Error(), http.StatusInternalServerError)
511 dstBuf := bufio.NewWriter(dst)
512 hasher := sha256.New()
513 wr := io.MultiWriter(hasher, dst)
514 if _, err = io.Copy(wr, src); err != nil {
515 os.Remove(dst.Name())
517 http.Error(w, err.Error(), http.StatusInternalServerError)
520 if err = dstBuf.Flush(); err != nil {
521 os.Remove(dst.Name())
523 http.Error(w, err.Error(), http.StatusInternalServerError)
526 if err = dst.Sync(); err != nil {
527 os.Remove(dst.Name())
529 http.Error(w, err.Error(), http.StatusInternalServerError)
533 digest := hasher.Sum(nil)
534 if digestExpected != nil {
535 if bytes.Compare(digestExpected, digest) == 0 {
536 log.Println(r.RemoteAddr, filename, "good checksum received")
538 log.Println(r.RemoteAddr, filename, "bad checksum received")
539 http.Error(w, "bad checksum", http.StatusBadRequest)
540 os.Remove(dst.Name())
544 if err = os.Rename(dst.Name(), path); err != nil {
545 http.Error(w, err.Error(), http.StatusInternalServerError)
548 if err = DirSync(dirPath); err != nil {
549 http.Error(w, err.Error(), http.StatusInternalServerError)
552 if err = WriteFileSync(dirPath, path+"."+HashAlgoSHA256, digest); err != nil {
553 http.Error(w, err.Error(), http.StatusInternalServerError)
557 for _, file := range r.MultipartForm.File["gpg_signature"] {
558 filename := file.Filename
559 if _, exists := gpgSigsExpected[filename]; !exists {
560 http.Error(w, "unexpected GPG signature filename", http.StatusBadRequest)
563 delete(gpgSigsExpected, filename)
564 log.Println(r.RemoteAddr, "put", filename, "by", username)
565 path := filepath.Join(dirPath, filename)
566 if _, err = os.Stat(path); err == nil {
567 log.Println(r.RemoteAddr, "already exists", filename)
568 http.Error(w, "already exists", http.StatusBadRequest)
571 src, err := file.Open()
573 http.Error(w, err.Error(), http.StatusInternalServerError)
576 sig, err := ioutil.ReadAll(src)
579 http.Error(w, err.Error(), http.StatusInternalServerError)
582 if err = WriteFileSync(dirPath, path, sig); err != nil {
583 http.Error(w, err.Error(), http.StatusInternalServerError)
589 func handler(w http.ResponseWriter, r *http.Request) {
595 if strings.HasPrefix(r.URL.Path, *norefreshURLPath) {
596 path = strings.TrimPrefix(r.URL.Path, *norefreshURLPath)
597 } else if strings.HasPrefix(r.URL.Path, *refreshURLPath) {
598 path = strings.TrimPrefix(r.URL.Path, *refreshURLPath)
600 } else if strings.HasPrefix(r.URL.Path, *gpgUpdateURLPath) {
601 path = strings.TrimPrefix(r.URL.Path, *gpgUpdateURLPath)
605 http.Error(w, "unknown action", http.StatusBadRequest)
608 parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
610 http.Error(w, "invalid path", http.StatusBadRequest)
617 listDir(w, r, parts[0], autorefresh, gpgUpdate)
620 servePkg(w, r, parts[0], parts[1])
625 http.Error(w, "unknown action", http.StatusBadRequest)
632 fmt.Println(Warranty)
636 fmt.Println("GoCheese version " + Version + " built with " + runtime.Version())
640 if !goodIntegrity() {
649 if (*tlsCert != "" && *tlsKey == "") || (*tlsCert == "" && *tlsKey != "") {
650 log.Fatalln("Both -tls-cert and -tls-key are required")
653 pypiURLParsed, err = url.Parse(*pypiURL)
658 log.Println("root:", *root, "bind:", *bind)
660 ln, err := net.Listen("tcp", *bind)
664 ln = netutil.LimitListener(ln, *maxClients)
665 server := &http.Server{
666 ReadTimeout: time.Minute,
667 WriteTimeout: time.Minute,
669 http.HandleFunc(*norefreshURLPath, handler)
670 http.HandleFunc(*refreshURLPath, handler)
671 if *gpgUpdateURLPath != "" {
672 http.HandleFunc(*gpgUpdateURLPath, handler)
675 needsRefreshPasswd := make(chan os.Signal, 0)
676 needsShutdown := make(chan os.Signal, 0)
677 exitErr := make(chan error, 0)
678 signal.Notify(needsRefreshPasswd, syscall.SIGHUP)
679 signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
681 for range needsRefreshPasswd {
682 log.Println("Refreshing passwords")
686 go func(s *http.Server) {
689 log.Println("Shutting down")
690 ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
691 exitErr <- s.Shutdown(ctx)
696 err = server.Serve(ln)
698 err = server.ServeTLS(ln, *tlsCert, *tlsKey)
700 if err != http.ErrServerClosed {
703 if err := <-exitErr; err != nil {