- ACD5EAFFDBA356D5B2B2E0CE494E3BE8AED35CCF0B96F9605BFD73FD3F758286F1908D043274A5480AC02C2D270550E1B061B32C0856E521A4EABA2F9F6B29F3+ 4F78202D4744A3284C00C4AAC9C055F4ABAE95EEA1C51C4ACD519A9723A990D4D1FC336254140F75B7D75995A781935A2A6250DD19C7E7610B6643365A47938Fblatta/lib/state.py(2 . 42)(2 . 53)
1053 import sqlite3
1054 import imp
1055 import hashlib
1056 import logging
1057 from itertools import chain
1058
1059 class State(object):
1060
1061 def __init__(self, server, db_path):
1062 self.server = server
1063 self.conn = sqlite3.connect(db_path)
1064 self.cursor = self.conn.cursor()
1065 self.cursor.execute("create table if not exists at(handle_id integer,\
1066 address text not null,\
1067 port integer not null,\
1068 active_at datetime default null,\
1069 updated_at datetime default current_timestamp,\
1070 unique(handle_id, address, port))")
1071
1072 self.cursor.execute("create table if not exists wot(peer_id integer primary key)")
1073
1074 self.cursor.execute("create table if not exists handles(handle_id integer primary key,\
1075 peer_id integer,\
1076 handle text,\
1077 unique(handle))")
1078
1079 self.cursor.execute("create table if not exists keys(peer_id intenger,\
1080 key text,\
1081 used_at datetime default current_timestamp,\
1082 unique(key))")
1083
1084 self.cursor.execute("create table if not exists logs(\
1085 handle text not null,\
1086 peer_id integer,\
1087 message_bytes blob not null,\
1088 created_at datetime default current_timestamp)")
1089
1090 self.cursor.execute("create table if not exists dedup_queue(\
1091 hash text not null,\
1092 created_at datetime default current_timestamp)")
1093 __instance = None
1094 @staticmethod
1095 def get_instance(socket=None, db_path=None):
1096 if State.__instance == None:
1097 State(socket, db_path)
1098 return State.__instance
1099
1100 def __init__(self, socket, db_path):
1101 if State.__instance != None:
1102 raise Exception("This class is a singleton")
1103 else:
1104 self.socket = socket
1105 self.conn = sqlite3.connect(db_path, check_same_thread=False)
1106 self.cursor = self.conn.cursor()
1107 self.cursor.execute("create table if not exists at(handle_id integer,\
1108 address text not null,\
1109 port integer not null,\
1110 active_at datetime default null,\
1111 updated_at datetime default current_timestamp,\
1112 unique(handle_id, address, port))")
1113
1114 self.cursor.execute("create table if not exists wot(peer_id integer primary key)")
1115
1116 self.cursor.execute("create table if not exists handles(handle_id integer primary key,\
1117 peer_id integer,\
1118 handle text,\
1119 unique(handle))")
1120
1121 self.cursor.execute("create table if not exists keys(peer_id intenger,\
1122 key text,\
1123 used_at datetime default current_timestamp,\
1124 unique(key))")
1125
1126 self.cursor.execute("create table if not exists logs(\
1127 handle text not null,\
1128 peer_id integer,\
1129 message_bytes blob not null,\
1130 created_at datetime default current_timestamp)")
1131
1132 self.cursor.execute("create table if not exists dedup_queue(\
1133 hash text not null,\
1134 created_at datetime default current_timestamp)")
1135 State.__instance = self
1136
1137 def get_at(self, handle=None):
1138 at = []
(60 . 7)(71 . 7)
1140 (handle_id,)).fetchone()[0]
1141 at.append({"handle": h,
1142 "address": "%s:%s" % (address, port),
1143 "active_at": updated_at})
1144 "active_at": updated_at if updated_at else "no packets received from this address"})
1145 return at
1146
1147
(69 . 6)(80 . 7)
1149 self.conn.commit()
1150 result = self.cursor.execute("select hash from dedup_queue where hash=?",
1151 (message_hash,)).fetchone()
1152 logging.debug("checking if %s is dupe" % message_hash)
1153 if(result != None):
1154 return True
1155 else:
(78 . 6)(90 . 7)
1157 self.cursor.execute("insert into dedup_queue(hash)\
1158 values(?)",
1159 (message_hash,))
1160 logging.debug("added %s to dedup" % message_hash)
1161 self.conn.commit()
1162
1163 def get_last_message_hash(self, handle, peer_id=None):
(96 . 9)(109 . 14)
1165 if message_bytes:
1166 return hashlib.sha256(message_bytes[0][:]).digest()
1167 else:
1168 return "0" * 32
1169 return "\x00" * 32
1170
1171 def log(self, handle, message_bytes, peer=None):
1172 if peer != None:
1173 peer_id = peer.peer_id
1174 else:
1175 peer_id = None
1176
1177 def log(self, handle, message_bytes, peer_id=None):
1178 self.cursor.execute("insert into logs(handle, peer_id, message_bytes)\
1179 values(?, ?, ?)",
1180 (handle, peer_id, buffer(message_bytes)))
(124 . 7)(142 . 7)
1182
1183 self.conn.commit()
1184
1185 def update_address_table(self, peer, set_active_at=True):
1186 def update_at(self, peer, set_active_at=True):
1187 row = self.cursor.execute("select handle_id from handles where handle=?",
1188 (peer["handle"],)).fetchone()
1189 if row != None:
(196 . 7)(214 . 7)
1191 (peer_id,)).fetchall()))
1192
1193 def get_peer_handles(self):
1194 handles = list(chain.from_iterable(self.cursor.execute("select handle from handles").fetchall()))
1195 handles = self.listify(self.cursor.execute("select handle from handles").fetchall())
1196 return handles
1197
1198 def get_peers(self):
(209 . 6)(227 . 20)
1200 peers.append(peer)
1201 return peers
1202
1203 def listify(self, results):
1204 return list(chain.from_iterable(results))
1205
1206 def get_keyed_peers(self):
1207 peer_ids = self.listify(self.cursor.execute("select peer_id from keys").fetchall())
1208 peers = []
1209 for peer_id in peer_ids:
1210 handle = self.cursor.execute("select handle from handles where peer_id=?", (peer_id,)).fetchone()[0]
1211 peer = self.get_peer_by_handle(handle)
1212 if not (self.is_duplicate(peers, peer)):
1213 peers.append(peer)
1214 return peers
1215
1216
1217 def get_peer_by_handle(self, handle):
1218 handle_info = self.cursor.execute("select handle_id, peer_id from handles where handle=?",
1219 (handle,)).fetchone()
(219 . 18)(251 . 19)
1221 address = self.cursor.execute("select address, port from at where handle_id=?\
1222 order by updated_at desc limit 1",
1223 (handle_info[0],)).fetchone()
1224 handles = list(chain.from_iterable(self.cursor.execute("select handle from handles where peer_id=?",
1225 (handle_info[1],)).fetchall()))
1226 keys = list(chain.from_iterable(self.cursor.execute("select key from keys where peer_id=?\
1227 handles = self.listify(self.cursor.execute("select handle from handles where peer_id=?",
1228 (handle_info[1],)).fetchall())
1229 keys = self.listify(self.cursor.execute("select key from keys where peer_id=?\
1230 order by used_at desc",
1231 (handle_info[1],)).fetchall()))
1232 return Peer(self.server, {
1233 (handle_info[1],)).fetchall())
1234 return Peer(self.socket, {
1235 "handles": handles,
1236 "peer_id": handle_info[1],
1237 "address": address[0] if address else "",
1238 "port": address[1] if address else "",
1239 "keys": keys
1240 })
1241
1242 def is_duplicate(self, peers, peer):
1243 for existing_peer in peers:
1244 if existing_peer.address == peer.address and existing_peer.port == peer.port: