diff --git a/yoyo-getfastmirror/.github/dependabot.yaml b/yoyo-getfastmirror/.github/dependabot.yaml new file mode 100644 index 0000000..63b035c --- /dev/null +++ b/yoyo-getfastmirror/.github/dependabot.yaml @@ -0,0 +1,11 @@ +version: 2 +enable-beta-ecosystems: true +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + - package-ecosystem: "pub" + directory: "/" + schedule: + interval: "daily" diff --git a/yoyo-getfastmirror/.github/workflows/yoyo_get_fast_mirror.yaml b/yoyo-getfastmirror/.github/workflows/yoyo_get_fast_mirror.yaml new file mode 100644 index 0000000..d6553fc --- /dev/null +++ b/yoyo-getfastmirror/.github/workflows/yoyo_get_fast_mirror.yaml @@ -0,0 +1,47 @@ +name: yoyo_get_fast_mirror + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + pull_request: + paths: + - ".github/workflows/yoyo_get_fast_mirror.yaml" + - "lib/**" + - "test/**" + - "pubspec.yaml" + push: + branches: + - main + paths: + - ".github/workflows/yoyo_get_fast_mirror.yaml" + - "lib/**" + - "test/**" + - "pubspec.yaml" + +jobs: + semantic-pull-request: + uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/semantic_pull_request.yml@v1 + + build: + uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1 + + + verify-version: + runs-on: ubuntu-latest + steps: + - name: 📚 Git Checkout + uses: actions/checkout@v2 + + - name: 🎯 Setup Dart + uses: dart-lang/setup-dart@v1 + with: + sdk: "stable" + + - name: 📦 Install Dependencies + run: | + dart pub get + + - name: 🔎 Verify version + run: dart run test --run-skipped -t version-verify diff --git a/yoyo-getfastmirror/.gitignore b/yoyo-getfastmirror/.gitignore new file mode 100644 index 0000000..5bf3533 --- /dev/null +++ b/yoyo-getfastmirror/.gitignore @@ -0,0 +1,16 @@ +# See https://www.dartlang.org/guides/libraries/private-files + +# Files and directories created by pub +.dart_tool/ +.packages +build/ +pubspec.lock + +# Files generated during tests +.test_coverage.dart +coverage/ +.test_runner.dart +release/yoyo* + +# Android studio and IntelliJ +.idea \ No newline at end of file diff --git a/yoyo-getfastmirror/README.md b/yoyo-getfastmirror/README.md new file mode 100644 index 0000000..5468644 --- /dev/null +++ b/yoyo-getfastmirror/README.md @@ -0,0 +1,29 @@ +## yoyo-getfastmirror + +[![License: MIT][license_badge]][license_link] + +--- + +## Usage 🚀 + +```sh +# getfastmirror command +$ yoyo-getfastmirror getfastmirror + +# Sample command optioon +$ yoyo-getfastmirror getfastmirror --only-test // yoyo-getfastmirror getfastmirror -o + +# Show CLI version +$ yoyo-getfastmirror --version + +# Show usage help +$ yoyo-getfastmirror --help +``` + +## Build + +```sh +$ ./release.sh +``` + +--- diff --git a/yoyo-getfastmirror/analysis_options.yaml b/yoyo-getfastmirror/analysis_options.yaml new file mode 100644 index 0000000..ff9c544 --- /dev/null +++ b/yoyo-getfastmirror/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:very_good_analysis/analysis_options.3.1.0.yaml +linter: + rules: + public_member_api_docs: false diff --git a/yoyo-getfastmirror/bin/yoyo_get_fast_mirror.dart b/yoyo-getfastmirror/bin/yoyo_get_fast_mirror.dart new file mode 100644 index 0000000..b134d35 --- /dev/null +++ b/yoyo-getfastmirror/bin/yoyo_get_fast_mirror.dart @@ -0,0 +1,18 @@ +import 'dart:io'; + +import 'package:yoyo_get_fast_mirror/src/command_runner.dart'; + +Future main(List args) async { + await _flushThenExit(await YoyoGetFastMirrorCommandRunner().run(args)); +} + +/// Flushes the stdout and stderr streams, then exits the program with the given +/// status code. +/// +/// This returns a Future that will never complete, since the program will have +/// exited already. This is useful to prevent Future chains from proceeding +/// after you've decided to exit. +Future _flushThenExit(int status) { + return Future.wait([stdout.close(), stderr.close()]) + .then((_) => exit(status)); +} diff --git a/yoyo-getfastmirror/config.config b/yoyo-getfastmirror/config.config new file mode 100644 index 0000000..0007cd1 --- /dev/null +++ b/yoyo-getfastmirror/config.config @@ -0,0 +1,165 @@ +################################################################### +# CONFIGURATION OPTIONS +################################################################### +# Every item has a default value besides MIRRORS (which is unset). + +# Use aptitude, apt-get, or apt? +# Note that apt-get is used as a fallback for outputting the +# package URI list for e.g. aptitude, which can't do this +# Optionally add the FULLPATH to apt-get or apt-rpm or aptitude +# e.g. /usr/bin/aptitude +# +# Default: apt-get +# +_APTMGR=apt + +if grep -Eqi "linuxmint" /etc/os-release;then +_APTMGR=apt-get +fi + +if [ -x "$(command -v oyo)" ]; then +_APTMGR=apt-get +fi + +#### +# +# UOS sources auth config +# +# +if grep -Eqi "UnionTech" /etc/issue || grep -Eq "UnionTech" /etc/*-release;then +AUTH_UOS_USER="uos-https://license.chinauos.com-apt" +AUTH_UOS_PASSWD="`cat /etc/apt/auth.conf.d/uos.conf | grep home-packages.chinauos.com`" +AUTH_UOS_PASSWD=`echo ${AUTH_UOS_PASSWD#*password }` + +fi +##### UOS自动读取账号密码以实现使用aptss来加速下载 + +# Enable DOWNLOADBEFORE to suppress apt-fast confirmation dialog and download +# packages directly. +# +# Default: dialog enabled +# +DOWNLOADBEFORE=true + + +# Choose mirror list to speed up downloads from same archive. To select some +# mirrors take a look at your distribution's archive mirror lists. +# Debian: http://www.debian.org/mirror/list +# Ubuntu: https://launchpad.net/ubuntu/+archivemirrors +# +# It is required to add mirrors in the sources.list to this array as well, so +# apt-fast can destinguish between different distributions. +# +# Examples: +# +# Different distributions (as in operating systems): +# +# sources.list: +# deb http://deb.debian.org/debian/ unstable main non-free contrib +# deb http://de.archive.ubuntu.com/ubuntu/ bionic main universe +# +# apt-fast.conf: +# MIRRORS=( 'http://deb.debian.org/debian','http://ftp.debian.org/debian,http://ftp2.de.debian.org/debian,http://ftp.de.debian.org/debian,ftp://ftp.uni-kl.de/debian' +# 'http://archive.ubuntu.com/ubuntu,http://de.archive.ubuntu.com/ubuntu,http://ftp.halifax.rwth-aachen.de/ubuntu,http://ftp.uni-kl.de/pub/linux/ubuntu,http://mirror.informatik.uni-mannheim.de/pub/linux/distributions/ubuntu/' ) +# +# +# Single distribution: +# +# sources.list: +# deb http://fr.archive.ubuntu.com/ubuntu/ bionic main +# deb http://fr.archive.ubuntu.com/ubuntu/ artful main +# +# apt-fast.conf: +# MIRRORS=( 'http://fr.archive.ubuntu.com/ubuntu,http://bouyguestelecom.ubuntu.lafibre.info/ubuntu,http://mirror.ovh.net/ubuntu,http://ubuntu-archive.mirrors.proxad.net/ubuntu' ) +# +# Default: disabled +# +MIRRORS=( 'https://code.gitlink.org.cn/yoyo-os/packages/raw/branch/master, https://packages.yzzi.icu/, http://mirror2.inuyasha.love/yoyo, https://yoyo-os.github.io/packages' ) + + +# Maximum number of connections +# You can use this value in _DOWNLOADER command. Escape with ${}: ${_MAXNUM} +# +# Default: 5 +# +_MAXNUM=16 + + +# Maximum number of connections per server +# Default: 10 +# +_MAXCONPERSRV=1 + + + + + +# Split size i.e. size of each piece +# Possible Values: 1M-1024M +# +_MINSPLITSZ=1M + + +# Piece selection algorithm to use +# Available values are: default, inorder, geom +# default: selects piece so that it reduces the number of establishing connection, reasonable for most cases +# inorder: selects pieces in sequential order starting from first piece +# geom: selects piece which has minimum index like inorder, but it exponentially increasingly keeps space from previously selected pieces +# +_PIECEALGO=default + + + +# Downloadmanager listfile +# You can use this value in _DOWNLOADER command. Escape with ${}: ${DLLIST} +# +# Default: /tmp/apt-fast.list +# +DLLIST='/tmp/apt-fast.list' + + +# Download command to use. Temporary download list is designed for aria2. But +# you can choose another download command or download manager. It has to +# support following input file syntax (\t is tab character): +# +# # Comment +# MIRROR1\tMIRROR2\tMIRROR3... +# out=FILENAME1 +# MIRROR1\tMIRROR2\tMIRROR3... +# out=FILENAME2 +# ... +# +# Examples: +# aria2c with a proxy (set username, proxy, ip and password!) +# _DOWNLOADER='aria2c --no-conf -c -j ${_MAXNUM} -x ${_MAXCONPERSRV} -s ${_SPLITCON} --min-split-size=${_MINSPLITSZ} --stream-piece-selector=${_PIECEALGO} --http-proxy=http://username:password@proxy_ip:proxy_port -i ${DLLIST}' +# +# Default: _DOWNLOADER='aria2c --no-conf -c -j ${_MAXNUM} -x ${_MAXCONPERSRV} -s ${_SPLITCON} --min-split-size=${_MINSPLITSZ} --stream-piece-selector=${_PIECEALGO} -i ${DLLIST} --connect-timeout=600 --timeout=600 -m0' +# +_DOWNLOADER='aria2c --no-conf -c -j ${_MAXNUM} -x ${_MAXCONPERSRV} --min-split-size=${_MINSPLITSZ} --stream-piece-selector=${_PIECEALGO} -i ${DLLIST} --connect-timeout=600 --timeout=600 -m0 --http-user ${AUTH_UOS_USER} --http-passwd ${AUTH_UOS_PASSWD}' + + +# Download temp folder for Downloadmanager +# example /tmp/apt-fast. Standard is /var/cache/apt-fast +# +# Default: /var/cache/apt/apt-fast +# +DLDIR='/var/cache/apt/apt-fast' + + +# APT archives cache directory +# +# Default /var/cache/apt/archives +# (APT configuration items Dir::Cache and Dir::Cache::archives) +# +APTCACHE='/var/cache/apt/archives' + + +# apt-fast colors +# Colors are disabled when not using a terminal. +# +# Default colors are: +# cGreen='\e[0;32m' +# cRed='\e[0;31m' +# cBlue='\e[0;34m' +# endColor='\e[0m' + diff --git a/yoyo-getfastmirror/coverage_badge.svg b/yoyo-getfastmirror/coverage_badge.svg new file mode 100644 index 0000000..88bfadf --- /dev/null +++ b/yoyo-getfastmirror/coverage_badge.svg @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + coverage + coverage + 100% + 100% + + \ No newline at end of file diff --git a/yoyo-getfastmirror/dart_test.yaml b/yoyo-getfastmirror/dart_test.yaml new file mode 100644 index 0000000..2f46c7e --- /dev/null +++ b/yoyo-getfastmirror/dart_test.yaml @@ -0,0 +1,3 @@ +tags: + version-verify: + skip: "Should only be run during pull request. Verifies if version file is updated." \ No newline at end of file diff --git a/yoyo-getfastmirror/lib/src/command_runner.dart b/yoyo-getfastmirror/lib/src/command_runner.dart new file mode 100644 index 0000000..94a9401 --- /dev/null +++ b/yoyo-getfastmirror/lib/src/command_runner.dart @@ -0,0 +1,98 @@ +import 'package:args/args.dart'; +import 'package:args/command_runner.dart'; +import 'package:mason_logger/mason_logger.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/commands.dart'; +import 'package:yoyo_get_fast_mirror/src/version.dart'; + +const executableName = 'yoyo-getfastmirror'; +const packageName = 'yoyo_get_fast_mirror'; +const description = 'A Very Good Project created by Yoyo.'; + +class YoyoGetFastMirrorCommandRunner extends CommandRunner { + /// {@macro yoyo_get_fast_mirror_command_runner} + YoyoGetFastMirrorCommandRunner({ + Logger? logger, + }) : _logger = logger ?? Logger(), + super(executableName, description) { + // Add root options and flags + argParser + ..addFlag( + 'version', + abbr: 'v', + negatable: false, + help: 'Print the current version.', + ) + ..addFlag( + 'verbose', + help: 'Noisy logging, including all shell commands executed.', + ); + + // Add sub commands + addCommand(GetfastmirroCommand(logger: _logger)); + } + + @override + void printUsage() => _logger.info(usage); + + final Logger _logger; + + @override + Future run(Iterable args) async { + try { + final topLevelResults = parse(args); + if (topLevelResults['verbose'] == true) { + _logger.level = Level.verbose; + } + return await runCommand(topLevelResults) ?? ExitCode.success.code; + } on FormatException catch (e, stackTrace) { + // On format errors, show the commands error message, root usage and + // exit with an error code + _logger + ..err(e.message) + ..err('$stackTrace') + ..info('') + ..info(usage); + return ExitCode.usage.code; + } on UsageException catch (e) { + // On usage errors, show the commands usage message and + // exit with an error code + _logger + ..err(e.message) + ..info('') + ..info(e.usage); + return ExitCode.usage.code; + } + } + + @override + Future runCommand(ArgResults topLevelResults) async { + _logger + ..detail('Argument information:') + ..detail(' Top level options:'); + for (final option in topLevelResults.options) { + if (topLevelResults.wasParsed(option)) { + _logger.detail(' - $option: ${topLevelResults[option]}'); + } + } + if (topLevelResults.command != null) { + final commandResult = topLevelResults.command!; + _logger + ..detail(' Command: ${commandResult.name}') + ..detail(' Command options:'); + for (final option in commandResult.options) { + if (commandResult.wasParsed(option)) { + _logger.detail(' - $option: ${commandResult[option]}'); + } + } + } + + final int? exitCode; + if (topLevelResults['version'] == true) { + _logger.info(packageVersion); + exitCode = ExitCode.success.code; + } else { + exitCode = await super.runCommand(topLevelResults); + } + return exitCode; + } +} diff --git a/yoyo-getfastmirror/lib/src/commands/commands.dart b/yoyo-getfastmirror/lib/src/commands/commands.dart new file mode 100644 index 0000000..b00ca55 --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/commands.dart @@ -0,0 +1 @@ +export 'getfastmirror_command.dart'; diff --git a/yoyo-getfastmirror/lib/src/commands/getfastmirror_command.dart b/yoyo-getfastmirror/lib/src/commands/getfastmirror_command.dart new file mode 100644 index 0000000..8d9912d --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/getfastmirror_command.dart @@ -0,0 +1,41 @@ +import 'package:args/command_runner.dart'; +import 'package:mason_logger/mason_logger.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/getfastmirror.dart'; + +class GetfastmirroCommand extends Command { + GetfastmirroCommand({ + required Logger logger, + }) : _logger = logger { + argParser.addFlag( + 'only-test', + abbr: 'o', + help: "Only test mirrors' speed", + negatable: false, + ); + } + + @override + String get description => + 'Get fastest mirro and generate config file for oyo'; + + @override + String get name => 'getfastmirror'; + + final Logger _logger; + + @override + Future run() async { + final output = lightBlue.wrap(''' +Get the fastest mirror which will be write into source file +And generate a config file for oyo +'''); + _logger.info(output); + final onlyTest = argResults?['only-test'] == true; + final commandResult = await getFastMirror(onlyTest: onlyTest); + if (commandResult['success'] == false) { + _logger.err(commandResult['msg'] as String); + return ExitCode.unavailable.code; + } + return ExitCode.success.code; + } +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/apt_fast_config_template.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/apt_fast_config_template.dart new file mode 100644 index 0000000..f5670f1 --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/apt_fast_config_template.dart @@ -0,0 +1,170 @@ +String aptFastConfigTemplate(String yoYoMirroList,String ubuntuMirroList) { + return ''' +################################################################### +# CONFIGURATION OPTIONS +################################################################### +# Every item has a default value besides MIRRORS (which is unset). + +# Use aptitude, apt-get, or apt? +# Note that apt-get is used as a fallback for outputting the +# package URI list for e.g. aptitude, which can't do this +# Optionally add the FULLPATH to apt-get or apt-rpm or aptitude +# e.g. /usr/bin/aptitude +# +# Default: apt-get +# +_APTMGR=apt + +if grep -Eqi "linuxmint" /etc/os-release;then +_APTMGR=apt-get +fi + +if [ -x "\$(command -v oyo)" ]; then +_APTMGR=apt-get +fi + +#### +# +# UOS sources auth config +# +# +if grep -Eqi "UnionTech" /etc/issue || grep -Eq "UnionTech" /etc/*-release;then +AUTH_UOS_USER="uos-https://license.chinauos.com-apt" +AUTH_UOS_PASSWD="`cat /etc/apt/auth.conf.d/uos.conf | grep home-packages.chinauos.com`" +AUTH_UOS_PASSWD=`echo \${AUTH_UOS_PASSWD#*password }` + +fi +##### UOS自动读取账号密码以实现使用aptss来加速下载 + +# Enable DOWNLOADBEFORE to suppress apt-fast confirmation dialog and download +# packages directly. +# +# Default: dialog enabled +# +DOWNLOADBEFORE=true + + +# Choose mirror list to speed up downloads from same archive. To select some +# mirrors take a look at your distribution's archive mirror lists. +# Debian: http://www.debian.org/mirror/list +# Ubuntu: https://launchpad.net/ubuntu/+archivemirrors +# +# It is required to add mirrors in the sources.list to this array as well, so +# apt-fast can destinguish between different distributions. +# +# Examples: +# +# Different distributions (as in operating systems): +# +# sources.list: +# deb http://deb.debian.org/debian/ unstable main non-free contrib +# deb http://de.archive.ubuntu.com/ubuntu/ bionic main universe +# +# apt-fast.conf: +# MIRRORS=( 'http://deb.debian.org/debian','http://ftp.debian.org/debian,http://ftp2.de.debian.org/debian,http://ftp.de.debian.org/debian,ftp://ftp.uni-kl.de/debian' +# 'http://archive.ubuntu.com/ubuntu,http://de.archive.ubuntu.com/ubuntu,http://ftp.halifax.rwth-aachen.de/ubuntu,http://ftp.uni-kl.de/pub/linux/ubuntu,http://mirror.informatik.uni-mannheim.de/pub/linux/distributions/ubuntu/' ) +# +# +# Single distribution: +# +# sources.list: +# deb http://fr.archive.ubuntu.com/ubuntu/ bionic main +# deb http://fr.archive.ubuntu.com/ubuntu/ artful main +# +# apt-fast.conf: +# MIRRORS=( 'http://fr.archive.ubuntu.com/ubuntu,http://bouyguestelecom.ubuntu.lafibre.info/ubuntu,http://mirror.ovh.net/ubuntu,http://ubuntu-archive.mirrors.proxad.net/ubuntu' ) +# +# Default: disabled + +MIRRORS=( '$ubuntuMirroList' + '$yoYoMirroList' ) + + +# Maximum number of connections +# You can use this value in _DOWNLOADER command. Escape with \${}: \${_MAXNUM} +# +# Default: 5 +# +_MAXNUM=16 + + +# Maximum number of connections per server +# Default: 10 +# +_MAXCONPERSRV=1 + + + + + +# Split size i.e. size of each piece +# Possible Values: 1M-1024M +# +_MINSPLITSZ=1M + + +# Piece selection algorithm to use +# Available values are: default, inorder, geom +# default: selects piece so that it reduces the number of establishing connection, reasonable for most cases +# inorder: selects pieces in sequential order starting from first piece +# geom: selects piece which has minimum index like inorder, but it exponentially increasingly keeps space from previously selected pieces +# +_PIECEALGO=default + + + +# Downloadmanager listfile +# You can use this value in _DOWNLOADER command. Escape with \${}: \${DLLIST} +# +# Default: /tmp/apt-fast.list +# +DLLIST='/tmp/apt-fast.list' + + +# Download command to use. Temporary download list is designed for aria2. But +# you can choose another download command or download manager. It has to +# support following input file syntax (\\t is tab character): +# +# # Comment +# MIRROR1\\tMIRROR2\\tMIRROR3... +# out=FILENAME1 +# MIRROR1\\tMIRROR2\\tMIRROR3... +# out=FILENAME2 +# ... +# +# Examples: +# aria2c with a proxy (set username, proxy, ip and password!) +# _DOWNLOADER='aria2c --no-conf -c -j \${_MAXNUM} -x \${_MAXCONPERSRV} -s \${_SPLITCON} --min-split-size=\${_MINSPLITSZ} --stream-piece-selector=\${_PIECEALGO} --http-proxy=http://username:password@proxy_ip:proxy_port -i \${DLLIST}' +# +# Default: _DOWNLOADER='aria2c --no-conf -c -j \${_MAXNUM} -x \${_MAXCONPERSRV} -s \${_SPLITCON} --min-split-size=\${_MINSPLITSZ} --stream-piece-selector=\${_PIECEALGO} -i \${DLLIST} --connect-timeout=600 --timeout=600 -m0' +# +_DOWNLOADER='aria2c --no-conf -c -j \${_MAXNUM} -x \${_MAXCONPERSRV} --min-split-size=\${_MINSPLITSZ} --stream-piece-selector=\${_PIECEALGO} -i \${DLLIST} --connect-timeout=600 --timeout=600 -m0 --http-user \${AUTH_UOS_USER} --http-passwd \${AUTH_UOS_PASSWD}' + + +# Download temp folder for Downloadmanager +# example /tmp/apt-fast. Standard is /var/cache/apt-fast +# +# Default: /var/cache/apt/apt-fast +# +DLDIR='/var/cache/apt/apt-fast' + + +# APT archives cache directory +# +# Default /var/cache/apt/archives +# (APT configuration items Dir::Cache and Dir::Cache::archives) +# +APTCACHE='/var/cache/apt/archives' + + +# apt-fast colors +# Colors are disabled when not using a terminal. +# +# Default colors are: +# cGreen='\\e[0;32m' +# cRed='\\e[0;31m' +# cBlue='\\e[0;34m' +# endColor='\\e[0m' + +'''; +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/generate_apt_fast_config_file.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/generate_apt_fast_config_file.dart new file mode 100644 index 0000000..4f3244c --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/generate_apt_fast_config_file.dart @@ -0,0 +1,37 @@ +import 'dart:io'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/apt_fast_config_template.dart'; + +final tmpDir = Directory.systemTemp.path; +final String filePath = '$tmpDir/oyo/apt-fast.conf'; +final File file = File(filePath); +final List mirroListInConfigFileYoyo = []; +final List mirroListInConfigFileUbuntu = []; + +void generateAptFastConfigFile( + List> sortedUrlListAndTimeYoyo, + List> sortedUrlListAndTimeUbuntu, +) { + //Yoyo + for (final element in sortedUrlListAndTimeYoyo) { + mirroListInConfigFileYoyo.add(element['url'] ?? ''); + } + final mirroListInConfigFileStringYoyo = mirroListInConfigFileYoyo + .toString() + .replaceAll('[', '') + .replaceAll(']', ''); + //Ubuntu + for (final element in sortedUrlListAndTimeUbuntu) { + mirroListInConfigFileUbuntu.add(element['url'] ?? ''); + } + final mirroListInConfigFileStringUbuntu = mirroListInConfigFileUbuntu + .toString() + .replaceAll('[', '') + .replaceAll(']', ''); + + final configContent = aptFastConfigTemplate( + mirroListInConfigFileStringYoyo, + mirroListInConfigFileStringUbuntu, + ); + if (!file.existsSync()) file.createSync(recursive: true); + file.writeAsStringSync(configContent); +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/get_mirro_list.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/get_mirro_list.dart new file mode 100644 index 0000000..c00610f --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/get_mirro_list.dart @@ -0,0 +1,14 @@ +import 'package:http/http.dart' as http; + +Uri mirrorsUri = Uri( + scheme: 'https', + host: 'packages.yzzi.icu', + path: '/sources_mirrors.list', +); + +/// 获取镜像列表,返回一个 List +Future> getMirrorList() async { + final response = await http.get(mirrorsUri); + final list = response.body.replaceAll('\n', ' ').trimRight().split(' '); + return list; +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/getfastmirror.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/getfastmirror.dart new file mode 100644 index 0000000..b4b427e --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/getfastmirror.dart @@ -0,0 +1,69 @@ +import 'package:mason_logger/mason_logger.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/generate_apt_fast_config_file.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/get_mirro_list.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/network_test.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/test_mirro_speed.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/ubuntu_sources_list_template.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/write_mirror_into_file.dart'; +import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/yoyo_sources_list_template.dart'; + +final Map status = {}; +final Logger _logger = Logger(); +const List ubuntuMirrorList = [ + 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu/', + 'https://mirrors.aliyun.com/ubuntu/', + 'https://repo.huaweicloud.com/ubuntu/' +]; + +final outputYoyo = lightGreen.wrap('Yoyo Mirrors:'); +final outputUbuntu = lightGreen.wrap('Ubuntu Mirrors:'); + +Future> getFastMirror({bool onlyTest = false}) async { + //首先测试网络链接 + if (!await networkTest()) { + status['success'] = false; + status['msg'] = 'NetWork Error'; + return status; + } + //获取Yoyo镜像地址 + final yoYoMirrorList = await getMirrorList(); + _logger.info("\n$outputYoyo\n${yoYoMirrorList.join('\n')}\n"); + //测试Yoyo镜像并获取时间和排序后的Map + final sortedUrlListAndTimeYoyo = await testMirroSpeed(yoYoMirrorList); + //测试Ubuntu镜像并获取时间和排序后的Map + _logger.info("\n$outputUbuntu\n${ubuntuMirrorList.join('\n')}\n"); + final sortedUrlListAndTimeUbuntu = await testMirroSpeed(ubuntuMirrorList); + //生成config + if (!onlyTest) { + try { + generateAptFastConfigFile( + sortedUrlListAndTimeYoyo, sortedUrlListAndTimeUbuntu,); + } catch (e) { + status['success'] = false; + status['msg'] = 'Generate Config File Error'; + return status; + } finally { + status['success'] = true; + } + //写入镜像源文件 + //Yoyo + final fastestYoyoMirror = + sortedUrlListAndTimeYoyo.first.entries.first.value; + final yoyoSourcesListFile = yoyoSourcesListTemplate(fastestYoyoMirror); + writeMirrorIntoFile( + yoyoSourcesListFile, + '/etc/apt/sources.list.d/yoyo-os.list', + ); + //Ubuntu + final fastestUbuntuMirror = + sortedUrlListAndTimeUbuntu.first.entries.first.value; + final ubuntuSourcesLIstFile = + ubuntuSourcesListTemplate(fastestUbuntuMirror); + writeMirrorIntoFile( + ubuntuSourcesLIstFile, + '/etc/apt/sources.list', + ); + } + //返回执行结果 + return status; +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/network_test.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/network_test.dart new file mode 100644 index 0000000..1d98f9d --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/network_test.dart @@ -0,0 +1,13 @@ +import 'package:http/http.dart' as http; + +Uri mirrosUri = Uri( + scheme: 'https', + host: 'baidu.com', + path: '/', +); + +/// 测试网络连接,成功返回true +Future networkTest() async { + final response = await http.get(mirrosUri); + return response.statusCode == 200; +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/test_mirro_speed.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/test_mirro_speed.dart new file mode 100644 index 0000000..c7b311e --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/test_mirro_speed.dart @@ -0,0 +1,44 @@ +import 'dart:collection'; + +import 'package:cli_spinner/cli_spinner.dart'; +import 'package:dart_ping/dart_ping.dart'; +import 'package:mason_logger/mason_logger.dart'; + +Logger _logger = Logger(); +final speedResult = lightGreen.wrap('Speed Result:'); + +Future>> testMirroSpeed( + List mirrorList, +) async { + final spinner = + Spinner.type('Speed Test ......', SpinnerType.boxCircle)..start(); + final testResult = >[]; + final logMsg = StringBuffer()..write(speedResult); + for (final element in mirrorList) { + late final PingData pingRes; + late final double time; + try { + pingRes = await Ping(Uri.parse(element).host, count: 1).stream.first; + time = pingRes.response!.time!.inMicroseconds * 0.001; + testResult.add({'url': element, 'time': time.toStringAsFixed(3)}); + logMsg.write('\n${time.toStringAsFixed(3)}ms \t$element'); + } catch (e) { + testResult.add({'url': element, 'time': '99999999'}); + logMsg.write('\nTimeout \t$element'); + } + } + spinner.updateMessage(''); + spinner.stop(); + _logger.info(logMsg.toString()); + final resultMap = testResult.asMap(); + final Map> sortedResult = SplayTreeMap.from( + resultMap, + (key1, key2) => double.parse(resultMap[key1]!['time'] ?? '') > + double.parse(resultMap[key2]!['time'] ?? '') + ? 1 + : -1, + ); + final sortedUrlListAndTime = + sortedResult.entries.map((e) => e.value).toList(); + return sortedUrlListAndTime; +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/ubuntu_sources_list_template.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/ubuntu_sources_list_template.dart new file mode 100644 index 0000000..b71e1c5 --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/ubuntu_sources_list_template.dart @@ -0,0 +1,11 @@ +String ubuntuSourcesListTemplate(String ubuntuMirrorUrl) { + return ''' +# Generate by yoyo-getfastmirror +# Author: Gtibhub/Riceneeder + +deb $ubuntuMirrorUrl jammy main restricted universe multiverse +deb $ubuntuMirrorUrl jammy-updates main restricted universe multiverse +deb $ubuntuMirrorUrl jammy-backports main restricted universe multiverse +deb $ubuntuMirrorUrl jammy-security main restricted universe multiverse +'''; +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/write_mirror_into_file.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/write_mirror_into_file.dart new file mode 100644 index 0000000..c9b6991 --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/write_mirror_into_file.dart @@ -0,0 +1,14 @@ +import 'dart:io'; +import 'package:mason_logger/mason_logger.dart'; + +final done = lightGreen.wrap('Done'); +final Logger _logger = Logger(); + +void writeMirrorIntoFile(String mirror, String path) { + final file = File(path); + final writting = lightYellow.wrap('Writting $path'); + _logger.info(writting); + if (!file.existsSync()) file.createSync(recursive: true); + file.writeAsStringSync(mirror); + _logger.info(done); +} diff --git a/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/yoyo_sources_list_template.dart b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/yoyo_sources_list_template.dart new file mode 100644 index 0000000..a7c6260 --- /dev/null +++ b/yoyo-getfastmirror/lib/src/commands/modules/getfastmirro/yoyo_sources_list_template.dart @@ -0,0 +1,8 @@ +String yoyoSourcesListTemplate(String yoyoMirrorUrl) { + return ''' +# Generate by yoyo-getfastmirror +# Author: Gtibhub/Riceneeder + +deb [arch=amd64] $yoyoMirrorUrl kokomi main +'''; +} diff --git a/yoyo-getfastmirror/lib/src/version.dart b/yoyo-getfastmirror/lib/src/version.dart new file mode 100644 index 0000000..af0834a --- /dev/null +++ b/yoyo-getfastmirror/lib/src/version.dart @@ -0,0 +1 @@ +const packageVersion = '0.0.1'; diff --git a/yoyo-getfastmirror/lib/yoyo_get_fast_mirror.dart b/yoyo-getfastmirror/lib/yoyo_get_fast_mirror.dart new file mode 100644 index 0000000..0a8c388 --- /dev/null +++ b/yoyo-getfastmirror/lib/yoyo_get_fast_mirror.dart @@ -0,0 +1 @@ +library yoyo_get_fast_mirror; diff --git a/yoyo-getfastmirror/pubspec.yaml b/yoyo-getfastmirror/pubspec.yaml new file mode 100644 index 0000000..4f2204c --- /dev/null +++ b/yoyo-getfastmirror/pubspec.yaml @@ -0,0 +1,26 @@ +name: yoyo_get_fast_mirror +description: A Very Good Project created by Very Good CLI. +version: 0.0.1 +publish_to: none + +environment: + sdk: ">=2.18.0 <3.0.0" + +dependencies: + args: ^2.3.1 + cli_spinner: ^1.0.4 + dart_ping: ^8.0.1 + http: ^0.13.5 + mason_logger: ^0.2.0 + pub_updater: ^0.2.4 + +dev_dependencies: + build_runner: ^2.0.0 + build_verify: ^3.0.0 + build_version: ^2.0.0 + mocktail: ^0.3.0 + test: ^1.19.2 + very_good_analysis: ^3.1.0 + +executables: + yoyo_get_fast_mirror: diff --git a/yoyo-getfastmirror/release.sh b/yoyo-getfastmirror/release.sh new file mode 100755 index 0000000..f271dcf --- /dev/null +++ b/yoyo-getfastmirror/release.sh @@ -0,0 +1,3 @@ +#! /bin/bash + +dart compile exe bin/yoyo_get_fast_mirror.dart -o release/yoyo-getfastmirror \ No newline at end of file diff --git a/yoyo-getfastmirror/release/log.md b/yoyo-getfastmirror/release/log.md new file mode 100644 index 0000000..e69de29