|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Serialization; |
| 3 | + |
| 4 | +var builder = WebApplication.CreateBuilder(args); |
| 5 | +builder.WebHost.UseUrls("http://0.0.0.0:5000"); |
| 6 | + |
| 7 | +// Allow all origins for local testing |
| 8 | +builder.Services.AddCors(options => |
| 9 | +{ |
| 10 | + options.AddDefaultPolicy(policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); |
| 11 | +}); |
| 12 | + |
| 13 | +var app = builder.Build(); |
| 14 | +app.UseCors(); |
| 15 | + |
| 16 | +var packagesDir = Path.Combine(app.Environment.ContentRootPath, "packages"); |
| 17 | +Directory.CreateDirectory(packagesDir); |
| 18 | + |
| 19 | +// GET /packages/{filename} - Serve APK files with range support |
| 20 | +app.MapGet("/packages/{filename}", (string filename) => |
| 21 | +{ |
| 22 | + var sanitized = Path.GetFileName(filename); |
| 23 | + var filePath = Path.Combine(packagesDir, sanitized); |
| 24 | + if (!File.Exists(filePath)) |
| 25 | + { |
| 26 | + return Results.NotFound(new { error = "Package not found.", filename = sanitized }); |
| 27 | + } |
| 28 | + |
| 29 | + return Results.File( |
| 30 | + filePath, |
| 31 | + contentType: "application/vnd.android.package-archive", |
| 32 | + fileDownloadName: sanitized, |
| 33 | + enableRangeProcessing: true); |
| 34 | +}); |
| 35 | + |
| 36 | +// POST /Upgrade/Verification - Version check API (matches GeneralUpdate server format) |
| 37 | +app.MapPost("/Upgrade/Verification", async (HttpContext context) => |
| 38 | +{ |
| 39 | + try |
| 40 | + { |
| 41 | + using var reader = new StreamReader(context.Request.Body); |
| 42 | + var body = await reader.ReadToEndAsync(); |
| 43 | + |
| 44 | + // Parse the request to extract current version |
| 45 | + using var doc = JsonDocument.Parse(body); |
| 46 | + var requestVersion = doc.RootElement.GetProperty("Version").GetString() ?? "0.0.0.0"; |
| 47 | + |
| 48 | + // Read versions.json |
| 49 | + var versionsPath = Path.Combine(packagesDir, "versions.json"); |
| 50 | + if (!File.Exists(versionsPath)) |
| 51 | + { |
| 52 | + return Results.Ok(new { code = 1, message = "No versions file.", body = new List<PackageEntry>() }); |
| 53 | + } |
| 54 | + |
| 55 | + var json = await File.ReadAllTextAsync(versionsPath); |
| 56 | + var packageOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; |
| 57 | + var packages = JsonSerializer.Deserialize<List<PackageEntry>>(json, packageOptions); |
| 58 | + |
| 59 | + if (packages is null || packages.Count == 0) |
| 60 | + { |
| 61 | + return Results.Ok(new { code = 1, message = "No packages configured.", body = new List<PackageEntry>() }); |
| 62 | + } |
| 63 | + |
| 64 | + // Find the latest version that is newer than the request |
| 65 | + var current = Version.TryParse(requestVersion, out var cv) ? cv : new Version(0, 0, 0, 0); |
| 66 | + var available = packages |
| 67 | + .Select(p => new { Package = p, ParsedVersion = Version.TryParse(p.Version, out var v) ? v : new Version(0, 0, 0, 0) }) |
| 68 | + .Where(x => x.ParsedVersion > current) |
| 69 | + .OrderByDescending(x => x.ParsedVersion) |
| 70 | + .ToList(); |
| 71 | + |
| 72 | + if (available.Count == 0) |
| 73 | + { |
| 74 | + return Results.Ok(new { code = 1, message = "No updates available.", body = new List<PackageEntry>() }); |
| 75 | + } |
| 76 | + |
| 77 | + var latest = available[0].Package; |
| 78 | + return Results.Ok(new { code = 0, message = "Success", body = new[] { latest } }); |
| 79 | + } |
| 80 | + catch (Exception ex) |
| 81 | + { |
| 82 | + return Results.Ok(new { code = -1, message = ex.Message, body = new List<PackageEntry>() }); |
| 83 | + } |
| 84 | +}); |
| 85 | + |
| 86 | +// POST /Upgrade/Report - Report update result |
| 87 | +app.MapPost("/Upgrade/Report", async (HttpContext context) => |
| 88 | +{ |
| 89 | + using var reader = new StreamReader(context.Request.Body); |
| 90 | + var body = await reader.ReadToEndAsync(); |
| 91 | + Console.WriteLine($"[Report] {body}"); |
| 92 | + return Results.Ok(new { code = 0, message = "Report received." }); |
| 93 | +}); |
| 94 | + |
| 95 | +// Health check |
| 96 | +app.MapGet("/", () => Results.Ok(new { status = "running", server = "MauiUpdate Server" })); |
| 97 | + |
| 98 | +Console.WriteLine("MauiUpdate Server running on http://0.0.0.0:5000"); |
| 99 | +app.Run(); |
| 100 | + |
| 101 | +/// <summary> |
| 102 | +/// Package entry matching the versions.json schema. |
| 103 | +/// JsonPropertyName attributes ensure correct serialization for the client. |
| 104 | +/// </summary> |
| 105 | +internal sealed record PackageEntry |
| 106 | +{ |
| 107 | + [JsonPropertyName("PacketName")] public string PacketName { get; init; } = string.Empty; |
| 108 | + [JsonPropertyName("Hash")] public string Hash { get; init; } = string.Empty; |
| 109 | + [JsonPropertyName("Version")] public string Version { get; init; } = string.Empty; |
| 110 | + [JsonPropertyName("PubTime")] public string PubTime { get; init; } = string.Empty; |
| 111 | + [JsonPropertyName("AppType")] public int AppType { get; init; } |
| 112 | + [JsonPropertyName("Platform")] public int Platform { get; init; } |
| 113 | + [JsonPropertyName("ProductId")] public string ProductId { get; init; } = string.Empty; |
| 114 | + [JsonPropertyName("IsForcibly")] public bool IsForcibly { get; init; } |
| 115 | + [JsonPropertyName("Format")] public string Format { get; init; } = ".apk"; |
| 116 | + [JsonPropertyName("Size")] public long Size { get; init; } |
| 117 | +} |
0 commit comments