Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JoCat committed Oct 15, 2023
1 parent a500643 commit 5e64ef6
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 49 deletions.
27 changes: 15 additions & 12 deletions lib/api_response.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dart:convert';
import 'package:json_annotation/json_annotation.dart';
part 'api_response.g.dart';

ApiResponse apiResponseFromJson(String str) =>
ApiResponse parseApiResponseFromJson(String str) =>
ApiResponse.fromJson(json.decode(str));

@JsonSerializable(fieldRename: FieldRename.snake)
class ApiResponse {
final int buildNumber;
final String version;
final String downloadUrl;
final Uri downloadUrl;
final Checksums checksums;

ApiResponse({
Expand All @@ -16,22 +19,22 @@ class ApiResponse {
required this.checksums,
});

factory ApiResponse.fromJson(Map<String, dynamic> json) => ApiResponse(
buildNumber: json["build_number"],
version: json["version"],
downloadUrl: json["download_url"],
checksums: Checksums.fromJson(json["checksums"]),
);
factory ApiResponse.fromJson(Map<String, dynamic> json) =>
_$ApiResponseFromJson(json);

Map<String, dynamic> toJson() => _$ApiResponseToJson(this);
}

@JsonSerializable()
class Checksums {
final String sha256;

Checksums({
required this.sha256,
});

final String sha256;
factory Checksums.fromJson(Map<String, dynamic> json) =>
_$ChecksumsFromJson(json);

factory Checksums.fromJson(Map<String, dynamic> json) => Checksums(
sha256: json["sha256"],
);
Map<String, dynamic> toJson() => _$ChecksumsToJson(this);
}
30 changes: 30 additions & 0 deletions lib/api_response.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 24 additions & 18 deletions lib/injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,45 @@ import 'package:serverwrapper/api_response.dart';

class Injector {
static final apiUrl =
Uri.parse('https://authlib-injector.yushi.moe/artifact/latest.json');
Uri.https('authlib-injector.yushi.moe', '/artifact/latest.json');

static Future<void> init(String filename) async {
var file = File(filename);
if (!await file.exists()) {
await download(file);
} else {
print("Injector loaded successfully");
await _download(file);
}
print("Injector loaded successfully");
}

static Future<void> download(File file) async {
var response = await get(apiUrl);
static Future<void> _download(File file) async {
ApiResponse parsedResponse;

if (response.statusCode == 200) {
parsedResponse = apiResponseFromJson(response.body);
} else {
try {
var response = await get(apiUrl);
if (response.statusCode != 200) {
throw Exception('Bad status code ${response.statusCode}');
}
parsedResponse = parseApiResponseFromJson(response.body);
} on Exception catch (e) {
print('Failed to check Authlib Injector API');
print(e);
exit(1);
}

var fileResponse = await get(Uri.parse(parsedResponse.downloadUrl));
await file.writeAsBytes(fileResponse.bodyBytes);
try {
var fileResponse = await get(parsedResponse.downloadUrl);
await file.writeAsBytes(fileResponse.bodyBytes);
} catch (e) {
print('Failed to download Authlib Injector');
exit(2);
}

var bytes = await file.readAsBytes();
var digest = sha256.convert(bytes);
var digest = sha256.convert(bytes).toString();

if (digest.toString() == parsedResponse.checksums.sha256) {
print('Injector downloaded successfully');
} else {
print("Failed to verify checksum");
exit(2);
if (digest != parsedResponse.checksums.sha256) {
print("Authlib checksum mismatch");
exit(3);
}
print('Injector downloaded successfully');
}
}
Loading

0 comments on commit 5e64ef6

Please sign in to comment.