diff options
Diffstat (limited to 'Reaktor')
-rw-r--r-- | Reaktor/UDP/README | 51 | ||||
-rwxr-xr-x | Reaktor/UDP/index | 76 |
2 files changed, 127 insertions, 0 deletions
diff --git a/Reaktor/UDP/README b/Reaktor/UDP/README new file mode 100644 index 00000000..ad7117fe --- /dev/null +++ b/Reaktor/UDP/README @@ -0,0 +1,51 @@ +# Specifications +From 2011-09-16: +## Use Case + [ + { + "port": 1337, + "pattern": "XXYY", + "action" : { + "POST": { + "url" : "xyz", + "data" : "abc" + } + } + } + ] +## Head definition + { + "bind" : "127.0.0.1", + "port" : 1234, + "pattern" : "XXZZ", + "terminator" : "\r\n" + "action" : { }, + } +bind is an optional entry which lets the user define a bind address for the server. +terminator is optional which lets the user define the EOM terminator. + +## Actions +### POST +"POST" : { + "url" : "http://euer.krebsco.de/", + "data" : "{ \"something\" : \"else\" }" + } + +### PROCESS +Process is taken from //hyper/process/test/bc.json - rev:a7fd3f + +stdout/stderr are optional and if left away data will be written to real stdout/stderr + + "PROCESS" : { + { + "path": "/usr/bin/bc", + "argv": [ + "bc" + ], + "envp": { + "was": "geht" + }, + "stdout": "uri:///path/to/somewhere", + "stderr": "uri:///path/to/somewhere" + } + } diff --git a/Reaktor/UDP/index b/Reaktor/UDP/index new file mode 100755 index 00000000..b7a21508 --- /dev/null +++ b/Reaktor/UDP/index @@ -0,0 +1,76 @@ +#!/usr/bin/python + +import asyncore, socket +import logging +import re +import sys +log = logging.getLogger('ubot') + +def enable_syslog(logger): + import logging.handlers as handlers + from logging import Formatter + hdlr = handlers.SysLogHandler( + facility=handlers.SysLogHandler.LOG_DAEMON) + formatter = Formatter( + '%(filename)s: %(levelname)s: %(message)s') + hdlr.setFormatter(formatter) + logger.addHandler(hdlr) + + +class ubot(asyncore.dispatcher): + """ UDP Bot """ + def __init__(self, port,pattern,action,bind_addr="",): + asyncore.dispatcher.__init__(self) + self.bind_addr = bind_addr + self.port = port + self.data = '' + self.pattern = pattern + self.action = action + self.bind_socket() + + def bind_socket(self): + """ + if the socket is already bound we want to reuse this socket anyway + """ + self.create_socket(socket.AF_INET,socket.SOCK_DGRAM) + self.set_reuse_addr() + self.socket.setsockopt( + socket.SOL_SOCKET,socket.SO_BROADCAST,1) + log.info("Binding Socket at %s:%d" + %(self.bind_addr,self.port)) + self.bind( (self.bind_addr,self.port) ) + + def handle_connect(self): + log.info("Server Started") + + def handle_read(self): + self.data,addr = self.recvfrom(2048) + log.debug('<< %s' % self.data) + + if self.find_pattern(): + self.start_action() + + def find_pattern(self): + """ returns true if own pattern is found""" + log.debug("Pattern is %s" %self.pattern) + ret = re.search(self.pattern,self.data) + if ret: + log.info("Match \"%s\" with pattern \"%s\"" % ((ret.string.strip()),self.pattern)) + else: + log.info("No Match") + return ret + + + def start_action(self): + """ runs all the defined actions""" + log.debug("Actions: %s" % str(self.action)) + log.error("not yet implemented") + sys.exit(23) + +if __name__ == "__main__": + import os + #enable_syslog(log) + lol = logging.DEBUG if os.environ.get('debug',False) else logging.INFO + logging.basicConfig(level=lol) + ubot(1337,r'abc',{}) + asyncore.loop() |