mailboxes ratelimit prepare

This commit is contained in:
Jorge Pastor 2024-07-08 18:30:00 +02:00
parent 9c1acfaf8a
commit 67c291cb99
5 changed files with 54 additions and 5 deletions

View file

@ -47,7 +47,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
)
add_fieldsets = (
(None, {
'fields': ('account_link', 'name', 'password1', 'password2', 'filtering'),
'fields': ('account_link', 'name', 'password1', 'password2', 'filtering', 'ratelimit'),
}),
(_("Custom filtering"), {
'classes': ('collapse',),
@ -61,7 +61,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
)
fieldsets = (
(None, {
'fields': ('name', 'password', 'is_active', 'account_link', 'filtering'),
'fields': ('name', 'password', 'is_active', 'account_link', 'filtering', 'ratelimit'),
}),
(_("Custom filtering"), {
'classes': ('collapse',),

View file

@ -679,6 +679,8 @@ class RSpamdRatelimitController(ServiceController):
def save(self, mailbox):
context = self.get_context(mailbox)
self.append(textwrap.dedent("""
# sed -i '/^%(user)s$/d' %(maps)s
# echo '%(user)s' >> %(path_maps)s%(ratelimit)s.map
if ! grep -qx '%(user)s' /etc/rspamd/local.d/maps/usuariosbase.map; then
echo '%(user)s' >> /etc/rspamd/local.d/maps/usuariosbase.map
RELOAD_RSPAMD=1
@ -689,6 +691,7 @@ class RSpamdRatelimitController(ServiceController):
def delete(self, mailbox):
context = self.get_context(mailbox)
self.append(textwrap.dedent("""
# sed -i '/^%(user)s$/d' %(maps)s
if grep -qx '%(user)s' /etc/rspamd/local.d/maps/usuariosbase.map; then
sed -i '/^%(user)s$/d' /etc/rspamd/local.d/maps/usuariosbase.map
RELOAD_RSPAMD=1
@ -697,11 +700,29 @@ class RSpamdRatelimitController(ServiceController):
)
def commit(self):
self.append('[[ $RELOAD_RSPAMD -eq 1 ]] && systemctl reload rspamd.service')
self.append('# [[ $RELOAD_RSPAMD -eq 1 ]] && systemctl reload rspamd.service')
super().commit()
def get_context(self, mailbox):
maps = self.extract_group_maps()
context = {
'user': mailbox.name,
'ratelimit': mailbox.ratelimit,
'maps': maps,
'path_maps': settings.MAILBOXES_RATELIMIT_PATH_MAPS,
}
return context
def extract_group_maps(self):
"""
debulve string de todos los ficheros de maps assignados en settings para ratelimit
return string
"""
choice_groups = settings.MAILBOXES_RATELIMIT_GROUP
path = settings.MAILBOXES_RATELIMIT_PATH_MAPS
group_maps = ''
if len(choice_groups) > 0:
for choice in choice_groups:
group_maps += f"{path}{choice[0]}.map "
return group_maps

View file

@ -54,6 +54,11 @@ class MailboxForm(forms.ModelForm):
raise ValidationError("Name length should be less than %i." % max_length)
return name
def clean_ratelimit(self):
ratelimit = self.cleaned_data['ratelimit']
if ratelimit is None:
ratelimit = settings.MAILBOXES_RATELIMIT_GROUP_DEFAULT
return ratelimit
class MailboxChangeForm(UserChangeForm, MailboxForm):
pass

View file

@ -33,6 +33,11 @@ class Mailbox(models.Model):
"<a href='https://tty1.net/blog/2011/sieve-tutorial_en.html'>sieve language</a>. "
"This overrides any automatic junk email filtering"))
is_active = models.BooleanField(_("active"), default=True)
ratelimit = models.CharField(_("ratelimit"),
max_length=100, null=True, blank=True,
choices=settings.MAILBOXES_RATELIMIT_GROUP,
default=settings.MAILBOXES_RATELIMIT_GROUP_DEFAULT,)
class Meta:
verbose_name_plural = _("mailboxes")

View file

@ -203,3 +203,21 @@ MAILBOXES_MOVE_ON_DELETE_PATH = Setting('MAILBOXES_MOVE_ON_DELETE_PATH',
help_text="Available fromat names: <tt>%s</tt>" % ', '.join(_backend_names),
validators=[Setting.string_format_validator(_backend_names)],
)
MAILBOXES_RATELIMIT_GROUP = Setting('MAILBOXES_RATELIMIT_GROUP', (
('userbase', 'base'),
('user100', '100'),
('user200', '200'),
('user500', '500'),
),
help_text="Available messages per second"
)
MAILBOXES_RATELIMIT_GROUP_DEFAULT = Setting('MAILBOXES_RATELIMIT_GROUP_DEFAULT',
'userbase',
choices=MAILBOXES_RATELIMIT_GROUP
)
MAILBOXES_RATELIMIT_PATH_MAPS = Setting('MAILBOXES_RATELIMIT_PATH_MAPS',
'/etc/rspamd/local.d/maps/',
)