Commit d41daa7d authored by zhifan huang's avatar zhifan huang

tunnel update -> 3

parent 5609588d
......@@ -65,7 +65,7 @@ class Cache(object):
@staticmethod
def _selectConfig(execute): # BBB: blob
return ((k, str(v) if type(v) is buffer else v)
return ((k, str(v) if type(v) is memoryview else v)
for k, v in execute("SELECT * FROM config"))
def _loadConfig(self, config):
......@@ -100,13 +100,13 @@ class Cache(object):
v = self._decrypt(v.decode('base64'))
elif k in base64:
v = v.decode('base64')
elif type(v) is unicode:
v = str(v)
elif type(v) is str:
v = bytes(v)
elif isinstance(v, (list, dict)):
k += ':json'
v = json.dumps(v)
config[k] = v
except socket.error, e:
except socket.error as e:
logging.warning(e)
return
except Exception:
......@@ -133,7 +133,7 @@ class Cache(object):
# BBB: Use buffer because of http://bugs.python.org/issue13676
# on Python 2.6
db.executemany("INSERT OR REPLACE INTO config VALUES(?,?)",
((k, buffer(v) if k in base64 or
((k, memoryview(v) if k in base64 or
k.startswith('babel_hmac') else v)
for k, v in config.iteritems()))
self._loadConfig(config.iteritems())
......@@ -240,7 +240,7 @@ class Cache(object):
try:
bootpeer = self._registry.getBootstrapPeer(self._prefix)
prefix, address = self._decrypt(bootpeer).split()
except (socket.error, subprocess.CalledProcessError, ValueError), e:
except (socket.error, subprocess.CalledProcessError, ValueError) as e:
logging.warning('Failed to bootstrap (%s)',
e if bootpeer else 'no peer returned')
else:
......@@ -276,5 +276,5 @@ class Cache(object):
def getCountry(self, ip):
try:
return self._registry.getCountry(self._prefix, ip)
except socket.error, e:
except socket.error as e:
logging.warning('Failed to get country (%s)', ip)
#!/usr/bin/python2
#!/usr/bin/python3
import os
import sys
import unittest
import time
from mock import patch, Mock
from unittest.mock import patch, Mock
from re6st import tunnel
from re6st import x509
......
#!/usr/bin/python2
import os
import sys
#!/usr/bin/python3
import unittest
from mock import patch
from unittest.mock import patch
from re6st import tunnel
......
......@@ -66,9 +66,9 @@ def create_cert_file(pkey_file, cert_file, ca, ca_key, prefix, serial):
pkey, csr = generate_csr()
cert = generate_cert(ca, ca_key, csr, prefix, serial)
with open(pkey_file, 'w') as f:
f.write(pkey)
f.write(pkey.decode())
with open(cert_file, 'w') as f:
f.write(cert)
f.write(cert.decode())
return pkey, cert
......@@ -94,9 +94,9 @@ def create_ca_file(pkey_file, cert_file, serial=0x120010db80042):
cert.sign(key, "sha512")
with open(pkey_file, 'w') as pkey_file:
pkey_file.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
pkey_file.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key).decode())
with open(cert_file, 'w') as cert_file:
cert_file.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
cert_file.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode())
return key, cert
......
......@@ -243,14 +243,14 @@ class BaseTunnelManager(object):
self._country = {}
address_dict = {family: self._updateCountry(address)
for family, address in address_dict.iteritems()}
for family, address in address_dict.items()}
elif cache.same_country:
sys.exit("Can not respect 'same_country' network configuration"
" (GEOIP2_MMDB not set)")
self._address = {family: utils.dump_address(address)
for family, address in address_dict.iteritems()
for family, address in address_dict.items()
if address}
cache.my_address = ';'.join(self._address.itervalues())
cache.my_address = ';'.join(self._address.values())
self.sock = socket.socket(socket.AF_INET6,
socket.SOCK_DGRAM | socket.SOCK_CLOEXEC)
......@@ -475,8 +475,8 @@ class BaseTunnelManager(object):
# Don't send country to old nodes
if self._getPeer(peer).protocol < 7:
return ';'.join(','.join(a.split(',')[:3]) for a in
';'.join(self._address.itervalues()).split(';'))
return ';'.join(self._address.itervalues())
';'.join(self._address.values()).split(';'))
return ';'.join(self._address.values())
elif not code: # network version
if peer:
try:
......@@ -581,8 +581,8 @@ class BaseTunnelManager(object):
if (not self.NEED_RESTART.isdisjoint(changed)
or version.protocol < self.cache.min_protocol
# TODO: With --management, we could kill clients without restarting.
or not all(crl.isdisjoint(serials.itervalues())
for serials in self._served.itervalues())):
or not all(crl.isdisjoint(serials.values())
for serials in self._served.values())):
# Wait at least 1 second to broadcast new version to neighbours.
self.selectTimeout(time.time() + 1 + self.cache.delay_restart,
self._restart)
......@@ -711,7 +711,7 @@ class TunnelManager(BaseTunnelManager):
self._client_count = client_count
self.new_iface_list = deque('re6stnet' + str(i)
for i in xrange(1, self._client_count + 1))
for i in range(1, self._client_count + 1))
self._free_iface_list = []
def close(self):
......@@ -931,7 +931,7 @@ class TunnelManager(BaseTunnelManager):
neighbours = self.ctl.neighbours
# Collect all nodes known by Babel
peers = {prefix
for neigh_routes in neighbours.itervalues()
for neigh_routes in neighbours.values()
for prefix in neigh_routes[1]
if prefix}
# Keep only distant peers.
......@@ -1039,7 +1039,7 @@ class TunnelManager(BaseTunnelManager):
if self.cache.same_country:
address = self._updateCountry(address)
self._address[family] = utils.dump_address(address)
self.cache.my_address = ';'.join(self._address.itervalues())
self.cache.my_address = ';'.join(self._address.values())
def broadcastNewVersion(self):
self._babel_dump_new_version()
......
......@@ -100,7 +100,8 @@ class Cert(object):
self.key = crypto.load_privatekey(crypto.FILETYPE_PEM, f.read())
if cert:
with open(cert) as f:
self.cert = self.loadVerify(f.read())
# cert need to be bytes type
self.cert = self.loadVerify(f.read().encode())
@property
def prefix(self):
......@@ -128,7 +129,8 @@ class Cert(object):
"CA Certificate", registry.getCa)
return min(next_renew, ca_renew)
def loadVerify(self, cert, strict=False, type=crypto.FILETYPE_PEM):
# because use cert as para for communicate, so type should be bytes
def loadVerify(self, cert: bytes, strict=False, type=crypto.FILETYPE_PEM):
try:
r = crypto.load_certificate(type, cert)
except crypto.Error:
......@@ -149,7 +151,8 @@ class Cert(object):
# error is printed to standard output.
for err in err, out:
for x in err.splitlines():
if x.startswith('error '):
# here err and out is bytes
if x.startswith(b'error '):
x, msg = x.split(':', 1)
_, code, _, depth, _ = x.split(None, 4)
raise VerifyError(int(code), int(depth), msg.strip())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment