diff --git a/alembic/versions/c3555b3a5332_added_ipaddress_table.py b/alembic/versions/c3555b3a5332_added_ipaddress_table.py new file mode 100644 index 0000000..c92e994 --- /dev/null +++ b/alembic/versions/c3555b3a5332_added_ipaddress_table.py @@ -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 ### diff --git a/dndserver/handlers/login.py b/dndserver/handlers/login.py index 830b277..73f0d9f 100644 --- a/dndserver/handlers/login.py +++ b/dndserver/handlers/login.py @@ -5,7 +5,7 @@ import arrow from dndserver.database import db -from dndserver.models import Hwid, Account +from dndserver.models import Hwid, 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 @@ -40,6 +40,12 @@ def process_login(ctx, msg): 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() + # Check if an hwId is associated to an account_id, if not add to db for hwid in req.hwIds: if not db.query(Hwid).filter_by(hwid=hwid).filter_by(account_id=account.id).first(): diff --git a/dndserver/models.py b/dndserver/models.py index 8a778ba..b6d0460 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 b0eb126..0213b8e 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]}")