[Pkg-privacy-commits] [obfsproxy] 76/353: Add a simple heartbeat.

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:01:42 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository obfsproxy.

commit 5bf75728897d7242388d525f75793749e28f00ae
Author: George Kadianakis <desnacked at riseup.net>
Date:   Sat Oct 27 18:58:11 2012 +0300

    Add a simple heartbeat.
---
 obfsproxy.py                  | 12 ++++++--
 obfsproxy/common/heartbeat.py | 71 +++++++++++++++++++++++++++++++++++++++++++
 obfsproxy/network/network.py  | 12 ++++++--
 3 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/obfsproxy.py b/obfsproxy.py
index f007eb3..335bd63 100755
--- a/obfsproxy.py
+++ b/obfsproxy.py
@@ -10,13 +10,16 @@ Currently, not all of the obfsproxy command line options have been implemented.
 import os
 import sys
 import argparse
+
 import obfsproxy.transports.transports as transports
 import obfsproxy.common.log as log
+import obfsproxy.common.heartbeat as heartbeat
+from obfsproxy.managed.server import ManagedServer
+from obfsproxy.managed.client import ManagedClient
 
 from pyptlib.util import checkClientMode
 
-from obfsproxy.managed.server import ManagedServer
-from obfsproxy.managed.client import ManagedClient
+from twisted.internet import task # for LoopingCall
 
 def set_up_cli_parsing():
     """Set up our CLI parser. Register our arguments and options and
@@ -118,6 +121,11 @@ def main(argv):
     log.debug('argv: ' + str(sys.argv))
     log.debug('args: ' + str(args))
 
+    # Fire up our heartbeat.
+    l = task.LoopingCall(heartbeat.heartbeat.talk)
+    l.start(3600.0) # do heartbeat every hour
+
+    # Initiate obfsproxy.
     if (args.name == 'managed'):
         do_managed_mode()
     else:
diff --git a/obfsproxy/common/heartbeat.py b/obfsproxy/common/heartbeat.py
new file mode 100644
index 0000000..3cb036d
--- /dev/null
+++ b/obfsproxy/common/heartbeat.py
@@ -0,0 +1,71 @@
+"""heartbeat code"""
+
+import datetime
+
+import obfsproxy.common.log as log
+
+class Heartbeat(object):
+    """
+    Represents obfsproxy's heartbeat.
+
+    It keeps stats on a number of things that the obfsproxy operator
+    might be interested in, and every now and then it reports them in
+    the logs.
+    """
+
+    def __init__(self):
+        self.n_unique_ips = 0
+        self.n_connections = 0
+        self.started = datetime.datetime.now()
+        self.last_reset = self.started
+
+    def register_ip(self, ip): # XXX NOP
+        pass
+
+    def register_connection(self):
+        """Register a new connection."""
+
+        self.n_connections += 1
+
+    def reset_stats(self):
+        """Reset stats."""
+
+        self.n_connections = 0
+        self.n_unique_ips = 0
+        self.last_reset = datetime.datetime.now()
+
+    def say_uptime(self):
+        """Log uptime information."""
+
+        now = datetime.datetime.now()
+        delta = now - self.started
+
+        if delta.days:
+            log.info("Heartbeat: obfsproxy's uptime is %d day(s), %d hour(s) and %d minute(s)." % \
+                         (delta.days, delta.seconds//3600, (delta.seconds//60)%60))
+        else:
+            log.info("Heartbeat: obfsproxy's uptime is %d hour(s) and %d minute(s)." % \
+                         (delta.seconds//3600, (delta.seconds//60)%60))
+
+    def say_stats(self):
+        """Log connection stats."""
+
+        now = datetime.datetime.now()
+        reset_delta = now - self.last_reset
+
+        log.info("Heartbeat: During the last %d hour(s) we saw %d connection(s)." % \
+                     (reset_delta.seconds//3600 + reset_delta.days*24, self.n_connections))
+
+        # Reset stats every 24 hours.
+        if (reset_delta.days > 0):
+            log.debug("Resetting heartbeat.")
+            self.reset_stats()
+
+    def talk(self):
+        """Do a heartbeat."""
+
+        self.say_uptime()
+        self.say_stats()
+
+# A heartbeat singleton.
+heartbeat = Heartbeat()
diff --git a/obfsproxy/network/network.py b/obfsproxy/network/network.py
index a74abe2..1e41bca 100644
--- a/obfsproxy/network/network.py
+++ b/obfsproxy/network/network.py
@@ -4,6 +4,8 @@ from twisted.internet import reactor, error, address, tcp
 from twisted.internet.protocol import Protocol, Factory, ClientFactory
 
 import obfsproxy.common.log as log
+import obfsproxy.common.heartbeat as heartbeat
+
 import obfsproxy.network.buffer as buffer
 import obfsproxy.transports.base as base
 
@@ -181,10 +183,11 @@ class StaticDestinationProtocol(Protocol):
             data before deciding what to do.
     """
 
-    def __init__(self, circuit, mode):
+    def __init__(self, circuit, mode, peer_addr):
         self.mode = mode
         self.circuit = circuit
         self.buffer = buffer.Buffer()
+        self.peer_addr = peer_addr # XXX unused
 
         self.closed = False # True if connection is closed.
 
@@ -213,6 +216,9 @@ class StaticDestinationProtocol(Protocol):
             log.debug("%s: connectionMade (server): " \
                       "Setting it as downstream on our circuit." % self.name)
 
+            # Gather some statistics for our heartbeat.
+            heartbeat.heartbeat.register_connection()
+
             self.circuit.setDownstreamConnection(self)
         elif self.mode == 'server':
             log.debug("%s: connectionMade (server): " \
@@ -285,7 +291,7 @@ class StaticDestinationClientFactory(Factory):
         self.name = "fact_c_%s" % hex(id(self))
 
     def buildProtocol(self, addr):
-        return StaticDestinationProtocol(self.circuit, self.mode)
+        return StaticDestinationProtocol(self.circuit, self.mode, addr)
 
     def startedConnecting(self, connector):
         log.debug("%s: Client factory started connecting." % self.name)
@@ -334,5 +340,5 @@ class StaticDestinationServerFactory(Factory):
         clientFactory = StaticDestinationClientFactory(circuit, self.mode)
         reactor.connectTCP(self.remote_host, self.remote_port, clientFactory)
 
-        return StaticDestinationProtocol(circuit, self.mode)
+        return StaticDestinationProtocol(circuit, self.mode, addr)
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/obfsproxy.git



More information about the Pkg-privacy-commits mailing list