Commit ac739f61 authored by oni's avatar oni

first working version

parent 79e48ea7
#!/usr/bin/python3
import argparse
import json
def main():
parser = argparse.ArgumentParser(description='This is to tunnel alfred and batadv-vis data over Layer3 networks.')
parser.add_argument('-r', default="0", action='store', dest='channel', help='alfred channels to transmit')
parser.add_argument('-z', default=False, action='store_true', dest='dummy', help='alfred channels to transmit')
parser.add_argument('-f', default="0", action='store', dest='dummy1', help='alfred channels to transmit')
args = parser.parse_args()
try:
f = open("/tmp/alfred.json",'r')
data = json.load(f)
print(json.dumps(data[args.channel]))
f.close()
except:
exit(0)
if __name__ == "__main__":
main()
import subprocess
import socket
import gzip
import json
import time
import threading
def sendData(channels, sock,serverAddress):
for channel in channels:
p = subprocess.Popen(['/usr/local/bin/alfred-json','-z','-r',channel, '-f', 'json'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
data, err = p.communicate()
data = json.loads(data.decode("utf-8")).values()
for d in data:
m=json.dumps(d)
message = json.dumps(["alfred",str(channel), m])
message = gzip.zlib.compress(bytes(message,'UTF-8'))
sock.sendto(message, (serverAddress['ip'], serverAddress['port']))
#!/bin/sh
cat /tmp/batadv.json
\ No newline at end of file
import subprocess
import socket
import gzip
import json
import time
import threading
def sendData(interface, sock,serverAddress):
p = subprocess.Popen(['/usr/sbin/batadv-vis','-i',interface,'-f', 'json'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
data, err = p.communicate()
for line in data.splitlines():
line = line.decode('utf-8')
line = json.loads(line)
message = json.dumps(["batadv-vis", json.dumps(line)])
message = gzip.zlib.compress(bytes(message,'UTF-8'))
sock.sendto(message, (serverAddress['ip'], serverAddress['port']))
if __name__ == "__main__":
sendData('bat0',None,None)
{
"server_port": 5050,
"server_address": "10.38.0.50",
"server_bind_to": "10.38.0.50",
"update_interval": 600,
"channels": [158],
"interface": "bat0",
"alfred_out": "/tmp/alfred.json",
"batadv_out": "/tmp/batadv.json"
}
#!/usr/bin/python3
import sys
import argparse
import subprocess
import socket
import gzip
import json
import time
import threading
import alfred
import batadv
import copy
def client(config, channels):
address = {'ip' : config['server_address']}
address['port'] = config['server_port']
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
alfred.sendData(channels,sock,address)
batadv.sendData(config['interface'], sock, address)
def alfred_cleaner(config, alfred_channels,batadv):
while True:
print('cleaner')
for channel in alfred_channels.keys():
to_del = []
for node in alfred_channels[channel].keys():
if time.time() - alfred_channels[channel][node]['time'] > config['update_interval'] * 2:
to_del.append(node)
for node in to_del:
del alfred_channels[channel][node]
f = open(config['alfred_out'],'w')
json.dump(alfred_channels,f)
f.close()
new = list(filter(lambda entry: time.time() - entry['time'] > config['update_interval'] * 2, batadv))
for entry in new:
batadv.remove(entry)
f = open(config['batadv_out'],'w')
output = ""
for entry in batadv:
output += entry['data']+'\n'
f.write(output)
f.close()
time.sleep(10)
def alfred_server(config, alfred_channels,batadv_data):
UDP_IP = config['server_bind_to']
UDP_PORT = config['server_port']
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(10000)
data = gzip.zlib.decompress(data)
rdata = json.loads(data.decode('utf-8'))
if rdata[0] == "alfred":
channel = rdata[1]
message = rdata[2]
node = json.loads(message)
mac = node['network']['mac']
if str(channel) not in alfred_channels:
alfred_channels[str(channel)] = {mac : node}
alfred_channels[str(channel)][mac]['time'] = int(time.time())
else:
alfred_channels[str(channel)][mac] = node
alfred_channels[str(channel)][mac]['time'] = int(time.time())
elif rdata[0] == "batadv-vis":
line = rdata[1]
entry = {'time': time.time()}
entry['data'] = line
batadv_data.append(entry)
else:
print('got unknown data')
def main():
parser = argparse.ArgumentParser(description='This is to tunnel alfred and batadv-vis data over Layer3 networks.')
parser.add_argument('-i', default=False, action='store_true', dest='client', help='enable data input mode (client)')
parser.add_argument('-o', default=False, action='store_true', dest='server', help='enable data output mode (server)')
parser.add_argument('-c', default="config.json", action='store', dest='configFile', help='config file')
parser.add_argument('-r', default=[], action='append', dest='channels', help='alfred channels to transmit')
args = parser.parse_args()
try:
f = open(args.configFile)
config=json.load(f)
except :
print('config file error')
parser.print_help()
exit(0)
if args.client:
print("client mode")
channels = set()
for c in config['channels']:
channels.add(str(c))
for c in args.channels:
channels.add(c)
client(config,channels)
exit(0)
if args.server:
print("server mode")
global_alfred_channels = {}
batadv_data=[]
threading.Thread(target=alfred_server, args=(config,global_alfred_channels,batadv_data)).start()
threading.Thread(target=alfred_cleaner, args=(config,global_alfred_channels,batadv_data)).start()
exit(0)
try:
if args.help:
parser.print_help()
exit(0)
except AttributeError as e:
parser.print_help()
exit(0)
if __name__ == "__main__":
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment