lineageos-ota-server/main.go

211 lines
6.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"sync"
)
// Information for a LineageOS ROM available for download
type LineageOSROM struct {
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
Romtype string `json:"romtype"` // i.e. NIGHTLY or UNOFFICIAL
Size int `json:"size"` // size of .zip file in bytes
URL string `json:"url"` // Accessible URL where client could download the .zip file
Version string `json:"version"` // LineageOS version - i.e. 20.0
}
// The HTTP response JSON should be a JSON array of lineageOSROMS available for download
type HTTPResponseJSON struct {
Response []LineageOSROM `json:"response"`
}
// Caches data about available ROMs in memory so we don't need to reference the filesystem for each request
type ROMCache struct {
ROMs []LineageOSROM `json:"roms"`
Cached map[string]bool `json:"-"` // to quickly lookup if a file is already cached
sync.RWMutex `json:"-"` // We have multiple goroutines that may be accessing this data simultaneously, so we much lock / unlock it to prevent race conditions
}
const (
romDirectory = "public" // directory where ROMs are available for download
buildOutDirectory = "out" // directory from build system containing artifacts which we can move to romDirectory
cacheFile = "public/romcache.json" // persistence between server restarts so we don't have to rehash all the ROM files each time the program starts
)
var ( // evil global variables
romCache ROMCache
baseURL string
)
func init() {
// intialize and load ROMCache from file - so we don't have to rehash all the big files again
romCache = ROMCache{}
baseURL = os.Getenv("baseurl")
if len(baseURL) < 1 {
log.Fatalf("required environment variable 'baseurl' is not set.")
}
romCacheJson, err := os.ReadFile(cacheFile)
if err != nil {
if err != os.ErrNotExist { // don't care if it doesn't exist, just skip it
log.Printf("failed to read romCache file : %v", err)
}
} else { // if opening the file's successful, then load the contents
err = json.Unmarshal(romCacheJson, &romCache)
if err != nil {
log.Printf("failed to unmarshal romCacheJson to romCache struct: %v", err)
}
}
romCache.Cached = make(map[string]bool)
for _, rom := range romCache.ROMs {
romCache.Cached[rom.Filename] = true
log.Printf("loaded cached file: %s", rom.Filename)
}
// Check if any new build artifacts and load any new files into the romCache
moveBuildArtifacts()
go updateROMCache()
}
// HTTP Server
func main() {
//Public static files
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
// ROM list
http.HandleFunc("/", lineageOSROMListHandler)
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
func updateROMCache() {
log.Printf("updating ROM cache")
f, err := os.Open(romDirectory)
if err != nil {
log.Printf("failed to open ROM directory: %v", err)
return
}
defer f.Close()
files, err := f.ReadDir(0)
if err != nil {
log.Printf("failed to read files in directory: %v", err)
return
}
wg := sync.WaitGroup{}
for i, v := range files {
isLineageROM, splitName := parseROMFileName(v)
if !isLineageROM {
continue
}
// skip already cached files
romCache.RLock()
if _, ok := romCache.Cached[v.Name()]; ok {
romCache.RUnlock()
continue
}
romCache.RUnlock()
wg.Add(1)
go func(v fs.DirEntry) {
defer wg.Done()
fInfo, err := v.Info()
if err != nil {
log.Printf("failed to get file info '%s': %v", v.Name(), err)
return
}
fileHash, err := hashFile(fmt.Sprintf("%s/%s", romDirectory, v.Name()))
if err != nil {
log.Printf("ingore zip file '%s', failed to get sha256 hash: %v", v.Name(), err)
return
}
lineageOSROM := LineageOSROM{
Datetime: int(fInfo.ModTime().Unix()),
Filename: v.Name(),
ID: fileHash,
Romtype: splitName[3], // UNOFFICIAL
Size: int(fInfo.Size()),
URL: fmt.Sprintf("%s/public/%s", baseURL, v.Name()),
Version: splitName[1],
}
romCache.Lock()
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
}
romCache.ROMs = append(romCache.ROMs, lineageOSROM)
romCache.Cached[v.Name()] = true
romCache.Unlock()
}(files[i])
}
// save file to disk for next startup so we don't have to rehash all the files again
wg.Wait()
romCache.RLock()
romCacheJson, err := json.Marshal(romCache)
romCache.RUnlock()
if err != nil {
log.Printf("failed to marshal romCache to json: %v", err)
return
}
err = os.WriteFile(cacheFile, romCacheJson, 0644)
if err != nil {
log.Printf("failed to write '%s' file: %v", cacheFile, err)
log.Printf("attempting to remove '%s' to ensure integrity during next program startup...", cacheFile)
err = os.Remove(cacheFile)
if err != nil {
log.Printf("failed to remove file '%s': %v", cacheFile, err)
}
return
}
}
// http - GET /
// The LineageOS updater app needs a JSON array of type LineageOSROM
// Marshal's romCache.ROMs to JSON to serve as the response body
func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
go func() { // Also checks for new builds - TBD need a better method as the first request will return no new updates. inotify?
newBuilds := moveBuildArtifacts()
if newBuilds {
updateROMCache()
}
}()
romCache.RLock()
httpResponseJSON := &HTTPResponseJSON{Response: romCache.ROMs}
romCache.RUnlock()
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)
}