Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration from python2 to python3 #593

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
build
*.pyc
__pycache__
8 changes: 4 additions & 4 deletions cpyrit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with Pyrit. If not, see <http://www.gnu.org/licenses/>.

from __future__ import with_statement

import os
import sys
Expand All @@ -43,12 +42,12 @@ def read_configfile(filename):
for line in f:
if line.startswith('#') or '=' not in line:
continue
option, value = map(str.strip, line.split('=', 1))
option, value = list(map(str.strip, line.split('=', 1)))
if option in config:
config[option] = value
else:
print >> sys.stderr, "WARNING: Unknown option '%s' " \
"in configfile '%s'" % (option, filename)
print("WARNING: Unknown option '%s' " \
"in configfile '%s'" % (option, filename), file=sys.stderr)
return config


Expand All @@ -68,3 +67,4 @@ def write_configfile(config, filename):
if not os.path.exists(configpath):
os.makedirs(configpath)
write_configfile(cfg, default_configfile)

64 changes: 32 additions & 32 deletions cpyrit/cpyrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@
CPyrit enumerates the available cores and schedules workunits among them.
"""

from __future__ import with_statement

from collections import deque
import BaseHTTPServer
import http.server
import hashlib
import random
import socket
import sys
import threading
import time
import uuid
import util
from . import util
import warnings
import xmlrpclib

import config
import network
import storage
import _cpyrit_cpu
import xmlrpc.client

from . import config
from . import network
from . import storage
import _cpyrit_cpu
from functools import reduce

# prevent call to socket.getfqdn
def fast_address_string(self):
return '%s' % self.client_address[0]
BaseHTTPServer.BaseHTTPRequestHandler.address_string = fast_address_string
http.server.BaseHTTPRequestHandler.address_string = fast_address_string
del fast_address_string


Expand Down Expand Up @@ -207,8 +207,8 @@ def run(self):
import _cpyrit_opencl
except ImportError:
pass
except Exception, e:
print >> sys.stderr, "Failed to load Pyrit's OpenCL-core ('%s')." % e
except Exception as e:
print("Failed to load Pyrit's CAL-core ('%s')." % e, file=sys.stderr)
else:
version_check(_cpyrit_opencl)

Expand All @@ -230,8 +230,8 @@ def __init__(self, queue, platform_idx, dev_idx):
import _cpyrit_cuda
except ImportError:
pass
except Exception, e:
print >> sys.stderr, "Failed to load Pyrit's CUDA-core ('%s')." % e
except Exception as e:
print("Failed to load Pyrit's CUDA-core ('%s')." % e, file=sys.stderr)
else:
version_check(_cpyrit_cuda)

Expand All @@ -252,8 +252,8 @@ def __init__(self, queue, dev_idx):
import _cpyrit_calpp
except ImportError:
pass
except Exception, e:
print >> sys.stderr, "Failed to load Pyrit's CAL-core ('%s')." % e
except Exception as e:
print("Failed to load Pyrit's CAL-core ('%s')." % e, file=sys.stderr)
else:
version_check(_cpyrit_calpp)

Expand Down Expand Up @@ -301,7 +301,7 @@ def __init__(self, core):

def run(self):
while True:
for _, client in self.core.clients.items():
for _, client in list(self.core.clients.items()):
if time.time() - client.lastseen > 15.0:
self.core.rpc_unregister(uuid)
time.sleep(3)
Expand Down Expand Up @@ -342,7 +342,7 @@ def _get_client(self, uuid):
client.ping()
return client
else:
raise xmlrpclib.Fault(403, "Client unknown or timed-out")
raise xmlrpc.client.Fault(403, "Client unknown or timed-out")

def rpc_register(self, uuids):
with self.client_lock:
Expand Down Expand Up @@ -373,7 +373,7 @@ def rpc_gather(self, client_uuid, buffersize):
else:
client.workunits.append((essid, pwlist))
key, buf = storage.PAW2_Buffer.pack(pwlist)
return (essid, xmlrpclib.Binary(buf))
return (essid, xmlrpc.client.Binary(buf))

def rpc_scatter(self, client_uuid, encoded_buf):
client = self._get_client(client_uuid)
Expand All @@ -387,7 +387,7 @@ def rpc_scatter(self, client_uuid, encoded_buf):
if len(buf) != len(pwlist) * 32:
raise ValueError("Result has invalid size of %i. Expected %i." %
(len(buf), len(pwlist) * 32))
results = [buf[i * 32:i * 32 + 32] for i in xrange(len(pwlist))]
results = [buf[i * 32:i * 32 + 32] for i in range(len(pwlist))]
self.compTime = time.time() - self.startTime
self.resCount += len(results)
self.callCount += 1
Expand All @@ -404,7 +404,7 @@ def rpc_revoke(self, client_uuid):

def __iter__(self):
with self.client_lock:
return self.clients.values().__iter__()
return list(self.clients.values()).__iter__()


class CPyrit(object):
Expand Down Expand Up @@ -459,14 +459,14 @@ def __init__(self):


# CPUs
for i in xrange(util.ncpus):
for i in range(util.ncpus):
self.cores.append(CPUCore(queue=self))


# Network

if config.cfg['rpc_server'] == 'true':
for port in xrange(17935, 18000):
for port in range(17935, 18000):
try:
ncore = NetworkCore(queue=self, port=port)
except socket.error:
Expand All @@ -476,7 +476,7 @@ def __init__(self):
self.cores.append(ncore)
if config.cfg['rpc_announce'] == 'true':
cl = config.cfg['rpc_knownclients'].split(' ')
cl = filter(lambda x: len(x) > 0, map(str.strip, cl))
cl = [x for x in map(str.strip, cl) if len(x) > 0]
bcst = config.cfg['rpc_announce_broadcast'] == 'true'
self.announcer = network.NetworkAnnouncer(port=port, \
clients=cl, \
Expand All @@ -501,7 +501,7 @@ def _check_cores(self):
raise SystemError("The core '%s' has died unexpectedly" % core)

def _len(self):
return sum((sum((len(pwlist) for pwlist in pwdict.itervalues()))
return sum((sum((len(pwlist) for pwlist in list(pwdict.values())))
for essid, pwdict in self.inqueue))

def __len__(self):
Expand Down Expand Up @@ -694,7 +694,7 @@ def _scatter(self, essid, passwords, results):
for idx, length in slices:
self.outqueue[idx] = list(results[ptr:ptr + length])
ptr += length
for idx in sorted(self.outqueue.iterkeys(), reverse=True)[1:]:
for idx in sorted(iter(list(self.outqueue.keys())), reverse=True)[1:]:
res = self.outqueue[idx]
o_idx = idx + len(res)
if o_idx in self.outqueue:
Expand Down Expand Up @@ -760,16 +760,16 @@ def __exit__(self, exc_type, exc_val, exc_tb):
if self.cp is not None:
self.cp.shutdown()

def next(self):
def __next__(self):
while True:
try:
key = self.iterkeys.next()
key = next(self.iterkeys)
except StopIteration:
if self.yieldNewResults:
solvedPMKs = self.cp.dequeue(block=True)
if solvedPMKs is not None:
solvedEssid, solvedKey, solvedPasswords = self.workunits.pop(0)
solvedResults = zip(solvedPasswords, solvedPMKs)
solvedResults = list(zip(solvedPasswords, solvedPMKs))
self.storage.essids[solvedEssid, solvedKey] = solvedResults
return solvedResults
assert len(self.workunits) == 0
Expand All @@ -789,7 +789,7 @@ def next(self):
solvedPMKs = self.cp.dequeue(block=False)
if solvedPMKs is not None:
solvedEssid, solvedKey, solvedPasswords = self.workunits.pop(0)
solvedResults = zip(solvedPasswords, solvedPMKs)
solvedResults = list(zip(solvedPasswords, solvedPMKs))
self.storage.essids[solvedEssid, solvedKey] = solvedResults
return solvedResults

Expand All @@ -816,7 +816,7 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
self.cp.shutdown()

def next(self):
def __next__(self):
pwbuffer = []
for line in self.iterator:
pw = line.strip('\r\n')[:63]
Expand All @@ -828,10 +828,10 @@ def next(self):
pwbuffer = []
solvedPMKs = self.cp.dequeue(block=False)
if solvedPMKs is not None:
return zip(self.workunits.pop(0), solvedPMKs)
return list(zip(self.workunits.pop(0), solvedPMKs))
if len(pwbuffer) > 0:
self.workunits.append(pwbuffer)
self.cp.enqueue(self.essid, self.workunits[-1])
for solvedPMKs in self.cp:
return zip(self.workunits.pop(0), solvedPMKs)
return list(zip(self.workunits.pop(0), solvedPMKs))
raise StopIteration
33 changes: 17 additions & 16 deletions cpyrit/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
# You should have received a copy of the GNU General Public License
# along with Pyrit. If not, see <http://www.gnu.org/licenses/>.

from __future__ import with_statement


import hashlib
import socket
import time
import threading
import xmlrpclib
import xmlrpc.client

import storage
import util
from . import storage
from . import util


class NetworkClient(util.Thread):
Expand All @@ -37,7 +37,7 @@ class NetworkGatherer(threading.Thread):
def __init__(self, client):
threading.Thread.__init__(self)
self.client = client
self.server = xmlrpclib.ServerProxy("http://%s:%s" % \
self.server = xmlrpc.client.ServerProxy("http://%s:%s" % \
client.srv_addr)
self.shallStop = False
self.setDaemon(True)
Expand All @@ -64,7 +64,7 @@ def shutdown(self):

def __init__(self, srv_addr, enqueue_callback, known_uuids):
util.Thread.__init__(self)
self.server = xmlrpclib.ServerProxy("http://%s:%s" % srv_addr)
self.server = xmlrpc.client.ServerProxy("http://%s:%s" % srv_addr)
self.srv_uuid, self.uuid = self.server.register(";".join(known_uuids))
if not self.uuid:
raise KeyError("Loop detected to %s" % self.srv_uuid)
Expand Down Expand Up @@ -92,7 +92,7 @@ def run(self):
buf = ''.join(solvedPMKs)
md = hashlib.sha1()
md.update(buf)
encoded_buf = xmlrpclib.Binary(md.digest() + buf)
encoded_buf = xmlrpc.client.Binary(md.digest() + buf)
self.server.scatter(self.uuid, encoded_buf)
self.stat_sent += len(solvedPMKs)
self.ping()
Expand All @@ -118,7 +118,7 @@ class NetworkServer(util.Thread):

def __init__(self):
util.Thread.__init__(self)
import cpyrit
from . import cpyrit
self.cp = cpyrit.CPyrit()
self.clients_lock = threading.Lock()
self.clients = {}
Expand All @@ -131,9 +131,9 @@ def __init__(self):

def addClient(self, srv_addr):
with self.clients_lock:
if any(c.srv_addr == srv_addr for c in self.clients.itervalues()):
if any(c.srv_addr == srv_addr for c in list(self.clients.values())):
return
known_uuids = set(c.srv_uuid for c in self.clients.itervalues())
known_uuids = set(c.srv_uuid for c in list(self.clients.values()))
if self.cp.ncore_uuid is not None:
known_uuids.add(self.cp.ncore_uuid)
try:
Expand All @@ -144,7 +144,8 @@ def addClient(self, srv_addr):
client.start()
self.clients[client.uuid] = client

def enqueue(self, uuid, (essid, pwlist)):
def enqueue(self, uuid, xxx_todo_changeme):
(essid, pwlist) = xxx_todo_changeme
with self.clients_lock:
if uuid not in self.clients:
raise KeyError("Client unknown or timed-out")
Expand All @@ -167,7 +168,7 @@ def run(self):
client.scatter(solvedPMKs)
self.stat_scattered += len(solvedPMKs)
with self.clients_lock:
for client in self.clients.values():
for client in list(self.clients.values()):
if not client.isAlive() or \
time.time() - client.lastseen > 15.0:
del self.clients[client.uuid]
Expand All @@ -177,7 +178,7 @@ def run(self):

def __contains__(self, srv_addr):
with self.clients_lock:
i = self.clients.itervalues()
i = iter(list(self.clients.values()))
return any(c.srv_addr == srv_addr for c in i)

def __len__(self):
Expand All @@ -186,12 +187,12 @@ def __len__(self):

def __iter__(self):
with self.clients_lock:
return self.clients.values().__iter__()
return list(self.clients.values()).__iter__()

def shutdown(self):
self.shallStop = True
with self.clients_lock:
for client in self.clients.itervalues():
for client in list(self.clients.values()):
client.shutdown()
self.join()

Expand Down Expand Up @@ -282,7 +283,7 @@ def waitForAnnouncement(self, block=True, timeout=None):
def __iter__(self):
return self

def next(self):
def __next__(self):
return self.waitForAnnouncement(block=True)

def shutdown(self):
Expand Down
Loading