-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathview.py
More file actions
458 lines (407 loc) · 16.9 KB
/
view.py
File metadata and controls
458 lines (407 loc) · 16.9 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
from __future__ import annotations
import re
from pathlib import Path
import discord
import netaddr
from netaddr import IPAddress, IPNetwork
from redbot.core.i18n import Translator
from redbot.core.utils.chat_formatting import humanize_list
from pylav.helpers.format.strings import shorten_string
from pylav.type_hints.bot import DISCORD_BOT_TYPE, DISCORD_COG_TYPE, DISCORD_INTERACTION_TYPE
_ = Translator("PyLavManagedNode", Path(__file__))
class ConfigureIPRotationView(discord.ui.View):
"""
A secure ``discord.ui.View`` used to configure the managed nodes IP Rotation.
"""
def __init__(
self,
bot: DISCORD_BOT_TYPE,
cog: DISCORD_COG_TYPE,
prefix: str,
timeout: int = 180,
):
self.bot = bot
self.cog = cog
self.prefix = prefix
super().__init__(timeout=timeout)
async def interaction_check(self, interaction: DISCORD_INTERACTION_TYPE) -> bool:
if not await self.bot.is_owner(interaction.user):
await interaction.response.send_message(_("You are not authorized to interact with this."), ephemeral=True)
return False
return True
@discord.ui.button(
label=shorten_string(max_length=100, string=_("Configure IP Rotation.")),
style=discord.ButtonStyle.grey,
)
async def add_ip_block(self, interaction: DISCORD_INTERACTION_TYPE, button: discord.ui.Button):
return await interaction.response.send_modal(
ConfigureIPRotationModal(bot=self.bot, cog=self.cog, prefix=self.prefix)
)
class ConfigureGoogleAccountView(discord.ui.View):
"""
A secure ``discord.ui.View`` used to configure the Google account for the node.
"""
def __init__(self, bot: DISCORD_BOT_TYPE, cog: DISCORD_COG_TYPE, prefix: str, timeout: int = 180):
self.bot = bot
self.cog = cog
self.prefix = prefix
super().__init__(timeout=timeout)
async def interaction_check(self, interaction: DISCORD_INTERACTION_TYPE) -> bool:
if not await self.bot.is_owner(interaction.user):
await interaction.response.send_message(_("You are not authorized to interact with this."), ephemeral=True)
return False
return True
@discord.ui.button(
label=shorten_string(max_length=100, string=_("Link Google Account.")),
style=discord.ButtonStyle.grey,
)
async def link_account(self, interaction: DISCORD_INTERACTION_TYPE, button: discord.ui.Button):
return await interaction.response.send_modal(
ConfigureGoogleAccountModal(bot=self.bot, cog=self.cog, prefix=self.prefix)
)
class ConfigureHTTPProxyView(discord.ui.View):
"""
A secure ``discord.ui.View`` used to configure a HTTP Proxy for the node.
"""
def __init__(
self,
bot: DISCORD_BOT_TYPE,
cog: DISCORD_COG_TYPE,
prefix: str,
timeout: int = 180,
):
self.bot = bot
self.cog = cog
self.prefix = prefix
super().__init__(timeout=timeout)
async def interaction_check(self, interaction: DISCORD_INTERACTION_TYPE) -> bool:
if not await self.bot.is_owner(interaction.user):
await interaction.response.send_message(_("You are not authorized to interact with this."), ephemeral=True)
return False
return True
@discord.ui.button(
label=shorten_string(max_length=100, string=_("Configure HTTP Proxy.")),
style=discord.ButtonStyle.grey,
)
async def configure_proxy(self, interaction: DISCORD_INTERACTION_TYPE, button: discord.ui.Button):
return await interaction.response.send_modal(
ConfigureHTTPProxyModal(bot=self.bot, cog=self.cog, prefix=self.prefix)
)
class ConfigureIPRotationModal(discord.ui.Modal):
"""A secure ``discord.ui.Modal`` used to configure the managed nodes IP Rotation"""
def __init__(
self,
bot: DISCORD_BOT_TYPE,
cog: DISCORD_COG_TYPE,
prefix: str,
):
self.bot = bot
self.cog = cog
self.prefix = prefix
super().__init__(title=shorten_string(max_length=100, string=_("IP Rotation Configurator.")))
self.ip_blocks = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("IP Blocks.")),
style=discord.TextStyle.long,
required=True,
placeholder=shorten_string(
max_length=100, string=_("1.0.0.0/8,...,... - Comma separated list of IP blocks")
),
)
self.strategy = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("Rotation strategy.")),
style=discord.TextStyle.long,
required=True,
placeholder="RotateOnBan | LoadBalance | NanoSwitch | RotatingNanoSwitch",
max_length=18,
min_length=10,
)
self.retry_limit = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("Retry limit.")),
style=discord.TextStyle.short,
required=False,
placeholder=shorten_string(max_length=100, string=_("-1 = default, 0 = infinity, >0 = number of retries")),
min_length=1,
max_length=3,
)
self.excluded_ips = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("IPs to exclude.")),
required=False,
style=discord.TextStyle.short,
placeholder=shorten_string(max_length=100, string=_("Comma separated list of IP to exclude from rotation")),
)
self.search_trigger = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("Search trigger rotation.")),
style=discord.TextStyle.short,
required=False,
placeholder=shorten_string(max_length=100, string=_("0 or 1 (0 = disabled, 1 = enabled)")),
min_length=1,
max_length=1,
)
self.add_item(self.ip_blocks)
self.add_item(self.strategy)
self.add_item(self.retry_limit)
self.add_item(self.search_trigger)
self.add_item(self.excluded_ips)
async def on_submit(self, interaction: DISCORD_INTERACTION_TYPE):
if not await self.bot.is_owner(
interaction.user
): # Prevent non-bot owners from somehow acquiring and saving the modal.
return await interaction.response.send_message(
_("You are not authorized to interact with this."), ephemeral=True
)
await interaction.response.defer(ephemeral=True)
send_method = interaction.followup.send
if self.ip_blocks.value:
try:
ip_blocks = list(map(str, map(IPNetwork, set(self.ip_blocks.value.split(",")))))
except netaddr.core.AddrFormatError as exc:
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_(
"The IP block you have provided is not valid; {error_variable_do_not_translate}."
).format(error_variable_do_not_translate=exc),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
else:
ip_blocks = []
if not ip_blocks:
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_("No IP blocks were provided."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
if self.ip_blocks.value:
try:
excluded_ips = list(map(str, map(IPAddress, set(self.ip_blocks.value.split(",")))))
except netaddr.core.AddrFormatError as exc:
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_(
"The IP address you have provided is not valid; {error_variable_do_not_translate}"
).format(error_variable_do_not_translate=exc),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
else:
excluded_ips = []
strategy = self.strategy.value.lower().strip()
stategy_mapping = {
"rotateonban": "RotateOnBan",
"loadbalance": "LoadBalance",
"nanoswitch": "NanoSwitch",
"rotatingnanoswitch": "RotatingNanoSwitch",
}
if strategy not in stategy_mapping:
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_(
"The strategy you provided is invalid. You must be one of: {options_variable_do_not_translate}."
).format(options_variable_do_not_translate=humanize_list(list(stategy_mapping.values()))),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
strategy = stategy_mapping[strategy]
try:
retry_limit = int(self.retry_limit.value.strip() or "-1")
if retry_limit < -1:
raise ValueError
except ValueError:
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_("The retry limit must be a number greater than or equal to -1."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
try:
search_trigger = int(self.search_trigger.value.strip() or "1")
if search_trigger not in [0, 1]:
raise ValueError
search_trigger = bool(search_trigger)
except ValueError:
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_("The search trigger must be 0 or 1."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
config = self.bot.pylav.node_db_manager.bundled_node_config()
yaml_data = await config.fetch_yaml()
yaml_data["lavalink"]["server"]["ratelimit"] = {
"ipBlocks": ip_blocks,
"strategy": strategy,
"retryLimit": retry_limit,
"excludedIps": excluded_ips,
"searchTriggersFail": search_trigger,
}
await config.update_yaml(yaml_data)
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_("IP rotation settings saved."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
class ConfigureGoogleAccountModal(discord.ui.Modal):
"""A secure ``discord.ui.Modal`` used to add a Google account to the node"""
def __init__(
self,
bot: DISCORD_BOT_TYPE,
cog: DISCORD_COG_TYPE,
prefix: str,
):
self.bot = bot
self.cog = cog
self.prefix = prefix
super().__init__(title=_("Google Account Configurator"))
self.email = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("Email address")),
style=discord.TextStyle.short,
required=True,
placeholder=shorten_string(max_length=100, string=_("Your Google account email")),
min_length=4,
)
self.password = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("password")),
style=discord.TextStyle.short,
required=True,
placeholder=shorten_string(
max_length=100, string=_("If you have 2FA you will need an application password")
),
min_length=8,
max_length=100,
)
self.add_item(self.email)
self.add_item(self.password)
async def on_submit(self, interaction: DISCORD_INTERACTION_TYPE):
if not await self.bot.is_owner(
interaction.user
): # Prevent non-bot owners from somehow acquiring and saving the modal.
return await interaction.response.send_message(
_("You are not authorized to interact with this"), ephemeral=True
)
await interaction.response.defer(ephemeral=True)
send_method = interaction.followup.send
if re.match(r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", self.email.value) is None:
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_("Invalid email address"),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
await self.bot.set_shared_api_tokens("google", email=self.email.value, password=self.password.value)
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_("Google account linked.").format(prefix=self.prefix),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
class ConfigureHTTPProxyModal(discord.ui.Modal):
"""A secure ``discord.ui.Modal`` used to configure a HTTP Proxy for the node"""
def __init__(
self,
bot: DISCORD_BOT_TYPE,
cog: DISCORD_COG_TYPE,
prefix: str,
):
self.bot = bot
self.cog = cog
self.prefix = prefix
super().__init__(title=_("HTTP Proxy Configurator"))
self.host = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("Hostname")),
style=discord.TextStyle.short,
required=True,
placeholder=shorten_string(max_length=100, string=_("Hostname of the proxy, (IP or domain or localhost)")),
)
self.port = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("Proxy port")),
style=discord.TextStyle.short,
required=True,
placeholder=shorten_string(max_length=100, string=_("Proxy port, 3128 is the default for squidProxy")),
min_length=1,
max_length=5,
)
self.user = discord.ui.TextInput(
label=shorten_string(max_length=100, string=_("User")),
style=discord.TextStyle.long,
required=False,
placeholder=shorten_string(
max_length=100,
string=_(
"Optional user for basic authentication fields. Leave blank if you do not use basic authentication"
),
),
)
self.password = discord.ui.TextInput(
label=_("Password"),
style=discord.TextStyle.long,
required=False,
placeholder=shorten_string(
max_length=100,
string=_(
"Optional password for basic authentication fields. Leave blank if you do not use basic authentication."
),
),
)
self.add_item(self.host)
self.add_item(self.port)
self.add_item(self.user)
self.add_item(self.password)
async def on_submit(self, interaction: DISCORD_INTERACTION_TYPE):
if not await self.bot.is_owner(
interaction.user
): # Prevent non-bot owners from somehow acquiring and saving the modal.
return await interaction.response.send_message(
_("You are not authorized to interact with this"), ephemeral=True
)
await interaction.response.defer(ephemeral=True)
send_method = interaction.followup.send
try:
port = int(self.port.value.strip())
if not (0 <= port <= 65536):
raise ValueError
except ValueError:
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_("The port provided is not valid. It must be between 0 and 65536."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)
config = self.bot.pylav.node_db_manager.bundled_node_config()
yaml_data = await config.fetch_yaml()
yaml_data["lavalink"]["server"]["httpConfig"] = {
"proxyHost": self.host.value,
"proxyPort": port,
"proxyUser": self.user.value,
"proxyPassword": self.password.value,
}
await config.update_yaml(yaml_data)
return await send_method(
embed=await self.bot.pylav.construct_embed(
description=_("HTTP proxy settings saved."),
messageable=interaction,
),
ephemeral=True,
wait=True,
)