-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdbconnector.py
153 lines (126 loc) · 3.89 KB
/
dbconnector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
# Sqlite database connector
# Author: skorov
import sqlite3
DATABASE = ""
def connect(database):
global DATABASE
DATABASE = database
def createdb():
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS domains (domain text primary key)''')
c.execute('''CREATE TABLE IF NOT EXISTS subdomains (subdomain text, domain text, primary key(subdomain, domain))''')
c.execute('''CREATE TABLE IF NOT EXISTS pushkeys (key text primary key)''')
conn.commit()
except Exception as e:
print(str(e))
def destroydb():
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('''DROP TABLE IF EXISTS domains''')
c.execute('''DROP TABLE IF EXISTS subdomains''')
c.execute('''DROP TABLE IF EXISTS pushkeys''')
conn.commit()
except Exception as e:
print(str(e))
def dbexists():
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute("SELECT * FROM domains")
c.execute("SELECT * FROM subdomains")
c.execute("SELECT * FROM pushkeys")
return True
except Exception:
return False
def getDomains():
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('''SELECT domain FROM domains''')
domains = c.fetchall()
conn.commit()
return [x[0] for x in domains]
except Exception as e:
print(str(e))
def addDomain(domain):
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute("INSERT INTO domains values(?)", [domain])
conn.commit()
except sqlite3.IntegrityError:
print("Domain already added: %s" % domain)
return False
except Exception as e:
print(str(e))
return False
def deleteDomain(domain):
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('DELETE FROM domains WHERE domain = ''?''', [domain])
c.execute('DELETE FROM subdomains WHERE domain = ''?''', [domain])
conn.commit()
except Exception as e:
print(str(e))
return False
def getPushkeys():
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('''SELECT key FROM pushkeys''')
keys = c.fetchall()
conn.commit()
return [x[0] for x in keys]
except Exception as e:
print(str(e))
def addPushkey(key):
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute("INSERT INTO pushkeys values(?)", [key])
conn.commit()
except sqlite3.IntegrityError:
print("Key already added: %s" % key)
return False
except Exception as e:
print(str(e))
return False
def deletePushkey(key):
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('DELETE FROM pushkeys WHERE key = ''?''', [key])
conn.commit()
except Exception as e:
print(str(e))
return False
def getSubdomains(domain):
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('SELECT * FROM subdomains WHERE domain = ''?''', [domain])
subdomains = c.fetchall()
conn.commit()
return [x[0] for x in subdomains]
except Exception as e:
print(str(e))
def addSubdomain(subdomain, domain):
if (domain not in getDomains()):
print("Domain is not being tracked: %s" % domain)
return False
try:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute("INSERT INTO subdomains values(?,?)", [subdomain, domain])
conn.commit()
except sqlite3.IntegrityError:
print("Subdomain already added: %s.%s" % (subdomain, domain))
return False
except Exception as e:
print(str(e))
return False