def handleEvent(s, body, circs, streamsByNonce, streamsByIdent): global N_CIRCS_DONE 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, TorControl.STREAM_STATUS.DETACHED): target, port = target.split(":") if not target.endswith(".exnonce"): TorControl.attach_stream(s, ident, 0) else: circid, (host, url) = streamsByNonce[target] streamsByIdent[ident] = circid, (host, url) print "Redirecting circuit", circid, "to", host TorControl.redirect_stream(s, ident, host) TorControl.attach_stream(s, ident, circid) elif status in (TorControl.STREAM_STATUS.CLOSED, TorControl.STREAM_STATUS.FAILED): circid, (host, url) = streamsByIdent[ident] if circs.has_key(circid): for name in circs[circid]: HOST_STATUS[name][1] += 1 del circs[circid] N_CIRCS_DONE += 1 print N_CIRCS_DONE, "circuit attempts done" del streamsByIdent[ident] elif event == TorControl.EVENT_TYPE.CIRCSTATUS: status, ident, path = args print "Got circuit event",TorControl.CIRC_STATUS.nameOf[status],\ ident,path if status in (TorControl.CIRC_STATUS.CLOSED, TorControl.CIRC_STATUS.FAILED): if circs.has_key(ident): print "Circuit failed." del circs[ident] N_CIRCS_DONE += 1 print N_CIRCS_DONE, "circuit attempts done" elif status == TorControl.CIRC_STATUS.BUILT: nonce = random.randint(1, 100000000) nonce = "%s.exnonce" % nonce host, url = random.choice(TARGETS) streamsByNonce[nonce] = ident, (host, url) print "Launching socks4a connection" t = threading.Thread(target=runSocks4A, args=(nonce, host, 80, url)) t.setDaemon(1) t.start()
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]