2023-06-04 05:07:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-04 15:32:23 +00:00
|
|
|
"crypto/sha256"
|
2023-06-04 05:07:55 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-06-04 15:32:23 +00:00
|
|
|
"io"
|
2023-06-04 05:07:55 +00:00
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2023-06-04 18:32:18 +00:00
|
|
|
"sync"
|
2023-06-04 05:07:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Information for a LineageOS ROM available for download
|
|
|
|
type LineageOSROM struct {
|
2023-06-04 21:52:08 +00:00
|
|
|
Datetime int `json:"datetime"` // Unix timestamp - i.e. 1685907926
|
|
|
|
Filename string `json:"filename"` // .zip filename - i.e. lineage-20.0-20230604-UNOFFICIAL-sunfish.zip
|
|
|
|
ID string `json:"id"` // A unique identifier such as a SHA256 hash of the .zip - i.e. 603bfc02e403e5fd1bf9ed74383f1d6c9ec7fb228d03c4b37753033d79488e93
|
2023-06-09 14:55:11 +00:00
|
|
|
Romtype string `json:"romtype"` // i.e. NIGHTLY or UNOFFICIAL
|
2023-06-04 21:52:08 +00:00
|
|
|
Size int `json:"size"` // size of .zip file in bytes
|
2023-06-09 14:55:11 +00:00
|
|
|
URL string `json:"url"` // Accessible URL where client could download the .zip file
|
2023-06-04 21:52:08 +00:00
|
|
|
Version string `json:"version"` // LineageOS version - i.e. 20.0
|
2023-06-04 05:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The HTTP response JSON should be a JSON array of lineageOSROMS available for download
|
2023-06-04 18:32:18 +00:00
|
|
|
type HTTPResponseJSON struct {
|
2023-06-04 05:07:55 +00:00
|
|
|
Response []LineageOSROM `json:"response"`
|
|
|
|
}
|
|
|
|
|
2023-06-04 21:52:08 +00:00
|
|
|
// Caches data about available ROMs in memory so we don't need to reference the filesystem for each request
|
2023-06-04 18:32:18 +00:00
|
|
|
type ROMCache struct {
|
2023-06-04 21:52:08 +00:00
|
|
|
ROMs []LineageOSROM
|
|
|
|
Cached map[string]bool // to quickly lookup if a file is already cached
|
|
|
|
sync.Mutex // We have multiple goroutines that may be accessing this data simultaneously, so we much lock / unlock it to prevent race conditions
|
2023-06-04 18:32:18 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 22:15:13 +00:00
|
|
|
const (
|
|
|
|
romDirectory = "public" // directory where ROMs are available for download
|
|
|
|
buildOutDirectory = "out" // directory from build system containing artifacts which we can move to romDirectory
|
|
|
|
)
|
|
|
|
|
2023-06-04 21:52:08 +00:00
|
|
|
var ( // evil global variable
|
2023-06-04 18:32:18 +00:00
|
|
|
romCache ROMCache
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-06-04 18:41:22 +00:00
|
|
|
romCache = ROMCache{}
|
2023-06-04 18:35:27 +00:00
|
|
|
romCache.Cached = make(map[string]bool)
|
2023-06-04 19:28:36 +00:00
|
|
|
|
2023-06-04 21:52:08 +00:00
|
|
|
// Check if any new build artifacts and preload the romCache
|
2023-06-04 22:15:13 +00:00
|
|
|
moveBuildArtifacts()
|
|
|
|
go updateROMCache()
|
2023-06-04 18:32:18 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 21:52:08 +00:00
|
|
|
// HTTP Server
|
2023-06-04 05:07:55 +00:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
//Public static files
|
|
|
|
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
|
|
|
|
|
|
|
|
// ROM list
|
2023-06-04 05:08:49 +00:00
|
|
|
http.HandleFunc("/", lineageOSROMListHandler)
|
2023-06-04 05:07:55 +00:00
|
|
|
|
|
|
|
log.Print("Service listening on :8080")
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the ROM files on the filesystem and populates a slice of linageOSROMs
|
2023-06-04 22:15:13 +00:00
|
|
|
func updateROMCache() {
|
2023-06-04 05:07:55 +00:00
|
|
|
|
2023-06-04 19:34:09 +00:00
|
|
|
log.Printf("updating ROM cache")
|
|
|
|
|
2023-06-04 18:56:58 +00:00
|
|
|
f, err := os.Open(romDirectory)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to open ROM directory: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-06-04 19:28:36 +00:00
|
|
|
defer f.Close()
|
2023-06-04 18:56:58 +00:00
|
|
|
files, err := f.ReadDir(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to read files in directory: %v", err)
|
2023-06-04 18:32:18 +00:00
|
|
|
return
|
2023-06-04 05:07:55 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 18:56:58 +00:00
|
|
|
for i, v := range files {
|
2023-06-04 18:32:18 +00:00
|
|
|
|
2023-06-04 21:41:01 +00:00
|
|
|
isLineageROM, splitName := isLineageROMZip(v)
|
|
|
|
if !isLineageROM {
|
2023-06-04 18:56:58 +00:00
|
|
|
continue
|
2023-06-04 05:07:55 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 18:32:18 +00:00
|
|
|
// skip already cached files
|
|
|
|
romCache.Lock()
|
2023-06-04 18:56:58 +00:00
|
|
|
if _, ok := romCache.Cached[v.Name()]; ok {
|
2023-06-04 18:32:18 +00:00
|
|
|
romCache.Unlock()
|
2023-06-04 18:56:58 +00:00
|
|
|
continue
|
2023-06-04 05:07:55 +00:00
|
|
|
}
|
2023-06-04 18:32:18 +00:00
|
|
|
romCache.Unlock()
|
2023-06-04 05:07:55 +00:00
|
|
|
|
2023-06-04 18:56:58 +00:00
|
|
|
go func(v fs.DirEntry) {
|
|
|
|
fInfo, err := v.Info()
|
2023-06-04 18:32:18 +00:00
|
|
|
if err != nil {
|
2023-06-04 18:56:58 +00:00
|
|
|
log.Printf("failed to get file info '%s': %v", v.Name(), err)
|
2023-06-04 18:32:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-04 18:56:58 +00:00
|
|
|
fileHash, err := hashFile(fmt.Sprintf("%s/%s", romDirectory, v.Name()))
|
2023-06-04 18:32:18 +00:00
|
|
|
if err != nil {
|
2023-06-04 18:56:58 +00:00
|
|
|
log.Printf("ingore zip file '%s', failed to get sha256 hash: %v", v.Name(), err)
|
2023-06-04 18:32:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lineageOSROM := LineageOSROM{
|
|
|
|
Datetime: int(fInfo.ModTime().Unix()),
|
2023-06-04 18:56:58 +00:00
|
|
|
Filename: v.Name(),
|
2023-06-04 18:32:18 +00:00
|
|
|
ID: fileHash,
|
2023-06-04 22:28:43 +00:00
|
|
|
Romtype: splitName[3], // UNOFFICIAL
|
2023-06-04 18:32:18 +00:00
|
|
|
Size: int(fInfo.Size()),
|
2023-06-04 18:56:58 +00:00
|
|
|
URL: fmt.Sprintf("https://lineageos-ota.deadbeef.codes/public/%s", v.Name()),
|
2023-06-04 18:32:18 +00:00
|
|
|
Version: splitName[1],
|
|
|
|
}
|
|
|
|
|
|
|
|
romCache.Lock()
|
2023-06-04 18:56:58 +00:00
|
|
|
if _, ok := romCache.Cached[v.Name()]; ok { // it's possible another goroutine was already working to cache the file, so we check at the end
|
|
|
|
romCache.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2023-06-04 18:32:18 +00:00
|
|
|
romCache.ROMs = append(romCache.ROMs, lineageOSROM)
|
2023-06-04 18:56:58 +00:00
|
|
|
romCache.Cached[v.Name()] = true
|
2023-06-04 18:32:18 +00:00
|
|
|
romCache.Unlock()
|
2023-06-04 18:56:58 +00:00
|
|
|
}(files[i])
|
2023-06-04 05:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 19:28:36 +00:00
|
|
|
// returns true if new builds were moved
|
2023-06-04 22:15:13 +00:00
|
|
|
func moveBuildArtifacts() bool {
|
2023-06-04 19:28:36 +00:00
|
|
|
|
2023-06-04 22:15:13 +00:00
|
|
|
f, err := os.Open(buildOutDirectory)
|
2023-06-04 19:28:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to open ROM directory: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
files, err := f.ReadDir(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to read files in directory: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var newROMs bool
|
|
|
|
|
|
|
|
for _, v := range files {
|
2023-06-04 21:41:01 +00:00
|
|
|
if isLineageROM, _ := isLineageROMZip(v); !isLineageROM { // skip files that aren't LineageOS ROMs
|
2023-06-04 19:28:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-06-04 21:41:01 +00:00
|
|
|
|
2023-06-04 19:28:36 +00:00
|
|
|
newROMs = true
|
2023-06-04 19:34:09 +00:00
|
|
|
log.Printf("new build found - moving file %s", v.Name())
|
2023-06-04 21:58:16 +00:00
|
|
|
romCache.Lock() // lock to prevent multiple concurrent goroutines moving the same file
|
2023-06-04 22:15:13 +00:00
|
|
|
err := moveBuildFile(fmt.Sprintf("%s/%s", buildOutDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name()))
|
2023-06-04 21:58:16 +00:00
|
|
|
romCache.Unlock()
|
2023-06-04 19:28:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newROMs
|
|
|
|
}
|
|
|
|
|
2023-06-04 21:41:01 +00:00
|
|
|
// false if a file is not a LineageOS ROM .zip file
|
|
|
|
// no formal validation is performed - only file naming convention is checked
|
|
|
|
// also returns a lineage ROM's filename sliced and delimited by -'s
|
|
|
|
// Example filename: lineage-20.0-20230604-UNOFFICIAL-sunfish.zip
|
|
|
|
func isLineageROMZip(v fs.DirEntry) (bool, []string) {
|
|
|
|
|
|
|
|
// skip directories, non .zip files and files that don't begin with lineage-
|
|
|
|
if v.Type().IsDir() || !strings.HasSuffix(v.Name(), ".zip") || !strings.HasPrefix(v.Name(), "lineage-") {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
splitName := strings.Split(v.Name(), "-")
|
|
|
|
// expect 5 dashes
|
|
|
|
return len(splitName) == 5, splitName
|
|
|
|
}
|
|
|
|
|
2023-06-04 05:07:55 +00:00
|
|
|
// http - GET /
|
|
|
|
// Writes JSON response for the updater app to know what versions are available to download
|
|
|
|
func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
|
2023-06-04 21:58:16 +00:00
|
|
|
go func() {
|
2023-06-04 22:15:13 +00:00
|
|
|
newBuilds := moveBuildArtifacts()
|
2023-06-04 21:58:16 +00:00
|
|
|
if newBuilds {
|
2023-06-04 22:15:13 +00:00
|
|
|
updateROMCache()
|
2023-06-04 21:58:16 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-06-04 18:32:18 +00:00
|
|
|
romCache.Lock()
|
2023-06-04 21:58:16 +00:00
|
|
|
httpResponseJSON := &HTTPResponseJSON{Response: romCache.ROMs}
|
2023-06-04 18:32:18 +00:00
|
|
|
romCache.Unlock()
|
2023-06-04 05:07:55 +00:00
|
|
|
|
|
|
|
b, err := json.Marshal(httpResponseJSON)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
log.Printf("failed to marshal lineageOSROMs to json: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
}
|
2023-06-04 15:32:23 +00:00
|
|
|
|
2023-06-04 21:41:01 +00:00
|
|
|
// Returns a sha256 hash of a file located at the path provided
|
2023-06-04 15:32:23 +00:00
|
|
|
func hashFile(filename string) (string, error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to open file '%s': %v: ", filename, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to copy data from file to hash function: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-06-04 18:32:18 +00:00
|
|
|
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
2023-06-04 15:32:23 +00:00
|
|
|
}
|
2023-06-04 19:42:44 +00:00
|
|
|
|
|
|
|
// A custom "move file" function because in docker container the mounted folders are different overlay filesystems
|
|
|
|
// Instead of os.Rename, we must copy and delete
|
|
|
|
func moveBuildFile(src, dst string) error {
|
|
|
|
sourceFileStat, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sourceFileStat.Mode().IsRegular() {
|
|
|
|
return fmt.Errorf("%s is not a regular file", src)
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
destination, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to copy file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete source file after copy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|