Skip to content

Commit

Permalink
Handle db transactions consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
jgor committed Jan 15, 2019
1 parent 771bed7 commit 6a87a19
Showing 1 changed file with 24 additions and 37 deletions.
61 changes: 24 additions & 37 deletions dorkbot.py
Expand Up @@ -9,6 +9,7 @@
from urllib.parse import urlparse from urllib.parse import urlparse
except ImportError: except ImportError:
from urlparse import urlparse from urlparse import urlparse
from contextlib import closing
import datetime import datetime
import hashlib import hashlib
import importlib import importlib
Expand Down Expand Up @@ -194,21 +195,18 @@ def __init__(self, database):
sys.exit(1) sys.exit(1)


try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("CREATE TABLE IF NOT EXISTS targets (url VARCHAR PRIMARY KEY)") c.execute("CREATE TABLE IF NOT EXISTS targets (url VARCHAR PRIMARY KEY)")
c.execute("CREATE TABLE IF NOT EXISTS fingerprints (fingerprint VARCHAR PRIMARY KEY)") c.execute("CREATE TABLE IF NOT EXISTS fingerprints (fingerprint VARCHAR PRIMARY KEY)")
self.db.commit()
c.close()
except self.module.Error as e: except self.module.Error as e:
print("ERROR loading database - %s" % e, file=sys.stderr) print("ERROR loading database - %s" % e, file=sys.stderr)
sys.exit(1) sys.exit(1)


def get_targets(self): def get_targets(self):
try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("SELECT url FROM targets") c.execute("SELECT url FROM targets")
targets = [row[0] for row in c.fetchall()] targets = [row[0] for row in c.fetchall()]
c.close()
except self.module.Error as e: except self.module.Error as e:
print("ERROR getting targets - %s" % e, file=sys.stderr) print("ERROR getting targets - %s" % e, file=sys.stderr)
sys.exit(1) sys.exit(1)
Expand All @@ -217,10 +215,9 @@ def get_targets(self):


def get_next_target(self): def get_next_target(self):
try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("SELECT url FROM targets LIMIT 1") c.execute("SELECT url FROM targets LIMIT 1")
row = c.fetchone() row = c.fetchone()
c.close()
except self.module.Error as e: except self.module.Error as e:
print("ERROR getting next target - %s" % e, file=sys.stderr) print("ERROR getting next target - %s" % e, file=sys.stderr)
sys.exit(1) sys.exit(1)
Expand All @@ -230,10 +227,9 @@ def get_next_target(self):


def get_random_target(self): def get_random_target(self):
try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("SELECT url FROM targets ORDER BY RANDOM() LIMIT 1") c.execute("SELECT url FROM targets ORDER BY RANDOM() LIMIT 1")
row = c.fetchone() row = c.fetchone()
c.close()
except self.module.Error as e: except self.module.Error as e:
print("ERROR getting random target - %s" % e, file=sys.stderr) print("ERROR getting random target - %s" % e, file=sys.stderr)
sys.exit(1) sys.exit(1)
Expand All @@ -243,10 +239,8 @@ def get_random_target(self):


def add_target(self, url): def add_target(self, url):
try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("%s INTO targets VALUES (%s)" % (self.insert, self.param), (url,)) c.execute("%s INTO targets VALUES (%s)" % (self.insert, self.param), (url,))
self.db.commit()
c.close()
except self.module.IntegrityError as e: except self.module.IntegrityError as e:
if "UNIQUE constraint failed" in e: if "UNIQUE constraint failed" in e:
return return
Expand All @@ -257,20 +251,17 @@ def add_target(self, url):


def delete_target(self, url): def delete_target(self, url):
try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("DELETE FROM targets WHERE url=(%s)" % self.param, (url,)) c.execute("DELETE FROM targets WHERE url=(%s)" % self.param, (url,))
self.db.commit()
c.close()
except self.module.Error as e: except self.module.Error as e:
print("ERROR deleting target - %s" % e, file=sys.stderr) print("ERROR deleting target - %s" % e, file=sys.stderr)
sys.exit(1) sys.exit(1)


def get_scanned(self, fingerprint): def get_scanned(self, fingerprint):
try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("SELECT fingerprint FROM fingerprints WHERE fingerprint = (%s)" % self.param, (fingerprint,)) c.execute("SELECT fingerprint FROM fingerprints WHERE fingerprint = (%s)" % self.param, (fingerprint,))
row = c.fetchone() row = c.fetchone()
c.close()
except self.module.Error as e: except self.module.Error as e:
print("ERROR looking up fingerprint - %s" % e, file=sys.stderr) print("ERROR looking up fingerprint - %s" % e, file=sys.stderr)
sys.exit(1) sys.exit(1)
Expand All @@ -280,20 +271,16 @@ def get_scanned(self, fingerprint):


def add_fingerprint(self, fingerprint): def add_fingerprint(self, fingerprint):
try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("%s INTO fingerprints VALUES (%s)" % (self.insert, self.param), (fingerprint,)) c.execute("%s INTO fingerprints VALUES (%s)" % (self.insert, self.param), (fingerprint,))
self.db.commit()
c.close()
except self.module.Error as e: except self.module.Error as e:
print("ERROR adding fingerprint - %s" % e, file=sys.stderr) print("ERROR adding fingerprint - %s" % e, file=sys.stderr)
sys.exit(1) sys.exit(1)


def flush_fingerprints(self): def flush_fingerprints(self):
try: try:
c = self.db.cursor() with self.db, closing(self.db.cursor()) as c:
c.execute("DELETE FROM fingerprints") c.execute("DELETE FROM fingerprints")
self.db.commit()
c.close()
except self.module.Error as e: except self.module.Error as e:
print("ERROR flushing fingerprints - %s" % e, file=sys.stderr) print("ERROR flushing fingerprints - %s" % e, file=sys.stderr)
sys.exit(1) sys.exit(1)
Expand Down

0 comments on commit 6a87a19

Please sign in to comment.