├── .gitattributes ├── .vscode └── launch.json ├── App.ps1 ├── Console-HostGenerator.ps1 └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Console-HostGenerator", 6 | "type": "PowerShell", 7 | "request": "launch", 8 | "script": "${workspaceFolder}/Console-HostGenerator.ps1", 9 | "args": [] 10 | } 11 | ] 12 | } -------------------------------------------------------------------------------- /App.ps1: -------------------------------------------------------------------------------- 1 | Class App { 2 | [void] Main() { 3 | $this.Welcome() 4 | $this.GenerateHost($this.GetHostPath()) 5 | $this.Closing() 6 | } 7 | 8 | hidden [void] Welcome() { 9 | Clear-Host 10 | Write-Host "Console HostGenerator 启动!" -ForegroundColor Red 11 | } 12 | 13 | hidden [string] GetHostPath() { 14 | [string] $hostPath = [string]::Empty 15 | 16 | while (-not (Test-Path $hostPath -PathType Container)) { 17 | $hostPath = (Read-Host "输入 Cealing-Host-List.json 文件保存目录路径 (默认脚本根目录)").Trim("""") 18 | 19 | if ([string]::IsNullOrWhiteSpace($hostPath)) { $hostPath = $PSScriptRoot } 20 | } 21 | 22 | return $hostPath 23 | } 24 | 25 | hidden [void] GenerateHost([string] $hostPath) { 26 | [bool] $isGeneralList = $false 27 | [string] $lastListDomain = [string]::Empty 28 | 29 | foreach ($listRule in [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String((Invoke-WebRequest "https://gitlab.com/gfwlist/gfwlist/raw/master/gfwlist.txt"))) -split "`n") { 30 | if (-not $isGeneralList) { 31 | if ($listRule.Contains("General List Start")) { 32 | Set-Content (Join-Path $hostPath "Cealing-Host-List.json") "[" 33 | 34 | $isGeneralList = $true 35 | } 36 | 37 | continue 38 | } 39 | elseif ($listRule.Contains("General List End")) { 40 | Add-Content (Join-Path $hostPath "Cealing-Host-List.json") "]" -NoNewline 41 | 42 | return 43 | } 44 | 45 | if ($listRule -notmatch "^[!@\[/]" -and -not [string]::IsNullOrWhiteSpace($listRule)) { 46 | [string] $listDomain = $listRule.Trim() -replace "^[|.*]+", [string]::Empty -replace "^https?://", [string]::Empty -replace "/.*$", [string]::Empty 47 | [int] $tryCount = 3 48 | 49 | if ($listDomain -eq $lastListDomain) { continue } 50 | else { $lastListDomain = $listDomain } 51 | 52 | while ($true) { 53 | try { 54 | [array] $hostIpAnswer = (Invoke-RestMethod "https://ns.net.kg/dns-query?name=$listDomain").Answer 55 | 56 | if ($hostIpAnswer) { Add-Content (Join-Path $hostPath "Cealing-Host-List.json") "`t[[""*$listDomain""],"""",""$($hostIpAnswer[-1].data)""]," } 57 | else { Write-Host "$listDomain 解析失败" } 58 | 59 | break 60 | } 61 | catch { 62 | if (-not $tryCount--) { 63 | Write-Host "$listDomain 解析失败" 64 | 65 | break 66 | } 67 | } 68 | } 69 | } 70 | } 71 | } 72 | 73 | hidden [void] Closing() { 74 | Write-Host "伪造规则,生出来啦!" -ForegroundColor Red 75 | } 76 | } -------------------------------------------------------------------------------- /Console-HostGenerator.ps1: -------------------------------------------------------------------------------- 1 | param ([string] $trans, [switch] $loop) 2 | 3 | do { 4 | if (Test-Path $trans) { Start-Transcript (Join-Path $trans "Trans.log") -UseMinimalHeader | Out-Null } 5 | 6 | if ($PSEdition -ne "Core") { 7 | Write-Host "该脚本需要在 PowerShell 7.x 环境运行" 8 | } 9 | else { 10 | foreach ($ps1File in Get-ChildItem $PSScriptRoot "*.ps1") { 11 | if ($ps1File.FullName -ne $PSCommandPath) { . $ps1File.FullName } 12 | } 13 | 14 | [App]::new().Main() 15 | } 16 | 17 | if (Test-Path $trans) { Stop-Transcript } 18 | if ($loop) { Start-Sleep 60 } 19 | } while ($loop) 20 | 21 | Read-Host "按回车键结束" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |