Skip to content

Commit

Permalink
chore: Add Python version 3.12.3 and Twisted dependency 24.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
drazisil committed May 26, 2024
1 parent d43309f commit 47520f5
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 0 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.3
39 changes: 39 additions & 0 deletions NPSLoginProtocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from twisted.internet.protocol import Protocol, Factory
import logging
from rusty_server_login.util import getMessageVersion
from rusty_server_login.BaseMessage import BaseMessage


class NPSLoginProtocol(Protocol):
def __init__(self, factory):
self.factory = factory
self.logger = logging.getLogger("NPSLoginProtocol")
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(logging.StreamHandler())

def connectionMade(self):
self.logger.info("Connection from: %s", self.transport.getPeer())

def dataReceived(self, data):
version = getMessageVersion(data)
self.logger.info("Message version: %s", version)
message = BaseMessage(version, data)
self.logger.info("Data received: %s", data.hex(" ", 0))
self.logger.info("Message: %s", message)
self.transport.write(data)

def connectionLost(self, reason):
self.logger.info("Connection lost: %s", reason)


class NPSLoginProtocolFactory(Factory):
protocol = NPSLoginProtocol

def __init__(self):
self.logger = logging.getLogger("NPSLoginProtocol")
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(logging.StreamHandler())
self.logger.info("Factory created")

def buildProtocol(self, addr):
return NPSLoginProtocol(self)
21 changes: 21 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet.selectreactor import install
import logging

from NPSLoginProtocol import NPSLoginProtocolFactory

install()
from twisted.internet import reactor


def main():
logging.basicConfig(level=logging.DEBUG)
logging.info("Server started")
endpoint = TCP4ServerEndpoint(reactor, 8226)
endpoint.listen(NPSLoginProtocolFactory())
reactor.run()


if __name__ == "__main__":
main()
248 changes: 248 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"
twisted = "^24.3.0"


[build-system]
Expand Down
36 changes: 36 additions & 0 deletions rusty_server_login/BaseMessage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from .util import MessageVersion, bigEndianBytesToInt


class BaseMessage:
def __init__(self, msg_version: MessageVersion, data: bytes):
self.msg_version = msg_version
self.msgId = bigEndianBytesToInt(data[0:2])
self.msgLen = bigEndianBytesToInt(data[2:4])

if msg_version == MessageVersion.V1:
if len(data) < 12:
raise ValueError(
"Message is too short, expected at least 12 bytes, got "
+ str(len(data))
+ " bytes."
)
self.data = data[12:]
else:
if len(data) < 6:
raise ValueError(
"Message is too short, expected at least 6 bytes, got "
+ str(len(data))
+ " bytes."
)
self.data = data[6:]

def to_json(self):
return {
'msg_version': self.msg_version.name,
'msg_id': self.msgId,
'msg_len': self.msgLen,
'data': self.data.hex(' ', 0)
}

def __str__(self):
return str(self.to_json())
33 changes: 33 additions & 0 deletions rusty_server_login/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from enum import Enum


class MessageVersion(Enum):
V0 = '1.0'
V1 = '1.1'


def littleEndianBytesToInt(data: bytes) -> int:
return int.from_bytes(data, byteorder="little")

def bigEndianBytesToInt(data: bytes) -> int:
return int.from_bytes(data, byteorder="big")


def getMessageVersion(msg: bytes) -> str:

if len(msg) < 6:
raise ValueError(
"Message is too short, expected at least 6 bytes, got "
+ str(len(msg))
+ " bytes."
)

# Get the message version
version = littleEndianBytesToInt(msg[4:6])

if version == 0x0101:
return MessageVersion.V1
if version == 0:
return MessageVersion.V0

raise ValueError("Unknown message version: " + str(version))

0 comments on commit 47520f5

Please sign in to comment.