summaryrefslogtreecommitdiffstats
path: root/Reaktor/UDP
diff options
context:
space:
mode:
Diffstat (limited to 'Reaktor/UDP')
-rw-r--r--Reaktor/UDP/README7
-rw-r--r--Reaktor/UDP/config15
-rwxr-xr-xReaktor/UDP/index82
3 files changed, 69 insertions, 35 deletions
diff --git a/Reaktor/UDP/README b/Reaktor/UDP/README
index ad7117fe..587c8046 100644
--- a/Reaktor/UDP/README
+++ b/Reaktor/UDP/README
@@ -5,6 +5,7 @@ From 2011-09-16:
{
"port": 1337,
"pattern": "XXYY",
+ "comment" : "does the right thing",
"action" : {
"POST": {
"url" : "xyz",
@@ -15,10 +16,9 @@ From 2011-09-16:
]
## Head definition
{
- "bind" : "127.0.0.1",
+ "bind_addr" : "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.
@@ -35,6 +35,7 @@ terminator is optional which lets the user define the EOM terminator.
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
+in the process in envp => "payload" will be allocated for the given payload from the network
"PROCESS" : {
{
@@ -43,7 +44,7 @@ stdout/stderr are optional and if left away data will be written to real stdout/
"bc"
],
"envp": {
- "was": "geht"
+ "was": "geht",
},
"stdout": "uri:///path/to/somewhere",
"stderr": "uri:///path/to/somewhere"
diff --git a/Reaktor/UDP/config b/Reaktor/UDP/config
new file mode 100644
index 00000000..623c721c
--- /dev/null
+++ b/Reaktor/UDP/config
@@ -0,0 +1,15 @@
+[
+ {
+ "comment" : "listener for licht.shack",
+ "bind_addr" : "0.0.0.0",
+ "port" : 2342,
+ "pattern" : "",
+ "action" : {
+ "PROCESS" : {
+ "path" : "/krebs/Reaktor/udp_commands/licht_resolver",
+ "argv" : [ ],
+ "envp" : { }
+ }
+ }
+ }
+]
diff --git a/Reaktor/UDP/index b/Reaktor/UDP/index
index b7a21508..ffe3d6b1 100755
--- a/Reaktor/UDP/index
+++ b/Reaktor/UDP/index
@@ -1,9 +1,9 @@
#!/usr/bin/python
import asyncore, socket
-import logging
+import logging,subprocess
import re
-import sys
+import json
log = logging.getLogger('ubot')
def enable_syslog(logger):
@@ -16,37 +16,33 @@ def enable_syslog(logger):
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
+from twisted.internet.protocol import DatagramProtocol
+from twisted.internet import reactor
+from twisted.application.internet import MulticastServer
+from socket import SOL_SOCKET,SO_BROADCAST
+class ubot(DatagramProtocol):
+# def startProtocol(self):
+# log.info("Starting Listener for Multicast")
+# self.transport.joinGroup("255.255.255.255")
-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
+ def startProtocol(self):
+ log.info("starting Protocol at host (%s)" % self.bind_addr)
+ #self.transport.
+ if self.bind_addr != "255.255.255.255":
+ self.transport.joinGroup(self.bind_addr)
+ else:
+ self.transport.socket.setsockopt(SOL_SOCKET,SO_BROADCAST,True)
+ def __init__(self, pattern,action,bind_addr,**kwargs):
+ #DatagramProtocol.__init__(self)
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")
+ self.bind_addr = bind_addr
- def handle_read(self):
- self.data,addr = self.recvfrom(2048)
+ def datagramReceived(self,datagram,addr):
+ self.data = datagram
log.debug('<< %s' % self.data)
-
if self.find_pattern():
self.start_action()
@@ -59,18 +55,40 @@ class ubot(asyncore.dispatcher):
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)
+ self.start_process()
+ self.start_post()
+
+ def start_process(self):
+ try:
+ act = self.action["PROCESS"]
+ proc = []
+ proc.append(act["path"])
+ proc.extend(act["argv"])
+
+ env = act["envp"]
+ env["payload"] = json.dumps(self.data)
+ log.info("Starting Process: %s (env: %s)" % (proc,env))
+ subprocess.Popen(proc,env=env)
+ except Exception as e:
+ log.error(e)
+ def start_post(self):
+ pass
+
+def load_conf(conf_file):
+ return json.load(open(conf_file))
+
if __name__ == "__main__":
import os
#enable_syslog(log)
+ HERE = os.path.dirname(os.path.realpath(__file__))
lol = logging.DEBUG if os.environ.get('debug',False) else logging.INFO
logging.basicConfig(level=lol)
- ubot(1337,r'abc',{})
- asyncore.loop()
+ for i in load_conf("%s/config" %HERE):
+ reactor.listenMulticast(i["port"], ubot(**i))
+ reactor.run()