-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add Python version 3.12.3 and Twisted dependency 24.3.0
- Loading branch information
Showing
7 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.12.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ readme = "README.md" | |
|
||
[tool.poetry.dependencies] | ||
python = "^3.12" | ||
twisted = "^24.3.0" | ||
|
||
|
||
[build-system] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |