package main import ( "errors" "flag" "fmt" "github.com/go-ini/ini" "github.com/spf13/afero" "golang.org/x/crypto/openpgp" "io" "io/ioutil" "log" "net/http" "os" "path" "runtime" "strings" "sync" ) var VERSION = "1.3.8" func packageName(name string) string { if index := strings.Index(name, "_"); index != -1 { name = name[:index] } return name } type DeleteObj struct { Filename string DistroName string Arch string } var ( mutex sync.RWMutex configFile = flag.String("c", "conf.ini", "config file location") flagShowVersion = flag.Bool("version", false, "Show deb-simple version") conf = Conf{} pgpEntity *openpgp.Entity fs afero.Fs baseTempDir string ) func main() { flag.Parse() if *flagShowVersion { fmt.Printf("deb-simple %s (%s)\n", VERSION, runtime.Version()) os.Exit(0) } file, err := ioutil.ReadFile(*configFile) if err != nil { log.Fatalln("unable to read config file, exiting...") } if err := ini.MapTo(&conf, file); err != nil { log.Fatalln("unable to marshal config file, exiting...", err) } baseTempDir = path.Join(os.TempDir(), "deb-simple") if _, err := os.Stat(baseTempDir); err != nil { if os.IsNotExist(err) { if err := os.MkdirAll(baseTempDir, 0755); err != nil { return } } } fs = afero.NewBasePathFs(afero.NewOsFs(), conf.Repo.Root) if err := createDirs(conf); err != nil { log.Println(err) log.Fatalln("error creating directory structure, exiting") } if err := setupPgp(conf); err != nil { log.Println(err) log.Fatalln("error loading pgp key, exiting") } log.Println("Indexing packages...") for _, dist := range conf.Repo.DistroNames() { if err := loadCache(dist); err != nil { log.Println("Unable to load cached data for", dist, "- reindexing") distro := &Distro{Name: dist, Architectures: make(map[string]map[string]*PackageFile)} go scanInitialPackages(conf, distro) distros[dist] = distro } } mux := http.NewServeMux() httpFs := afero.NewHttpFs(fs) mux.Handle("/", http.StripPrefix("/", http.FileServer(httpFs))) mux.HandleFunc("/rescan", requireAuth(rescanHandler)) mux.HandleFunc("/upload", requireAuth(uploadHandler)) mux.HandleFunc("/delete", requireAuth(deleteHandler)) bind := fmt.Sprintf(":%d", conf.Http.Port) if conf.Http.SSL { log.Println("running with SSL enabled") log.Fatalln(http.ListenAndServeTLS(bind, conf.Http.SSLCert, conf.Http.SSLKey, mux)) } else { log.Println("running without SSL enabled") log.Fatalln(http.ListenAndServe(bind, mux)) } } func scanInitialPackages(config Conf, dist *Distro) { for _, arch := range config.Repo.ArchitectureNames() { files, err := buildPackageList(config, dist.Name, arch) if err != nil { log.Fatalln("Unable to load packages:", err) } dist.Architectures[arch] = files log.Println("Generating packages file for", dist.Name, arch) createPackagesCached(config, dist.Name, arch, files) } log.Println("Generating Release for", dist.Name) createRelease(config, dist.Name) saveCache(dist) } func setupPgp(config Conf) error { if config.PGP.Key == "" { return nil } secretKey, err := os.Open(config.PGP.Key) if err != nil { return fmt.Errorf("failed to open private key ring file: %s", err) } defer secretKey.Close() entitylist, err := openpgp.ReadKeyRing(secretKey) if err != nil { return fmt.Errorf("failed to read key ring: %s", err) } if len(entitylist) < 1 { return errors.New("no keys in key ring") } pgpEntity = entitylist[0] passphrase := []byte(config.PGP.Passphrase) if err := pgpEntity.PrivateKey.Decrypt(passphrase); err != nil { return err } for _, subkey := range pgpEntity.Subkeys { err := subkey.PrivateKey.Decrypt(passphrase) if err != nil { return err } } return nil } func createDirs(config Conf) error { for _, distro := range config.Repo.DistroNames() { for _, arch := range config.Repo.ArchitectureNames() { if _, err := fs.Stat(config.ArchPath(distro, arch)); err != nil { if os.IsNotExist(err) { log.Printf("Directory for %s (%s) does not exist, creating", distro, arch) if err := fs.MkdirAll(config.ArchPath(distro, arch), 0755); err != nil { return fmt.Errorf("error creating directory for %s (%s): %s", distro, arch, err) } } else { return fmt.Errorf("error inspecting %s (%s): %s", distro, arch, err) } } if _, err := fs.Stat(config.PoolPath(distro, arch)); err != nil { if os.IsNotExist(err) { log.Printf("Directory for %s (%s) does not exist, creating", distro, arch) if err := fs.MkdirAll(config.PoolPath(distro, arch), 0755); err != nil { return fmt.Errorf("error creating directory for %s (%s): %s", distro, arch, err) } } else { return fmt.Errorf("error inspecting %s (%s): %s", distro, arch, err) } } } } return nil } func stripPrefix(str, prefix string) string { if strings.Index(str, prefix) == 0 { return strings.TrimLeft(str[len(prefix):], "/") } return str } func copyFile(oldPath, newPath string) error { old, err := os.Open(oldPath) if err != nil { return err } defer old.Close() n, err := fs.Create(newPath) if err != nil { return err } defer n.Close() _, err = io.Copy(n, old) return err }