Commit 84af516a authored by anonym's avatar anonym
Browse files

tor-controlport-filter: log to stderr and flush.

Python runs in buffered mode so the output won't hit the journal until
the buffer is full so let's flush after each log message to make them
more useful.
parent 0e6ac580
...@@ -53,6 +53,7 @@ import re ...@@ -53,6 +53,7 @@ import re
import socketserver import socketserver
import stem import stem
import stem.control import stem.control
import sys
import yaml import yaml
DEFAULT_LISTEN_PORT = 9051 DEFAULT_LISTEN_PORT = 9051
...@@ -70,6 +71,11 @@ global_args = None ...@@ -70,6 +71,11 @@ global_args = None
MAX_LINESIZE = 1024 MAX_LINESIZE = 1024
def log(msg):
print(msg, file=sys.stderr)
sys.stderr.flush()
def exe_path_of_pid(pid): def exe_path_of_pid(pid):
# Here we leverage AppArmor's in-kernel solution for determining # Here we leverage AppArmor's in-kernel solution for determining
# the exact executable invoked. Looking at /proc/pid/exe when an # the exact executable invoked. Looking at /proc/pid/exe when an
...@@ -104,12 +110,12 @@ def handle_controlport_session(controller, readh, writeh, allowed_commands, allo ...@@ -104,12 +110,12 @@ def handle_controlport_session(controller, readh, writeh, allowed_commands, allo
return any(re.match(regex + "$", args) for regex in allowed_args) return any(re.match(regex + "$", args) for regex in allowed_args)
def proxy_line(line): def proxy_line(line):
if global_args.complain: print("-> {}".format(line)) if global_args.complain: log("-> {}".format(line))
response = controller.msg(line) response = controller.msg(line)
respond(response.raw_content(), raw=True) respond(response.raw_content(), raw=True)
def filter_line(line): def filter_line(line):
print("Command filtered: {}".format(line)) log("Command filtered: {}".format(line))
respond("510 Command filtered") respond("510 Command filtered")
subscribed_event_listeners = [] subscribed_event_listeners = []
...@@ -224,8 +230,8 @@ class FilteredControlPortProxyHandler(socketserver.StreamRequestHandler): ...@@ -224,8 +230,8 @@ class FilteredControlPortProxyHandler(socketserver.StreamRequestHandler):
for key, expected_val in matchers: for key, expected_val in matchers:
if not key in filter_: if not key in filter_:
is_ok = False is_ok = False
print("Filter '{}' lacks obligatory key '{}': skipping" log("Filter '{}' lacks obligatory key '{}': skipping"
.format(filter_['name'], key) .format(filter_['name'], key)
) )
break break
if not any(val for val in filter_[key] \ if not any(val for val in filter_[key] \
...@@ -249,8 +255,8 @@ class FilteredControlPortProxyHandler(socketserver.StreamRequestHandler): ...@@ -249,8 +255,8 @@ class FilteredControlPortProxyHandler(socketserver.StreamRequestHandler):
status = 'no matching filter found, using an empty one' status = 'no matching filter found, using an empty one'
else: else:
status = 'loaded filter(s): {}'.format(", ".join(matched_filters)) status = 'loaded filter(s): {}'.format(", ".join(matched_filters))
print('{} (PID: {}, user: {}) connected: {}' log('{} (PID: {}, user: {}) connected: {}'
.format(client_exe_path, client_conn.pid, client_user, status) .format(client_exe_path, client_conn.pid, client_user, status)
) )
controller = self.connect_to_real_control_port() controller = self.connect_to_real_control_port()
disconnect_reason = "client quit" disconnect_reason = "client quit"
...@@ -262,9 +268,9 @@ class FilteredControlPortProxyHandler(socketserver.StreamRequestHandler): ...@@ -262,9 +268,9 @@ class FilteredControlPortProxyHandler(socketserver.StreamRequestHandler):
disconnect_reason = str(err) disconnect_reason = str(err)
finally: finally:
controller.close() controller.close()
print('{} (PID: {}, user: {}) disconnected: {}' log('{} (PID: {}, user: {}) disconnected: {}'
.format(client_exe_path, client_conn.pid, client_user, .format(client_exe_path, client_conn.pid, client_user,
disconnect_reason) disconnect_reason)
) )
...@@ -296,8 +302,8 @@ def main(): ...@@ -296,8 +302,8 @@ def main():
("localhost", global_args.listen_port), ("localhost", global_args.listen_port),
FilteredControlPortProxyHandler FilteredControlPortProxyHandler
) )
print("Tor control port filter started, listening on {}" log("Tor control port filter started, listening on {}"
.format(global_args.listen_port)) .format(global_args.listen_port))
try: try:
server.serve_forever() server.serve_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
......
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