-
Notifications
You must be signed in to change notification settings - Fork 0
/
libfox.py
executable file
·95 lines (78 loc) · 2.13 KB
/
libfox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import libsqlalchemy
import botconfig
from db_wctf import db
import json
import libverify
from db_wctf.tables import FoxTrack
from random import randint
LOG = logging.getLogger(__name__)
LOG.setLevel(botconfig.DEFAULT_LOGGING)
FIELD_RESULT = 'result'
def json_normalize(obj):
try:
json.dump(obj)
return obj
except Exception:
return str(obj)
def get_rand_mac_address():
return ":".join([
str('{:02x}'.format(randint(1, 254)))
for i in range(6)
])
def get_bssid(data):
bssid = data.get(FoxTrack.bssid.name)
if not bssid:
raise ValueError("No 'bssid' field given")
return libverify.verify_bssid(bssid)
def add_fox(data, source):
""" add new fox """
bssid = get_bssid(data)
ssid = data.get(FoxTrack.ssid.name)
# Im dumb, so lets avoid issues with bad json objects
jdump = json.dumps(source, default=json_normalize)
ins_source = json.loads(jdump)
ins_data = {
FoxTrack.bssid.name: bssid,
FoxTrack.ssid.name: ssid,
FoxTrack.source.name: ins_source,
}
res = libsqlalchemy.add_update_one(
db,
FoxTrack,
ins_data,
)
data[FIELD_RESULT] = str(res)
return data
def update_fox(data):
""" update fox """
bssid = get_bssid(data)
# make sure its an existing record
libsqlalchemy.get_by_id(db, FoxTrack, bssid)
ssid = data.get(FoxTrack.ssid.name)
passphrase = data.get(FoxTrack.passphrase.name)
ins_data = {
FoxTrack.bssid.name: bssid,
FoxTrack.ssid.name: ssid,
FoxTrack.passphrase.name: passphrase,
}
res = libsqlalchemy.add_update_one(
db,
FoxTrack,
ins_data,
bssid, # key to update
)
data[FIELD_RESULT] = str(res)
return data
def delete_fox(data):
""" delete fox entry """
bssid = get_bssid(data)
return {
FIELD_RESULT: libsqlalchemy.delete(db, FoxTrack, bssid),
'deleted': bssid,
}
def get_foxes():
""" return a json list of all foxes """
return libsqlalchemy.get_all(db, FoxTrack)