Asks Garage for the newest release of a project, downloads it with resume, checks the sha256, installs it beside the old one and restarts into it. Splash on garage_ui, a native stub and a first time installer for windows, bundle swap on mac, the system installer on android. No-op on web. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KHotisA4hV2xmuZsHTksFX
155 lines
4.5 KiB
Dart
155 lines
4.5 KiB
Dart
import "dart:async";
|
|
import "dart:convert";
|
|
import "dart:io";
|
|
import "dart:typed_data";
|
|
|
|
import "package:crypto/crypto.dart";
|
|
import "package:garage_updates/core.dart";
|
|
|
|
/// A file server on loopback that can be told to misbehave.
|
|
class FakeServer {
|
|
FakeServer._(this._server, this.data);
|
|
|
|
final HttpServer _server;
|
|
Uint8List data;
|
|
|
|
/// "GET /blob bytes=100-" for every request, in order
|
|
final List<String> log = [];
|
|
|
|
/// send a plain 200 with the whole file whatever Range says
|
|
bool ignoreRange = false;
|
|
|
|
/// how many of the next downloads to cut dead, and after how many bytes
|
|
int cutsLeft = 0;
|
|
int cutAfter = 0;
|
|
|
|
/// the json `/v1/updates/<pid>` answers with, and the status it uses
|
|
Object? updateBody;
|
|
int updateStatus = 200;
|
|
|
|
/// a /blob request without this sig gets a 403, like an expired presign
|
|
String? wantSig;
|
|
int _sig = 0;
|
|
|
|
String get base => "http://127.0.0.1:${_server.port}";
|
|
|
|
static Future<FakeServer> start(List<int> data) async {
|
|
final s = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
final fake = FakeServer._(s, Uint8List.fromList(data));
|
|
s.listen(fake._handle);
|
|
return fake;
|
|
}
|
|
|
|
Future<void> close() => _server.close(force: true);
|
|
|
|
String get sha => sha256.convert(data).toString();
|
|
|
|
UpdateRelease release({int build = 13, String path = "/blob", String format = "zip", String? sha256}) {
|
|
return UpdateRelease(
|
|
label: "v$build",
|
|
build: build,
|
|
notes: "notes for $build",
|
|
asset: UpdateAsset(
|
|
url: "$base$path",
|
|
filename: "app.$format",
|
|
format: format,
|
|
size: data.length,
|
|
sha256: sha256 ?? sha,
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _handle(HttpRequest req) async {
|
|
final range = req.headers.value(HttpHeaders.rangeHeader);
|
|
log.add("${req.method} ${req.uri.path}${range == null ? "" : " $range"}");
|
|
|
|
if (req.uri.path.startsWith("/v1/updates/")) {
|
|
req.response.statusCode = updateStatus;
|
|
req.response.headers.contentType = ContentType.json;
|
|
final body = updateBody;
|
|
req.response.write(body is String ? body : jsonEncode(body));
|
|
await req.response.close();
|
|
return;
|
|
}
|
|
|
|
if (req.uri.path == "/hosted") {
|
|
// a fresh "presigned" url every time, the old ones stop working
|
|
wantSig = "s${++_sig}";
|
|
req.response.statusCode = HttpStatus.found;
|
|
req.response.headers.set(HttpHeaders.locationHeader, "/blob?sig=$wantSig");
|
|
await req.response.close();
|
|
return;
|
|
}
|
|
|
|
if (req.uri.path == "/gone") {
|
|
req.response.statusCode = HttpStatus.notFound;
|
|
await req.response.close();
|
|
return;
|
|
}
|
|
|
|
if (wantSig != null && req.uri.queryParameters["sig"] != wantSig) {
|
|
req.response.statusCode = HttpStatus.forbidden;
|
|
await req.response.close();
|
|
return;
|
|
}
|
|
|
|
var start = 0;
|
|
var status = "200 OK";
|
|
final extra = <String>[];
|
|
if (range != null && !ignoreRange) {
|
|
start = int.parse(RegExp(r"bytes=(\d+)-").firstMatch(range)!.group(1)!);
|
|
if (start >= data.length) {
|
|
req.response.statusCode = HttpStatus.requestedRangeNotSatisfiable;
|
|
await req.response.close();
|
|
return;
|
|
}
|
|
status = "206 Partial Content";
|
|
extra.add("Content-Range: bytes $start-${data.length - 1}/${data.length}");
|
|
}
|
|
|
|
final body = Uint8List.sublistView(data, start);
|
|
|
|
// written by hand on the raw socket so it can be dropped mid body
|
|
final socket = await req.response.detachSocket(writeHeaders: false);
|
|
socket.write("HTTP/1.1 $status\r\n"
|
|
"Content-Length: ${body.length}\r\n"
|
|
"${extra.map((h) => "$h\r\n").join()}"
|
|
"Connection: close\r\n\r\n");
|
|
|
|
if (cutsLeft > 0) {
|
|
cutsLeft--;
|
|
socket.add(Uint8List.sublistView(body, 0, cutAfter.clamp(0, body.length)));
|
|
await socket.flush();
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
|
|
socket.add(body);
|
|
await socket.flush();
|
|
await socket.close();
|
|
}
|
|
}
|
|
|
|
/// some bytes that arent all the same
|
|
List<int> noise(int length, {int seed = 7}) {
|
|
var x = seed;
|
|
return List<int>.generate(length, (_) {
|
|
x = (x * 1103515245 + 12345) & 0x7fffffff;
|
|
return x & 0xff;
|
|
});
|
|
}
|
|
|
|
/// runs [body] and hands back everything it printed
|
|
Future<List<String>> printsOf(Future<void> Function() body) async {
|
|
final lines = <String>[];
|
|
await runZonedPrints(body, lines);
|
|
return lines;
|
|
}
|
|
|
|
Future<void> runZonedPrints(Future<void> Function() body, List<String> into) {
|
|
return runZoned(
|
|
body,
|
|
zoneSpecification: ZoneSpecification(print: (self, parent, zone, line) => into.add(line)),
|
|
);
|
|
}
|