This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tool.py
179 lines (144 loc) · 4.6 KB
/
tool.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import urllib.parse,base64,requests,paramiko,random,string,re,chardet
from paramiko import SSHClient
from scp import SCPClient
def get_encoding(file):
with open(file,'rb') as f:
return chardet.detect(f.read())['encoding']
def saveFile(path,content):
file = open(path,'w')
file.write(content)
file.close()
def urlDecode(str):
str = str.strip()
str += (len(str)%4)*'='
return base64.urlsafe_b64decode(str)
def b64Decode(str):
str = str.strip()
str += (len(str)%4)*'='
#print(str)
return base64.b64decode(str)
def readFile(path):
file = open(path,'rb')
content = file.read()
file.close()
return content
def noblankLine(data):
lines = data.splitlines()
newdata = ''
for index in range(len(lines)):
line = lines[index]
t = line.strip()
if len(t)>0:
newdata += t
if index+1<len(lines):
newdata += '\n'
return newdata
def firstLine(data):
lines = data.splitlines()
for line in lines:
line = line.strip()
if line:
return line
def genName(length=8):
name = ''
for i in range(length):
name += random.choice(string.ascii_letters+string.digits)
return name
def is_ip(str):
return re.search(r'^\d+\.\d+\.\d+\.\d+$',str)
def get_protocol(str):
m = re.search(r'^(.+?)://',str)
if m:
return m.group(1)
return None
def checkKeywords(keywords,str):
if not keywords:
return False
for keyword in keywords:
if str.find(keyword)>-1:
return True
return False
def filterNodes(nodelist,keywords):
newlist = []
if not keywords:
return nodelist
for node in nodelist:
if not checkKeywords(keywords,node['name']):
newlist.append(node)
else:
print('过滤节点名称 '+node['name'])
return newlist
def replaceStr(nodelist,keywords):
if not keywords:
return nodelist
for node in nodelist:
for k in keywords:
node['name'] = node['name'].replace(k,'').strip()
return nodelist
def proDuplicateNodeName(nodes):
names = []
for key in nodes.keys():
nodelist = nodes[key]
for node in nodelist:
index = 0
s = node['tag']
while node['tag'] in names:
node['tag'] = s+str(index)
index += 1
names.append(node['tag'])
def removeNodes(nodelist):
newlist = []
temp_list=[]
i=0
for node in nodelist:
_node = {'server':node['server'],'port':node['port']}
if _node in temp_list:
i+=1
else:
temp_list.append(_node)
newlist.append(node)
print('去除了 '+str(i)+' 个重复节点')
print('实际获取 '+str(len(newlist))+' 个节点')
return newlist
def prefixStr(nodelist,prestr):
for node in nodelist:
node['name'] = prestr+node['name'].strip()
return nodelist
def getResponse(url):
response = None
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
try:
response = requests.get(url,headers=headers,timeout=5000)
if response.status_code==200:
return response
else:
return None
except:
return None
class ConfigSSH:
server = {'ip':None,'port':22,'user':None,'password':''}
def __init__(self,server:dict) -> None:
for k in self.server:
if k != 'port' and not k in server.keys():
return None
if k in server.keys():
self.server[k] = server[k]
def connect(self):
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=self.server['ip'],port=22, username=self.server['user'], password=self.server['password'])
self.ssh = ssh
def execCMD(self,command:str):
stdin, stdout, stderr = self.ssh.exec_command(command)
print(stdout.read().decode('utf-8'))
def uploadFile(self,source:str,target:str):
scp = SCPClient(self.ssh.get_transport())
scp.put(source, recursive=True, remote_path=target)
def getFile(self,remote:str,local:str):
scp = SCPClient(self.ssh.get_transport())
scp.get(remote,local)
def close(self):
self.ssh.close()