Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

Added support for storing IP addresses at login #197

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions alembic/versions/c3555b3a5332_added_ipaddress_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Added IPAddress Table

Revision ID: c3555b3a5332
Revises: b53c793562c7
Create Date: 2023-05-16 18:59:05.677516

"""
from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils


# revision identifiers, used by Alembic.
revision = "c3555b3a5332"
down_revision = "b53c793562c7"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"ip_addresses",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("address", sa.String(length=45), nullable=False),
sa.Column("account_id", sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("ip_addresses")
# ### end Alembic commands ###
8 changes: 7 additions & 1 deletion dndserver/handlers/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import argon2

from dndserver.database import db
from dndserver.models import Account
from dndserver.models import Account, IPAddress
from dndserver.persistent import sessions
from dndserver.protos.Account import SC2S_ACCOUNT_LOGIN_REQ, SLOGIN_ACCOUNT_INFO, SS2C_ACCOUNT_LOGIN_RES
from dndserver.protos.Common import SS2C_SERVICE_POLICY_NOT, FSERVICE_POLICY
Expand Down Expand Up @@ -39,6 +39,12 @@ def process_login(ctx, msg):
# TODO: Create new hwid objects and save them to the db here
res.secretToken = account.secret_token

# Retrive ip address and associate the ip address to the account id
ip_address = ctx.transport.client[0]
if not db.query(IPAddress).filter_by(address=ip_address).filter_by(account_id=account.id).first():
address = IPAddress(account_id=account.id, address=ip_address)
address.save()

# Return FAIL_PASSWORD on invalid password.
try:
argon2.PasswordHasher().verify(account.password, req.password)
Expand Down
12 changes: 12 additions & 0 deletions dndserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ def save(self):
db.commit()


class IPAddress(base):
__tablename__ = "ip_addresses"
Copy link
Owner

@Snaacky Snaacky May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table name should be something more along the lines of user_logins or something of that nature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the login table in PR #186. Let me know which names you prefer for what


id = Column(Integer, primary_key=True, autoincrement=True)
address = Column(String(45), nullable=False)
account_id = Column(Integer, nullable=True)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not storing the seen_at time that the logins are occurring which makes the logs less useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are storing the time already in another table in PR #186, let me know if you want aswell another timestamp in this table too. I tought it was not usefull to store two times the same info, we can just join the two tables and we get the same result

def save(self):
db.add(self)
db.commit()


class Character(base):
__tablename__ = "characters"

Expand Down
8 changes: 8 additions & 0 deletions dndserver/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
ranking,
trade,
)
from dndserver.models import IPAddress
from dndserver.database import db
from dndserver.objects.user import User
from dndserver.persistent import sessions
from dndserver.protos import PacketCommand as pc
Expand All @@ -38,6 +40,12 @@ def connectionMade(self) -> None:
user = User()
sessions[self.transport] = user

# Retrive ip address of connected client for logging
ip_address = self.transport.client[0]
if not db.query(IPAddress).filter((IPAddress.address.ilike(ip_address))).first():
address = IPAddress(address=ip_address)
address.save()

def connectionLost(self, reason):
"""Event for when a client disconnects from the server."""
logger.debug(f"Lost connection to: {self.transport.client[0]}:{self.transport.client[1]}")
Expand Down