-
Notifications
You must be signed in to change notification settings - Fork 2
/
inquire_dns.py
133 lines (113 loc) · 3.97 KB
/
inquire_dns.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @Author : bajins https://www.bajins.com
# @File : inquire_dns.py
# @Version: 1.0.0
# @Time : 2019/8/21 15:56
# @Project: windows-wallpaper-python
# @Package:
# @Software: PyCharm
import fileinput
import json
import os
import re
import shutil
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
import constants
from utils import http_util, file_util, string_util, object_util, reptile_util
def get_chinaz_ip():
"""
通过chinaz查询dns
:return:
"""
try:
for domain in constants.GITHUB_DOMAIN:
try:
driver = reptile_util.selenium_driver(constants.CHINAZ_DNS)
# driver.set_page_load_timeout(20)
input_element = driver.find_element_by_xpath('//*[@id="host"]')
# 传入值,输入的内容
input_element.send_keys(domain)
# 提交
input_element.submit()
# driver.set_script_timeout(60)
# https://zhuanlan.zhihu.com/p/61536685
# WebDriverWait(driver, 120).until(expected_conditions.presence_of_element_located(
# (By.XPATH, "//*[@class='ReListCent ReLists bor-b1s clearfix']")))
lis = driver.find_elements_by_xpath("//*[@class='ReListCent ReLists bor-b1s clearfix']")
for li in lis:
if not li.text == "":
li_split = li.text.split("\n")
print(li_split[1], li_split[3])
except Exception as e:
print(e)
finally:
# 关闭当前窗口。
driver.close()
# 关闭浏览器并关闭chreomedriver进程
driver.quit()
def delete_dns(dns):
"""
删除数组中的dns
:param dns: 数组
:return: 删除后的hosts
"""
hosts = file_util.read_file(constants.HOSTS_PATH)
new_hosts = []
for host in hosts:
if not object_util.is_in_array(host.strip("\n"), dns):
new_hosts.append(host)
return new_hosts
def update_hosts(new_hosts):
"""
更新覆写hosts
:param new_hosts: 新的dns数组
:return:
"""
file_util.remove_read_only(constants.HOSTS_PATH)
file_util.write_lines(constants.HOSTS_PATH, new_hosts)
# 刷新dns
os.system("ipconfig /flushdns")
def get_myssl_ip():
"""
通过myssl查询dns
:return:
"""
new_hosts = delete_dns(constants.GITHUB_DOMAIN)
for domain in constants.GITHUB_DOMAIN:
try:
data = {"qtype": 1, "host": domain, "qmode": -1}
response = http_util.get(url=constants.MYSSL_DNS, data=data)
jsp = json.loads(response.text)
if jsp["code"] == 0 and jsp["error"] is None:
result_data = jsp["data"]
addr_us = result_data["01"][0]["answer"]["records"]
# addr_hk = result_data["852"][0]["answer"]["records"]
# addr_cn = result_data["86"][0]["answer"]["records"]
# 拼接host
for us in addr_us:
new_hosts.append(us["value"] + " " + domain + "\n")
except Exception as e:
print("错误:", e)
update_hosts(new_hosts)
def get_short_time_mail_dns():
"""
通过shorttimemail.com查询DNS
:return:
"""
new_hosts = delete_dns(constants.GITHUB_DOMAIN)
for domain in constants.GITHUB_DOMAIN:
try:
data = {"server": "8.8.8.8", "rrtype": "A", "domain": domain}
response = http_util.get(url=constants.SHORT_TIME_MAIL_DNS, data=data)
jsp = json.loads(response.text)
if jsp["code"] == 0:
# 拼接host
for us in jsp["data"]:
new_hosts.append(us["value"] + " " + domain + "\n")
except Exception as e:
print("错误:", e)
update_hosts(new_hosts)