deb-simple/server.go

216 lines
4.8 KiB
Go
Raw Normal View History

2018-10-14 08:17:04 +00:00
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"strings"
"golang.org/x/crypto/openpgp"
"runtime"
"errors"
2017-06-12 00:41:49 +00:00
"github.com/go-ini/ini"
2017-09-14 04:23:39 +00:00
"io"
)
var VERSION string = "1.3.6"
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
2017-06-12 00:41:49 +00:00
configFile = flag.String("c", "conf.ini", "config file location")
flagShowVersion = flag.Bool("version", false, "Show deb-simple version")
2017-06-12 00:41:49 +00:00
conf = Conf{}
pgpEntity *openpgp.Entity
)
2018-10-14 08:19:33 +00:00
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...")
}
2017-06-12 00:41:49 +00:00
if err := ini.MapTo(&conf, file); err != nil {
log.Fatalln("unable to marshal config file, exiting...", err)
}
2017-06-12 00:41:49 +00:00
if err := createDirs(conf); err != nil {
log.Println(err)
log.Fatalln("error creating directory structure, exiting")
}
2017-06-12 00:41:49 +00:00
if err := setupPgp(conf); err != nil {
log.Println(err)
log.Fatalln("error loading pgp key, exiting")
}
log.Println("Indexing packages...")
2017-06-12 00:41:49 +00:00
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)}
2017-06-12 00:41:49 +00:00
go scanInitialPackages(conf, distro)
2017-06-12 00:41:49 +00:00
distros[dist] = distro
}
}
2017-06-12 00:41:49 +00:00
mux := http.NewServeMux()
2017-06-12 00:41:49 +00:00
mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(conf.Repo.Root))))
mux.HandleFunc("/rescan", rescanHandler)
mux.HandleFunc("/upload", uploadHandler)
mux.HandleFunc("/delete", deleteHandler)
bind := fmt.Sprintf(":%d", conf.Http.Port)
if conf.Http.SSL {
log.Println("running with SSL enabled")
2017-06-12 00:41:49 +00:00
log.Fatalln(http.ListenAndServeTLS(bind, conf.Http.SSLCert, conf.Http.SSLKey, mux))
} else {
log.Println("running without SSL enabled")
2017-06-12 00:41:49 +00:00
log.Fatalln(http.ListenAndServe(bind, mux))
}
}
2017-06-11 07:35:27 +00:00
func scanInitialPackages(config Conf, dist *Distro) {
2017-06-12 00:41:49 +00:00
for _, arch := range config.Repo.ArchitectureNames() {
2017-06-11 07:35:27 +00:00
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)
}
2017-06-12 00:41:49 +00:00
log.Println("Generating Release for", dist.Name)
createRelease(config, dist.Name)
2017-06-12 00:41:49 +00:00
saveCache(dist)
2017-06-11 07:35:27 +00:00
}
func setupPgp(config Conf) error {
2017-06-12 00:41:49 +00:00
if config.PGP.Key == "" {
return nil
}
2017-06-12 00:41:49 +00:00
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]
2017-06-12 00:41:49 +00:00
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 {
2017-06-12 00:41:49 +00:00
for _, distro := range config.Repo.DistroNames() {
for _, arch := range config.Repo.ArchitectureNames() {
if _, err := os.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 := os.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 := os.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 := os.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
2017-09-14 04:23:39 +00:00
}
func copyFile(oldPath, newPath string) error {
old, err := os.Open(oldPath)
if err != nil {
return err
}
defer old.Close()
n, err := os.Create(newPath)
if err != nil {
return err
}
defer n.Close()
_, err = io.Copy(n, old)
return err
}