Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 9 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/da3ce2b00c91_added_ipaddress_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""added IPAddress Table

Revision ID: da3ce2b00c91
Revises: 64412c0be1f7
Create Date: 2023-05-11 16:26:33.766461

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


# revision identifiers, used by Alembic.
revision = "da3ce2b00c91"
down_revision = "64412c0be1f7"
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 ###
12 changes: 11 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

Expand Down Expand Up @@ -39,6 +39,16 @@ 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((IPAddress.address.ilike(ip_address)) & (IPAddress.account_id.ilike(account.id)))
Bakk-f4 marked this conversation as resolved.
Show resolved Hide resolved
.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()):
Bakk-f4 marked this conversation as resolved.
Show resolved Hide resolved
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