use py3 like str/byte handling

This commit is contained in:
j 2014-09-09 12:24:57 +02:00
parent 4fe6bba4c4
commit 089b987fea
5 changed files with 25 additions and 15 deletions

View file

@ -20,7 +20,7 @@ def get(vk):
if isinstance(vk, str): if isinstance(vk, str):
id = vk id = vk
else: else:
id = vk.to_ascii(encoding='base64') id = vk.to_ascii(encoding='base64').decode()
url = '%s/%s' % (base, id) url = '%s/%s' % (base, id)
headers = { headers = {
'User-Agent': settings.USER_AGENT 'User-Agent': settings.USER_AGENT
@ -40,9 +40,9 @@ def get(vk):
return data return data
def put(sk, data): def put(sk, data):
id = sk.get_verifying_key().to_ascii(encoding='base64') id = sk.get_verifying_key().to_ascii(encoding='base64').decode()
data = json.dumps(data) data = json.dumps(data)
sig = sk.sign(data, encoding='base64') sig = sk.sign(data, encoding='base64').decode()
url = '%s/%s' % (base, id) url = '%s/%s' % (base, id)
headers = { headers = {
'User-Agent': settings.USER_AGENT, 'User-Agent': settings.USER_AGENT,

View file

@ -48,8 +48,8 @@ class LocalNodesBase(Thread):
'port': server['node_port'], 'port': server['node_port'],
'cert': server['cert'] 'cert': server['cert']
}) })
sig = sk.sign(message, encoding=ENCODING) sig = sk.sign(message.encode(), encoding=ENCODING).decode()
packet = json.dumps([sig, USER_ID, message]) packet = json.dumps([sig, USER_ID, message]).encode()
else: else:
packet = None packet = None
return packet return packet
@ -85,7 +85,7 @@ class LocalNodesBase(Thread):
while self._active: while self._active:
data, addr = s.recvfrom(1024) data, addr = s.recvfrom(1024)
if self._active: if self._active:
while data[-1] == '\0': while data[-1] == b'\0':
data = data[:-1] # Strip trailing \0's data = data[:-1] # Strip trailing \0's
data = self.verify(data) data = self.verify(data)
if data: if data:
@ -155,7 +155,7 @@ class LocalNodes4(LocalNodesBase):
s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt (socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self._TTL) s.setsockopt (socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self._TTL)
try: try:
s.sendto(packet + '\0', sockaddr) s.sendto(packet + b'\0', sockaddr)
except: except:
logger.debug('LocalNodes4.send failed', exc_info=1) logger.debug('LocalNodes4.send failed', exc_info=1)
s.close() s.close()
@ -189,7 +189,7 @@ class LocalNodes6(LocalNodesBase):
s = socket.socket(family, socktype, proto) s = socket.socket(family, socktype, proto)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl) s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl)
try: try:
s.sendto(packet + '\0', sockaddr) s.sendto(packet + b'\0', sockaddr)
except: except:
logger.debug('LocalNodes6.send failed', exc_info=1) logger.debug('LocalNodes6.send failed', exc_info=1)
s.close() s.close()

View file

@ -42,7 +42,7 @@ class NodeHandler(ProxyHandler):
if response.body: if response.body:
response.data = response.body.read() response.data = response.body.read()
response.body = None response.body = None
sig = settings.sk.sign(response.data, encoding=settings.ENCODING) sig = settings.sk.sign(response.data, encoding=settings.ENCODING).decode()
response.headers['X-Ed25519-Key'] = settings.USER_ID response.headers['X-Ed25519-Key'] = settings.USER_ID
response.headers['X-Ed25519-Signature'] = sig response.headers['X-Ed25519-Signature'] = sig
return ProxyHandler._handle_response(self, response) return ProxyHandler._handle_response(self, response)

View file

@ -37,13 +37,13 @@ if os.path.exists(key_path):
vk = sk.get_verifying_key() vk = sk.get_verifying_key()
else: else:
sk, vk = ed25519.create_keypair() sk, vk = ed25519.create_keypair()
with open(key_path, 'w') as fd: with open(key_path, 'wb') as fd:
os.chmod(key_path, 0600) os.chmod(key_path, 0600)
fd.write(sk.to_bytes()) fd.write(sk.to_bytes())
os.chmod(key_path, 0400) os.chmod(key_path, 0400)
ENCODING='base64' ENCODING='base64'
USER_ID = vk.to_ascii(encoding=ENCODING) USER_ID = vk.to_ascii(encoding=ENCODING).decode()
if not os.path.exists(tls_cert_path): if not os.path.exists(tls_cert_path):
from tls import create_certificate from tls import create_certificate

View file

@ -23,6 +23,7 @@ def get_interface():
cmd = ['/sbin/route', '-n', 'get', 'default'] cmd = ['/sbin/route', '-n', 'get', 'default']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
stdout = stdout.decode()
interface = [[p.strip() interface = [[p.strip()
for p in s.split(':', 1)] for p in s.split(':', 1)]
for s in stdout.strip().split('\n') if 'interface' in s] for s in stdout.strip().split('\n') if 'interface' in s]
@ -38,6 +39,7 @@ def get_local_ipv4():
cmd = ['/sbin/route', '-n', 'get', 'default'] cmd = ['/sbin/route', '-n', 'get', 'default']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
stdout = stdout.decode()
interface = [[p.strip() for p in s.split(':', 1)] interface = [[p.strip() for p in s.split(':', 1)]
for s in stdout.strip().split('\n') if 'interface' in s] for s in stdout.strip().split('\n') if 'interface' in s]
if interface: if interface:
@ -45,6 +47,7 @@ def get_local_ipv4():
cmd = ['ifconfig', interface] cmd = ['ifconfig', interface]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
stdout = stdout.decode()
ips = [l for l in stdout.split('\n') if 'inet ' in l] ips = [l for l in stdout.split('\n') if 'inet ' in l]
if ips: if ips:
ip = ips[0].strip().split(' ')[1] ip = ips[0].strip().split(' ')[1]
@ -52,6 +55,7 @@ def get_local_ipv4():
cmd = ['ip', 'route', 'show'] cmd = ['ip', 'route', 'show']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
stdout = stdout.decode()
local = [l for l in stdout.split('\n') if 'default' in l] local = [l for l in stdout.split('\n') if 'default' in l]
if local: if local:
dev = local[0].split(' ')[4] dev = local[0].split(' ')[4]
@ -87,9 +91,15 @@ def valid(key, value, sig):
''' '''
validate that value was signed by key validate that value was signed by key
''' '''
vk = ed25519.VerifyingKey(str(key), encoding=settings.ENCODING) if isinstance(sig, unicode):
sig = sig.encode('utf-8')
if isinstance(value, unicode):
value = value.encode('utf-8')
if isinstance(key, unicode):
key = key.encode('utf-8')
vk = ed25519.VerifyingKey(key, encoding=settings.ENCODING)
try: try:
vk.verify(str(sig), str(value), encoding=settings.ENCODING) vk.verify(sig, value, encoding=settings.ENCODING)
#except ed25519.BadSignatureError: #except ed25519.BadSignatureError:
except: except:
return False return False