lineageos-ota-server/main.go

204 lines
5.6 KiB
Go
Raw Normal View History

2023-06-04 05:07:55 +00:00
package main
import (
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"os"
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 {
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
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-24 19:36:23 +00:00
cacheFile = "public/romcache.json"
2023-06-04 22:15:13 +00:00
)
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-24 19:36:23 +00:00
// intialize and load ROMCache from file - so we don't have to rehash all the big files again
2023-06-04 18:41:22 +00:00
romCache = ROMCache{}
2023-06-24 19:36:23 +00:00
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)
}
2023-06-24 19:36:23 +00:00
}
romCache.Cached = make(map[string]bool)
2023-06-24 19:36:23 +00:00
for _, rom := range romCache.ROMs {
romCache.Cached[rom.Filename] = true
2023-06-24 19:36:23 +00:00
log.Printf("loaded cached file: %s", rom.Filename)
}
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
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
}
wg := sync.WaitGroup{}
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
wg.Add(1)
2023-06-04 18:56:58 +00:00
go func(v fs.DirEntry) {
defer wg.Done()
2023-06-04 18:56:58 +00:00
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-24 19:36:23 +00:00
// save file to disk for next startup so we don't have to rehash all the files again
wg.Wait()
romCache.RLock()
2023-06-24 19:36:23 +00:00
romCacheJson, err := json.Marshal(romCache)
romCache.RUnlock()
2023-06-24 19:36:23 +00:00
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
}
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)
}