diff options
Diffstat (limited to 'monitor/josef_leveldb.py')
| -rwxr-xr-x | monitor/josef_leveldb.py | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/monitor/josef_leveldb.py b/monitor/josef_leveldb.py index 5c7fc8a..b4b3255 100755 --- a/monitor/josef_leveldb.py +++ b/monitor/josef_leveldb.py @@ -8,6 +8,8 @@ import leveldb SEP = ";" # db = None +dbs = {} + def match_domain(d1, d2): # Exact match @@ -31,8 +33,14 @@ def match_domain(d1, d2): def db_open(fn='./cert_db'): - db = leveldb.LevelDB(fn) - return db + global dbs + if fn in dbs: + return dbs[fn] + else: + print "Opening " + fn + db = leveldb.LevelDB(fn) + dbs[fn] = db + return db def db_append(db, key, val): if db is None: @@ -50,11 +58,25 @@ def db_append(db, key, val): tmpl.append(val) db.Put(key,SEP.join(tmpl)) -def db_add_domain_2(db, key, val): - if db is None: - print "ERROR: NO DATABASE SET!" - return +def db_add_domain_2(db_dir, key_in, val, db_in=None): + if db_in == None: + key_list = key_in.split('/')[0].split(',')[0].split('.') + if key_list[-1] == "com": + key = '.'.join(key_list[:-1]) + db = db_open(db_dir + key_list[-1]) + else: + key = '.'.join(key_list) + db = db_open(db_dir + "other") + # key = '.'.join(key_list) + # db = db_open(db_dir + key_list[-1]) + # key = '.'.join(key_list) + # db = db_open(db_dir) + # print "DB: " + db_dir + key_list[-1] + else: + db = db_in + key = key_in + # print "key: ", key, " val: ", val try: tmp = db.Get(key) tmpl = tmp.split(SEP) @@ -67,7 +89,7 @@ def db_add_domain_2(db, key, val): db.Put(key,SEP.join([val])) new_key = key.split('.',1)[-1] if new_key != key: - db_add_domain_2(db,new_key,key) + db_add_domain_2(db,new_key,key, db) def db_add_domain(db, domain, data): @@ -87,32 +109,33 @@ def db_add_domain(db, domain, data): db_append(db, k, data) -def db_add_certs(db, data): - if db is None: +def db_add_certs(db_dir, data): + if db_dir is None: print "ERROR: NO DATABASE SET!" return - # batch = leveldb.WriteBatch() for cert in data: try: - db_add_domain_2(db, cert["subject"].split("CN=")[1], str(cert)) + db_add_domain_2(db_dir, cert["subject"].split("CN=")[1], str(cert)) except IndexError: pass try: for line in cert["SAN"].split("DNS:")[1:]: - db_add_domain_2(db, line, str(cert)) + db_add_domain_2(db_dir, line, str(cert)) except KeyError: pass except IndexError: pass - # db.Write(batch, sync = True) -def db_lookup_domain(db, domain): +def db_lookup_domain(db_dir, domain): domain_list = domain.split('.') res = [] + db = db_open(db_dir + domain_list[-1]) + domain_list = domain_list[:-1] cur_domain = domain_list.pop() + # print cur_domain intermediate = db.Get(cur_domain).split(SEP) last = False one_more = False |
