diff options
author | Josef Gustafsson <josef.gson@gmail.com> | 2015-09-10 11:53:26 +0200 |
---|---|---|
committer | Josef Gustafsson <josef.gson@gmail.com> | 2015-09-10 11:53:26 +0200 |
commit | dbee1f90fc8e49c008bb9032c94eb8be6612c1de (patch) | |
tree | 6f313501ff4277ca5b02e3ea9a8fb04262254a51 /monitor/josef_leveldb.py | |
parent | f6ab7792f3ec3fa173f597c2219bc32f64e86783 (diff) |
adding experimental support for leveldb
Diffstat (limited to 'monitor/josef_leveldb.py')
-rwxr-xr-x | monitor/josef_leveldb.py | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/monitor/josef_leveldb.py b/monitor/josef_leveldb.py new file mode 100755 index 0000000..e985e8d --- /dev/null +++ b/monitor/josef_leveldb.py @@ -0,0 +1,175 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import sys +from josef_lib import * +import leveldb + +SEP = ";" +# db = None + + +def match_domain(d1, d2): + # Exact match + if d1 == d2: + return True + + # Wildcard match + d1l = d1.split('.') + d2l = d2.split('.') + + if d1l[0] == '*': + # print d1l[1:], d2l[-(len(d1l)-1):] + if d1l[1:] == d2l[-(len(d1l)-1):]: + return True + + if d2l[0] == '*': + # print d2l[1:], d1l[-(len(d2l)-1):] + if d2l[1:] == d1l[-(len(d2l)-1):]: + return True + + # No match + return False + + +def db_open(fn='./cert_db'): + db = leveldb.LevelDB(fn) + return db + +def db_append(db, key, val): + if db is None: + print "ERROR: NO DATABASE SET!" + return + + try: + tmp = db.Get(key) + except KeyError: + tmp = "" + tmpl = tmp.split(SEP) + if val in tmpl: + pass + else: + tmpl.append(val) + db.Put(key,SEP.join(tmpl)) + +def db_add_domain(db, domain, data): + if db is None: + print "ERROR: NO DATABASE SET!" + return + + tmpl = domain.split('.') + k = "" + for item in reversed(tmpl): + next_k = item + '.' + k + if k != "": + db_append(db, k[:-1], next_k[:-1]) + k = next_k + db.Delete(k[:-1]) + db_append(db, k[:-1], data) + + +def db_add_certs(db, data): + if db is None: + print "ERROR: NO DATABASE SET!" + return + # print data, type(data) + for cert in data: + try: + db_add_domain(db, cert["subject"].split("CN=")[1], str(cert)) + except: + # print "Failed adding Subject in " + str(cert) + pass + try: + for line in cert["SAN"].split("DNS:")[1:]: + db_add_domain(db, line, str(cert)) + except: + # print "Failed adding SAN in " + str(cert) + pass + + + +def db_lookup_domain(db, domain): + domain_list = domain.split('.') + res = [] + + cur_domain = domain_list.pop() + intermediate = db.Get(cur_domain).split(SEP) + + while True: + try: + cur_domain = domain_list.pop() + "." + cur_domain + except IndexError: + return res + # Prune + next_level = [] + for item in intermediate: + if match_domain(cur_domain, item): + # print item + try: + tmp = db.Get(item) + if tmp[1] == '{': + res.append(tmp[1:-1]) + next_level += tmp.split(SEP) + except KeyError: + # print "Could not find " + item + pass + + else: + intermediate.remove(item) + intermediate = next_level + try: + intermediate.remove("") + except ValueError: + pass + + return res + + +# db_open() +# # print db_lookup_domain("*.cox.com") +# print db.Get("wush.net") + +# f = open("output/cert_data.json") +# max_count = 1 +# for line in f: +# # print max_count +# # try: +# tmp = json.loads(line) +# # print tmp +# # d = tmp["subject"].split("CN=")[1] +# db_add_cert(tmp) +# # print d + +# max_count -= 1 +# if max_count == 0: +# break + # except: + # pass + + # tmp_res = "" + # # print domain_list + # # print tmp_res[:-1] + # last = False + + # for i in range(3): + # try: + # except: + # last = True + # new_res_list = [] + # print len(tmp_res_list) + # print tmp_res + # for item in tmp_res_list: + # if not last: + # if match_domain(tmp_res, item): + # new_res_list.append(item) + # else: + # res.append(item) + # # print item + # tmp_res_list = new_res_list + # return res + + + + + + |