Commit f7325484 authored by anonym's avatar anonym

tor-controlport-filter: refactor.

parent 7bdee446
......@@ -354,6 +354,34 @@ def handle_controlport_session(controller, readh, writeh, client_desc, client_pi
raw_event_content = new_raw_event_content
respond(raw_event_content)
def update_event_subscriptions(events):
if not global_args.disable_filtering and \
any(event not in allowed_events for event in events):
filter_line(line)
return
for listener, event in subscribed_event_listeners:
if event in events:
events.remove(event)
else:
controller.remove_event_listener(listener)
subscribed_event_listeners.remove((listener, event))
for event in events:
rule = allowed_events[event] or {}
if not rule.get('suppress', False) or \
global_args.disable_filtering:
event_rewriter = None
if 'response' in rule:
def _event_rewriter(line):
return rewrite_matched_line(rule['response'], line)
event_rewriter = _event_rewriter
def _event_cb(event):
event_cb(event, event_rewriter=event_rewriter)
controller.add_event_listener(
_event_cb, getattr(stem.control.EventType, event)
)
subscribed_event_listeners.append((_event_cb, event))
respond("250 OK")
subscribed_event_listeners = []
client_streams = set()
while True:
......@@ -385,32 +413,7 @@ def handle_controlport_session(controller, readh, writeh, client_desc, client_pi
# The control language doesn't care about case for
# the event type.
events = [event.upper() for event in args]
if not global_args.disable_filtering and \
any(event not in allowed_events for event in events):
filter_line(line)
continue
for listener, event in subscribed_event_listeners:
if event in events:
events.remove(event)
else:
controller.remove_event_listener(listener)
subscribed_event_listeners.remove((listener, event))
for event in events:
rule = allowed_events[event] or {}
if not rule.get('suppress', False) or \
global_args.disable_filtering:
event_rewriter = None
if 'response' in rule:
def _event_rewriter(line):
return rewrite_matched_line(rule['response'], line)
event_rewriter = _event_rewriter
def _event_cb(event):
event_cb(event, event_rewriter=event_rewriter)
controller.add_event_listener(
_event_cb, getattr(stem.control.EventType, event)
)
subscribed_event_listeners.append((_event_cb, event))
respond("250 OK")
update_event_subscriptions(events)
else:
rule = get_rule(line)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment