use MAX_ACTIVITY_DURING_MINUTE instead of MIN_TIME_BETWEEN_ACTIVITY
This commit is contained in:
parent
8caebc0142
commit
eccc8a71e9
|
@ -209,12 +209,11 @@ LOCAL_FILE_UPLOAD = "LOCAL_FILE_UPLOAD" in os.environ
|
|||
UPLOAD_DIR = None
|
||||
|
||||
# Greylisting features
|
||||
# minimal time in seconds an alias can receive/send emails
|
||||
MIN_TIME_BETWEEN_ACTIVITY_PER_ALIAS = 8
|
||||
|
||||
# minimal time in seconds a mailbox can receive/send emails
|
||||
MIN_TIME_BETWEEN_ACTIVITY_PER_MAILBOX = 3
|
||||
# nb max of activity (forward/reply) an alias can have during 1 min
|
||||
MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS = 5
|
||||
|
||||
# nb max of activity (forward/reply) a mailbox can have during 1 min
|
||||
MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX = 10
|
||||
|
||||
if LOCAL_FILE_UPLOAD:
|
||||
print("Upload files to local dir")
|
||||
|
|
|
@ -2,8 +2,8 @@ import arrow
|
|||
|
||||
from app.alias_utils import try_auto_create
|
||||
from app.config import (
|
||||
MIN_TIME_BETWEEN_ACTIVITY_PER_ALIAS,
|
||||
MIN_TIME_BETWEEN_ACTIVITY_PER_MAILBOX,
|
||||
MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS,
|
||||
MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX,
|
||||
)
|
||||
from app.extensions import db
|
||||
from app.log import LOG
|
||||
|
@ -11,50 +11,53 @@ from app.models import Alias, EmailLog, Contact
|
|||
|
||||
|
||||
def greylisting_needed_for_alias(alias: Alias) -> bool:
|
||||
# get the latest email activity on this alias
|
||||
r = (
|
||||
min_time = arrow.now().shift(minutes=-1)
|
||||
|
||||
# get the nb of activity on this alias
|
||||
nb_activity = (
|
||||
db.session.query(EmailLog, Contact)
|
||||
.filter(EmailLog.contact_id == Contact.id, Contact.alias_id == alias.id)
|
||||
.order_by(EmailLog.id.desc())
|
||||
.first()
|
||||
.filter(
|
||||
EmailLog.contact_id == Contact.id,
|
||||
Contact.alias_id == alias.id,
|
||||
EmailLog.created_at > min_time,
|
||||
)
|
||||
.group_by(EmailLog.id)
|
||||
.count()
|
||||
)
|
||||
|
||||
if r:
|
||||
email_log, _ = r
|
||||
now = arrow.now()
|
||||
if (now - email_log.created_at).seconds < MIN_TIME_BETWEEN_ACTIVITY_PER_ALIAS:
|
||||
LOG.d(
|
||||
"Too much forward on alias %s. Latest email log %s", alias, email_log,
|
||||
)
|
||||
return True
|
||||
if nb_activity > MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS:
|
||||
LOG.d(
|
||||
"Too much forward on alias %s. Nb Activity %s", alias, nb_activity,
|
||||
)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def greylisting_needed_for_mailbox(alias: Alias) -> bool:
|
||||
# get the latest email activity on this mailbox
|
||||
r = (
|
||||
min_time = arrow.now().shift(minutes=-1)
|
||||
|
||||
# get nb of activity on this mailbox
|
||||
nb_activity = (
|
||||
db.session.query(EmailLog, Contact, Alias)
|
||||
.filter(
|
||||
EmailLog.contact_id == Contact.id,
|
||||
Contact.alias_id == Alias.id,
|
||||
Alias.mailbox_id == alias.mailbox_id,
|
||||
EmailLog.created_at > min_time,
|
||||
)
|
||||
.order_by(EmailLog.id.desc())
|
||||
.first()
|
||||
.group_by(EmailLog.id)
|
||||
.count()
|
||||
)
|
||||
|
||||
if r:
|
||||
email_log, _, _ = r
|
||||
now = arrow.now()
|
||||
if (now - email_log.created_at).seconds < MIN_TIME_BETWEEN_ACTIVITY_PER_MAILBOX:
|
||||
LOG.d(
|
||||
"Too much forward on mailbox %s. Latest email log %s. Alias %s",
|
||||
alias.mailbox,
|
||||
email_log,
|
||||
alias,
|
||||
)
|
||||
return True
|
||||
if nb_activity > MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX:
|
||||
LOG.d(
|
||||
"Too much forward on mailbox %s, alias %s. Nb Activity %s",
|
||||
alias.mailbox,
|
||||
alias,
|
||||
nb_activity,
|
||||
)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
from app.config import (
|
||||
MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS,
|
||||
MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX,
|
||||
)
|
||||
from app.extensions import db
|
||||
from app.greylisting import (
|
||||
greylisting_needed_forward_phase,
|
||||
|
@ -27,7 +31,10 @@ def test_greylisting_needed_forward_phase_for_alias(flask_client):
|
|||
reply_email="rep@sl.local",
|
||||
)
|
||||
db.session.commit()
|
||||
EmailLog.create(user_id=user.id, contact_id=contact.id)
|
||||
for _ in range(MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS + 1):
|
||||
EmailLog.create(user_id=user.id, contact_id=contact.id)
|
||||
db.session.commit()
|
||||
|
||||
assert greylisting_needed_for_alias(alias)
|
||||
|
||||
|
||||
|
@ -47,13 +54,17 @@ def test_greylisting_needed_forward_phase_for_mailbox(flask_client):
|
|||
reply_email="rep@sl.local",
|
||||
)
|
||||
db.session.commit()
|
||||
for _ in range(MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX + 1):
|
||||
EmailLog.create(user_id=user.id, contact_id=contact.id)
|
||||
db.session.commit()
|
||||
|
||||
EmailLog.create(user_id=user.id, contact_id=contact.id)
|
||||
|
||||
# Create another alias with the same mailbox
|
||||
# will be greylisted as there's a previous activity on mailbox
|
||||
alias = Alias.create_new_random(user)
|
||||
alias2 = Alias.create_new_random(user)
|
||||
db.session.commit()
|
||||
assert greylisting_needed_for_mailbox(alias)
|
||||
assert greylisting_needed_for_mailbox(alias2)
|
||||
|
||||
|
||||
def test_greylisting_needed_forward_phase(flask_client):
|
||||
|
@ -80,6 +91,8 @@ def test_greylisting_needed_reply_phase(flask_client):
|
|||
reply_email="rep@sl.local",
|
||||
)
|
||||
db.session.commit()
|
||||
EmailLog.create(user_id=user.id, contact_id=contact.id)
|
||||
for _ in range(MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS + 1):
|
||||
EmailLog.create(user_id=user.id, contact_id=contact.id)
|
||||
db.session.commit()
|
||||
|
||||
assert greylisting_needed_reply_phase("rep@sl.local")
|
||||
|
|
Loading…
Reference in a new issue