123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import 'dart:io';
- import 'package:dio/dio.dart';
- import 'package:archive/archive.dart';
- import 'package:naiyouwl/app/const/const.dart';
- import 'package:path/path.dart' as path;
- final dio = Dio();
- final assetsPath = path.normalize(path.join(Platform.script.toFilePath(), '../../assets'));
- final binDir = Directory(path.join(assetsPath, 'bin'));
- final depDir = Directory(path.join(assetsPath, 'dep'));
- Future downloadLatestClashCore() async {
- const version = "v1.16.0";
- final String clashCoreName = 'clash.meta-${ClashName.platform}-${ClashName.arch}-cgo-$version';
- final info = await dio.get('https://api.github.com/repos/MetaCubeX/Clash.Meta/releases/tags/$version');
- final Map<String, dynamic> latest = (info.data['assets'] as List<dynamic>)
- .firstWhere((it) => (it['name'] as String).contains(clashCoreName));
- final String downloadUrl = latest['browser_download_url'];
-
- final String name = latest['name'];
- final tempFile = File(path.join(binDir.path, '$name.temp'));
- print('Downloading $name');
- await dio.download(downloadUrl, tempFile.path);
- print('Download Success');
- print('Unarchiving $name');
- final tempBetys = await tempFile.readAsBytes();
- if (name.contains('.gz')) {
- final bytes = GZipDecoder().decodeBytes(tempBetys);
- final String filePath = path.join(binDir.path, ClashName.name);
- await File(filePath).writeAsBytes(bytes);
- await Process.run('chmod', ['+x', filePath]);
- } else {
- final file = ZipDecoder().decodeBytes(tempBetys).first;
- await File(path.join(binDir.path, ClashName.name)).writeAsBytes(file.content);
- }
- await tempFile.delete();
- print('Unarchiv Success');
- }
- Future<void> downloadAndUnarchiveService() async {
-
- final serviceName = 'ccore-service-${ClashName.platform}-${ClashName.arch}';
-
-
-
-
- final response = await dio.get('https://api.github.com/repos/alroyso/core-service/releases/latest');
- final Map<String, dynamic> latest = (response.data['assets'] as List<dynamic>)
- .firstWhere((it) => (it['name'] as String).contains(serviceName));
- final downloadUrl = latest['browser_download_url'];
- print('Downloading $downloadUrl');
- final tempFile = File(path.join(binDir.path, '${latest['name']}.temp'));
- try
- {
- print(dio.options.headers);
- await dio.download(downloadUrl, tempFile.path);
- } catch (e){
- print(e);
- return;
- }
- print('Download Success');
- print('Unarchiving ${latest['name']}');
- final tempBytes = await tempFile.readAsBytes();
- if (latest['name'].contains('.gz')) {
- final bytes = GZipDecoder().decodeBytes(tempBytes);
- await File(path.join(binDir.path, serviceName)).writeAsBytes(bytes);
- final filePath = path.join(binDir.path, serviceName);
- await Process.run('chmod', ['+x', filePath]);
- } else {
- final file = ZipDecoder().decodeBytes(tempBytes).first;
- await File(path.join(binDir.path, file.name)).writeAsBytes(file.content);
- }
- await tempFile.delete();
- print('Unarchive Success');
- }
- Future downloadLatestClashService() async {
- final String serviceName = 'ccore-service-${ClashName.platform}-${ClashName.arch}';
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- final String filePath = path.join(binDir.path, serviceName);
- await Process.run('chmod', ['+x', filePath]);
-
- print('Unarchiv Success');
- }
- Future downloadCountryMmdb() async {
- print('Downloading Country.mmdb');
- final String geoipFilePath = path.join(depDir.path, 'Country.mmdb');
- await dio.download('https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb', geoipFilePath);
- print('Download Success');
- final String geositedat = path.join(depDir.path, 'geosite.dat');
- await dio.download('https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat', geositedat);
- print('Download Success');
- final String geoip = path.join(depDir.path, 'geoip.dat');
- await dio.download('https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat', geoip);
- print('Download Success');
- }
- Future downloadWintun() async {
- print('Downloading Wintun');
- final File wintunFile = File(path.join(depDir.path, 'wintun.zip.temp'));
- await dio.download('https://www.wintun.net/builds/wintun-0.14.1.zip', wintunFile.path);
- print('Download Success');
- print('Unarchiving wintun.zip');
- final files = ZipDecoder().decodeBytes(await wintunFile.readAsBytes());
- final file = files.firstWhere((it) => it.name == 'wintun/bin/${ClashName.arch}/wintun.dll');
- await File(path.join(depDir.path, 'wintun.dll')).writeAsBytes(file.content);
- await wintunFile.delete();
- print('Unarchiv Success');
- }
- void main() async {
- ClashName.init();
- if (!(await binDir.exists())) await binDir.create();
- if (!(await depDir.exists())) await depDir.create();
- await downloadLatestClashCore();
- await downloadAndUnarchiveService();
- await downloadCountryMmdb();
- if (Platform.isWindows) await downloadWintun();
- }
|