-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipyou.ps1
More file actions
104 lines (94 loc) · 3.59 KB
/
Copy pathipyou.ps1
File metadata and controls
104 lines (94 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<#
.SYNOPSIS
ipyou.net IP 查询工具(Windows PowerShell / PowerShell 7)
.DESCRIPTION
查本机或指定 IP 的归属地、运营商、ASN、类型、风控值与适用场景。
无需注册、无需 API Key。
.EXAMPLE
.\ipyou.ps1 # 本机 IP 详情
.EXAMPLE
.\ipyou.ps1 -Quiet # 只输出 IP
.EXAMPLE
.\ipyou.ps1 1.1.1.1 # 查指定 IP
.EXAMPLE
.\ipyou.ps1 1.1.1.1 -Json # 返回对象(可继续用 . 取字段)
.EXAMPLE
.\ipyou.ps1 8.8.8.8,1.1.1.1 -Table # 多个 IP,表格输出
.EXAMPLE
.\ipyou.ps1 -Watch 10 # 每 10 秒盯出口 IP,变化时提示
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)] [string[]] $IP,
[switch] $Quiet,
[switch] $Json,
[switch] $Table,
[int] $Watch = 0
)
$Base = if ($env:IPYOU_BASE) { $env:IPYOU_BASE } else { "https://ipyou.net" }
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# 站点对外只有两个地址:/(查自己) 与 /<IP>(查指定 IP)。纯文本直接取。
function Get-IpyouText([string]$path) {
# Accept: text/plain 显式要纯文本(不依赖服务端的 User-Agent 判定)
(Invoke-WebRequest -Uri "$Base/$path" -UseBasicParsing -TimeoutSec 20 `
-Headers @{ "Accept" = "text/plain" } -UserAgent "ipyou.ps1 (PowerShell)").Content
}
function Get-IpyouJson([string]$path) {
# 站点只有两个接口(/ 与 /<IP>);加 Accept 头即返回 JSON
Invoke-RestMethod -Uri "$Base/$path" -TimeoutSec 20 `
-Headers @{ "Accept" = "application/json" } -UserAgent "ipyou.ps1 (PowerShell)"
}
function Get-IpyouIP {
(Get-IpyouJson "").ip
}
# --- 监控模式 ---
if ($Watch -gt 0) {
Write-Host "监控出口 IP,每 ${Watch}s 检查一次(Ctrl-C 退出)"
$last = ""
while ($true) {
try { $now = (Get-IpyouIP).Trim() } catch { $now = "" }
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
if (-not $now) {
if ($last -ne "__down__") { Write-Host "[$ts] 查询失败(网络不通或超时)" -ForegroundColor Yellow }
$last = "__down__"
} elseif ($now -ne $last) {
if (-not $last -or $last -eq "__down__") { Write-Host "[$ts] 当前出口: $now" -ForegroundColor Cyan }
else { Write-Host "[$ts] IP 变化: $last -> $now" -ForegroundColor Green }
$last = $now
}
Start-Sleep -Seconds $Watch
}
}
# --- 只取 IP ---
if ($Quiet) { (Get-IpyouIP).Trim(); return }
# --- 本机详情 ---
if (-not $IP -or $IP.Count -eq 0) {
if ($Json) { Get-IpyouJson "" } else { Get-IpyouText "" }
return
}
# --- 表格模式(多个 IP)---
# 站点不提供批量接口,这里循环调用单个接口,每次之间留间隔避免触发限流。
if ($Table) {
$rows = @()
$n = 0
foreach ($one in $IP) {
if ($n -gt 0) { Start-Sleep -Seconds 1 }
$n++
try { $d = Get-IpyouJson "$one" } catch { Write-Warning "$one 查询失败"; continue }
$rows += [pscustomobject]@{
IP = $d.ip
类型 = $d.usage_inferred.label
风控 = $d.ip_score.score
等级 = $d.ip_score.level
黑名单 = $(if ($d.blocklisted) { "命中" } else { "-" })
归属 = "$($d.geo_consensus.country_name) $($d.geo_consensus.city)".Trim()
运营商 = $(if ($d.geo.isp) { $d.geo.isp } else { $d.geo_consensus.isp })
}
}
$rows | Format-Table -AutoSize
return
}
# --- 逐个查询 ---
foreach ($one in $IP) {
if ($Json) { Get-IpyouJson "$one" } else { Get-IpyouText "$one" }
}