d4-core/server/server.py

58 lines
1.5 KiB
Python
Raw Normal View History

2018-12-05 16:24:10 +01:00
#!/usr/bin/env python3
import sys
from twisted.internet import ssl, task, protocol, endpoints, defer
from twisted.python import log
from twisted.python.modules import getModule
from twisted.internet.protocol import Protocol
2018-12-05 17:05:46 +01:00
from ctypes import *
2018-12-05 16:24:10 +01:00
class Echo(Protocol):
#def __init__(self, factory):
# self.factory = factory
def dataReceived(self, data):
print(data)
2018-12-05 17:05:46 +01:00
d = unpack(D4Header, data)
print('-----')
print(d.version)
print(d.type)
print('{}-{}'.format(d.uuid1, d.uuid2))
print(d.timestamp)
print('{}-{}-{}-{}'.format(d.hmac1, d.hmac2, d.hmac3, d.hmac4))
print(d.size)
class D4Header(Structure):
_fields_ = [
("version", c_uint8),
("type", c_uint8),
("uuid1", c_uint64),
("uuid2", c_uint64),
("timestamp", c_uint64),
("hmac1", c_uint64),
("hmac2", c_uint64),
("hmac3", c_uint64),
("hmac4", c_uint64),
("size", c_uint32),
]
def unpack(ctype, buffer):
c_str = create_string_buffer(buffer)
return cast(pointer(c_str), POINTER(ctype)).contents
2018-12-05 16:24:10 +01:00
def main(reactor):
log.startLogging(sys.stdout)
certData = getModule(__name__).filePath.sibling('server.pem').getContent()
certificate = ssl.PrivateCertificate.loadPEM(certData)
factory = protocol.Factory.forProtocol(Echo)
reactor.listenSSL(4443, factory, certificate.options())
return defer.Deferred()
if __name__ == "__main__":
task.react(main)