466 lines
13 KiB
Go
466 lines
13 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// 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
|
|
// v2 API fields
|
|
SHA1 string `json:"sha1,omitempty"`
|
|
SHA256 string `json:"sha256,omitempty"`
|
|
Date string `json:"date,omitempty"`
|
|
OSPatchLevel string `json:"os_patch_level,omitempty"`
|
|
OSSDKLevel int `json:"os_sdk_level,omitempty"`
|
|
OTAPropertyFiles string `json:"ota_property_files,omitempty"`
|
|
}
|
|
|
|
// Structs for v2 API responses
|
|
type V2BuildResponse struct {
|
|
Date string `json:"date"`
|
|
Datetime int64 `json:"datetime"`
|
|
Files []V2BuildFile `json:"files"`
|
|
Type string `json:"type"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
type V2BuildFile struct {
|
|
Date string `json:"date,omitempty"`
|
|
Datetime int64 `json:"datetime,omitempty"`
|
|
Filename string `json:"filename"`
|
|
Filepath string `json:"filepath"`
|
|
OSPatchLevel string `json:"os_patch_level,omitempty"`
|
|
OSSDKLevel int `json:"os_sdk_level,omitempty"`
|
|
OTAPropertyFiles string `json:"ota_property_files,omitempty"`
|
|
SHA1 string `json:"sha1"`
|
|
SHA256 string `json:"sha256"`
|
|
Size int64 `json:"size"`
|
|
Type string `json:"type,omitempty"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
// 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 must 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)
|
|
|
|
var validROMs []LineageOSROM
|
|
for _, rom := range romCache.ROMs {
|
|
// A ROM is valid for v2 if it has SHA1 and Date fields populated
|
|
if rom.SHA1 != "" && rom.Date != "" {
|
|
romCache.Cached[rom.Filename] = true
|
|
validROMs = append(validROMs, rom)
|
|
log.Printf("loaded cached file: %s", rom.Filename)
|
|
} else {
|
|
log.Printf("cache entry for %s is missing v2 metadata, will re-process", rom.Filename)
|
|
}
|
|
}
|
|
romCache.ROMs = validROMs
|
|
|
|
// 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 (legacy / catch-all)
|
|
http.HandleFunc("/", lineageOSROMListHandler)
|
|
|
|
// V2 ROM list and device info
|
|
http.HandleFunc("/api/v2/devices/", lineageOSROMListHandlerV2)
|
|
|
|
log.Print("Service listening on :8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|
|
|
|
// Reads the ROM files on the filesystem recursively and populates a slice of lineageOSROMs
|
|
func updateROMCache() {
|
|
|
|
log.Printf("updating ROM cache")
|
|
|
|
type fileInfoPath struct {
|
|
path string
|
|
info fs.DirEntry
|
|
splitName []string
|
|
}
|
|
|
|
var romFiles []fileInfoPath
|
|
|
|
err := filepath.WalkDir(romDirectory, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() {
|
|
return nil
|
|
}
|
|
isLineageROM, splitName := parseROMFileName(d)
|
|
if isLineageROM {
|
|
romFiles = append(romFiles, fileInfoPath{
|
|
path: path,
|
|
info: d,
|
|
splitName: splitName,
|
|
})
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
log.Printf("failed to walk ROM directory: %v", err)
|
|
return
|
|
}
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i := range romFiles {
|
|
v := romFiles[i]
|
|
|
|
// skip already cached files
|
|
romCache.RLock()
|
|
if _, ok := romCache.Cached[v.info.Name()]; ok {
|
|
romCache.RUnlock()
|
|
continue
|
|
}
|
|
romCache.RUnlock()
|
|
|
|
wg.Add(1)
|
|
go func(file fileInfoPath) {
|
|
defer wg.Done()
|
|
fInfo, err := file.info.Info()
|
|
if err != nil {
|
|
log.Printf("failed to get file info '%s': %v", file.info.Name(), err)
|
|
return
|
|
}
|
|
|
|
sha1Val, sha256Val, err := hashFileSha1AndSha256(file.path)
|
|
if err != nil {
|
|
log.Printf("ignore zip file '%s', failed to calculate hashes: %v", file.info.Name(), err)
|
|
return
|
|
}
|
|
|
|
// Read ZIP metadata
|
|
meta, err := readZipMetadata(file.path)
|
|
if err != nil {
|
|
log.Printf("failed to read metadata inside zip '%s': %v", file.info.Name(), err)
|
|
}
|
|
|
|
// Determine timestamp
|
|
timestamp := int(fInfo.ModTime().Unix())
|
|
if meta.Timestamp > 0 {
|
|
timestamp = meta.Timestamp
|
|
}
|
|
|
|
// Format build date as YYYY-MM-DD
|
|
buildDate := time.Unix(int64(timestamp), 0).UTC().Format("2006-01-02")
|
|
|
|
// Calculate URL relative path from public directory
|
|
relPath, err := filepath.Rel(romDirectory, file.path)
|
|
if err != nil {
|
|
log.Printf("failed to calculate relative path for '%s': %v", file.path, err)
|
|
relPath = file.info.Name()
|
|
}
|
|
relPathSlash := filepath.ToSlash(relPath)
|
|
|
|
lineageOSROM := LineageOSROM{
|
|
Datetime: timestamp,
|
|
Filename: file.info.Name(),
|
|
ID: sha256Val, // Keep ID as SHA256 for backward compatibility
|
|
Romtype: file.splitName[3],
|
|
Size: int(fInfo.Size()),
|
|
URL: fmt.Sprintf("%s/public/%s", baseURL, relPathSlash),
|
|
Version: file.splitName[1],
|
|
SHA1: sha1Val,
|
|
SHA256: sha256Val,
|
|
Date: buildDate,
|
|
OSPatchLevel: meta.PatchLevel,
|
|
OSSDKLevel: meta.SDKLevel,
|
|
OTAPropertyFiles: meta.OTAPropertyFiles,
|
|
}
|
|
|
|
romCache.Lock()
|
|
if _, ok := romCache.Cached[file.info.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[file.info.Name()] = true
|
|
romCache.Unlock()
|
|
|
|
}(v)
|
|
}
|
|
|
|
// 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 v2 builds
|
|
// Marshals romCache.ROMs to V2 JSON format to serve as the response body
|
|
func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
|
|
checkNewBuilds()
|
|
|
|
romCache.RLock()
|
|
defer romCache.RUnlock()
|
|
|
|
var builds []V2BuildResponse
|
|
|
|
for _, rom := range romCache.ROMs {
|
|
relPath := rom.Filename
|
|
if strings.Contains(rom.URL, "/public/") {
|
|
parts := strings.SplitN(rom.URL, "/public/", 2)
|
|
if len(parts) == 2 {
|
|
relPath = parts[1]
|
|
}
|
|
}
|
|
|
|
file := V2BuildFile{
|
|
Date: rom.Date,
|
|
Datetime: int64(rom.Datetime),
|
|
Filename: rom.Filename,
|
|
Filepath: "/public/" + relPath,
|
|
OSPatchLevel: rom.OSPatchLevel,
|
|
OSSDKLevel: rom.OSSDKLevel,
|
|
OTAPropertyFiles: rom.OTAPropertyFiles,
|
|
SHA1: rom.SHA1,
|
|
SHA256: rom.SHA256,
|
|
Size: int64(rom.Size),
|
|
Type: strings.ToLower(rom.Romtype),
|
|
URL: rom.URL,
|
|
}
|
|
|
|
build := V2BuildResponse{
|
|
Date: rom.Date,
|
|
Datetime: int64(rom.Datetime),
|
|
Files: []V2BuildFile{file},
|
|
Type: strings.ToLower(rom.Romtype),
|
|
Version: rom.Version,
|
|
}
|
|
|
|
builds = append(builds, build)
|
|
}
|
|
|
|
if builds == nil {
|
|
builds = []V2BuildResponse{}
|
|
}
|
|
|
|
b, err := json.Marshal(builds)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
log.Printf("failed to marshal v2 builds to json: %v", err)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(b)
|
|
}
|
|
|
|
func checkNewBuilds() {
|
|
go func() { // Also checks for new builds
|
|
newBuilds := moveBuildArtifacts()
|
|
if newBuilds {
|
|
updateROMCache()
|
|
}
|
|
}()
|
|
}
|
|
|
|
func lineageOSROMListHandlerV2(w http.ResponseWriter, r *http.Request) {
|
|
checkNewBuilds()
|
|
|
|
// Path: /api/v2/devices/{device}/builds
|
|
// or: /api/v2/devices/{device}
|
|
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
|
if len(parts) < 4 {
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
device := parts[3]
|
|
isBuildsRequest := len(parts) >= 5 && parts[4] == "builds"
|
|
|
|
if isBuildsRequest {
|
|
romCache.RLock()
|
|
defer romCache.RUnlock()
|
|
|
|
var builds []V2BuildResponse
|
|
|
|
for _, rom := range romCache.ROMs {
|
|
filenameParts := strings.Split(strings.TrimSuffix(rom.Filename, ".zip"), "-")
|
|
romDevice := ""
|
|
if len(filenameParts) > 0 {
|
|
romDevice = filenameParts[len(filenameParts)-1]
|
|
}
|
|
|
|
if romDevice != device {
|
|
continue
|
|
}
|
|
|
|
relPath := rom.Filename
|
|
if strings.Contains(rom.URL, "/public/") {
|
|
parts := strings.SplitN(rom.URL, "/public/", 2)
|
|
if len(parts) == 2 {
|
|
relPath = parts[1]
|
|
}
|
|
}
|
|
|
|
file := V2BuildFile{
|
|
Date: rom.Date,
|
|
Datetime: int64(rom.Datetime),
|
|
Filename: rom.Filename,
|
|
Filepath: "/public/" + relPath,
|
|
OSPatchLevel: rom.OSPatchLevel,
|
|
OSSDKLevel: rom.OSSDKLevel,
|
|
OTAPropertyFiles: rom.OTAPropertyFiles,
|
|
SHA1: rom.SHA1,
|
|
SHA256: rom.SHA256,
|
|
Size: int64(rom.Size),
|
|
Type: strings.ToLower(rom.Romtype),
|
|
URL: rom.URL,
|
|
}
|
|
|
|
build := V2BuildResponse{
|
|
Date: rom.Date,
|
|
Datetime: int64(rom.Datetime),
|
|
Files: []V2BuildFile{file},
|
|
Type: strings.ToLower(rom.Romtype),
|
|
Version: rom.Version,
|
|
}
|
|
|
|
builds = append(builds, build)
|
|
}
|
|
|
|
if builds == nil {
|
|
builds = []V2BuildResponse{}
|
|
}
|
|
|
|
b, err := json.Marshal(builds)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
log.Printf("failed to marshal v2 builds to json: %v", err)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(b)
|
|
} else {
|
|
romCache.RLock()
|
|
defer romCache.RUnlock()
|
|
|
|
versionsSet := make(map[string]bool)
|
|
for _, rom := range romCache.ROMs {
|
|
filenameParts := strings.Split(strings.TrimSuffix(rom.Filename, ".zip"), "-")
|
|
romDevice := ""
|
|
if len(filenameParts) > 0 {
|
|
romDevice = filenameParts[len(filenameParts)-1]
|
|
}
|
|
if romDevice == device {
|
|
versionsSet[rom.Version] = true
|
|
}
|
|
}
|
|
|
|
var versions []string
|
|
for v := range versionsSet {
|
|
versions = append(versions, v)
|
|
}
|
|
if versions == nil {
|
|
versions = []string{}
|
|
}
|
|
|
|
deviceInfo := map[string]interface{}{
|
|
"name": device,
|
|
"model": device,
|
|
"oem": "LineageOS",
|
|
"info_url": fmt.Sprintf("https://wiki.lineageos.org/devices/%s/", device),
|
|
"versions": versions,
|
|
"dependencies": []string{},
|
|
}
|
|
|
|
b, err := json.Marshal(deviceInfo)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
log.Printf("failed to marshal v2 device info to json: %v", err)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(b)
|
|
}
|
|
}
|