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
105
| """
In order to solve the problem that github cannot be accessed,
by replacing ip in the hosts file
"""
import ctypes
import os, sys
import traceback
import aiohttp
import asyncio
from fake_useragent import UserAgent
from lxml import etree
from functools import partial
import shutil
ua = UserAgent(use_external_data=True)
ip_dict = dict()
def build_headers():
headers = {
'use_agent': ua.random
}
return headers
async def fetch_latest_ip(url):
async with aiohttp.ClientSession(headers=build_headers()) as session:
async with session.get(url) as response:
result = await response.text()
html = etree.HTML(result)
ip = html.xpath("//th[text()='IPv4 Addresses']/following-sibling::td//text()")
print(f"正在获取ip: {url}\n解析完成: {ip}")
return ip
async def main():
base_url = 'https://www.ipaddress.com/site/'
targets = (
'github.com',
'assets-cdn.github.com',
'github.global.ssl.fastly.net'
)
tasks = []
for url in targets:
task = asyncio.ensure_future(fetch_latest_ip(base_url + url))
# 回调
task.add_done_callback(partial(parse, url))
tasks.append(task)
await asyncio.gather(*tasks)
def parse(url, task):
ip_dict[url] = task.result()
def save():
host = r'C:/Windows/System32/drivers/etc/hosts'
temp = r'./temp'
try:
if not os.path.exists(host):
print("hosts文件不存在")
return False
with open(host, mode='r') as f1, open(temp, mode='w') as f2:
lines = f1.readlines()
for line in reversed(lines):
if 'github.' in line:
lines.remove(line)
for name in ip_dict:
for ip in ip_dict[name]:
lines.append(f"{ip} {name}\n")
# 提取一行中的ip和name
# ip, name = re.split(r"\s+", line) f2.writelines(lines)
os.remove(host)
shutil.copy(temp, host)
os.system("ipconfig/flushdns")
print("DNS刷新成功")
return True
except Exception:
traceback.print_exc()
return False
if __name__ == '__main__':
try:
is_admin = ctypes.windll.shell32.IsUserAnAdmin()
if not is_admin:
# 解决无权访问的问题,重新以管理员权限运行
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
# 这里如果使用asyncio.run(main())会报错,不知道原因
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
ret = save()
if ret:
print("Succeeded!!")
else:
print("Failed!!")
except Exception as e:
traceback.print_exc()
print("Failed!!")
finally:
input("\nPlease press Enter to exit!")
|