正式2
This commit is contained in:
146
check_ip.py
Normal file
146
check_ip.py
Normal file
@@ -0,0 +1,146 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
IP地址检查工具
|
||||
用于检查当前使用的IP地址和代理设置
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
def load_proxy_config():
|
||||
"""加载代理配置"""
|
||||
config_file = 'config.json'
|
||||
if os.path.exists(config_file):
|
||||
try:
|
||||
with open(config_file, 'r', encoding='utf-8') as f:
|
||||
config = json.load(f)
|
||||
return config.get('proxy')
|
||||
except:
|
||||
return None
|
||||
return None
|
||||
|
||||
def get_ip_info(use_proxy=True):
|
||||
"""获取IP信息"""
|
||||
session = requests.Session()
|
||||
|
||||
# 设置代理
|
||||
proxy_config = load_proxy_config() if use_proxy else None
|
||||
if proxy_config:
|
||||
proxy_url = f"http://{proxy_config['username']}:{proxy_config['password']}@{proxy_config['host']}:{proxy_config['port']}"
|
||||
session.proxies = {
|
||||
'http': proxy_url,
|
||||
'https': proxy_url
|
||||
}
|
||||
print(f"🌐 使用代理: {proxy_config['host']}:{proxy_config['port']}")
|
||||
else:
|
||||
print("🌐 不使用代理")
|
||||
|
||||
# IP查询服务
|
||||
services = [
|
||||
{
|
||||
'name': 'httpbin.org',
|
||||
'url': 'https://httpbin.org/ip',
|
||||
'parse': lambda r: r.json().get('origin', 'N/A')
|
||||
},
|
||||
{
|
||||
'name': 'ipify.org',
|
||||
'url': 'https://api.ipify.org?format=json',
|
||||
'parse': lambda r: r.json().get('ip', 'N/A')
|
||||
},
|
||||
{
|
||||
'name': 'ipinfo.io',
|
||||
'url': 'https://ipinfo.io/json',
|
||||
'parse': lambda r: f"{r.json().get('ip', 'N/A')} ({r.json().get('city', 'N/A')}, {r.json().get('country', 'N/A')})"
|
||||
},
|
||||
{
|
||||
'name': 'ifconfig.me',
|
||||
'url': 'https://ifconfig.me/ip',
|
||||
'parse': lambda r: r.text.strip()
|
||||
}
|
||||
]
|
||||
|
||||
results = []
|
||||
|
||||
for service in services:
|
||||
try:
|
||||
print(f"正在查询 {service['name']}...")
|
||||
response = session.get(service['url'], timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
ip_info = service['parse'](response)
|
||||
results.append({
|
||||
'service': service['name'],
|
||||
'ip_info': ip_info,
|
||||
'status': 'success'
|
||||
})
|
||||
print(f"✅ {service['name']}: {ip_info}")
|
||||
else:
|
||||
results.append({
|
||||
'service': service['name'],
|
||||
'ip_info': f"HTTP {response.status_code}",
|
||||
'status': 'error'
|
||||
})
|
||||
print(f"❌ {service['name']}: HTTP {response.status_code}")
|
||||
|
||||
except Exception as e:
|
||||
results.append({
|
||||
'service': service['name'],
|
||||
'ip_info': str(e),
|
||||
'status': 'error'
|
||||
})
|
||||
print(f"❌ {service['name']}: {e}")
|
||||
|
||||
return results
|
||||
|
||||
def main():
|
||||
print("=" * 50)
|
||||
print("🌍 IP地址检查工具")
|
||||
print("=" * 50)
|
||||
print()
|
||||
|
||||
# 检查是否有代理配置
|
||||
proxy_config = load_proxy_config()
|
||||
|
||||
if proxy_config:
|
||||
print("发现代理配置,正在检查代理IP...")
|
||||
print()
|
||||
proxy_results = get_ip_info(use_proxy=True)
|
||||
|
||||
print()
|
||||
print("-" * 30)
|
||||
print("为了对比,检查本地IP...")
|
||||
print()
|
||||
local_results = get_ip_info(use_proxy=False)
|
||||
|
||||
print()
|
||||
print("=" * 50)
|
||||
print("📊 结果对比")
|
||||
print("=" * 50)
|
||||
|
||||
# 提取主要IP地址进行对比
|
||||
proxy_ips = [r['ip_info'].split()[0] for r in proxy_results if r['status'] == 'success']
|
||||
local_ips = [r['ip_info'].split()[0] for r in local_results if r['status'] == 'success']
|
||||
|
||||
if proxy_ips and local_ips:
|
||||
if proxy_ips[0] != local_ips[0]:
|
||||
print("✅ 代理工作正常!IP地址已更改")
|
||||
print(f"代理IP: {proxy_ips[0]}")
|
||||
print(f"本地IP: {local_ips[0]}")
|
||||
else:
|
||||
print("⚠️ 代理可能未生效!IP地址相同")
|
||||
print(f"当前IP: {proxy_ips[0]}")
|
||||
else:
|
||||
print("❌ 无法获取足够的IP信息进行对比")
|
||||
|
||||
else:
|
||||
print("未发现代理配置,检查本地IP...")
|
||||
print()
|
||||
get_ip_info(use_proxy=False)
|
||||
|
||||
print()
|
||||
print("✨ 检查完成!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user