-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.py
255 lines (220 loc) · 8.34 KB
/
client.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
# Specify the interpreter running path as the cfw project directory.
current_path = os.path.dirname(__file__)
os.chdir(current_path)
import httpx
import click
import pandas as pd
from cfw import cmd, shell, config, ParameterCFWError
# Show all columns and rows.
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.colheader_justify', 'center')
@click.group()
def cli():
pass
"""
ipv4 block / unblock
"""
@cli.command(help="Manually block a single ipv4.")
@click.argument("ip", type=str)
@click.option("-t", "--timeout", default=600, type=int)
def block(ip: str, timeout: int):
if timeout > 2000000:
raise ParameterCFWError("The maximum ban time cannot exceed 2,000,000 seconds. If you want to ban permanently, please use 0 instead.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/block_ip",
params={"ip": ip, "timeout": timeout})
if r.json()["code"]:
pass
else:
print(r.json()["message"])
@cli.command(help="Manually unblock a single ipv4.")
@click.argument("ip", type=str)
def unblock(ip: str):
r = httpx.get(f"http://127.0.0.1:{config['port']}/unblock_ip",
params={"ip": ip})
if r.json()["code"]:
pass
else:
print(r.json()["message"])
@cli.command(help="View ipv4 blacklist.")
def blacklist():
r = httpx.get(f"http://127.0.0.1:{config['port']}/blacklist")
text = r.json()["message"]
ips = []
elements = text.split("Members:\n")[1].strip().split("\n")
if elements[0] == '':
return
for element in elements:
ip, timeout = element.split(" timeout ")
ips.append([ip, timeout])
data = pd.DataFrame(ips, columns=["blacklist", "timeout"])
print(data.to_string(index=False))
"""
ipv6 block / unblock
"""
@cli.command(help="Manually block a single ipv6.")
@click.argument("ip", type=str)
@click.option("-t", "--timeout", default=600, type=int)
def block6(ip: str, timeout: int):
if timeout > 2000000:
raise ParameterCFWError("The maximum ban time cannot exceed 2,000,000 seconds. If you want to ban permanently, please use 0 instead.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/block_ip6",
params={"ip": ip, "timeout": timeout})
if r.json()["code"]:
pass
else:
print(r.json()["message"])
@cli.command(help="Manually unblock a single ipv6.")
@click.argument("ip", type=str)
def unblock6(ip: str):
r = httpx.get(f"http://127.0.0.1:{config['port']}/unblock_ip6",
params={"ip": ip})
if r.json()["code"]:
pass
else:
print(r.json()["message"])
@cli.command(help="View ipv6 blacklist.")
def blacklist6():
r = httpx.get(f"http://127.0.0.1:{config['port']}/blacklist6")
text = r.json()["message"]
ips = []
elements = text.split("Members:\n")[1].strip().split("\n")
if elements[0] == '':
return
for element in elements:
ip, timeout = element.split(" timeout ")
ips.append([ip, timeout])
data = pd.DataFrame(ips, columns=["blacklist6", "timeout"])
print(data.to_string(index=False))
"""
ipv4 port
"""
@cli.command(help="Allow ipv4 port.")
@click.argument("port", type=str)
def allow(port: str):
try:
if int(port) < 0 and int(port) > 65535:
raise ParameterCFWError("The port number range can only be 0 to 65535.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/allow_port",
params={"port": port, "protocol": "all"})
except ValueError:
try:
port, protocol = port.split("/")
if int(port) < 0 and int(port) > 65535:
raise ParameterCFWError("The port number range can only be 0 to 65535.")
if protocol != "tcp" and protocol != "udp":
raise ParameterCFWError("The port protocol can only be tcp or udp.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/allow_port",
params={"port": port, "protocol": protocol})
except ValueError:
raise ParameterCFWError("'cfw allow' syntax error.")
if r.json()["code"]:
pass
else:
print(r.json()["message"])
@cli.command(help="Block ipv4 port.")
@click.argument("port", type=str)
def deny(port: str):
try:
if int(port) < 0 and int(port) > 65535:
raise ParameterCFWError("The port number range can only be 0 to 65535.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/deny_port",
params={"port": port, "protocol": "all"})
except ValueError:
try:
port, protocol = port.split("/")
if int(port) < 0 and int(port) > 65535:
raise ParameterCFWError("The port number range can only be 0 to 65535.")
if protocol != "tcp" and protocol != "udp":
raise ParameterCFWError("The port protocol can only be tcp or udp.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/deny_port",
params={"port": port, "protocol": protocol})
except ValueError:
raise ParameterCFWError("'cfw deny' syntax error.")
if r.json()["code"]:
pass
else:
print(r.json()["message"])
@cli.command(help="View all allowed ipv4 ports.")
def status():
r = httpx.get(f"http://127.0.0.1:{config['port']}/status")
data = r.json()["message"]
if not data:
return
data = pd.DataFrame(data, columns=["port", "protocol"])
print(data.sort_values("port").to_string(index=False))
"""
ipv6 port
"""
@cli.command(help="Allow ipv6 port.")
@click.argument("port", type=str)
def allow6(port: str):
try:
if int(port) < 0 and int(port) > 65535:
raise ParameterCFWError("The port number range can only be 0 to 65535.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/allow_port6",
params={"port": port, "protocol": "all"})
except ValueError:
try:
port, protocol = port.split("/")
if int(port) < 0 and int(port) > 65535:
raise ParameterCFWError("The port number range can only be 0 to 65535.")
if protocol != "tcp" and protocol != "udp":
raise ParameterCFWError("The port protocol can only be tcp or udp.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/allow_port6",
params={"port": port, "protocol": protocol})
except ValueError:
raise ParameterCFWError("'cfw allow' syntax error.")
if r.json()["code"]:
pass
else:
print(r.json()["message"])
@cli.command(help="Block ipv6 port.")
@click.argument("port", type=str)
def deny6(port: str):
try:
if int(port) < 0 and int(port) > 65535:
raise ParameterCFWError("The port number range can only be 0 to 65535.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/deny_port6",
params={"port": port, "protocol": "all"})
except ValueError:
try:
port, protocol = port.split("/")
if int(port) < 0 and int(port) > 65535:
raise ParameterCFWError("The port number range can only be 0 to 65535.")
if protocol != "tcp" and protocol != "udp":
raise ParameterCFWError("The port protocol can only be tcp or udp.")
r = httpx.get(f"http://127.0.0.1:{config['port']}/deny_port6",
params={"port": port, "protocol": protocol})
except ValueError:
raise ParameterCFWError("'cfw deny' syntax error.")
if r.json()["code"]:
pass
else:
print(r.json()["message"])
@cli.command(help="View all allowed ipv6 ports.")
def status6():
r = httpx.get(f"http://127.0.0.1:{config['port']}/status6")
data = r.json()["message"]
if not data:
return
data = pd.DataFrame(data, columns=["port", "protocol"])
print(data.sort_values("port").to_string(index=False))
"""
Log
"""
@cli.command(help="Dynamic query log.")
@click.argument("num", type=str)
def log(num: int = 1000):
cmd(f"tail -f -n {num} {config['log_file_path']}")
"""
Update CFW
"""
@cli.command(help="Update CFW")
def update():
shell("git --git-dir=/etc/cfw/.git --work-tree=/etc/cfw pull https://github.com/Cyberbolt/cfw.git --quiet")
cmd("systemctl start cfw")
print("CFW has been updated.")
if __name__ == '__main__':
cli(obj={})