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/net/netutil"
52 HTMLBegin = `<!DOCTYPE html>
55 <title>Links for %s</title>
59 HTMLEnd = " </body>\n</html>\n"
60 HTMLElement = " <a href=\"%s\"%s>%s</a><br/>\n"
61 InternalFlag = ".internal"
63 GPGSigAttr = " data-gpg-sig=true"
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 Version string = "UNKNOWN"
82 root = flag.String("root", "./packages", "Path to packages directory")
83 bind = flag.String("bind", "[::]:8080", "Address to bind to")
84 tlsCert = flag.String("tls-cert", "", "Path to TLS X.509 certificate")
85 tlsKey = flag.String("tls-key", "", "Path to TLS X.509 private key")
86 norefreshURLPath = flag.String("norefresh", "/norefresh/", "Non-refreshing URL path")
87 refreshURLPath = flag.String("refresh", "/simple/", "Auto-refreshing URL path")
88 gpgUpdateURLPath = flag.String("gpgupdate", "/gpgupdate/", "GPG forceful refreshing URL path")
89 pypiURL = flag.String("pypi", "https://pypi.org/simple/", "Upstream PyPI URL")
90 passwdPath = flag.String("passwd", "passwd", "Path to file with authenticators")
91 passwdCheck = flag.Bool("passwd-check", false, "Test the -passwd file for syntax errors and exit")
92 fsck = flag.Bool("fsck", false, "Check integrity of all packages")
93 maxClients = flag.Int("maxclients", 128, "Maximal amount of simultaneous clients")
94 version = flag.Bool("version", false, "Print version information")
95 warranty = flag.Bool("warranty", false, "Print warranty information")
99 pypiURLParsed *url.URL
100 normalizationRe *regexp.Regexp = regexp.MustCompilePOSIX("[-_.]+")
103 func mkdirForPkg(w http.ResponseWriter, r *http.Request, dir string) bool {
104 path := filepath.Join(*root, dir)
105 if _, err := os.Stat(path); os.IsNotExist(err) {
106 if err = os.Mkdir(path, os.FileMode(0777)); err != nil {
107 http.Error(w, err.Error(), http.StatusInternalServerError)
110 log.Println(r.RemoteAddr, "mkdir", dir)
116 w http.ResponseWriter,
122 if _, err := os.Stat(filepath.Join(*root, dir, InternalFlag)); err == nil {
125 resp, err := http.Get(*pypiURL + dir + "/")
127 http.Error(w, err.Error(), http.StatusBadGateway)
130 body, err := ioutil.ReadAll(resp.Body)
133 http.Error(w, err.Error(), http.StatusBadGateway)
136 if !mkdirForPkg(w, r, dir) {
139 dirPath := filepath.Join(*root, dir)
140 var submatches []string
146 for _, lineRaw := range bytes.Split(body, []byte("\n")) {
147 submatches = pkgPyPI.FindStringSubmatch(string(lineRaw))
148 if len(submatches) == 0 {
152 filename = submatches[2]
153 if pkgURL, err = url.Parse(uri); err != nil {
154 http.Error(w, err.Error(), http.StatusBadGateway)
158 if pkgURL.Fragment == "" {
159 log.Println(r.RemoteAddr, "pypi", filename, "no digest provided")
160 http.Error(w, "no digest provided", http.StatusBadGateway)
163 digestInfo := strings.Split(pkgURL.Fragment, "=")
164 if len(digestInfo) == 1 {
165 // Ancient non PEP-0503 PyPIs, assume MD5
166 digestInfo = []string{"md5", digestInfo[0]}
167 } else if len(digestInfo) != 2 {
168 log.Println(r.RemoteAddr, "pypi", filename, "invalid digest provided")
169 http.Error(w, "invalid digest provided", http.StatusBadGateway)
172 digest, err = hex.DecodeString(digestInfo[1])
174 http.Error(w, err.Error(), http.StatusBadGateway)
177 var hasherNew func() hash.Hash
180 switch digestInfo[0] {
187 hasherNew = sha256.New
188 hashSize = sha256.Size
191 hasherNew = sha512.New
192 hashSize = sha512.Size
195 r.RemoteAddr, "pypi", filename,
196 "unknown digest algorithm", digestInfo[0],
198 http.Error(w, "unknown digest algorithm", http.StatusBadGateway)
201 if len(digest) != hashSize {
202 log.Println(r.RemoteAddr, "pypi", filename, "invalid digest length")
203 http.Error(w, "invalid digest length", http.StatusBadGateway)
208 if pkgURL.Host == "" {
209 uri = pypiURLParsed.ResolveReference(pkgURL).String()
211 uri = pkgURL.String()
213 path = filepath.Join(dirPath, filename)
214 if filename == filenameGet {
216 // Skip heavy remote call, when shutting down
217 http.Error(w, "shutting down", http.StatusInternalServerError)
220 log.Println(r.RemoteAddr, "pypi download", filename)
221 resp, err = http.Get(uri)
223 log.Println(r.RemoteAddr, "pypi download error:", err.Error())
224 http.Error(w, err.Error(), http.StatusBadGateway)
227 defer resp.Body.Close()
228 hasher := hasherNew()
229 hasherOur := sha256.New()
230 dst, err := TempFile(dirPath)
232 http.Error(w, err.Error(), http.StatusInternalServerError)
235 dstBuf := bufio.NewWriter(dst)
236 wrs := []io.Writer{hasher, dstBuf}
237 if hashExt != ".sha256" {
238 wrs = append(wrs, hasherOur)
240 wr := io.MultiWriter(wrs...)
241 if _, err = io.Copy(wr, resp.Body); err != nil {
242 os.Remove(dst.Name())
244 http.Error(w, err.Error(), http.StatusInternalServerError)
247 if err = dstBuf.Flush(); err != nil {
248 os.Remove(dst.Name())
250 http.Error(w, err.Error(), http.StatusInternalServerError)
253 if bytes.Compare(hasher.Sum(nil), digest) != 0 {
254 log.Println(r.RemoteAddr, "pypi", filename, "digest mismatch")
255 os.Remove(dst.Name())
257 http.Error(w, "digest mismatch", http.StatusBadGateway)
260 if err = dst.Sync(); err != nil {
261 os.Remove(dst.Name())
263 http.Error(w, err.Error(), http.StatusInternalServerError)
266 if err = dst.Close(); err != nil {
267 http.Error(w, err.Error(), http.StatusInternalServerError)
270 if err = os.Rename(dst.Name(), path); err != nil {
271 http.Error(w, err.Error(), http.StatusInternalServerError)
274 if err = DirSync(dirPath); err != nil {
275 http.Error(w, err.Error(), http.StatusInternalServerError)
278 if hashExt != ".sha256" {
280 digest = hasherOur.Sum(nil)
283 if filename == filenameGet || gpgUpdate {
284 if _, err = os.Stat(path); err != nil {
287 resp, err := http.Get(uri + GPGSigExt)
291 if resp.StatusCode != http.StatusOK {
295 sig, err := ioutil.ReadAll(resp.Body)
300 if err = WriteFileSync(dirPath, path+GPGSigExt, sig); err != nil {
301 http.Error(w, err.Error(), http.StatusInternalServerError)
304 log.Println(r.RemoteAddr, "pypi downloaded signature", filename)
307 path = path + hashExt
308 _, err = os.Stat(path)
312 if !os.IsNotExist(err) {
313 http.Error(w, err.Error(), http.StatusInternalServerError)
316 log.Println(r.RemoteAddr, "pypi touch", filename)
317 if err = WriteFileSync(dirPath, path, digest); err != nil {
318 http.Error(w, err.Error(), http.StatusInternalServerError)
325 func listRoot(w http.ResponseWriter, r *http.Request) {
326 files, err := ioutil.ReadDir(*root)
328 http.Error(w, err.Error(), http.StatusInternalServerError)
331 var result bytes.Buffer
332 result.WriteString(fmt.Sprintf(HTMLBegin, "root"))
333 for _, file := range files {
334 if file.Mode().IsDir() {
335 result.WriteString(fmt.Sprintf(
337 *refreshURLPath+file.Name()+"/",
342 result.WriteString(HTMLEnd)
343 w.Write(result.Bytes())
347 w http.ResponseWriter,
353 dirPath := filepath.Join(*root, dir)
355 if !refreshDir(w, r, dir, "", gpgUpdate) {
358 } else if _, err := os.Stat(dirPath); os.IsNotExist(err) && !refreshDir(w, r, dir, "", false) {
361 fis, err := ioutil.ReadDir(dirPath)
363 http.Error(w, err.Error(), http.StatusInternalServerError)
366 var result bytes.Buffer
367 result.WriteString(fmt.Sprintf(HTMLBegin, dir))
369 var gpgSigAttr string
371 files := make(map[string]struct{}, len(fis)/2)
372 for _, fi := range fis {
373 files[fi.Name()] = struct{}{}
375 for _, algoExt := range []string{".sha256", ".sha512", ".md5"} {
376 for fn, _ := range files {
378 // Skip expensive I/O when shutting down
379 http.Error(w, "shutting down", http.StatusInternalServerError)
382 if !strings.HasSuffix(fn, algoExt) {
385 digest, err = ioutil.ReadFile(filepath.Join(dirPath, fn))
387 http.Error(w, err.Error(), http.StatusInternalServerError)
390 fnClean = strings.TrimSuffix(fn, algoExt)
391 if _, err = os.Stat(filepath.Join(dirPath, fnClean+GPGSigExt)); os.IsNotExist(err) {
394 gpgSigAttr = GPGSigAttr
396 result.WriteString(fmt.Sprintf(
398 strings.Join([]string{
399 *refreshURLPath, dir, "/", fnClean,
400 "#", algoExt[1:], "=", hex.EncodeToString(digest),
405 for _, n := range []string{
416 result.WriteString(HTMLEnd)
417 w.Write(result.Bytes())
420 func servePkg(w http.ResponseWriter, r *http.Request, dir, filename string) {
421 log.Println(r.RemoteAddr, "get", filename)
422 path := filepath.Join(*root, dir, filename)
423 if _, err := os.Stat(path); os.IsNotExist(err) {
424 if !refreshDir(w, r, dir, filename, false) {
428 http.ServeFile(w, r, path)
431 func serveUpload(w http.ResponseWriter, r *http.Request) {
433 username, password, ok := r.BasicAuth()
435 log.Println(r.RemoteAddr, "unauthenticated", username)
436 http.Error(w, "unauthenticated", http.StatusUnauthorized)
439 auther, ok := passwords[username]
440 if !ok || !auther.Auth(password) {
441 log.Println(r.RemoteAddr, "unauthenticated", username)
442 http.Error(w, "unauthenticated", http.StatusUnauthorized)
448 if err = r.ParseMultipartForm(1 << 20); err != nil {
449 http.Error(w, err.Error(), http.StatusBadRequest)
452 pkgNames, exists := r.MultipartForm.Value["name"]
453 if !exists || len(pkgNames) != 1 {
454 http.Error(w, "single name is expected in request", http.StatusBadRequest)
457 dir := normalizationRe.ReplaceAllString(pkgNames[0], "-")
458 dirPath := filepath.Join(*root, dir)
459 var digestExpected []byte
460 if digestExpectedHex, exists := r.MultipartForm.Value["sha256_digest"]; exists {
461 digestExpected, err = hex.DecodeString(digestExpectedHex[0])
463 http.Error(w, "bad sha256_digest: "+err.Error(), http.StatusBadRequest)
467 gpgSigsExpected := make(map[string]struct{})
469 // Checking is it internal package
470 if _, err = os.Stat(filepath.Join(dirPath, InternalFlag)); err != nil {
471 log.Println(r.RemoteAddr, "non-internal package", dir)
472 http.Error(w, "unknown internal package", http.StatusUnauthorized)
476 for _, file := range r.MultipartForm.File["content"] {
477 filename := file.Filename
478 gpgSigsExpected[filename+GPGSigExt] = struct{}{}
479 log.Println(r.RemoteAddr, "put", filename, "by", username)
480 path := filepath.Join(dirPath, filename)
481 if _, err = os.Stat(path); err == nil {
482 log.Println(r.RemoteAddr, "already exists", filename)
483 http.Error(w, "already exists", http.StatusBadRequest)
486 if !mkdirForPkg(w, r, dir) {
489 src, err := file.Open()
492 http.Error(w, err.Error(), http.StatusInternalServerError)
495 dst, err := TempFile(dirPath)
497 http.Error(w, err.Error(), http.StatusInternalServerError)
500 dstBuf := bufio.NewWriter(dst)
501 hasher := sha256.New()
502 wr := io.MultiWriter(hasher, dst)
503 if _, err = io.Copy(wr, src); err != nil {
504 os.Remove(dst.Name())
506 http.Error(w, err.Error(), http.StatusInternalServerError)
509 if err = dstBuf.Flush(); err != nil {
510 os.Remove(dst.Name())
512 http.Error(w, err.Error(), http.StatusInternalServerError)
515 if err = dst.Sync(); err != nil {
516 os.Remove(dst.Name())
518 http.Error(w, err.Error(), http.StatusInternalServerError)
522 digest := hasher.Sum(nil)
523 if digestExpected != nil {
524 if bytes.Compare(digestExpected, digest) == 0 {
525 log.Println(r.RemoteAddr, filename, "good checksum received")
527 log.Println(r.RemoteAddr, filename, "bad checksum received")
528 http.Error(w, "bad checksum", http.StatusBadRequest)
529 os.Remove(dst.Name())
533 if err = os.Rename(dst.Name(), path); err != nil {
534 http.Error(w, err.Error(), http.StatusInternalServerError)
537 if err = DirSync(dirPath); err != nil {
538 http.Error(w, err.Error(), http.StatusInternalServerError)
541 if err = WriteFileSync(dirPath, path+".sha256", digest); err != nil {
542 http.Error(w, err.Error(), http.StatusInternalServerError)
546 for _, file := range r.MultipartForm.File["gpg_signature"] {
547 filename := file.Filename
548 if _, exists := gpgSigsExpected[filename]; !exists {
549 http.Error(w, "unexpected GPG signature filename", http.StatusBadRequest)
552 delete(gpgSigsExpected, filename)
553 log.Println(r.RemoteAddr, "put", filename, "by", username)
554 path := filepath.Join(dirPath, filename)
555 if _, err = os.Stat(path); err == nil {
556 log.Println(r.RemoteAddr, "already exists", filename)
557 http.Error(w, "already exists", http.StatusBadRequest)
560 src, err := file.Open()
562 http.Error(w, err.Error(), http.StatusInternalServerError)
565 sig, err := ioutil.ReadAll(src)
568 http.Error(w, err.Error(), http.StatusInternalServerError)
571 if err = WriteFileSync(dirPath, path, sig); err != nil {
572 http.Error(w, err.Error(), http.StatusInternalServerError)
578 func handler(w http.ResponseWriter, r *http.Request) {
584 if strings.HasPrefix(r.URL.Path, *norefreshURLPath) {
585 path = strings.TrimPrefix(r.URL.Path, *norefreshURLPath)
586 } else if strings.HasPrefix(r.URL.Path, *refreshURLPath) {
587 path = strings.TrimPrefix(r.URL.Path, *refreshURLPath)
589 } else if strings.HasPrefix(r.URL.Path, *gpgUpdateURLPath) {
590 path = strings.TrimPrefix(r.URL.Path, *gpgUpdateURLPath)
594 http.Error(w, "unknown action", http.StatusBadRequest)
597 parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
599 http.Error(w, "invalid path", http.StatusBadRequest)
606 listDir(w, r, parts[0], autorefresh, gpgUpdate)
609 servePkg(w, r, parts[0], parts[1])
614 http.Error(w, "unknown action", http.StatusBadRequest)
618 func goodIntegrity() bool {
619 dirs, err := ioutil.ReadDir(*root)
623 hasher := sha256.New()
624 digest := make([]byte, sha256.Size)
628 for _, dir := range dirs {
629 files, err := ioutil.ReadDir(filepath.Join(*root, dir.Name()))
633 for _, file := range files {
634 if !strings.HasSuffix(file.Name(), ".sha256") {
637 pkgName = strings.TrimSuffix(file.Name(), ".sha256")
638 data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), pkgName))
640 if os.IsNotExist(err) {
646 data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), file.Name()))
650 if bytes.Compare(hasher.Sum(digest[:0]), data) == 0 {
651 fmt.Println(pkgName, "GOOD")
654 fmt.Println(pkgName, "BAD")
665 fmt.Println(Warranty)
669 fmt.Println("GoCheese version " + Version + " built with " + runtime.Version())
673 if !goodIntegrity() {
682 if (*tlsCert != "" && *tlsKey == "") || (*tlsCert == "" && *tlsKey != "") {
683 log.Fatalln("Both -tls-cert and -tls-key are required")
686 pypiURLParsed, err = url.Parse(*pypiURL)
691 log.Println("root:", *root, "bind:", *bind)
693 ln, err := net.Listen("tcp", *bind)
697 ln = netutil.LimitListener(ln, *maxClients)
698 server := &http.Server{
699 ReadTimeout: time.Minute,
700 WriteTimeout: time.Minute,
702 http.HandleFunc(*norefreshURLPath, handler)
703 http.HandleFunc(*refreshURLPath, handler)
704 http.HandleFunc(*gpgUpdateURLPath, handler)
706 needsRefreshPasswd := make(chan os.Signal, 0)
707 needsShutdown := make(chan os.Signal, 0)
708 exitErr := make(chan error, 0)
709 signal.Notify(needsRefreshPasswd, syscall.SIGHUP)
710 signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
712 for range needsRefreshPasswd {
713 log.Println("Refreshing passwords")
717 go func(s *http.Server) {
720 log.Println("Shutting down")
721 ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
722 exitErr <- s.Shutdown(ctx)
727 err = server.Serve(ln)
729 err = server.ServeTLS(ln, *tlsCert, *tlsKey)
731 if err != http.ErrServerClosed {
734 if err := <-exitErr; err != nil {