Skip to content

Instantly share code, notes, and snippets.

@barneygale
Created February 27, 2013 21:14
Show Gist options
  • Save barneygale/5051791 to your computer and use it in GitHub Desktop.
Save barneygale/5051791 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import struct
from twisted.internet import protocol, reactor
PR_VERSION = "51"
MC_VERSION = "1.4.7"
class DowntimeProtocol(protocol.Protocol):
def __init__(self, message):
self.res1 = u'\x00'.join((
u'\xa71',
PR_VERSION,
MC_VERSION,
message,
'0',
'0'))
self.res2 = message
def dataReceived(self, d):
res = self.res1 if d[0] == '\xfe' else self.res2
self.transport.write(
'\xff' +
struct.pack('!h', len(res)) +
res.encode('utf-16be'))
self.transport.loseConnection()
class DowntimeFactory(protocol.ServerFactory):
def __init__(self, message):
self.message = message
def buildProtocol(self, addr):
return DowntimeProtocol(self.message)
def main():
import optparse
parser = optparse.OptionParser()
parser.add_option("-a", "--address", dest="address", default = "", help="address to listen on - defaults to all addresses")
parser.add_option("-p", "--port", dest="port", default="25565", type="int", help="port to listen on")
parser.add_option("-m", "--message", dest="message", default="down for maintenance", help="message to kick users with")
(options, args) = parser.parse_args()
reactor.listenTCP(options.port, DowntimeFactory(options.message), interface=options.address)
reactor.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment