init.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // ignore_for_file: avoid_print
  2. import 'dart:io';
  3. import 'package:dio/dio.dart';
  4. import 'package:archive/archive.dart';
  5. import 'package:naiyouwl/app/const/const.dart';
  6. import 'package:path/path.dart' as path;
  7. final dio = Dio();
  8. // https://github.com/dart-lang/sdk/issues/31610
  9. final assetsPath = path.normalize(path.join(Platform.script.toFilePath(), '../../assets'));
  10. final binDir = Directory(path.join(assetsPath, 'bin'));
  11. final depDir = Directory(path.join(assetsPath, 'dep'));
  12. Future downloadLatestClashCore() async {
  13. final String clashCoreName = 'clash-${ClashName.platform}-${ClashName.arch}';
  14. final info = await dio.get('https://api.github.com/repos/MetaCubeX/Clash.Meta/releases');
  15. final Map<String, dynamic> latest = (info.data['assets'] as List<dynamic>).firstWhere((it) => (it['name'] as String).contains(clashCoreName));
  16. final String name = latest['name'];
  17. final tempFile = File(path.join(binDir.path, '$name.temp'));
  18. print('Downloading $name');
  19. await dio.download(latest['browser_download_url'], tempFile.path);
  20. print('Download Success');
  21. print('Unarchiving $name');
  22. final tempBetys = await tempFile.readAsBytes();
  23. if (name.contains('.gz')) {
  24. final bytes = GZipDecoder().decodeBytes(tempBetys);
  25. final String filePath = path.join(binDir.path, clashCoreName);
  26. await File(filePath).writeAsBytes(bytes);
  27. await Process.run('chmod', ['+x', filePath]);
  28. } else {
  29. final file = ZipDecoder().decodeBytes(tempBetys).first;
  30. await File(path.join(binDir.path, file.name)).writeAsBytes(file.content);
  31. }
  32. await tempFile.delete();
  33. print('Unarchiv Success');
  34. }
  35. Future downloadLatestClashService() async {
  36. final String serviceName = 'clash-for-flutter-service-${ClashName.platform}-${ClashName.arch}';
  37. final info = await dio.get('https://api.github.com/repos/csj8520/clash-for-flutter-service/releases/latest');
  38. final Map<String, dynamic> latest = (info.data['assets'] as List<dynamic>).firstWhere((it) => (it['name'] as String).contains(serviceName));
  39. final String name = latest['name'];
  40. final tempFile = File(path.join(binDir.path, '$name.temp'));
  41. print('Downloading $name');
  42. await dio.download(latest['browser_download_url'], tempFile.path);
  43. print('Download Success');
  44. print('Unarchiving $name');
  45. final tempBetys = await tempFile.readAsBytes();
  46. if (name.contains('.gz')) {
  47. final bytes = GZipDecoder().decodeBytes(tempBetys);
  48. final String filePath = path.join(binDir.path, serviceName);
  49. await File(filePath).writeAsBytes(bytes);
  50. await Process.run('chmod', ['+x', filePath]);
  51. } else {
  52. final file = ZipDecoder().decodeBytes(tempBetys).first;
  53. await File(path.join(binDir.path, file.name)).writeAsBytes(file.content);
  54. }
  55. await tempFile.delete();
  56. print('Unarchiv Success');
  57. }
  58. Future downloadCountryMmdb() async {
  59. print('Downloading Country.mmdb');
  60. final String geoipFilePath = path.join(depDir.path, 'Country.mmdb');
  61. await dio.download('https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb', geoipFilePath);
  62. print('Download Success');
  63. }
  64. Future downloadWintun() async {
  65. print('Downloading Wintun');
  66. final File wintunFile = File(path.join(depDir.path, 'wintun.zip.temp'));
  67. await dio.download('https://www.wintun.net/builds/wintun-0.14.1.zip', wintunFile.path);
  68. print('Download Success');
  69. print('Unarchiving wintun.zip');
  70. final files = ZipDecoder().decodeBytes(await wintunFile.readAsBytes());
  71. final file = files.firstWhere((it) => it.name == 'wintun/bin/${ClashName.arch}/wintun.dll');
  72. await File(path.join(depDir.path, 'wintun.dll')).writeAsBytes(file.content);
  73. await wintunFile.delete();
  74. print('Unarchiv Success');
  75. }
  76. void main() async {
  77. if (!(await binDir.exists())) await binDir.create();
  78. if (!(await depDir.exists())) await depDir.create();
  79. // await downloadLatestClashCore();
  80. // await downloadLatestClashService();
  81. await downloadCountryMmdb();
  82. if (Platform.isWindows) await downloadWintun();
  83. }