Onionr/static-data/default-plugins/torgossip/peerdb.py

113 lines
3.6 KiB
Python
Raw Normal View History

2021-02-02 06:58:06 +00:00
"""Onionr - Private P2P Communication.
Torgossip peer safedb interface
"""
from base64 import b32decode
from struct import unpack, pack
2021-02-04 01:52:36 +00:00
from time import time
from typing import List
2021-02-02 06:58:06 +00:00
from utils.identifyhome import identify_home
import safedb
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
2021-02-02 23:24:35 +00:00
class TorGossipPeers: # name it this way to avoid collisions in SharedState
2021-02-02 06:58:06 +00:00
PACK_FORMAT = "qQ"
2021-02-05 02:19:23 +00:00
def __init__(self, encrypt=False):
2021-02-02 06:58:06 +00:00
self.db = safedb.SafeDB(
identify_home() + "/torgossip-peers.db", protected=encrypt)
def _shrink_peer_address(self, peer):
# strip .onion and b32decode peer address for lower database mem usage
if len(peer) == 62: # constant time
return b32decode(peer[:-6]) # O(N)
return peer
def _pack_and_store_info(self, peer, new_score=None, new_seen=None):
# Repack peer information with new value(s) and store it
2021-02-02 23:24:35 +00:00
peer = self._shrink_peer_address(peer)
try:
score, seen = unpack(self.PACK_FORMAT, self.db.get(peer))
except KeyError:
score = seen = 0
2021-02-02 06:58:06 +00:00
if new_score:
score = new_score
if new_seen:
seen = new_seen
self.db.put(peer, pack(self.PACK_FORMAT, score, seen))
def get_highest_score_peers(self, max) -> List:
2021-02-02 23:24:35 +00:00
assert max >= 1
peer = self.db.db_conn.firstkey()
2021-02-05 02:19:23 +00:00
if peer == b'enc':
peer = self.db.db_conn.nextkey(peer)
2021-02-05 04:38:57 +00:00
2021-02-05 02:19:23 +00:00
assert len(peer) == 34
if not peer:
return []
2021-02-02 23:24:35 +00:00
top = [(peer, self.db.get(peer))]
2021-02-05 02:19:23 +00:00
while peer:
peer = self.db.db_conn.nextkey(peer)
if not peer:
break
if peer == b"enc":
continue
2021-02-02 23:24:35 +00:00
peer_data = self.db.get(peer)
overwrite = None
if len(top) != max:
top.append((peer, peer_data))
2021-02-05 02:19:23 +00:00
peer = self.db.db_conn.nextkey(peer)
2021-02-02 23:24:35 +00:00
continue
for count, cur_top in enumerate(top):
# if peer score is greater than any set peer, overwrite
if unpack(self.PACK_FORMAT, cur_top[1])[0] < \
unpack(self.PACK_FORMAT, peer_data)[0]:
overwrite = count
break # below else won't execute, so it will be overwritten
else:
# if not overwriting, go to next peer
2021-02-05 02:19:23 +00:00
peer = self.db.db_conn.nextkey(peer)
2021-02-02 23:24:35 +00:00
continue
top[overwrite] = (peer, peer_data)
return top
2021-02-05 02:19:23 +00:00
def add_score(self, peer, plus):
2021-02-02 06:58:06 +00:00
shrunk = self._shrink_peer_address(peer)
score, _ = unpack("qQ", self.db.get(shrunk))
self._pack_and_store_info(
2021-02-05 02:19:23 +00:00
shrunk, new_score=score + plus)
2021-02-02 06:58:06 +00:00
def update_seen(self, peer):
peer = self._shrink_peer_address(peer)
2021-02-02 23:24:35 +00:00
self._pack_and_store_info(peer, new_seen=int(time()))
2021-02-02 06:58:06 +00:00
2021-02-02 23:24:35 +00:00
def add_peer(self, peer):
2021-02-05 04:38:57 +00:00
self.update_seen(peer)
2021-02-05 02:19:23 +00:00
def remove_peer(self, peer):
peer = self._shrink_peer_address(peer)
del self.db.db_conn[peer]