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
44 "golang.org/x/net/netutil"
48 HTMLBegin = "<!DOCTYPE html><html><head><title>Links for %s</title></head><body><h1>Links for %s</h1>\n"
49 HTMLEnd = "</body></html>"
50 HTMLElement = "<a href='%s'>%s</a><br/>\n"
51 SHA256Prefix = "sha256="
53 InternalFlag = ".internal"
55 Warranty = `This program is free software: you can redistribute it and/or modify
56 it under the terms of the GNU General Public License as published by
57 the Free Software Foundation, version 3 of the License.
59 This program is distributed in the hope that it will be useful,
60 but WITHOUT ANY WARRANTY; without even the implied warranty of
61 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62 GNU General Public License for more details.
64 You should have received a copy of the GNU General Public License
65 along with this program. If not, see <http://www.gnu.org/licenses/>.`
69 pkgPyPI = regexp.MustCompile(`^.*<a href="([^"]+)"[^>]*>(.+)</a><br/>.*$`)
70 Version string = "UNKNOWN"
72 root = flag.String("root", "./packages", "Path to packages directory")
73 bind = flag.String("bind", "[::]:8080", "Address to bind to")
74 tlsCert = flag.String("tls-cert", "", "Path to TLS X.509 certificate")
75 tlsKey = flag.String("tls-key", "", "Path to TLS X.509 private key")
76 norefreshURLPath = flag.String("norefresh", "/norefresh/", "Non-refreshing URL path")
77 refreshURLPath = flag.String("refresh", "/simple/", "Auto-refreshing URL path")
78 pypiURL = flag.String("pypi", "https://pypi.org/simple/", "Upstream PyPI URL")
79 passwdPath = flag.String("passwd", "passwd", "Path to file with authenticators")
80 passwdCheck = flag.Bool("passwd-check", false, "Test the -passwd file for syntax errors and exit")
81 fsck = flag.Bool("fsck", false, "Check integrity of all packages")
82 maxClients = flag.Int("maxclients", 128, "Maximal amount of simultaneous clients")
83 version = flag.Bool("version", false, "Print version information")
84 warranty = flag.Bool("warranty", false, "Print warranty information")
89 func mkdirForPkg(w http.ResponseWriter, r *http.Request, dir string) bool {
90 path := filepath.Join(*root, dir)
91 if _, err := os.Stat(path); os.IsNotExist(err) {
92 if err = os.Mkdir(path, os.FileMode(0777)); err != nil {
93 http.Error(w, err.Error(), http.StatusInternalServerError)
96 log.Println(r.RemoteAddr, "mkdir", dir)
101 func refreshDir(w http.ResponseWriter, r *http.Request, dir, filenameGet string) bool {
102 if _, err := os.Stat(filepath.Join(*root, dir, InternalFlag)); err == nil {
103 log.Println(r.RemoteAddr, "pypi refresh skip, internal package", dir)
106 log.Println(r.RemoteAddr, "pypi refresh", dir)
107 resp, err := http.Get(*pypiURL + dir + "/")
109 http.Error(w, err.Error(), http.StatusBadGateway)
112 body, err := ioutil.ReadAll(resp.Body)
115 http.Error(w, err.Error(), http.StatusBadGateway)
118 if !mkdirForPkg(w, r, dir) {
121 var submatches []string
127 for _, lineRaw := range bytes.Split(body, []byte("\n")) {
128 submatches = pkgPyPI.FindStringSubmatch(string(lineRaw))
129 if len(submatches) == 0 {
133 filename = submatches[2]
134 if pkgURL, err = url.Parse(uri); err != nil {
135 http.Error(w, err.Error(), http.StatusInternalServerError)
138 digest, err = hex.DecodeString(strings.TrimPrefix(pkgURL.Fragment, SHA256Prefix))
140 http.Error(w, err.Error(), http.StatusBadGateway)
143 if filename == filenameGet {
145 // Skip heavy remote call, when shutting down
146 http.Error(w, "shutting down", http.StatusInternalServerError)
149 log.Println(r.RemoteAddr, "pypi download", filename)
150 path = filepath.Join(*root, dir, filename)
151 resp, err = http.Get(uri)
153 http.Error(w, err.Error(), http.StatusBadGateway)
156 defer resp.Body.Close()
157 hasher := sha256.New()
158 dst, err := TempFile(filepath.Join(*root, dir))
160 http.Error(w, err.Error(), http.StatusInternalServerError)
163 wr := io.MultiWriter(hasher, dst)
164 if _, err = io.Copy(wr, resp.Body); err != nil {
165 os.Remove(dst.Name())
167 http.Error(w, err.Error(), http.StatusInternalServerError)
170 if bytes.Compare(hasher.Sum(nil), digest) != 0 {
171 log.Println(r.RemoteAddr, "pypi", filename, "digest mismatch")
172 os.Remove(dst.Name())
174 http.Error(w, err.Error(), http.StatusBadGateway)
177 if err = dst.Sync(); err != nil {
178 os.Remove(dst.Name())
180 http.Error(w, err.Error(), http.StatusInternalServerError)
184 if err = os.Rename(dst.Name(), path); err != nil {
185 http.Error(w, err.Error(), http.StatusInternalServerError)
189 path = filepath.Join(*root, dir, filename+SHA256Ext)
190 _, err = os.Stat(path)
194 if !os.IsNotExist(err) {
195 http.Error(w, err.Error(), http.StatusInternalServerError)
198 log.Println(r.RemoteAddr, "pypi touch", filename)
199 if err = ioutil.WriteFile(path, digest, os.FileMode(0666)); err != nil {
200 http.Error(w, err.Error(), http.StatusInternalServerError)
207 func listRoot(w http.ResponseWriter, r *http.Request) {
208 log.Println(r.RemoteAddr, "root")
209 files, err := ioutil.ReadDir(*root)
211 http.Error(w, err.Error(), http.StatusInternalServerError)
214 var result bytes.Buffer
215 result.WriteString(fmt.Sprintf(HTMLBegin, "root", "root"))
216 for _, file := range files {
217 if file.Mode().IsDir() {
218 result.WriteString(fmt.Sprintf(
220 *refreshURLPath+file.Name()+"/",
225 result.WriteString(HTMLEnd)
226 w.Write(result.Bytes())
229 func listDir(w http.ResponseWriter, r *http.Request, dir string, autorefresh bool) {
230 log.Println(r.RemoteAddr, "dir", dir)
231 dirPath := filepath.Join(*root, dir)
233 if !refreshDir(w, r, dir, "") {
236 } else if _, err := os.Stat(dirPath); os.IsNotExist(err) && !refreshDir(w, r, dir, "") {
239 files, err := ioutil.ReadDir(dirPath)
241 http.Error(w, err.Error(), http.StatusInternalServerError)
244 var result bytes.Buffer
245 result.WriteString(fmt.Sprintf(HTMLBegin, dir, dir))
247 var filenameClean string
248 for _, file := range files {
249 if !strings.HasSuffix(file.Name(), SHA256Ext) {
253 // Skip expensive I/O when shutting down
254 http.Error(w, "shutting down", http.StatusInternalServerError)
257 data, err = ioutil.ReadFile(filepath.Join(dirPath, file.Name()))
259 http.Error(w, err.Error(), http.StatusInternalServerError)
262 filenameClean = strings.TrimSuffix(file.Name(), SHA256Ext)
263 result.WriteString(fmt.Sprintf(
265 strings.Join([]string{
266 *refreshURLPath, dir, "/",
267 filenameClean, "#", SHA256Prefix, hex.EncodeToString(data),
272 result.WriteString(HTMLEnd)
273 w.Write(result.Bytes())
276 func servePkg(w http.ResponseWriter, r *http.Request, dir, filename string) {
277 log.Println(r.RemoteAddr, "pkg", filename)
278 path := filepath.Join(*root, dir, filename)
279 if _, err := os.Stat(path); os.IsNotExist(err) {
280 if !refreshDir(w, r, dir, filename) {
284 http.ServeFile(w, r, path)
287 func serveUpload(w http.ResponseWriter, r *http.Request) {
288 username, password, ok := r.BasicAuth()
290 log.Println(r.RemoteAddr, "unauthenticated", username)
291 http.Error(w, "unauthenticated", http.StatusUnauthorized)
294 auther, ok := passwords[username]
295 if !ok || !auther.Auth(password) {
296 log.Println(r.RemoteAddr, "unauthenticated", username)
297 http.Error(w, "unauthenticated", http.StatusUnauthorized)
301 if err = r.ParseMultipartForm(1 << 20); err != nil {
302 http.Error(w, err.Error(), http.StatusBadRequest)
305 for _, file := range r.MultipartForm.File["content"] {
306 filename := file.Filename
307 log.Println(r.RemoteAddr, "upload", filename, "by", username)
308 dir := filename[:strings.LastIndex(filename, "-")]
309 dirPath := filepath.Join(*root, dir)
310 path := filepath.Join(dirPath, filename)
311 if _, err = os.Stat(path); err == nil {
312 log.Println(r.RemoteAddr, "already exists", filename)
313 http.Error(w, "Already exists", http.StatusBadRequest)
316 if !mkdirForPkg(w, r, dir) {
319 internalPath := filepath.Join(dirPath, InternalFlag)
321 if _, err = os.Stat(internalPath); os.IsNotExist(err) {
322 if dst, err = os.Create(internalPath); err != nil {
323 http.Error(w, err.Error(), http.StatusInternalServerError)
328 src, err := file.Open()
331 http.Error(w, err.Error(), http.StatusInternalServerError)
334 dst, err = TempFile(dirPath)
336 http.Error(w, err.Error(), http.StatusInternalServerError)
339 hasher := sha256.New()
340 wr := io.MultiWriter(hasher, dst)
341 if _, err = io.Copy(wr, src); err != nil {
342 os.Remove(dst.Name())
344 http.Error(w, err.Error(), http.StatusInternalServerError)
347 if err = dst.Sync(); err != nil {
348 os.Remove(dst.Name())
350 http.Error(w, err.Error(), http.StatusInternalServerError)
354 if err = os.Rename(dst.Name(), path); err != nil {
355 http.Error(w, err.Error(), http.StatusInternalServerError)
358 if err = ioutil.WriteFile(path+SHA256Ext, hasher.Sum(nil), os.FileMode(0666)); err != nil {
359 http.Error(w, err.Error(), http.StatusInternalServerError)
365 func handler(w http.ResponseWriter, r *http.Request) {
366 if r.Method == "GET" {
369 if strings.HasPrefix(r.URL.Path, *norefreshURLPath) {
370 path = strings.TrimPrefix(r.URL.Path, *norefreshURLPath)
373 path = strings.TrimPrefix(r.URL.Path, *refreshURLPath)
376 parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
378 http.Error(w, "invalid path", http.StatusBadRequest)
385 listDir(w, r, parts[0], autorefresh)
388 servePkg(w, r, parts[0], parts[1])
390 } else if r.Method == "POST" {
395 func goodIntegrity() bool {
396 dirs, err := ioutil.ReadDir(*root)
400 hasher := sha256.New()
401 digest := make([]byte, sha256.Size)
405 for _, dir := range dirs {
406 files, err := ioutil.ReadDir(filepath.Join(*root, dir.Name()))
410 for _, file := range files {
411 if !strings.HasSuffix(file.Name(), SHA256Ext) {
414 pkgName = strings.TrimSuffix(file.Name(), SHA256Ext)
415 data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), pkgName))
417 if os.IsNotExist(err) {
423 data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), file.Name()))
427 if bytes.Compare(hasher.Sum(digest[:0]), data) == 0 {
428 log.Println(pkgName, "GOOD")
431 log.Println(pkgName, "BAD")
442 fmt.Println(Warranty)
446 fmt.Println("GoCheese version " + Version + " built with " + runtime.Version())
450 if !goodIntegrity() {
459 if (*tlsCert != "" && *tlsKey == "") || (*tlsCert == "" && *tlsKey != "") {
460 log.Fatalln("Both -tls-cert and -tls-key are required")
463 log.Println("root:", *root, "bind:", *bind)
465 ln, err := net.Listen("tcp", *bind)
469 ln = netutil.LimitListener(ln, *maxClients)
470 server := &http.Server{
471 ReadTimeout: time.Minute,
472 WriteTimeout: time.Minute,
474 http.HandleFunc(*norefreshURLPath, handler)
475 http.HandleFunc(*refreshURLPath, handler)
477 needsRefreshPasswd := make(chan os.Signal, 0)
478 needsShutdown := make(chan os.Signal, 0)
479 exitErr := make(chan error, 0)
480 signal.Notify(needsRefreshPasswd, syscall.SIGHUP)
481 signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
483 for range needsRefreshPasswd {
484 log.Println("Refreshing passwords")
488 go func(s *http.Server) {
491 log.Println("Shutting down")
492 ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
493 exitErr <- s.Shutdown(ctx)
498 err = server.Serve(ln)
500 err = server.ServeTLS(ln, *tlsCert, *tlsKey)
502 if err != http.ErrServerClosed {
505 if err := <-exitErr; err != nil {