From 54770c4addd9a1969d27fcd3693d3044aa2b63bc Mon Sep 17 00:00:00 2001 From: sinspired Date: Thu, 5 Dec 2024 12:15:21 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_release.yml | 16 +- README.md | 169 +++++++++++------- hosts | 167 +++++++++++------- setDNS.py | 47 ++--- setHosts_Classic.py | 263 ++++++++++++++-------------- 5 files changed, 358 insertions(+), 304 deletions(-) diff --git a/.github/workflows/build_release.yml b/.github/workflows/build_release.yml index 8e263d4..8298a51 100644 --- a/.github/workflows/build_release.yml +++ b/.github/workflows/build_release.yml @@ -26,22 +26,22 @@ jobs: with: fetch-depth: 0 - - name: Set up Python 3.11 + - name: Set up Python 3.12 uses: actions/setup-python@v5 with: - python-version: '3.11' + python-version: '3.12' - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - pip install pyinstaller + pip install -U pyinstaller - name: Build executables (Linux) if: matrix.os == 'ubuntu-latest' run: | for script in setDNS.py setHosts.py setHosts_Classic.py; do - pyinstaller --onefile "$script" + pyinstaller --clean --onefile "$script" mv "dist/${script%.py}" "dist/${script%.py}-Linux-x64" done zip -j "cnNetTool-Linux-x64.zip" dist/*-Linux-x64 @@ -57,9 +57,9 @@ jobs: $icoName = $script -replace '\.py$', '.ico' if (Test-Path -Path $icoName) { - pyinstaller --onefile $script --uac-admin --icon $icoName + pyinstaller --clean --onefile $script --uac-admin --icon $icoName } else { - pyinstaller --onefile $script --uac-admin + pyinstaller --clean --onefile $script --uac-admin } # pyinstaller --onefile $script --uac-admin @@ -73,7 +73,7 @@ jobs: if: matrix.os == 'macos-latest' run: | for script in setDNS.py setHosts.py setHosts_Classic.py; do - pyinstaller --onefile "$script" + pyinstaller --clean --onefile "$script" mv "dist/${script%.py}" "dist/${script%.py}-macOS-x64" done zip -j "cnNetTool-macOS-x64.zip" dist/*-macOS-x64 @@ -144,4 +144,4 @@ jobs: **/cnNetTool-Linux-x64.zip \ **/cnNetTool-Windows-x64.zip \ **/cnNetTool-macOS-x64.zip - shell: bash \ No newline at end of file + shell: bash diff --git a/README.md b/README.md index 49ffb0f..09b7bae 100644 --- a/README.md +++ b/README.md @@ -30,80 +30,123 @@ ```bash -# cnNetTool Start in 2024-11-30 12:24:58 +08:00 +# cnNetTool Start in 2024-12-05 11:37:05 +08:00 140.82.113.26 alive.github.com -140.82.112.5 api.github.com -140.82.112.21 central.github.com -140.82.113.9 codeload.github.com -140.82.114.22 collector.github.com -140.82.113.4 gist.github.com -140.82.114.3 github.com -140.82.114.18 github.community -146.75.29.194 github.global.ssl.fastly.net -16.182.38.33 github-com.s3.amazonaws.com -16.15.185.16 github-production-release-asset-2e65be.s3.amazonaws.com -140.82.114.26 live.github.com +20.205.243.168 api.github.com +140.82.113.22 central.github.com +20.205.243.165 codeload.github.com +140.82.113.22 collector.github.com +140.82.112.3 gist.github.com +20.205.243.166 github.com +140.82.113.18 github.community +151.101.129.194 github.global.ssl.fastly.net +2a03:2880:f131:83:face:b00c:0:25de github.global.ssl.fastly.net +16.182.37.73 github-com.s3.amazonaws.com +3.5.21.112 github-production-release-asset-2e65be.s3.amazonaws.com +140.82.112.25 live.github.com 13.107.42.16 pipelines.actions.githubusercontent.com -185.199.110.154 github.githubassets.com -185.199.108.153 github.io +185.199.108.154 github.githubassets.com 185.199.108.153 githubstatus.com +2606:50c0:8001::153 githubstatus.com 185.199.108.153 assets-cdn.github.com -185.199.109.133 avatars.githubusercontent.com -185.199.109.133 avatars0.githubusercontent.com -185.199.109.133 avatars1.githubusercontent.com -185.199.109.133 avatars2.githubusercontent.com -185.199.109.133 avatars3.githubusercontent.com -185.199.109.133 avatars4.githubusercontent.com -185.199.109.133 avatars5.githubusercontent.com -185.199.109.133 camo.githubusercontent.com -185.199.109.133 cloud.githubusercontent.com -185.199.109.133 desktop.githubusercontent.com -185.199.109.133 favicons.githubusercontent.com -185.199.109.133 github.map.fastly.net -185.199.109.133 media.githubusercontent.com -185.199.109.133 objects.githubusercontent.com -185.199.109.133 private-user-images.githubusercontent.com -185.199.109.133 raw.githubusercontent.com -185.199.109.133 user-images.githubusercontent.com -18.160.200.13 tmdb.org -18.160.200.13 api.tmdb.org -18.160.200.13 files.tmdb.org -52.85.247.30 themoviedb.org -52.85.247.30 api.themoviedb.org -52.85.247.30 www.themoviedb.org -52.85.247.30 auth.themoviedb.org -185.93.1.244 image.tmdb.org -185.93.1.244 images.tmdb.org -52.94.225.248 imdb.com -3.168.35.144 www.imdb.com +2606:50c0:8001::153 assets-cdn.github.com +185.199.108.153 github.io +2606:50c0:8001::153 github.io +185.199.108.133 avatars.githubusercontent.com +2606:50c0:8002::154 avatars.githubusercontent.com +185.199.108.133 avatars0.githubusercontent.com +2606:50c0:8002::154 avatars0.githubusercontent.com +185.199.108.133 avatars1.githubusercontent.com +2606:50c0:8002::154 avatars1.githubusercontent.com +185.199.108.133 avatars2.githubusercontent.com +2606:50c0:8002::154 avatars2.githubusercontent.com +185.199.108.133 avatars3.githubusercontent.com +2606:50c0:8002::154 avatars3.githubusercontent.com +185.199.108.133 avatars4.githubusercontent.com +2606:50c0:8002::154 avatars4.githubusercontent.com +185.199.108.133 avatars5.githubusercontent.com +2606:50c0:8002::154 avatars5.githubusercontent.com +185.199.108.133 camo.githubusercontent.com +2606:50c0:8002::154 camo.githubusercontent.com +185.199.108.133 cloud.githubusercontent.com +2606:50c0:8002::154 cloud.githubusercontent.com +185.199.108.133 desktop.githubusercontent.com +2606:50c0:8002::154 desktop.githubusercontent.com +185.199.108.133 favicons.githubusercontent.com +2606:50c0:8002::154 favicons.githubusercontent.com +185.199.108.133 github.map.fastly.net +2606:50c0:8002::154 github.map.fastly.net +185.199.108.133 media.githubusercontent.com +2606:50c0:8002::154 media.githubusercontent.com +185.199.108.133 objects.githubusercontent.com +2606:50c0:8002::154 objects.githubusercontent.com +185.199.108.133 private-user-images.githubusercontent.com +2606:50c0:8002::154 private-user-images.githubusercontent.com +185.199.108.133 raw.githubusercontent.com +2606:50c0:8002::154 raw.githubusercontent.com +185.199.108.133 user-images.githubusercontent.com +2606:50c0:8002::154 user-images.githubusercontent.com +54.230.71.49 tmdb.org +2600:9000:2014:8a00:5:da10:7440:93a1 tmdb.org +54.230.71.49 api.tmdb.org +2600:9000:2014:8a00:5:da10:7440:93a1 api.tmdb.org +54.230.71.49 files.tmdb.org +2600:9000:2014:8a00:5:da10:7440:93a1 files.tmdb.org +13.226.61.96 themoviedb.org +2600:9000:2816:5000:e:5373:440:93a1 themoviedb.org +13.226.61.96 api.themoviedb.org +2600:9000:2816:5000:e:5373:440:93a1 api.themoviedb.org +13.226.61.96 www.themoviedb.org +2600:9000:2816:5000:e:5373:440:93a1 www.themoviedb.org +13.226.61.96 auth.themoviedb.org +2600:9000:2816:5000:e:5373:440:93a1 auth.themoviedb.org +109.61.83.98 image.tmdb.org +2400:52e0:1500::1092:1 image.tmdb.org +109.61.83.98 images.tmdb.org +2400:52e0:1500::1092:1 images.tmdb.org +52.94.228.167 imdb.com +13.35.217.192 www.imdb.com 52.94.237.74 secure.imdb.com -3.168.35.144 s.media-imdb.com -52.94.237.74 us.dd.imdb.com -3.168.35.144 www.imdb.to -52.94.225.248 imdb-webservice.amazon.com +18.172.46.173 s.media-imdb.com +52.94.225.248 us.dd.imdb.com +13.35.217.192 www.imdb.to +52.94.228.167 imdb-webservice.amazon.com 44.215.137.99 origin-www.imdb.com -146.75.29.16 m.media-amazon.com -146.75.29.16 Images-na.ssl-images-amazon.com -104.123.159.75 images-fe.ssl-images-amazon.com -3.167.162.195 images-eu.ssl-images-amazon.com -146.75.29.16 ia.media-imdb.com -146.75.29.16 f.media-amazon.com -52.84.18.90 imdb-video.media-imdb.com -3.167.180.205 dqpnq362acqdi.cloudfront.net -142.250.190.74 translate.google.com -142.250.190.74 translate.googleapis.com -142.250.190.74 translate-pa.googleapis.com -3.167.138.84 plugins.jetbrains.com -3.167.138.84 download.jetbrains.com -3.167.138.84 cache-redirector.jetbrains.com - -# Update time: 2024-11-30 12:24:58 +08:00 +23.49.104.166 m.media-amazon.com +2600:9000:2078:f800:1d:d7f6:39d4:e6e1 m.media-amazon.com +151.101.65.16 Images-na.ssl-images-amazon.com +2600:1417:4400:12::1721:b8e7 Images-na.ssl-images-amazon.com +23.49.104.166 images-fe.ssl-images-amazon.com +2600:9000:2079:fa00:1d:d7f6:39d4:e6e1 images-fe.ssl-images-amazon.com +151.101.129.16 images-eu.ssl-images-amazon.com +2600:9000:2751:1a00:1d:d7f6:39d4:e6e1 images-eu.ssl-images-amazon.com +23.49.104.166 ia.media-imdb.com +2600:1417:4400:12::1721:b8e7 ia.media-imdb.com +151.101.129.16 f.media-amazon.com +2a04:4e42:48::272 f.media-amazon.com +13.35.186.103 imdb-video.media-imdb.com +13.32.32.190 dqpnq362acqdi.cloudfront.net +2600:9000:27af:7600:5:ce70:a180:21 dqpnq362acqdi.cloudfront.net +34.105.140.105 translate.google.com +2404:6800:4005:823::200a translate.google.com +34.105.140.105 translate.googleapis.com +2404:6800:4005:823::200a translate.googleapis.com +34.105.140.105 translate-pa.googleapis.com +2404:6800:4005:823::200a translate-pa.googleapis.com +13.33.183.20 plugins.jetbrains.com +2600:9000:2014:f600:12:7c44:15c0:93a1 plugins.jetbrains.com +13.33.183.20 download.jetbrains.com +2600:9000:2014:f600:12:7c44:15c0:93a1 download.jetbrains.com +13.33.183.20 cache-redirector.jetbrains.com +2600:9000:2014:f600:12:7c44:15c0:93a1 cache-redirector.jetbrains.com + +# Update time: 2024-12-05 11:37:05 +08:00 # GitHub仓库: https://github.com/sinspired/cnNetTool # cnNetTool End ``` -以上内容会自动定时更新, 数据更新时间:2024-11-30 12:24:58 +08:00 +以上内容会自动定时更新, 数据更新时间:2024-12-05 11:37:05 +08:00 #### 1.2.2 修改 hosts 文件 diff --git a/hosts b/hosts index 5f9e059..2a35e94 100644 --- a/hosts +++ b/hosts @@ -1,71 +1,114 @@ -# cnNetTool Start in 2024-11-30 12:24:58 +08:00 +# cnNetTool Start in 2024-12-05 11:37:05 +08:00 140.82.113.26 alive.github.com -140.82.112.5 api.github.com -140.82.112.21 central.github.com -140.82.113.9 codeload.github.com -140.82.114.22 collector.github.com -140.82.113.4 gist.github.com -140.82.114.3 github.com -140.82.114.18 github.community -146.75.29.194 github.global.ssl.fastly.net -16.182.38.33 github-com.s3.amazonaws.com -16.15.185.16 github-production-release-asset-2e65be.s3.amazonaws.com -140.82.114.26 live.github.com +20.205.243.168 api.github.com +140.82.113.22 central.github.com +20.205.243.165 codeload.github.com +140.82.113.22 collector.github.com +140.82.112.3 gist.github.com +20.205.243.166 github.com +140.82.113.18 github.community +151.101.129.194 github.global.ssl.fastly.net +2a03:2880:f131:83:face:b00c:0:25de github.global.ssl.fastly.net +16.182.37.73 github-com.s3.amazonaws.com +3.5.21.112 github-production-release-asset-2e65be.s3.amazonaws.com +140.82.112.25 live.github.com 13.107.42.16 pipelines.actions.githubusercontent.com -185.199.110.154 github.githubassets.com -185.199.108.153 github.io +185.199.108.154 github.githubassets.com 185.199.108.153 githubstatus.com +2606:50c0:8001::153 githubstatus.com 185.199.108.153 assets-cdn.github.com -185.199.109.133 avatars.githubusercontent.com -185.199.109.133 avatars0.githubusercontent.com -185.199.109.133 avatars1.githubusercontent.com -185.199.109.133 avatars2.githubusercontent.com -185.199.109.133 avatars3.githubusercontent.com -185.199.109.133 avatars4.githubusercontent.com -185.199.109.133 avatars5.githubusercontent.com -185.199.109.133 camo.githubusercontent.com -185.199.109.133 cloud.githubusercontent.com -185.199.109.133 desktop.githubusercontent.com -185.199.109.133 favicons.githubusercontent.com -185.199.109.133 github.map.fastly.net -185.199.109.133 media.githubusercontent.com -185.199.109.133 objects.githubusercontent.com -185.199.109.133 private-user-images.githubusercontent.com -185.199.109.133 raw.githubusercontent.com -185.199.109.133 user-images.githubusercontent.com -18.160.200.13 tmdb.org -18.160.200.13 api.tmdb.org -18.160.200.13 files.tmdb.org -52.85.247.30 themoviedb.org -52.85.247.30 api.themoviedb.org -52.85.247.30 www.themoviedb.org -52.85.247.30 auth.themoviedb.org -185.93.1.244 image.tmdb.org -185.93.1.244 images.tmdb.org -52.94.225.248 imdb.com -3.168.35.144 www.imdb.com +2606:50c0:8001::153 assets-cdn.github.com +185.199.108.153 github.io +2606:50c0:8001::153 github.io +185.199.108.133 avatars.githubusercontent.com +2606:50c0:8002::154 avatars.githubusercontent.com +185.199.108.133 avatars0.githubusercontent.com +2606:50c0:8002::154 avatars0.githubusercontent.com +185.199.108.133 avatars1.githubusercontent.com +2606:50c0:8002::154 avatars1.githubusercontent.com +185.199.108.133 avatars2.githubusercontent.com +2606:50c0:8002::154 avatars2.githubusercontent.com +185.199.108.133 avatars3.githubusercontent.com +2606:50c0:8002::154 avatars3.githubusercontent.com +185.199.108.133 avatars4.githubusercontent.com +2606:50c0:8002::154 avatars4.githubusercontent.com +185.199.108.133 avatars5.githubusercontent.com +2606:50c0:8002::154 avatars5.githubusercontent.com +185.199.108.133 camo.githubusercontent.com +2606:50c0:8002::154 camo.githubusercontent.com +185.199.108.133 cloud.githubusercontent.com +2606:50c0:8002::154 cloud.githubusercontent.com +185.199.108.133 desktop.githubusercontent.com +2606:50c0:8002::154 desktop.githubusercontent.com +185.199.108.133 favicons.githubusercontent.com +2606:50c0:8002::154 favicons.githubusercontent.com +185.199.108.133 github.map.fastly.net +2606:50c0:8002::154 github.map.fastly.net +185.199.108.133 media.githubusercontent.com +2606:50c0:8002::154 media.githubusercontent.com +185.199.108.133 objects.githubusercontent.com +2606:50c0:8002::154 objects.githubusercontent.com +185.199.108.133 private-user-images.githubusercontent.com +2606:50c0:8002::154 private-user-images.githubusercontent.com +185.199.108.133 raw.githubusercontent.com +2606:50c0:8002::154 raw.githubusercontent.com +185.199.108.133 user-images.githubusercontent.com +2606:50c0:8002::154 user-images.githubusercontent.com +54.230.71.49 tmdb.org +2600:9000:2014:8a00:5:da10:7440:93a1 tmdb.org +54.230.71.49 api.tmdb.org +2600:9000:2014:8a00:5:da10:7440:93a1 api.tmdb.org +54.230.71.49 files.tmdb.org +2600:9000:2014:8a00:5:da10:7440:93a1 files.tmdb.org +13.226.61.96 themoviedb.org +2600:9000:2816:5000:e:5373:440:93a1 themoviedb.org +13.226.61.96 api.themoviedb.org +2600:9000:2816:5000:e:5373:440:93a1 api.themoviedb.org +13.226.61.96 www.themoviedb.org +2600:9000:2816:5000:e:5373:440:93a1 www.themoviedb.org +13.226.61.96 auth.themoviedb.org +2600:9000:2816:5000:e:5373:440:93a1 auth.themoviedb.org +109.61.83.98 image.tmdb.org +2400:52e0:1500::1092:1 image.tmdb.org +109.61.83.98 images.tmdb.org +2400:52e0:1500::1092:1 images.tmdb.org +52.94.228.167 imdb.com +13.35.217.192 www.imdb.com 52.94.237.74 secure.imdb.com -3.168.35.144 s.media-imdb.com -52.94.237.74 us.dd.imdb.com -3.168.35.144 www.imdb.to -52.94.225.248 imdb-webservice.amazon.com +18.172.46.173 s.media-imdb.com +52.94.225.248 us.dd.imdb.com +13.35.217.192 www.imdb.to +52.94.228.167 imdb-webservice.amazon.com 44.215.137.99 origin-www.imdb.com -146.75.29.16 m.media-amazon.com -146.75.29.16 Images-na.ssl-images-amazon.com -104.123.159.75 images-fe.ssl-images-amazon.com -3.167.162.195 images-eu.ssl-images-amazon.com -146.75.29.16 ia.media-imdb.com -146.75.29.16 f.media-amazon.com -52.84.18.90 imdb-video.media-imdb.com -3.167.180.205 dqpnq362acqdi.cloudfront.net -142.250.190.74 translate.google.com -142.250.190.74 translate.googleapis.com -142.250.190.74 translate-pa.googleapis.com -3.167.138.84 plugins.jetbrains.com -3.167.138.84 download.jetbrains.com -3.167.138.84 cache-redirector.jetbrains.com +23.49.104.166 m.media-amazon.com +2600:9000:2078:f800:1d:d7f6:39d4:e6e1 m.media-amazon.com +151.101.65.16 Images-na.ssl-images-amazon.com +2600:1417:4400:12::1721:b8e7 Images-na.ssl-images-amazon.com +23.49.104.166 images-fe.ssl-images-amazon.com +2600:9000:2079:fa00:1d:d7f6:39d4:e6e1 images-fe.ssl-images-amazon.com +151.101.129.16 images-eu.ssl-images-amazon.com +2600:9000:2751:1a00:1d:d7f6:39d4:e6e1 images-eu.ssl-images-amazon.com +23.49.104.166 ia.media-imdb.com +2600:1417:4400:12::1721:b8e7 ia.media-imdb.com +151.101.129.16 f.media-amazon.com +2a04:4e42:48::272 f.media-amazon.com +13.35.186.103 imdb-video.media-imdb.com +13.32.32.190 dqpnq362acqdi.cloudfront.net +2600:9000:27af:7600:5:ce70:a180:21 dqpnq362acqdi.cloudfront.net +34.105.140.105 translate.google.com +2404:6800:4005:823::200a translate.google.com +34.105.140.105 translate.googleapis.com +2404:6800:4005:823::200a translate.googleapis.com +34.105.140.105 translate-pa.googleapis.com +2404:6800:4005:823::200a translate-pa.googleapis.com +13.33.183.20 plugins.jetbrains.com +2600:9000:2014:f600:12:7c44:15c0:93a1 plugins.jetbrains.com +13.33.183.20 download.jetbrains.com +2600:9000:2014:f600:12:7c44:15c0:93a1 download.jetbrains.com +13.33.183.20 cache-redirector.jetbrains.com +2600:9000:2014:f600:12:7c44:15c0:93a1 cache-redirector.jetbrains.com -# Update time: 2024-11-30 12:24:58 +08:00 -# GitHub仓库: https://github.com/sinspired/cnNetTool +# Update time: 2024-12-05 11:37:05 +08:00 +# GitHubֿ: https://github.com/sinspired/cnNetTool # cnNetTool End diff --git a/setDNS.py b/setDNS.py index 1bb3158..d22dc8e 100644 --- a/setDNS.py +++ b/setDNS.py @@ -58,34 +58,13 @@ "ipv4": ["64.6.64.6", "64.6.65.6"], "ipv6": ["2620:74:1b::1:1", "2620:74:1c::2:2"], }, - "NTT Communications DNS": { - "ipv4": ["129.250.35.250"], - "ipv6": [] - }, - "KT DNS": { - "ipv4": ["168.126.63.1"], - "ipv6": [] - }, - "CPC HK": { - "ipv4": ["210.184.24.65"], - "ipv6": [] - }, - "Soft Bank": { - "ipv4": ["101.110.50.106"], - "ipv6": [] - }, - "SingNet": { - "ipv4": ["118.201.189.90"], - "ipv6": [] - }, - "SK Broadband": { - "ipv4": ["1.228.180.5"], - "ipv6": [] - }, - "Korea Telecom": { - "ipv4": ["183.99.33.6"], - "ipv6": [] - }, + "NTT Communications DNS": {"ipv4": ["129.250.35.250"], "ipv6": []}, + "KT DNS": {"ipv4": ["168.126.63.1"], "ipv6": []}, + "CPC HK": {"ipv4": ["210.184.24.65"], "ipv6": []}, + "Soft Bank": {"ipv4": ["101.110.50.106"], "ipv6": []}, + "SingNet": {"ipv4": ["118.201.189.90"], "ipv6": []}, + "SK Broadband": {"ipv4": ["1.228.180.5"], "ipv6": []}, + "Korea Telecom": {"ipv4": ["183.99.33.6"], "ipv6": []}, }, "中国大陆": { "114DNS": { @@ -309,8 +288,8 @@ def set_dns_servers(ipv4_dns_list: list[str], ipv6_dns_list: list[str]): continue if ipv4_dns_list: logger.debug( - f"设置IPv4 DNS for {interface}: { - ', '.join(ipv4_dns_list)}" + f"设置IPv4 DNS for {interface}:{ + ', '.join(ipv4_dns_list)}" ) try: subprocess.run( @@ -462,8 +441,7 @@ def print_recommended_dns_table(dns_list: list, ip_version: str, available_dns: if dns: # 在best_dns列表中查找正确的服务器信息 server_info = next( - (info for server, - info in available_dns[ip_version] if server == dns), + (info for server, info in available_dns[ip_version] if server == dns), None, ) if server_info: @@ -573,8 +551,7 @@ def main(): print_domain_resolutions(domain_resolutions, dns_performance) # 防止 best_dns_num 数值超过数组长度 - num_servers = min( - len(available_dns["ipv4"]), len(available_dns["ipv6"])) + num_servers = min(len(available_dns["ipv4"]), len(available_dns["ipv6"])) if args.best_dns_num > num_servers: args.best_dns_num = num_servers @@ -598,7 +575,7 @@ def main(): set_dns_servers(recommended_dns["ipv4"], recommended_dns["ipv6"]) logger.info("DNS服务器已更新") if thread.is_alive(): # 确认输入线程是否仍在运行 - thread.join() # 等待线程完成 + thread.join() # 等待线程完成 input("任务执行完毕,按任意键退出!") else: logger.info("操作已取消") diff --git a/setHosts_Classic.py b/setHosts_Classic.py index 453c4a6..29b1262 100644 --- a/setHosts_Classic.py +++ b/setHosts_Classic.py @@ -1,3 +1,4 @@ +from math import floor import os import ssl import sys @@ -22,6 +23,8 @@ from rich.progress import Progress, SpinnerColumn, TextColumn import wcwidth +# if sys.platform == "win32": +# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # -------------------- 常量设置 -------------------- # RESOLVER_TIMEOUT = 1 # DNS 解析超时时间 秒 HOSTS_NUM = 1 # 每个域名限定Hosts主机 ipv4 数量 @@ -346,15 +349,19 @@ async def resolve_domain(self, domain: str) -> Set[str]: ipv6_ips = domain_hosts.get("ipv6", []) ips.update(ipv4_ips + ipv6_ips) - logging.debug(f"成功通过缓存文件解析 {domain}, 发现 {len( - ipv4_ips)+len(ipv6_ips)} 个 DNS 主机:\n{ipv4_ips}\n{ipv6_ips if ipv6_ips else ''}\n") + logging.debug( + f"成功通过缓存文件解析 {domain}, 发现 {len( + ipv4_ips)+len(ipv6_ips)} 个 DNS 主机:\n{ipv4_ips}\n{ipv6_ips if ipv6_ips else ''}\n" + ) else: ipaddress_ips = await self._resolve_via_ipaddress(domain) ips.update(ipaddress_ips) if ips: - logging.debug(f"成功通过 DNS服务器 和 DNS记录 解析 {domain}, 发现 { - len(ips)} 个 唯一 DNS 主机\n{ips}\n") + logging.debug( + f"成功通过 DNS服务器 和 DNS记录 解析 {domain}, 发现 { + len(ips)} 个 唯一 DNS 主机\n{ips}\n" + ) else: logging.debug(f"警告: 无法解析 {domain}") @@ -371,10 +378,11 @@ async def _resolve_via_dns(self, domain: str, dns_type: str = "all") -> Set[str] - "international": 仅使用国际 DNS :return: 解析得到的 IP 集合 """ + async def resolve_with_dns_server(dns_server_info: dict) -> Set[str]: """单个DNS服务器的解析协程""" - dns_server = dns_server_info['ip'] - dns_provider = dns_server_info['provider'] + dns_server = dns_server_info["ip"] + dns_provider = dns_server_info["provider"] ips = set() resolver = dns.resolver.Resolver(configure=False) resolver.nameservers = [dns_server] @@ -395,8 +403,10 @@ async def resolve_with_dns_server(dns_server_info: dict) -> Set[str]: logging.debug(f"DNS 查询异常 ({qtype}, {dns_server}): {e}") if ips: - logging.debug(f"成功使用 {dns_provider} : {dns_server} 解析 { - domain},共 {len(ips)} 个主机: {ips}") + logging.debug( + f"成功使用 {dns_provider} : {dns_server} 解析 { + domain},共 {len(ips)} 个主机: {ips}" + ) return ips @@ -406,15 +416,19 @@ async def resolve_with_dns_server(dns_server_info: dict) -> Set[str]: # 根据 dns_type 选择要使用的 DNS 服务器 if dns_type.lower() == "all": - dns_servers = self.dns_servers['china_mainland'] + \ - self.dns_servers['international'] + dns_servers = ( + self.dns_servers["china_mainland"] + + self.dns_servers["international"] + ) elif dns_type.lower() == "china": - dns_servers = self.dns_servers['china_mainland'] + dns_servers = self.dns_servers["china_mainland"] elif dns_type.lower() == "global" or dns_type.lower() == "international": - dns_servers = self.dns_servers['international'] + dns_servers = self.dns_servers["international"] else: - dns_servers = self.dns_servers['china_mainland'] + \ - self.dns_servers['international'] + dns_servers = ( + self.dns_servers["china_mainland"] + + self.dns_servers["international"] + ) # raise ValueError(f"无效的 DNS 类型:{dns_type}") with concurrent.futures.ThreadPoolExecutor(max_workers=10): # 并发解析所有选定的 DNS 服务器,并保留非空结果 @@ -426,8 +440,10 @@ async def resolve_with_dns_server(dns_server_info: dict) -> Set[str]: ips = set(ip for result in results for ip in result if ip) if ips: - logging.debug(f"成功使用多个 DNS 服务器解析 {domain},共 { - len(ips)} 个主机:\n{ips}\n") + logging.debug( + f"成功使用多个 DNS 服务器解析 {domain},共 { + len(ips)} 个主机:\n{ips}\n" + ) return ips @@ -542,15 +558,16 @@ async def get_lowest_latency_hosts( rprint(f"[bright_black]- 检测主机延迟...[/bright_black]") # 使用线程池来并发处理SSL证书验证 - with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor: + with concurrent.futures.ThreadPoolExecutor( + max_workers=self.max_workers + ) as executor: # 第一步:并发获取IP延迟 ping_tasks = [self.get_host_average_latency(ip) for ip in all_ips] latency_results = await asyncio.gather(*ping_tasks) # 筛选有效延迟的IP valid_latency_results = [ - result for result in latency_results - if result[1] != float("inf") + result for result in latency_results if result[1] != float("inf") ] if valid_latency_results: if len(valid_latency_results) < len(all_ips): @@ -559,15 +576,21 @@ async def get_lowest_latency_hosts( len(valid_latency_results)}[/bold bright_green] 个有效IP地址[/bright_black]" ) valid_latency_ips = [ - result for result in valid_latency_results if result[1] < latency_limit] + result + for result in valid_latency_results + if result[1] < latency_limit + ] if not valid_latency_ips: logging.warning(f"未发现延迟小于 {latency_limit}ms 的IP。") min_result = [ min(valid_latency_results, key=lambda x: x[1])] - latency_limit = min_result[0][1]*2 + latency_limit = min_result[0][1] * 2 logging.debug(f"主机IP最低延迟 {latency_limit:.0f}ms") valid_latency_ips = [ - result for result in valid_latency_results if result[1] <= latency_limit] + result + for result in valid_latency_results + if result[1] <= latency_limit + ] else: rprint("[red]延迟检测没有获得有效IP[/red]") @@ -592,10 +615,10 @@ async def get_lowest_latency_hosts( # ssl_verification_tasks = [] # if "github" in group_name.lower(): - if len(valid_latency_ips) > 1 and any(keyword in group_name.lower() for keyword in ["github", "google"]): - rprint( - f"[bright_black]- 验证SSL证书...[/bright_black]" - ) + if len(valid_latency_ips) > 1 and any( + keyword in group_name.lower() for keyword in ["github", "google"] + ): + rprint(f"[bright_black]- 验证SSL证书...[/bright_black]") # batch_size = args.batch_size # total_results = len(valid_latency_ips) @@ -620,7 +643,7 @@ async def get_lowest_latency_hosts( for i in range(0, total_results, batch_size): min_len = min(total_results, batch_size) - batch = valid_latency_ips[i:i + min_len] + batch = valid_latency_ips[i: i + min_len] ssl_verification_tasks = [ loop.run_in_executor( executor, @@ -629,7 +652,7 @@ async def get_lowest_latency_hosts( # self._sync_is_cert_valid_dict_average, # domains, ip, - latency + latency, ) for ip, latency in batch ] @@ -883,18 +906,15 @@ async def process_hosts(self, domains, batch, executor, valid_latency_ips): for ip, latency in batch: ssl_verification_tasks.append( loop.run_in_executor( - executor, - self._sync_is_cert_valid, - domains[0], - ip - ) + executor, self._sync_is_cert_valid, domains[0], ip) ) # 等待SSL证书验证结果 ssl_results = await asyncio.gather(*ssl_verification_tasks) # 结合延迟和SSL验证结果 valid_results = [ - (ip, latency) for (ip, latency), ssl_valid in zip(valid_latency_ips, ssl_results) + (ip, latency) + for (ip, latency), ssl_valid in zip(valid_latency_ips, ssl_results) if ssl_valid ] return valid_results @@ -926,12 +946,14 @@ def _sync_is_cert_valid(self, domain: str, ip: str, port: int = 443) -> bool: except ConnectionError as e: logging.debug(f"{domain} ({ip}): 连接被强迫关闭,ip有效 - {e}") - return False + return True except Exception as e: logging.debug(f"{domain} ({ip}): 证书验证失败 - {e}") return False - def _sync_is_cert_valid_dict(self, domain: str, ip: str, latency: float, port: int = 443) -> Tuple[str, float, bool]: + def _sync_is_cert_valid_dict( + self, domain: str, ip: str, latency: float, port: int = 443 + ) -> Tuple[str, float, bool]: """ 同步版本的证书验证方法,用于在线程池中执行 """ @@ -944,28 +966,34 @@ def _sync_is_cert_valid_dict(self, domain: str, ip: str, latency: float, port: i with context.wrap_socket(sock, server_hostname=domain) as ssock: cert = ssock.getpeercert() not_after = datetime.strptime( - cert["notAfter"], "%b %d %H:%M:%S %Y %Z") + cert["notAfter"], "%b %d %H:%M:%S %Y %Z" + ) if not_after < datetime.now(): - logging.debug(f"{domain} ({ip}) { - latency:.0f}ms: 证书已过期") + logging.debug( + f"{domain} ({ip}) { + latency:.0f}ms: 证书已过期" + ) return (ip, latency, False) - logging.debug(f"{domain} ({ip}) { - latency:.0f}ms: SSL证书有效,截止日期为 {not_after}") + logging.debug( + f"{domain} ({ip}) { + latency:.0f}ms: SSL证书有效,截止日期为 {not_after}" + ) return (ip, latency, True) except ConnectionError as e: - logging.debug(f"{domain} ({ip}) { - latency:.0f}ms: 连接被强迫关闭,ip有效 - {e}") - rprint(f"{domain} ({ip}) { - latency:.0f}ms: 连接被强迫关闭,ip有效 - {e}") - input("按任意键继续...") - return (ip, latency, False) + logging.debug( + f"{domain} ({ip}) { + latency:.0f}ms: 连接被强迫关闭,ip有效 - {e}" + ) + return (ip, latency, True) except Exception as e: logging.debug(f"{domain} ({ip}) {latency:.0f}ms: 证书验证失败 - {e}") return (ip, latency, False) - def _sync_is_cert_valid_dict_average(self, domains: List[str], ip: str, latency: float, port: int = 443) -> Tuple[str, float, bool]: + def _sync_is_cert_valid_dict_average( + self, domains: List[str], ip: str, latency: float, port: int = 443 + ) -> Tuple[str, float, bool]: """ 同步版本的证书验证方法,用于在线程池中执行。 任意一个 domain 验证通过就视为通过。 @@ -980,23 +1008,27 @@ def _sync_is_cert_valid_dict_average(self, domains: List[str], ip: str, latency: with context.wrap_socket(sock, server_hostname=domain) as ssock: cert = ssock.getpeercert() not_after = datetime.strptime( - cert["notAfter"], "%b %d %H:%M:%S %Y %Z") + cert["notAfter"], "%b %d %H:%M:%S %Y %Z" + ) if not_after < datetime.now(): - logging.debug(f"{domain} ({ip}) { - latency:.0f}ms: 证书已过期") + logging.debug( + f"{domain} ({ip}) { + latency:.0f}ms: 证书已过期" + ) continue # 检查下一个 domain - logging.debug(f"{domain} ({ip}) { - latency:.0f}ms: SSL证书有效,截止日期为 {not_after}") + logging.debug( + f"{domain} ({ip}) { + latency:.0f}ms: SSL证书有效,截止日期为 {not_after}" + ) return (ip, latency, True) # 任意一个验证通过即返回成功 except ConnectionError as e: - # logging.debug(f"{domain} ({ip}) { - # latency:.0f}ms: 连接被强迫关闭,ip有效 - {e}") - rprint(f"[red]{domain} ({ip}) { - latency:.0f}ms: 连接被强迫关闭,ip有效[/red]") - # input("按任意键继续...\n") - continue # 检查下一个 domain + logging.debug( + f"{domain} ({ip}) { + latency:.0f}ms: 连接被强迫关闭,ip有效 - {e}" + ) + return (ip, latency, True) except Exception as e: logging.debug(f"{domain} ({ip}) {latency:.0f}ms: 证书验证失败 - {e}") continue # 检查下一个 domain @@ -1004,7 +1036,9 @@ def _sync_is_cert_valid_dict_average(self, domains: List[str], ip: str, latency: # 如果所有 domain 都验证失败 return (ip, latency, False) - def _select_best_hosts(self, valid_results: List[Tuple[str, float]]) -> List[Tuple[str, float]]: + def _select_best_hosts( + self, valid_results: List[Tuple[str, float]] + ) -> List[Tuple[str, float]]: """ 选择最佳主机,优先考虑IPv4和IPv6 """ @@ -1019,7 +1053,9 @@ def _select_best_hosts(self, valid_results: List[Tuple[str, float]]) -> List[Tup for ip, latency in ipv4_results: best_hosts.append((ip, latency)) selected_count += 1 - if (ipv6_results and selected_count >= 1) or selected_count >= self.hosts_num: + if ( + ipv6_results and selected_count >= 1 + ) or selected_count >= self.hosts_num: break # 再选择IPv6 @@ -1030,20 +1066,31 @@ def _select_best_hosts(self, valid_results: List[Tuple[str, float]]) -> List[Tup return best_hosts - def _print_results(self, best_hosts: List[Tuple[str, float]], latency_limit: int, start_time: datetime): + def _print_results( + self, + best_hosts: List[Tuple[str, float]], + latency_limit: int, + start_time: datetime, + ): """ 打印结果的方法 """ - rprint(f"[bold yellow]最快的 DNS主机 IP(优先选择 IPv6) 丨 延迟 < { - latency_limit:.0f}ms :[/bold yellow]") + rprint( + f"[bold yellow]最快的 DNS主机 IP(优先选择 IPv6) 丨 延迟 < { + latency_limit:.0f}ms :[/bold yellow]" + ) for ip, time in best_hosts: - rprint(f" [green]{ - ip}[/green] [bright_black]{time:.2f} ms[/bright_black]") + rprint( + f" [green]{ + ip}[/green] [bright_black]{time:.2f} ms[/bright_black]" + ) end_time = datetime.now() total_time = end_time - start_time rprint( - f"[bold]运行时间:[/bold] [cyan]{total_time.total_seconds():.2f} 秒[/cyan]") + f"[bright_black]- 运行时间:[/bright_black] [cyan]{ + total_time.total_seconds():.2f} 秒[/cyan]" + ) # -------------------- Hosts文件管理 -------------------- # @@ -1758,55 +1805,25 @@ class Config: "international": [ # 国际 DNS 服务器 # 第 1 梯队: 延迟较低 - { - "ip": "208.67.222.222", # Open DNS - "provider": "OpenDNS", - "type": "ipv4" - }, - { - "ip": "2620:0:ccc::2", # Open DNS - "provider": "OpenDNS", - "type": "ipv6" - }, + {"ip": "208.67.222.222", "provider": "OpenDNS", + "type": "ipv4"}, # Open DNS + {"ip": "2620:0:ccc::2", "provider": "OpenDNS", + "type": "ipv6"}, # Open DNS { "ip": "2001:4860:4860::8888", # Google Public DNS "provider": "Google", - "type": "ipv6" + "type": "ipv6", }, { "ip": "2001:4860:4860::8844", # Google Public DNS "provider": "Google", - "type": "ipv6" - }, - { - "ip": "210.184.24.65", - "provider": "CPC HK", - "type": "ipv4" - }, - { - "ip": "118.201.189.90", - "provider": "SingNet", # 新加坡 - "type": "ipv4" - }, - { - "ip": "1.228.180.5", - "provider": "SK Broadband ", # 韩国 - "type": "ipv4" - }, - { - "ip": "183.99.33.6", - "provider": "Korea Telecom ", # 韩国 - "type": "ipv4" + "type": "ipv6", }, - { - "ip": "203.248.252.2", - "provider": "LG DACOM ", # 韩国 - "type": "ipv4" - }, - - - - + {"ip": "210.184.24.65", "provider": "CPC HK", "type": "ipv4"}, + {"ip": "118.201.189.90", "provider": "SingNet", "type": "ipv4"}, # 新加坡 + {"ip": "1.228.180.5", "provider": "SK Broadband ", "type": "ipv4"}, # 韩国 + {"ip": "183.99.33.6", "provider": "Korea Telecom ", "type": "ipv4"}, # 韩国 + {"ip": "203.248.252.2", "provider": "LG DACOM ", "type": "ipv4"}, # 韩国 # 第 2 梯队:延迟适中 # { # "ip": "129.250.35.250", @@ -1818,16 +1835,11 @@ class Config: # "provider": "KT DNS", # 韩国 # "type": "ipv4" # }, - - - - # { # "ip": "101.110.50.106", # "provider": "Soft Bank", # "type": "ipv4" # }, - # { # "ip": "202.175.86.206", # "provider": "Telecomunicacoes de Macau", #澳门 @@ -1838,20 +1850,16 @@ class Config: # "provider": "Informacoes Tecnologia de Macau", #澳门 # "type": "ipv4" # }, - - # { # "ip": "2400:6180:0:d0::5f6e:4001", # "provider": "DigitalOcean", # 新加坡 # "type": "ipv6" # }, - # { # "ip": "2a09::", # DNS.SB 德国 2a11:: # "provider": "DNS.SB", # "type": "ipv6" # }, - # { # "ip": "185.222.222.222", # DNS.SB 德国45.11.45.11 # "provider": "DNS.SB", @@ -1862,8 +1870,6 @@ class Config: # "provider": "Quad9", # "type": "ipv4" # }, - - # { # "ip": "149.112.112.112", # Quad9 DNS # "provider": "Quad9", @@ -1909,7 +1915,6 @@ class Config: # "provider": "CIRA Canadian Shield",# 加拿大 # "type": "ipv6" # }, - # { # "ip": "77.88.8.1", # "provider": "Yandex DNS",# 俄国 @@ -1925,18 +1930,8 @@ class Config: # 国内 DNS 服务器 # 第 1 梯队:正确解析Google翻译 # 首选:延迟较低,相对稳定: - { - "ip": "114.114.114.114", # 114 DNS - "provider": "114DNS", - "type": "ipv4" - }, - { - "ip": "1.1.8.8", # 中国联通 - "provider": "China Unicom", - "type": "ipv4" - }, - - + {"ip": "114.114.114.114", "provider": "114DNS", "type": "ipv4"}, # 114 DNS + {"ip": "1.1.8.8", "provider": "China Unicom", "type": "ipv4"}, # 中国联通 # 备选:延迟一般: # { # "ip": "180.76.76.76", # 百度 @@ -1953,16 +1948,12 @@ class Config: # "provider": "Shanghai Communications", # "type": "ipv4" # },240c::6644 - - - # 第 2 梯队:无法正确解析Google翻译 # { # "ip": "223.5.5.5", # 阿里云 DNS # "provider": "Alibaba", # "type": "ipv4" # }, - # { # "ip": "2400:3200::1", # 阿里云 DNS # "provider": "Alibaba",