Permalink
Browse files

selfstats improving

  • Loading branch information...
1 parent 113bada commit 2d1d39bff12f3616449464658eba8d9b5c5505cc David Fendrich committed Apr 3, 2012
Showing with 187 additions and 54 deletions.
  1. +7 −28 activity_store.py
  2. +49 −4 models.py
  3. +3 −2 selfspy.py
  4. +128 −20 selfstats.py
View
@@ -1,5 +1,3 @@
-import zlib
-import json
import time
from datetime import datetime
NOW = datetime.now
@@ -15,19 +13,12 @@
#Mouse buttons: left button: 1, middle: 2, right: 3, scroll up: 4, down:5
-def pad(s, padnum):
- ls = len(s)
- if ls % padnum == 0:
- return s
- return s + '\0' * (padnum - (ls % padnum))
-
class ActivityStore:
def __init__(self, db_name, encrypter=None):
self.session_maker = models.initialize(db_name)
self.session = None
- if encrypter:
- self.encrypter = encrypter
+ models.ENCRYPTER = encrypter
self.nrmoves = 0
self.latestx = 0
@@ -36,6 +27,7 @@ def __init__(self, db_name, encrypter=None):
self.specials_in_row = 0
self.curtext = u""
+ self.keys = []
self.timings = []
self.last_key_time = time.time()
@@ -59,16 +51,6 @@ def close(self):
self.sniffer.cancel()
self.store_keys()
- def maybe_encrypt(self, s):
- if self.encrypter:
- s = pad(s, 8)
- s = self.encrypter.encrypt(s)
- return s
-
- def timings_to_str(self):
- z = zlib.compress(json.dumps(self.timings))
- return self.maybe_encrypt(z)
-
def store_window(self):
cur_window = self.session.query(Window).filter_by(title=self.cur_name.decode('latin1'), process_id=self.cur_process_id).scalar()
if cur_window is None:
@@ -89,25 +71,21 @@ def maybe_end_specials(self):
def store_click(self, button, press):
if press:
print 'mouse', button, self.nrmoves
- self.session.add(Click(button, press, self.latestx, self.latesty, self.nrmoves, self.cur_win_id, self.cur_geo_id))
+ self.session.add(Click(button, press, self.latestx, self.latesty, self.nrmoves, self.cur_process_id, self.cur_win_id, self.cur_geo_id))
self.session.commit()
self.nrmoves = 0
def store_keys(self):
if self.timings:
self.maybe_end_specials()
- enc_timings = self.timings_to_str()
- enc_curtext = self.maybe_encrypt(self.curtext.encode('utf8'))
-
- self.session.add(Keys(enc_curtext, enc_timings, self.started, self.cur_win_id, self.cur_geo_id))
+ self.session.add(Keys(self.curtext.encode('utf8'), self.keys, self.timings, self.started, self.cur_process_id, self.cur_win_id, self.cur_geo_id))
self.session.commit()
- print 'keys', len(self.timings), len(cPickle.dumps(self.timings, 2)), len(enc_timings)
-
self.started = NOW()
self.curtext = u""
self.timings = []
+ self.keys = []
self.last_key_time = time.time()
def get_cur_window(self):
@@ -200,7 +178,8 @@ def got_key(self, keycode, state, s, press):
self.specials_in_row += 1
self.lastspecial = s
if self.specials_in_row < 2:
- self.timings.append((s, now - self.last_key_time))
+ self.keys.append(s)
+ self.timings.append(now - self.last_key_time)
self.last_key_time = now
def got_mouse_click(self, button, press):
View
@@ -1,3 +1,6 @@
+import zlib
+import json
+
import datetime
from sqlalchemy.ext.declarative import declarative_base, declared_attr
@@ -9,6 +12,7 @@ def initialize(fname):
Base.metadata.create_all(engine)
return sessionmaker(bind=engine)
+ENCRYPTER = None
Base = declarative_base()
@@ -69,45 +73,86 @@ class Click(SpookMixin, Base):
y = Column(Integer, nullable=False)
nrmoves = Column(Integer, nullable=False)
+ process_id = Column(Integer, ForeignKey('process.id'), nullable=False, index=True)
+ process = relationship("Process", backref=backref('clicks'))
+
window_id = Column(Integer, ForeignKey('window.id'), nullable=False)
window = relationship("Window", backref=backref('clicks'))
geometry_id = Column(Integer, ForeignKey('geometry.id'), nullable=False)
geometry = relationship("Geometry", backref=backref('clicks'))
- def __init__(self, button, press, x, y, nrmoves, window_id, geometry_id):
+ def __init__(self, button, press, x, y, nrmoves, process_id, window_id, geometry_id):
self.button = button
self.press = press
self.x = x
self.y = y
self.nrmoves = nrmoves
+ self.process_id = process_id
self.window_id = window_id
self.geometry_id = geometry_id
def __repr__(self):
return "<Click (%d, %d), (%d, %d, %d)>" % (self.xpos, self.ypos, self.button, self.press, self.nrmoves)
+def pad(s, padnum):
+ ls = len(s)
+ if ls % padnum == 0:
+ return s
+ return s + '\0' * (padnum - (ls % padnum))
+
+def maybe_encrypt(s):
+ if ENCRYPTER:
+ s = pad(s, 8)
+ s = ENCRYPTER.encrypt(s)
+ return s
+
+def maybe_decrypt(s):
+ if ENCRYPTER:
+ s = ENCRYPTER.decrypt(s)
+ return s
+
class Keys(SpookMixin, Base):
text = Column(Binary, nullable=False)
started = Column(DateTime, nullable=False)
+ process_id = Column(Integer, ForeignKey('process.id'), nullable=False, index=True)
+ process = relationship("Process", backref=backref('keys'))
+
window_id = Column(Integer, ForeignKey('window.id'), nullable=False)
window = relationship("Window", backref=backref('keys'))
geometry_id = Column(Integer, ForeignKey('geometry.id'), nullable=False)
geometry = relationship("Geometry", backref=backref('keys'))
+ nrkeys = Column(Integer, index=True)
+
+ keys = Column(Binary)
timings = Column(Binary)
- def __init__(self, text, timings, started, window_id, geometry_id):
- self.text = text
- self.timings = timings
+ def __init__(self, text, keys, timings, started, process_id, window_id, geometry_id):
+ ztimings = zlib.compress(json.dumps(timings))
+ zkeys = maybe_encrypt(zlib.compress(json.dumps(timings)))
+ ztext = maybe_encrypt(text)
+
+ self.text = ztext
+ self.keys = zkeys
+ self.nrkeys = len(keys)
+ self.timings = ztimings
self.started = started
+ self.process_id = process_id
self.window_id = window_id
self.geometry_id = geometry_id
+ def decrypt_text(self):
+ return maybe_decrypt(self.text)
+
+ def decrypt_keys(self):
+ keys = maybe_decrypt(self.keys)
+ return json.loads(zlib.decompress(keys))
+
def __repr__(self):
return "<Keys %s>" % self.text
View
@@ -28,16 +28,17 @@
allow not-text argument to avoid storing text at all. This makes the program never ask for passwords
remove guid and uid flags
- periodic emails from selfspy (or perhaps just have note in the README on how this can be accomplished with cron, mail and selfstats?)
-
README
+
+ periodic emails from selfspy (or perhaps just have note in the README on how this can be accomplished with cron, mail and selfstats?)
--
test map switch
general testing
no printing
remove stdout and stderr from DaemonContext
remove cPickle
-
+ requirements file
---Later
code documentation, unittests, pychecker ;)
Oops, something went wrong.

0 comments on commit 2d1d39b

Please sign in to comment.