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,8 +20,8 @@ def get(vk):
if isinstance(vk, str):
id = vk
else:
id = vk.to_ascii(encoding='base64')
url ='%s/%s' % (base, id)
id = vk.to_ascii(encoding='base64').decode()
url = '%s/%s' % (base, id)
headers = {
'User-Agent': settings.USER_AGENT
}
@ -40,10 +40,10 @@ def get(vk):
return 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)
sig = sk.sign(data, encoding='base64')
url ='%s/%s' % (base, id)
sig = sk.sign(data, encoding='base64').decode()
url = '%s/%s' % (base, id)
headers = {
'User-Agent': settings.USER_AGENT,
'X-Ed25519-Signature': sig

View file

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

View file

@ -42,7 +42,7 @@ class NodeHandler(ProxyHandler):
if response.body:
response.data = response.body.read()
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-Signature'] = sig
return ProxyHandler._handle_response(self, response)

View file

@ -37,13 +37,13 @@ if os.path.exists(key_path):
vk = sk.get_verifying_key()
else:
sk, vk = ed25519.create_keypair()
with open(key_path, 'w') as fd:
with open(key_path, 'wb') as fd:
os.chmod(key_path, 0600)
fd.write(sk.to_bytes())
os.chmod(key_path, 0400)
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):
from tls import create_certificate

View file

@ -23,6 +23,7 @@ def get_interface():
cmd = ['/sbin/route', '-n', 'get', 'default']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
stdout = stdout.decode()
interface = [[p.strip()
for p in s.split(':', 1)]
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']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
stdout = stdout.decode()
interface = [[p.strip() for p in s.split(':', 1)]
for s in stdout.strip().split('\n') if 'interface' in s]
if interface:
@ -45,6 +47,7 @@ def get_local_ipv4():
cmd = ['ifconfig', interface]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
stdout = stdout.decode()
ips = [l for l in stdout.split('\n') if 'inet ' in l]
if ips:
ip = ips[0].strip().split(' ')[1]
@ -52,6 +55,7 @@ def get_local_ipv4():
cmd = ['ip', 'route', 'show']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
stdout = stdout.decode()
local = [l for l in stdout.split('\n') if 'default' in l]
if local:
dev = local[0].split(' ')[4]
@ -87,9 +91,15 @@ def valid(key, value, sig):
'''
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:
vk.verify(str(sig), str(value), encoding=settings.ENCODING)
vk.verify(sig, value, encoding=settings.ENCODING)
#except ed25519.BadSignatureError:
except:
return False