sign local node broadcasts
This commit is contained in:
parent
2f821bf6a7
commit
edd42dfd76
1 changed files with 17 additions and 10 deletions
|
@ -7,8 +7,9 @@ import json
|
||||||
import struct
|
import struct
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
from settings import preferences, server, USER_ID
|
from settings import preferences, server, USER_ID, sk
|
||||||
from node.utils import get_public_ipv6
|
from node.utils import get_public_ipv6
|
||||||
|
from ed25519_utils import valid
|
||||||
|
|
||||||
def can_connect(data):
|
def can_connect(data):
|
||||||
try:
|
try:
|
||||||
|
@ -43,12 +44,13 @@ class LocalNodes(Thread):
|
||||||
ttl = struct.pack('@i', self.TTL)
|
ttl = struct.pack('@i', self.TTL)
|
||||||
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl)
|
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl)
|
||||||
message = json.dumps({
|
message = json.dumps({
|
||||||
'id': USER_ID,
|
|
||||||
'username': preferences.get('username', 'anonymous'),
|
'username': preferences.get('username', 'anonymous'),
|
||||||
'host': self.host,
|
'host': self.host,
|
||||||
'port': server['node_port'],
|
'port': server['node_port'],
|
||||||
})
|
})
|
||||||
s.sendto(message + '\0', (self._BROADCAST, self._PORT))
|
sig = sk.sign(message, encoding='base64')
|
||||||
|
packet = json.dumps([sig, USER_ID, message])
|
||||||
|
s.sendto(packet + '\0', (self._BROADCAST, self._PORT))
|
||||||
|
|
||||||
def receive(self):
|
def receive(self):
|
||||||
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
|
@ -61,7 +63,7 @@ class LocalNodes(Thread):
|
||||||
data, addr = s.recvfrom(1024)
|
data, addr = s.recvfrom(1024)
|
||||||
while data[-1] == '\0':
|
while data[-1] == '\0':
|
||||||
data = data[:-1] # Strip trailing \0's
|
data = data[:-1] # Strip trailing \0's
|
||||||
data = self.validate(data)
|
data = self.verify(data)
|
||||||
if data:
|
if data:
|
||||||
if data['id'] not in self._nodes:
|
if data['id'] not in self._nodes:
|
||||||
thread.start_new_thread(self.new_node, (data, ))
|
thread.start_new_thread(self.new_node, (data, ))
|
||||||
|
@ -69,15 +71,20 @@ class LocalNodes(Thread):
|
||||||
print 'UPDATE NODE', data
|
print 'UPDATE NODE', data
|
||||||
self._nodes[data['id']] = data
|
self._nodes[data['id']] = data
|
||||||
|
|
||||||
def validate(self, data):
|
def verify(self, data):
|
||||||
try:
|
try:
|
||||||
data = json.loads(data)
|
packet = json.loads(data)
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
if len(packet) == 3:
|
||||||
|
sig, user_id, data = packet
|
||||||
|
if valid(user_id, data, sig):
|
||||||
|
message = json.loads(data)
|
||||||
|
message['id'] = user_id
|
||||||
for key in ['id', 'username', 'host', 'port']:
|
for key in ['id', 'username', 'host', 'port']:
|
||||||
if key not in data:
|
if key not in message:
|
||||||
return None
|
return None
|
||||||
return data
|
return message
|
||||||
|
|
||||||
def get(self, user_id):
|
def get(self, user_id):
|
||||||
if user_id in self._nodes:
|
if user_id in self._nodes:
|
||||||
|
|
Loading…
Reference in a new issue