more fixes, so many API endpoints
[kismet-logviewer.git] / logviewer / devices / views.py
1 from django.shortcuts import render
2 from django.http import HttpResponse, HttpRequest
3 import os
4 import sqlite3
5 import time
6 import json
7 import pprint
8
9 from django.views.decorators.csrf import csrf_exempt
10
11 def load_db(query):
12     dir_list = os.listdir("logs/")
13     connection = sqlite3.connect("logs/"+dir_list[0])
14     cursor = connection.cursor()
15     rows = cursor.execute(query).fetchall()
16     return(rows)
17
18 @csrf_exempt
19 def index(request,devicename):
20     if request.path[0:8] == "/devices":
21         dev=list(load_db("select cast(device as text) from devices where devkey = \""+devicename+"\""))
22         (dev_info,) = dev[0]
23         #dev_string = "{ \"recordsTotal\": "+str(dev_count)+", \"data\": ["
24         #dev_list = list(load_db("select cast(device as text) from devices limit 50"))
25         #for device in dev_list:
26         #    (dev,) = device
27         #    dev_string = dev_string + dev + ","
28         #dev_string = dev_string[:-1]
29         #dev_string = dev_string + "],\"draw\": 5,\"recordsFiltered\": "+str(dev_count)+"}"
30         return HttpResponse(dev_info, content_type='text/json')
31     elif request.path[0:11] == "/datasource":
32         datasource=list(load_db("select cast(json as text) from datasources where uuid = \""+str(devicename)+"\""))
33         (json_result,) = datasource[0]
34         return HttpResponse(json_result, content_type='text/json')
35     elif request.path[0:4] == "/phy":
36         #INCOMPLETE - Need to work out device mappings still
37         devices=list(load_db("select cast(device as text) from devices where type='Wi-Fi AP'"))
38         for device in devices:
39             (json_result,) = device
40             device_json = json.loads(json_result)
41             try:
42                 if str(devicename) == str(device_json['dot11.device']['dot11.device.last_beaconed_ssid_record']['dot11.advertisedssid.ssid_hash']):
43                     print("MATCH")
44                     print(device_json)
45             except:
46                 print("skipping")
47         return HttpResponse("{}", content_type='text/json')