ai-ipcheck 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ __pycache__/
2
+ dist/
3
+ build/
4
+ *.egg-info/
5
+ *.egg
6
+ .DS_Store
7
+ .env
8
+ .env.*
9
+ .vscode/
10
+ .idea/
11
+ .claude/
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,93 @@
1
+ Metadata-Version: 2.4
2
+ Name: ai-ipcheck
3
+ Version: 0.1.0
4
+ Summary: A lightweight diagnostic tool for AI developers to verify network environment compatibility and IP reputation.
5
+ Project-URL: Repository, https://github.com/stormzhang/ip_check
6
+ Project-URL: Issues, https://github.com/stormzhang/ip_check/issues
7
+ Author-email: stormzhang <stormzhang.dev@gmail.com>
8
+ License: MIT
9
+ License-File: LICENSE
10
+ Keywords: ai,claude,diagnostic,ip-check,network,openai
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: System :: Networking
20
+ Requires-Python: >=3.10
21
+ Requires-Dist: colorama; sys_platform == 'win32'
22
+ Requires-Dist: requests>=2.28.0
23
+ Requires-Dist: tzdata; sys_platform == 'win32'
24
+ Description-Content-Type: text/markdown
25
+
26
+ # ipcheck
27
+
28
+ 网络环境诊断工具,一键检测 IP、DNS、代理、风控、时区,确保 AI 工具流畅运行。
29
+
30
+ [English](./README_EN.md)
31
+
32
+ ![screenshot](./screenshot.png)
33
+
34
+ ## 为什么需要这个工具
35
+
36
+ 想让 Claude Code、OpenAI API、Cursor 等 AI 工具流畅稳定运行,网络环境配置至关重要。以下问题可能影响使用体验:
37
+
38
+ - **IPv6 泄露真实地址** — 代理通常只处理 IPv4,IPv6 会暴露你的实际位置
39
+ - **DNS 泄露** — 使用国内 DNS 会暴露真实地理位置
40
+ - **IP 风险过高** — 机房 IP 或被滥用的 IP 可能影响连接质量
41
+ - **时区不一致** — 本地时区配置与 IP 所在地不匹配
42
+
43
+ `ipcheck` 一键检测这些问题,确保你的 AI 工具流畅稳定运行,尤其 Claude 启动之前先检测下,原因你懂得,规避封号。
44
+
45
+ ## 功能
46
+
47
+ | 检测项 | 说明 |
48
+ |--------|------|
49
+ | 局域网 IP / IPv6 | 检测本机 IP,确认 IPv6 是否已禁用 |
50
+ | DNS 服务器 | 识别 DNS 来源(国内/国外),标注已知 DNS 服务商 |
51
+ | 公网 IP 信息 | 出口 IP、国家、地区、ISP、运营商 |
52
+ | 代理检测 | 环境变量代理配置、IP 是否被标记为代理 |
53
+ | IP 类型 | 住宅 IP / 机房 IP 识别 |
54
+ | IP 风险评分 | 通过 proxycheck.io 查询风险分数 |
55
+ | 滥用记录 | 通过 StopForumSpam 查询 IP 是否被举报 |
56
+ | 时区一致性 | 对比本地 CLI 时区与公网 IP 所在时区是否匹配 |
57
+
58
+ ## 安装
59
+
60
+ ```bash
61
+ pip install ai-ipcheck
62
+ ```
63
+
64
+ 升级到最新版:
65
+
66
+ ```bash
67
+ pip install --upgrade ipcheck
68
+ ```
69
+
70
+ ## 使用
71
+
72
+ ```bash
73
+ ipcheck
74
+ ```
75
+
76
+ ### 环境要求
77
+
78
+ - Python 3.10+
79
+ - 支持 macOS / Linux / Windows
80
+
81
+ ## 结果说明
82
+
83
+ **局域网 & DNS** — IPv6 建议禁用,大部分代理不处理 IPv6 流量,开启后可能同时暴露两个不同地区的 IP 地址。如果检测到国内 DNS,需要在代理软件中调整 DNS 设置。
84
+
85
+ **公网 IP 信息** — 显示经过代理后的出口 IP、所在国家/地区、ISP 和时区。这些信息直接影响 AI 服务对你请求来源的判断。
86
+
87
+ **IP 风险评估** — 检测 IP 是住宅还是机房类型。机房 IP 不一定有问题,但会进一步查询风险评分和滥用记录。如果风险评分偏高,建议更换节点。
88
+
89
+ **时区一致性** — 对比本地 `$TZ` 环境变量(或系统时区)与公网 IP 所在时区。保持一致可以获得更好的服务体验。建议在 shell 配置中设置 `TZ` 为与 IP 所在地匹配的 IANA 时区(如 `America/Los_Angeles`)。
90
+
91
+ ## License
92
+
93
+ [MIT](LICENSE) © 2026 stormzhang
@@ -0,0 +1,68 @@
1
+ # ipcheck
2
+
3
+ 网络环境诊断工具,一键检测 IP、DNS、代理、风控、时区,确保 AI 工具流畅运行。
4
+
5
+ [English](./README_EN.md)
6
+
7
+ ![screenshot](./screenshot.png)
8
+
9
+ ## 为什么需要这个工具
10
+
11
+ 想让 Claude Code、OpenAI API、Cursor 等 AI 工具流畅稳定运行,网络环境配置至关重要。以下问题可能影响使用体验:
12
+
13
+ - **IPv6 泄露真实地址** — 代理通常只处理 IPv4,IPv6 会暴露你的实际位置
14
+ - **DNS 泄露** — 使用国内 DNS 会暴露真实地理位置
15
+ - **IP 风险过高** — 机房 IP 或被滥用的 IP 可能影响连接质量
16
+ - **时区不一致** — 本地时区配置与 IP 所在地不匹配
17
+
18
+ `ipcheck` 一键检测这些问题,确保你的 AI 工具流畅稳定运行,尤其 Claude 启动之前先检测下,原因你懂得,规避封号。
19
+
20
+ ## 功能
21
+
22
+ | 检测项 | 说明 |
23
+ |--------|------|
24
+ | 局域网 IP / IPv6 | 检测本机 IP,确认 IPv6 是否已禁用 |
25
+ | DNS 服务器 | 识别 DNS 来源(国内/国外),标注已知 DNS 服务商 |
26
+ | 公网 IP 信息 | 出口 IP、国家、地区、ISP、运营商 |
27
+ | 代理检测 | 环境变量代理配置、IP 是否被标记为代理 |
28
+ | IP 类型 | 住宅 IP / 机房 IP 识别 |
29
+ | IP 风险评分 | 通过 proxycheck.io 查询风险分数 |
30
+ | 滥用记录 | 通过 StopForumSpam 查询 IP 是否被举报 |
31
+ | 时区一致性 | 对比本地 CLI 时区与公网 IP 所在时区是否匹配 |
32
+
33
+ ## 安装
34
+
35
+ ```bash
36
+ pip install ai-ipcheck
37
+ ```
38
+
39
+ 升级到最新版:
40
+
41
+ ```bash
42
+ pip install --upgrade ipcheck
43
+ ```
44
+
45
+ ## 使用
46
+
47
+ ```bash
48
+ ipcheck
49
+ ```
50
+
51
+ ### 环境要求
52
+
53
+ - Python 3.10+
54
+ - 支持 macOS / Linux / Windows
55
+
56
+ ## 结果说明
57
+
58
+ **局域网 & DNS** — IPv6 建议禁用,大部分代理不处理 IPv6 流量,开启后可能同时暴露两个不同地区的 IP 地址。如果检测到国内 DNS,需要在代理软件中调整 DNS 设置。
59
+
60
+ **公网 IP 信息** — 显示经过代理后的出口 IP、所在国家/地区、ISP 和时区。这些信息直接影响 AI 服务对你请求来源的判断。
61
+
62
+ **IP 风险评估** — 检测 IP 是住宅还是机房类型。机房 IP 不一定有问题,但会进一步查询风险评分和滥用记录。如果风险评分偏高,建议更换节点。
63
+
64
+ **时区一致性** — 对比本地 `$TZ` 环境变量(或系统时区)与公网 IP 所在时区。保持一致可以获得更好的服务体验。建议在 shell 配置中设置 `TZ` 为与 IP 所在地匹配的 IANA 时区(如 `America/Los_Angeles`)。
65
+
66
+ ## License
67
+
68
+ [MIT](LICENSE) © 2026 stormzhang
@@ -0,0 +1,62 @@
1
+ # ipcheck
2
+
3
+ A lightweight diagnostic tool for AI developers to verify network environment compatibility and IP reputation.
4
+
5
+ [中文](./README.md)
6
+
7
+ ![screenshot](./screenshot.png)
8
+
9
+ ## Why
10
+
11
+ To ensure AI tools like Claude Code, OpenAI API, and Cursor run smoothly and reliably, a properly configured network environment is essential. Common issues that may affect performance:
12
+
13
+ - **IPv6 leaking real location** — Most proxies only handle IPv4; IPv6 can expose your actual geographic location
14
+ - **DNS leakage** — Local DNS servers can reveal your true location to AI services
15
+ - **High-risk IP** — Datacenter IPs or abused IPs may affect connection quality
16
+ - **Timezone mismatch** — Inconsistency between local timezone and IP geolocation
17
+
18
+ `ipcheck` detects all these issues in one run, ensuring your AI tools run smoothly and stably.
19
+
20
+ ## Features
21
+
22
+ | Check Item | Description |
23
+ |------------|-------------|
24
+ | LAN IP / IPv6 | Detect local IP, verify if IPv6 is disabled |
25
+ | DNS Servers | Identify DNS origin (domestic/foreign), label known DNS providers |
26
+ | Public IP Info | Exit IP, country, region, ISP, organization |
27
+ | Proxy Detection | Env proxy settings, whether IP is flagged as proxy |
28
+ | IP Type | Residential vs. datacenter IP identification |
29
+ | IP Risk Score | Risk scoring via proxycheck.io |
30
+ | Abuse Records | IP abuse lookup via StopForumSpam |
31
+ | Timezone Consistency | Compare local CLI timezone with public IP geolocation timezone |
32
+
33
+ ## Install
34
+
35
+ ```bash
36
+ pip install ai-ipcheck
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```bash
42
+ ipcheck
43
+ ```
44
+
45
+ ### Requirements
46
+
47
+ - Python 3.10+
48
+ - macOS / Linux / Windows
49
+
50
+ ## Understanding the Results
51
+
52
+ **LAN & DNS** — Disable IPv6 if possible. Most proxies don't handle IPv6 traffic, which may expose two IPs from different regions simultaneously. If a domestic DNS is detected, adjust DNS settings in your proxy software.
53
+
54
+ **Public IP Info** — Shows your exit IP after proxy, including country/region, ISP, and timezone. These directly affect how AI services evaluate your request origin.
55
+
56
+ **IP Risk Assessment** — Identifies whether your IP is residential or datacenter. Datacenter IPs aren't necessarily problematic, but the tool will query risk scores and abuse records. Switch nodes if your risk score is high.
57
+
58
+ **Timezone Consistency** — Compares your local `$TZ` environment variable (or system timezone) with the public IP's timezone. Keeping them consistent ensures a better service experience. Set `TZ` in your shell config to match your IP's IANA timezone (e.g., `America/Los_Angeles`).
59
+
60
+ ## License
61
+
62
+ [MIT](LICENSE) © 2026 stormzhang
@@ -0,0 +1,39 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "ai-ipcheck"
7
+ version = "0.1.0"
8
+ description = "A lightweight diagnostic tool for AI developers to verify network environment compatibility and IP reputation."
9
+ readme = "README.md"
10
+ license = {text = "MIT"}
11
+ authors = [{name = "stormzhang", email = "stormzhang.dev@gmail.com"}]
12
+ requires-python = ">=3.10"
13
+ keywords = ["ip-check", "network", "ai", "claude", "openai", "diagnostic"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Intended Audience :: Developers",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3.10",
20
+ "Programming Language :: Python :: 3.11",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Programming Language :: Python :: 3.13",
23
+ "Topic :: System :: Networking",
24
+ ]
25
+ dependencies = [
26
+ "requests>=2.28.0",
27
+ "colorama; sys_platform == 'win32'",
28
+ "tzdata; sys_platform == 'win32'",
29
+ ]
30
+
31
+ [tool.hatch.build.targets.wheel]
32
+ packages = ["src/ipcheck"]
33
+
34
+ [project.scripts]
35
+ ipcheck = "ipcheck.cli:main"
36
+
37
+ [project.urls]
38
+ Repository = "https://github.com/stormzhang/ip_check"
39
+ Issues = "https://github.com/stormzhang/ip_check/issues"
Binary file
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0"
@@ -0,0 +1,3 @@
1
+ from ipcheck.cli import main
2
+
3
+ main()
@@ -0,0 +1,444 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ ipcheck — 网络环境诊断工具
4
+ 检测本机 IP、IPv6、DNS、公网信息、代理状态、时区
5
+ 支持 macOS / Linux / Windows
6
+ """
7
+
8
+ import socket
9
+ import ipaddress
10
+ import os
11
+ import sys
12
+ import subprocess
13
+ import datetime
14
+ import re
15
+ import platform
16
+
17
+ import requests
18
+
19
+ try:
20
+ from zoneinfo import ZoneInfo as _ZI
21
+ except ImportError:
22
+ _ZI = None
23
+
24
+ # ── 编码修正(Windows cmd 默认非 UTF-8)────────────────────
25
+ if sys.stdout.encoding and sys.stdout.encoding.lower() not in ('utf-8', 'utf8'):
26
+ try:
27
+ sys.stdout.reconfigure(encoding='utf-8', errors='replace')
28
+ except AttributeError:
29
+ pass
30
+
31
+ IS_WIN = platform.system() == 'Windows'
32
+
33
+
34
+ # ── 已知 DNS ──────────────────────────────────────────────
35
+ KNOWN_DNS = {
36
+ '1.1.1.1': 'Cloudflare (US)',
37
+ '1.0.0.1': 'Cloudflare (US)',
38
+ '1.1.1.2': 'Cloudflare for Families (US)',
39
+ '1.0.0.2': 'Cloudflare for Families (US)',
40
+ '1.1.1.3': 'Cloudflare for Families (US)',
41
+ '1.0.0.3': 'Cloudflare for Families (US)',
42
+ '8.8.8.8': 'Google Public DNS (US)',
43
+ '8.8.4.4': 'Google Public DNS (US)',
44
+ '9.9.9.9': 'Quad9 (US)',
45
+ '149.112.112.112': 'Quad9 (US)',
46
+ '208.67.222.222': 'OpenDNS/Cisco (US)',
47
+ '208.67.220.220': 'OpenDNS/Cisco (US)',
48
+ '223.5.5.5': 'AliDNS 阿里 (CN)',
49
+ '223.6.6.6': 'AliDNS 阿里 (CN)',
50
+ '119.29.29.29': 'DNSPod 腾讯 (CN)',
51
+ '182.254.116.116': 'DNSPod 腾讯 (CN)',
52
+ '114.114.114.114': '114DNS (CN)',
53
+ '114.114.115.115': '114DNS (CN)',
54
+ '180.76.76.76': 'BaiduDNS 百度 (CN)',
55
+ '1.2.4.8': 'CNNIC (CN)',
56
+ '210.2.4.8': 'CNNIC (CN)',
57
+ '94.140.14.14': 'AdGuard (CY)',
58
+ '94.140.15.15': 'AdGuard (CY)',
59
+ '185.228.168.9': 'CleanBrowsing (US)',
60
+ '185.228.169.9': 'CleanBrowsing (US)',
61
+ '76.76.2.0': 'Alternate DNS (US)',
62
+ '76.76.10.0': 'Alternate DNS (US)',
63
+ }
64
+
65
+
66
+ def dns_label(ip):
67
+ if ip in KNOWN_DNS:
68
+ return f"{ip} {KNOWN_DNS[ip]}"
69
+ try:
70
+ if ipaddress.ip_address(ip).is_private:
71
+ return f"{ip} 局域网路由器"
72
+ except Exception:
73
+ pass
74
+ return ip
75
+
76
+
77
+ def make_zone(name):
78
+ if not _ZI or not name:
79
+ return None
80
+ try:
81
+ return _ZI(name)
82
+ except Exception:
83
+ return None
84
+
85
+
86
+ def _val(v, fallback="未知"):
87
+ return v if v else warn(fallback)
88
+
89
+
90
+ # ── 颜色 ─────────────────────────────────────────────────
91
+ def _init_color():
92
+ if IS_WIN:
93
+ try:
94
+ import colorama
95
+ colorama.init()
96
+ return True
97
+ except ImportError:
98
+ pass
99
+ try:
100
+ import ctypes
101
+ h = ctypes.windll.kernel32.GetStdHandle(-11)
102
+ m = ctypes.c_ulong()
103
+ ctypes.windll.kernel32.GetConsoleMode(h, ctypes.byref(m))
104
+ ctypes.windll.kernel32.SetConsoleMode(h, m.value | 0x0004)
105
+ return True
106
+ except Exception:
107
+ return False
108
+ return True
109
+
110
+ _COLOR = _init_color()
111
+
112
+
113
+ class C:
114
+ RESET = "\033[0m" if _COLOR else ""
115
+ BOLD = "\033[1m" if _COLOR else ""
116
+ RED = "\033[91m" if _COLOR else ""
117
+ GREEN = "\033[92m" if _COLOR else ""
118
+ YELLOW = "\033[93m" if _COLOR else ""
119
+ CYAN = "\033[96m" if _COLOR else ""
120
+ GRAY = "\033[90m" if _COLOR else ""
121
+
122
+ ANSI_RE = re.compile(r'\033\[[0-9;]*m')
123
+
124
+
125
+ def char_width(c):
126
+ cp = ord(c)
127
+ if (0x2E80 <= cp <= 0x303E or 0x3040 <= cp <= 0x33FF or
128
+ 0x3400 <= cp <= 0x4DBF or 0x4E00 <= cp <= 0x9FFF or
129
+ 0xAC00 <= cp <= 0xD7AF or 0xF900 <= cp <= 0xFAFF or
130
+ 0xFE30 <= cp <= 0xFE6F or 0xFF00 <= cp <= 0xFF60 or
131
+ 0x20000 <= cp <= 0x2FFFD):
132
+ return 2
133
+ return 1
134
+
135
+
136
+ def display_len(s):
137
+ return sum(char_width(c) for c in ANSI_RE.sub('', s))
138
+
139
+
140
+ def ok(v): return f"{C.GREEN}{v}{C.RESET}"
141
+ def warn(v): return f"{C.YELLOW}{v}{C.RESET}"
142
+ def bad(v): return f"{C.RED}{v}{C.RESET}"
143
+
144
+
145
+ # ── 表格渲染 ──────────────────────────────────────────────
146
+ COL_LABEL, COL_VALUE = 18, 46
147
+
148
+ def tbl_top(): print(f" ╔{'═'*(COL_LABEL+2)}╤{'═'*(COL_VALUE+2)}╗")
149
+ def tbl_sep(): print(f" ╠{'═'*(COL_LABEL+2)}╪{'═'*(COL_VALUE+2)}╣")
150
+ def tbl_bot(): print(f" ╚{'═'*(COL_LABEL+2)}╧{'═'*(COL_VALUE+2)}╝")
151
+
152
+
153
+ def tbl_row(label, value):
154
+ value = str(value)
155
+ lpad = ' ' * max(0, COL_LABEL - display_len(label))
156
+ vpad = ' ' * max(0, COL_VALUE - display_len(value))
157
+ lstr = f"{label}{lpad}" if label else ' ' * COL_LABEL
158
+ print(f" ║ {lstr} │ {value}{vpad} ║")
159
+
160
+
161
+ # ── 数据采集 ─────────────────────────────────────────────
162
+ def get_lan_ip():
163
+ try:
164
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
165
+ s.connect(("8.8.8.8", 80))
166
+ ip = s.getsockname()[0]
167
+ s.close()
168
+ return ip
169
+ except Exception:
170
+ return warn("获取失败")
171
+
172
+
173
+ def get_ipv6():
174
+ try:
175
+ s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
176
+ s.connect(("2001:4860:4860::8888", 80))
177
+ ip = s.getsockname()[0]
178
+ s.close()
179
+ if ip and ip not in ('', '::'):
180
+ return ip
181
+ except Exception:
182
+ pass
183
+ return warn("已禁用")
184
+
185
+
186
+ def get_dns_servers():
187
+ servers = []
188
+ if IS_WIN:
189
+ try:
190
+ r = subprocess.run(
191
+ ['powershell', '-NoProfile', '-Command',
192
+ 'Get-DnsClientServerAddress -AddressFamily IPv4 | '
193
+ 'Select-Object -ExpandProperty ServerAddresses'],
194
+ capture_output=True, text=True, timeout=5, encoding='utf-8',
195
+ )
196
+ seen = set()
197
+ for line in r.stdout.splitlines():
198
+ ip = line.strip()
199
+ if not ip:
200
+ continue
201
+ try:
202
+ ipaddress.ip_address(ip)
203
+ if ip not in seen:
204
+ seen.add(ip)
205
+ servers.append(ip)
206
+ except ValueError:
207
+ pass
208
+ except Exception:
209
+ pass
210
+ else:
211
+ try:
212
+ seen = set()
213
+ with open('/etc/resolv.conf') as f:
214
+ for line in f:
215
+ if line.strip().startswith('nameserver'):
216
+ ip = line.split()[1]
217
+ if ip not in seen:
218
+ seen.add(ip)
219
+ servers.append(ip)
220
+ except Exception:
221
+ pass
222
+ if not servers:
223
+ try:
224
+ r = subprocess.run(
225
+ ['scutil', '--dns'], capture_output=True, text=True, timeout=3,
226
+ )
227
+ seen = set()
228
+ for line in r.stdout.splitlines():
229
+ line = line.strip()
230
+ if line.startswith('nameserver['):
231
+ ip = line.split(':', 1)[1].strip()
232
+ if ip not in seen:
233
+ seen.add(ip)
234
+ servers.append(ip)
235
+ except Exception:
236
+ pass
237
+ return servers
238
+
239
+
240
+ def get_public_info():
241
+ try:
242
+ resp = requests.get(
243
+ "http://ip-api.com/json/",
244
+ params={"fields": "status,message,country,regionName,city,isp,org,proxy,hosting,query,timezone"},
245
+ timeout=6,
246
+ )
247
+ return resp.json()
248
+ except Exception as e:
249
+ return {"status": "fail", "message": str(e)}
250
+
251
+
252
+ def get_ip_risk(ip):
253
+ try:
254
+ resp = requests.get(
255
+ f"https://proxycheck.io/v2/{ip}",
256
+ params={"risk": 1, "vpn": 1, "asn": 1},
257
+ timeout=6,
258
+ )
259
+ data = resp.json().get(ip, {})
260
+ risk = data.get("risk")
261
+ itype = data.get("type", "")
262
+ proxy = data.get("proxy", "")
263
+ parts = []
264
+ if risk is not None:
265
+ score = int(risk)
266
+ if score < 30:
267
+ color, level = C.GREEN, "低风险"
268
+ elif score < 70:
269
+ color, level = C.YELLOW, "中风险"
270
+ else:
271
+ color, level = C.RED, "高风险"
272
+ parts.append(f"{color}{score}/100 {level}{C.RESET}")
273
+ if itype:
274
+ parts.append(f"类型 {itype}")
275
+ if proxy == "yes":
276
+ parts.append(bad("已标记为代理"))
277
+ return " ".join(parts) if parts else warn("暂无数据")
278
+ except Exception as e:
279
+ return warn(f"查询失败({e})")
280
+
281
+
282
+ def get_stopforumspam(ip):
283
+ try:
284
+ resp = requests.get(
285
+ "https://api.stopforumspam.org/api",
286
+ params={"json": 1, "ip": ip},
287
+ timeout=6,
288
+ )
289
+ data = resp.json().get("ip", {})
290
+ if not data.get("appears"):
291
+ return [ok("未收录 低风险 ✓")]
292
+ confidence = float(data.get("confidence", 0))
293
+ frequency = int(data.get("frequency", 0))
294
+ last_seen = (data.get("lastseen") or "")[:10]
295
+ if confidence < 30:
296
+ color, level = C.GREEN, "低风险"
297
+ elif confidence < 70:
298
+ color, level = C.YELLOW, "中风险"
299
+ else:
300
+ color, level = C.RED, "高风险"
301
+ lines = [f"{color}{confidence:.1f}/100 {level}{C.RESET} 举报 {frequency} 次"]
302
+ if last_seen:
303
+ lines.append(f"最近举报 {last_seen}")
304
+ return lines
305
+ except Exception as e:
306
+ return [warn(f"查询失败({e})")]
307
+
308
+
309
+ def get_proxy_envs():
310
+ seen = {}
311
+ for key in ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY",
312
+ "http_proxy", "https_proxy", "all_proxy"]:
313
+ val = os.environ.get(key)
314
+ if val and val not in seen.values():
315
+ seen[key.upper()] = val
316
+ return seen
317
+
318
+
319
+ def _utc_str(offset):
320
+ total = int(offset.total_seconds())
321
+ h, r = divmod(abs(total), 3600)
322
+ sign = "+" if total >= 0 else "-"
323
+ return f"UTC{sign}{h:02d}:{r//60:02d}"
324
+
325
+
326
+ def get_cli_tz_name():
327
+ tz_env = os.environ.get('TZ', '')
328
+ if tz_env:
329
+ return tz_env, True
330
+
331
+ if IS_WIN:
332
+ try:
333
+ r = subprocess.run(
334
+ ['powershell', '-NoProfile', '-Command',
335
+ '[System.TimeZoneInfo]::Local.Id'],
336
+ capture_output=True, text=True, timeout=3, encoding='utf-8',
337
+ )
338
+ win_id = r.stdout.strip()
339
+ if win_id:
340
+ return win_id, False
341
+ except Exception:
342
+ pass
343
+
344
+ name = datetime.datetime.now().astimezone().tzname() or "Unknown"
345
+ return name, False
346
+
347
+
348
+ # ── 主程序 ────────────────────────────────────────────────
349
+ def main():
350
+ if len(sys.argv) > 1 and sys.argv[1] in ('--version', '-v', '-V'):
351
+ from ipcheck import __version__
352
+ print(f"ipcheck {__version__}")
353
+ return
354
+
355
+ pub = get_public_info()
356
+
357
+ print(f"\n {C.BOLD}ipcheck — 网络环境诊断工具{C.RESET} "
358
+ f"{C.GRAY}({platform.system()} / Python {platform.python_version()}){C.RESET}\n")
359
+ tbl_top()
360
+
361
+ # 本机网络
362
+ tbl_row("局域网 IP", get_lan_ip())
363
+ tbl_row("IPv6 地址", get_ipv6())
364
+ dns = get_dns_servers()
365
+ if dns:
366
+ tbl_row("DNS 服务器", dns_label(dns[0]))
367
+ for d in dns[1:]:
368
+ tbl_row("", dns_label(d))
369
+ else:
370
+ tbl_row("DNS 服务器", warn("获取失败"))
371
+
372
+ tbl_sep()
373
+
374
+ # 公网信息
375
+ if pub.get("status") == "success":
376
+ pub_ip = pub.get("query")
377
+ tbl_row("公网 IP", pub_ip or bad("获取失败"))
378
+ tbl_row("国家 / 省份", f"{_val(pub.get('country'))} / {_val(pub.get('regionName'))}")
379
+ tbl_row("城市", _val(pub.get("city")))
380
+ tbl_row("ISP(互联网服务商)", _val(pub.get("isp")))
381
+ tbl_row("组织", _val(pub.get("org")))
382
+ pub_tz_name = pub.get("timezone")
383
+ if pub_tz_name:
384
+ zi = make_zone(pub_tz_name)
385
+ if zi:
386
+ off = datetime.datetime.now(zi).utcoffset()
387
+ tbl_row("所处时区", f"{pub_tz_name} ({_utc_str(off)})")
388
+ else:
389
+ tbl_row("所处时区", pub_tz_name)
390
+ else:
391
+ tbl_row("所处时区", _val(None))
392
+ else:
393
+ tbl_row("公网请求", bad(pub.get("message") or "未知错误"))
394
+
395
+ tbl_sep()
396
+
397
+ # 代理检测
398
+ proxy_envs = get_proxy_envs()
399
+ if proxy_envs:
400
+ for k, v in proxy_envs.items():
401
+ tbl_row(k, warn(v))
402
+ else:
403
+ tbl_row("环境变量代理", ok("未设置"))
404
+ if pub.get("status") == "success":
405
+ tbl_row("IP 标记为代理", bad("是 ✗") if pub.get("proxy") else ok("否 ✓"))
406
+ tbl_row("机房 / 托管", bad("是 ✗") if pub.get("hosting") else ok("否 ✓"))
407
+ if (pub.get("hosting") or pub.get("proxy")) and pub_ip:
408
+ tbl_row("IP 风险查询", get_ip_risk(pub_ip))
409
+ spam_lines = get_stopforumspam(pub_ip)
410
+ tbl_row("垃圾滥用记录", spam_lines[0])
411
+ for line in spam_lines[1:]:
412
+ tbl_row("", line)
413
+
414
+ tbl_sep()
415
+
416
+ # 时区
417
+ cli_dt = datetime.datetime.now().astimezone()
418
+ cli_offset = cli_dt.utcoffset()
419
+ tz_name, is_iana = get_cli_tz_name()
420
+ tbl_row("CLI 时区", f"{tz_name} ({_utc_str(cli_offset)})")
421
+
422
+ pub_tz_name = pub.get("timezone") if pub.get("status") == "success" else None
423
+ if pub_tz_name:
424
+ pub_zi = make_zone(pub_tz_name)
425
+ pub_offset = datetime.datetime.now(pub_zi).utcoffset() if pub_zi else None
426
+
427
+ if is_iana:
428
+ match = ok("一致 ✓") if tz_name == pub_tz_name else bad("不一致 ✗")
429
+ elif pub_offset is not None:
430
+ if cli_offset == pub_offset:
431
+ match = warn("UTC 偏移一致(建议设置 $TZ=IANA 名称精确比对)")
432
+ else:
433
+ match = bad("不一致 ✗(UTC 偏移不同)")
434
+ else:
435
+ match = warn("无法比对(tzdata 未安装?pip install tzdata)")
436
+ tbl_row("时区一致性", match)
437
+
438
+ tbl_bot()
439
+
440
+ if IS_WIN and _ZI is None:
441
+ print(f"\n {C.YELLOW}提示:pip install tzdata (Windows 时区精确比对所需){C.RESET}")
442
+ if IS_WIN and not _COLOR:
443
+ print(f"\n 提示:pip install colorama (启用彩色输出)")
444
+ print()