From f5fa2f06a015175d1ab606f49c79a97bfd37d983 Mon Sep 17 00:00:00 2001 From: Bakk Date: Thu, 11 May 2023 16:38:36 +0200 Subject: [PATCH 1/6] Logging ip address --- dndserver/handlers/login.py | 11 ++++++++++- dndserver/models.py | 12 ++++++++++++ dndserver/protocol.py | 8 ++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/dndserver/handlers/login.py b/dndserver/handlers/login.py index c7a1a8cd..04ebe032 100644 --- a/dndserver/handlers/login.py +++ b/dndserver/handlers/login.py @@ -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 @@ -39,6 +39,15 @@ 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))).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) diff --git a/dndserver/models.py b/dndserver/models.py index 682bf99a..e84bced6 100644 --- a/dndserver/models.py +++ b/dndserver/models.py @@ -27,6 +27,18 @@ def save(self): db.commit() +class IPAddress(base): + __tablename__ = "ip_addresses" + + id = Column(Integer, primary_key=True, autoincrement=True) + address = Column(String(45), nullable=False) + account_id = Column(Integer, nullable=True) + + def save(self): + db.add(self) + db.commit() + + class Character(base): __tablename__ = "characters" diff --git a/dndserver/protocol.py b/dndserver/protocol.py index 1a0a980f..92868fb1 100644 --- a/dndserver/protocol.py +++ b/dndserver/protocol.py @@ -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 @@ -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]}") From 6db1b38fcf1cd603eb274c5837094e156c55ae5e Mon Sep 17 00:00:00 2001 From: Bakk Date: Fri, 12 May 2023 17:34:08 +0200 Subject: [PATCH 2/6] Added alembic file --- alembic/da3ce2b00c91_added_ipaddress_table.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 alembic/da3ce2b00c91_added_ipaddress_table.py diff --git a/alembic/da3ce2b00c91_added_ipaddress_table.py b/alembic/da3ce2b00c91_added_ipaddress_table.py new file mode 100644 index 00000000..a1108ca2 --- /dev/null +++ b/alembic/da3ce2b00c91_added_ipaddress_table.py @@ -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 ### From 760d09e9dab44af4519cc92b66ad1b747b5efde9 Mon Sep 17 00:00:00 2001 From: Bakk Date: Sun, 14 May 2023 15:42:16 +0200 Subject: [PATCH 3/6] corrected alembic file location --- alembic/{ => versions}/da3ce2b00c91_added_ipaddress_table.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename alembic/{ => versions}/da3ce2b00c91_added_ipaddress_table.py (100%) diff --git a/alembic/da3ce2b00c91_added_ipaddress_table.py b/alembic/versions/da3ce2b00c91_added_ipaddress_table.py similarity index 100% rename from alembic/da3ce2b00c91_added_ipaddress_table.py rename to alembic/versions/da3ce2b00c91_added_ipaddress_table.py From 07b62d8bb125a5e94bc4a76bd53eb649fa8ed666 Mon Sep 17 00:00:00 2001 From: Bakk Date: Mon, 15 May 2023 16:35:53 +0200 Subject: [PATCH 4/6] black formatting --- dndserver/handlers/login.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dndserver/handlers/login.py b/dndserver/handlers/login.py index 04ebe032..9c01657d 100644 --- a/dndserver/handlers/login.py +++ b/dndserver/handlers/login.py @@ -43,8 +43,9 @@ def process_login(ctx, msg): ip_address = ctx.transport.client[0] if not ( db.query(IPAddress) - .filter((IPAddress.address.ilike(ip_address)) - & (IPAddress.account_id.ilike(account.id))).first()): + .filter((IPAddress.address.ilike(ip_address)) & (IPAddress.account_id.ilike(account.id))) + .first() + ): address = IPAddress(account_id=account.id, address=ip_address) address.save() From 3fad280b338a4f0cb7b3f786cbcd021ea96d01c2 Mon Sep 17 00:00:00 2001 From: Bakk Date: Tue, 16 May 2023 18:24:32 +0200 Subject: [PATCH 5/6] PR corrections --- dndserver/handlers/login.py | 6 +----- dndserver/protocol.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/dndserver/handlers/login.py b/dndserver/handlers/login.py index 9c01657d..8d762bb9 100644 --- a/dndserver/handlers/login.py +++ b/dndserver/handlers/login.py @@ -41,11 +41,7 @@ def process_login(ctx, msg): # 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))) - .first() - ): + 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() diff --git a/dndserver/protocol.py b/dndserver/protocol.py index bfccc0ca..acb16f1b 100644 --- a/dndserver/protocol.py +++ b/dndserver/protocol.py @@ -42,7 +42,7 @@ def connectionMade(self) -> None: # 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()): + if not db.query(IPAddress).filter((IPAddress.address.ilike(ip_address))).first(): address = IPAddress(address=ip_address) address.save() From 1eab04f7b852c15f4869d69f7296bbd977485c03 Mon Sep 17 00:00:00 2001 From: Bakk Date: Tue, 16 May 2023 19:00:59 +0200 Subject: [PATCH 6/6] alembic heads correction --- ...able.py => c3555b3a5332_added_ipaddress_table.py} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename alembic/versions/{da3ce2b00c91_added_ipaddress_table.py => c3555b3a5332_added_ipaddress_table.py} (81%) diff --git a/alembic/versions/da3ce2b00c91_added_ipaddress_table.py b/alembic/versions/c3555b3a5332_added_ipaddress_table.py similarity index 81% rename from alembic/versions/da3ce2b00c91_added_ipaddress_table.py rename to alembic/versions/c3555b3a5332_added_ipaddress_table.py index a1108ca2..c92e9943 100644 --- a/alembic/versions/da3ce2b00c91_added_ipaddress_table.py +++ b/alembic/versions/c3555b3a5332_added_ipaddress_table.py @@ -1,8 +1,8 @@ -"""added IPAddress Table +"""Added IPAddress Table -Revision ID: da3ce2b00c91 -Revises: 64412c0be1f7 -Create Date: 2023-05-11 16:26:33.766461 +Revision ID: c3555b3a5332 +Revises: b53c793562c7 +Create Date: 2023-05-16 18:59:05.677516 """ from alembic import op @@ -11,8 +11,8 @@ # revision identifiers, used by Alembic. -revision = "da3ce2b00c91" -down_revision = "64412c0be1f7" +revision = "c3555b3a5332" +down_revision = "b53c793562c7" branch_labels = None depends_on = None