[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Add close_foo functions to TorControl



Update of /home/or/cvsroot/tor/contrib
In directory moria.mit.edu:/tmp/cvs-serv31189

Modified Files:
	TorControl.py package_nsis.sh 
Added Files:
	PathDemo.py 
Log Message:
Add close_foo functions to TorControl

--- NEW FILE: PathDemo.py ---
#!/usr/bin/python

import TorControl
import threading
import socket
import struct
import random

circuits = {}
streams = {}

def runControl(s):
    pendingEvents = [] #XXX This tric. should become standard
    TorControl._event_handler = pendingEvents.append
    TorControl.set_events(s,
                          [TorControl.EVENT_TYPE.CIRCSTATUS,
                           TorControl.EVENT_TYPE.STREAMSTATUS])
    TorControl.set_option(s,"__LeaveStreamsUnattached 1")
    while 1:
        e = pendingEvents[:]
        del pendingEvents[:]
        for ev in e:
            handleEvent(s, ev)
        _, tp, body = TorControl.receive_message(s)
        if tp == TorControl.MSG_TYPE.EVENT:
            handleEvent(s, body)


def parsePath(name):
    assert name.endswith(".path")
    items = name.split(".")
    try:
        n = int(items[-2])
    except:
        return None,None
    path = items[-(2+n):-2]
    host = items[:-(2+n)]
    print path,host
    return path,".".join(host)

def handleEvent(s,body):
    event, args = TorControl.unpack_event(body)
    if event == TorControl.EVENT_TYPE.STREAMSTATUS:
        status, ident, target = args
        print "Got stream event:",TorControl.STREAM_STATUS.nameOf[status],\
              ident,target
        if status in (TorControl.STREAM_STATUS.NEW_CONNECT,
                      TorControl.STREAM_STATUS.NEW_RESOLVE):
            target,port=target.split(":")
            if not target.endswith(".path"):
                TorControl.attach_stream(s, ident, 0)
            else:
                path,host = parsePath(target)
                #XXXX Don't launch so many circuits!
                streams[ident] = path,host
                circid = TorControl.extend_circuit(s, 0, path)
                circuits[circid] = path
        elif status == TorControl.STREAM_STATUS.DETACHED:
            if not streams.has_key(ident):
                TorControl.attach_stream(s, ident, 0)
            else:
                TorControl.close_stream(s, ident, 1)
    elif event == TorControl.EVENT_TYPE.CIRCSTATUS:
        status, ident, path = args
        print "Got circuit event",TorControl.CIRC_STATUS.nameOf[status],\
              ident,path
        if not circuits.has_key(ident):
            return
        if status in (TorControl.CIRC_STATUS.CLOSED,
                      TorControl.CIRC_STATUS.FAILED):
            ok = 0
        elif status == TorControl.CIRC_STATUS.BUILT:
            ok = 1
        else:
            return

        ids = [ streamID for (streamID, (path,host)) in streams.items()
                if path == circuits[ident] ]

        for streamID in ids:
            if ok:
                _,host = streams[streamID]
                TorControl.redirect_stream(s, streamID, host)
                TorControl.attach_stream(s, streamID, ident)
                #XXXX Don't do this twice.
            else:
                TorControl.close_stream(s, streamID, 1)
        if not ok:
            del circuits[ident]


def run():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("127.0.0.1", 9051))
    TorControl.authenticate(s)
    runControl(s)

if __name__ == '__main__':
    run()

Index: TorControl.py
===================================================================
RCS file: /home/or/cvsroot/tor/contrib/TorControl.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- TorControl.py	31 Mar 2005 05:52:23 -0000	1.4
+++ TorControl.py	31 Mar 2005 07:05:29 -0000	1.5
@@ -314,6 +314,16 @@
     send_message(s,MSG_TYPE.ATTACHSTREAM,msg)
     tp,body = receive_reply(s,[MSG_TYPE.DONE])
 
+def close_stream(s, streamid, reason=0, flags=0):
+    msg = struct.pack("!LBB",streamid,reason,flags)
+    send_message(s,MSG_TYPE.CLOSESTREAM,msg)
+    tp,body = receive_reply(s,[MSG_TYPE.DONE])
+
+def close_circuit(s, circid, flags=0):
+    msg = struct.pack("!LB",circid,flags)
+    send_message(s,MSG_TYPE.CLOSECIRCUIT,msg)
+    tp,body = receive_reply(s,[MSG_TYPE.DONE])
+
 def _unterminate(s):
     if s[-1] == '\0':
         return s[:-1]

Index: package_nsis.sh
===================================================================
RCS file: /home/or/cvsroot/tor/contrib/package_nsis.sh,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- package_nsis.sh	23 Mar 2005 21:28:37 -0000	1.5
+++ package_nsis.sh	31 Mar 2005 07:05:29 -0000	1.6
@@ -28,7 +28,7 @@
 }
 
 clean_localstatedir() {
-    perl -pe 'BEGIN {undef $/;} s/^\n$/\r\n/mg; s/([^\r])\n$/\1\r\n/mg; s{\@LOCALSTATEDIR\@/(lib|log)/tor/}{\\Application Data\\Tor\\}' $1 >$2
+    perl -pe 'BEGIN {undef $/;} s/^\n$/\r\n/mg; s/([^\r])\n$/\1\r\n/mg; s{\@LOCALSTATEDIR\@/(lib|log)/tor/}{C:\\Documents and Settings\\Application Data\\Tor\\}' $1 >$2
 }
 
 for fn in CLIENTS tor-spec.txt HACKING rend-spec.txt control-spec.txt \