Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactored classes to ease reuse

  • Loading branch information...
commit 1fd665568b24789063d73eb3c221bf7b282c1395 1 parent 9953e48
Alvaro Videla authored

Showing 2 changed files with 83 additions and 57 deletions. Show diff stats Hide diff stats

  1. +58 0 receiver.py
  2. +25 57 session_inspector.py
58 receiver.py
... ... @@ -0,0 +1,58 @@
  1 +from twisted.internet import reactor
  2 +from twisted.internet.protocol import Factory
  3 +from twisted.internet.error import ConnectionDone
  4 +from twisted.protocols.basic import LineOnlyReceiver
  5 +import sys
  6 +from conf import *
  7 +from session_inspector import SessionInspectorMemcache
  8 +
  9 +class InspectorReceiver(LineOnlyReceiver):
  10 + delimiter = '\r\n'
  11 +
  12 + def __init__(self):
  13 + self.inspector = SessionInspectorMemcache()
  14 +
  15 + def connectionMade(self):
  16 + self.peer = self.transport.getPeer()
  17 + self.peerAddr = "%s:%d" % (self.peer.host, self.peer.port)
  18 + print("%s connection with %s established" % (self.__class__.__name__, self.peerAddr))
  19 +
  20 + def connectionLost(self, reason):
  21 + if reason.check(ConnectionDone):
  22 + print("%s connection with %s closed cleanly" % (self.__class__.__name__, self.peerAddr))
  23 + else:
  24 + print("%s connection with %s lost: %s" % (self.__class__.__name__, self.peerAddr, reason.value))
  25 +
  26 + def isAuth(self, is_auth):
  27 + self.transport.write("%d\r\n" % (is_auth, ))
  28 +
  29 + def lineReceived(self, line):
  30 + try:
  31 + values = line.strip().split()
  32 +
  33 + if len(values) < 1:
  34 + pass
  35 +
  36 + if values[0] == 'isauth' and len(values) > 1:
  37 + for key in values[1:]:
  38 + self.isAuth(self.inspector.isauth(key))
  39 +
  40 + elif values[0] == 'quit':
  41 + self.transport.loseConnection()
  42 + else:
  43 + raise Exception('Invalid command')
  44 + except Exception as inst:
  45 + print type(inst)
  46 + print inst.args
  47 + print inst
  48 + print('invalid line received from client %s' % self.peerAddr)
  49 + return
  50 +
  51 +def startListener(interface, port, protocol):
  52 + factory = Factory()
  53 + factory.protocol = protocol
  54 + return reactor.listenTCP( int(port), factory, interface=interface )
  55 +
  56 +startListener(INSPECTOR_INTERFACE, INSPECTOR_PORT, InspectorReceiver)
  57 +
  58 +reactor.run()
82 session_inspector.py
... ... @@ -1,65 +1,33 @@
1   -from twisted.internet import reactor
2   -from twisted.internet.protocol import Factory
3   -from twisted.internet.error import ConnectionDone
4   -from twisted.protocols.basic import LineOnlyReceiver
5 1 import memcache
6   -import sys
7 2 from PHPUnserialize import *
8 3 from conf import *
9 4
10   -mc = memcache.Client(sessServers, debug=0)
11   -
12   -class InspectorReceiver(LineOnlyReceiver):
13   - delimiter = '\r\n'
14   -
15   - def connectionMade(self):
16   - self.peer = self.transport.getPeer()
17   - self.peerAddr = "%s:%d" % (self.peer.host, self.peer.port)
18   - print("%s connection with %s established" % (self.__class__.__name__, self.peerAddr))
19   -
20   - def connectionLost(self, reason):
21   - if reason.check(ConnectionDone):
22   - print("%s connection with %s closed cleanly" % (self.__class__.__name__, self.peerAddr))
23   - else:
24   - print("%s connection with %s lost: %s" % (self.__class__.__name__, self.peerAddr, reason.value))
  5 +class SessionInspector():
25 6
26   - def isAuth(self, is_auth):
27   - self.transport.write("%d\r\n" % (is_auth, ))
  7 + def __init__(self):
  8 + global prefix, auth_key
  9 + self.prefix = prefix
  10 + self.auth_key = auth_key
28 11
29   - def lineReceived(self, line):
30   - global mc, prefix, auth_key
31   -
32   - try:
33   - values = line.strip().split()
34   -
35   - if len(values) < 1:
36   - pass
37   -
38   - if values[0] == 'isauth' and len(values) > 1:
39   - for key in values[1:]:
40   - data = mc.get(prefix + key)
41   - if not data:
42   - self.isAuth(False)
43   - else:
44   - try:
45   - session = PHPUnserialize().session_decode(data)
46   - self.isAuth(session[auth_key])
47   - except:
48   - self.isAuth(False)
49   -
50   - elif values[0] == 'quit':
51   - self.transport.loseConnection()
52   - else:
53   - raise Exception('Invalid command')
54   - except:
55   - print('invalid line received from client %s' % self.peerAddr)
56   - return
  12 + def getData(self, key):
  13 + raise NotImplementedError("getData is an abstract method")
57 14
58   -def startListener(interface, port, protocol):
59   - factory = Factory()
60   - factory.protocol = protocol
61   - return reactor.listenTCP( int(port), factory, interface=interface )
62   -
63   -startListener(INSPECTOR_INTERFACE, INSPECTOR_PORT, InspectorReceiver)
  15 + def isauth(self, key):
  16 + data = self.getData(key)
  17 + if not data:
  18 + return False
  19 + else:
  20 + try:
  21 + session = PHPUnserialize().session_decode(data)
  22 + return session[self.auth_key]
  23 + except:
  24 + return False
  25 +
  26 +class SessionInspectorMemcache(SessionInspector):
  27 + def __init__(self):
  28 + SessionInspector.__init__(self)
  29 + global sessServers
  30 + self.mc = memcache.Client(sessServers, debug=0)
64 31
65   -reactor.run()
  32 + def getData(self, key):
  33 + return self.mc.get(self.prefix + key)

0 comments on commit 1fd6655

Please sign in to comment.
Something went wrong with that request. Please try again.