I was thinking of using tor-ctrl SETEVENTS GUARD but figuring out exactly what to do on what events would require a lot of testing.
GETINFO entry-guards shows 3 ‘up’ out of 20. When I reboot in immutable mode it’s the same 3 but it seems to favor a different guard for most circuits according to GETINFO circuit-status.
Anyway, tor-ctrl (or something) should have a socat dependency since it’s somehow not installed on Whonix-Gateway-CLI.
I added a monitor mode to tor-ctrl where it does not exit but simply continues to output events. Something like
100a101,104
> [-w] = wait instead of exiting after command
> default: not set
> notice: implies verbose. Useful with SETEVENTS.
>
146c150,154
< sendcmd "QUIT"
---
> if [ -z "$WAIT" ]; then
> sendcmd "QUIT"
> else
> while sleep 3600; do :; done
> fi
159,163c167,174
< STR="$(cat)"
< vecho "$STR"
<
< echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]; then
< exit 0
---
> if [ -z "$WAIT" ]; then
> STR="$(cat)"
> vecho "$STR"
> echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]; then
> exit 0
> else
> exit 1
> fi
165c176
< exit 1
---
> cat
179c190
< while getopts ":a:c:s:p:P:f:vh" Option
---
> while getopts ":a:c:s:p:P:f:vwh" Option
188a200
> w) WAIT=1;;
194c206
< if [ -e "$FILE" ]; then
---
> if [ -e "$FILE" -o "$FILE" = "-" ]; then
I don’t know what the option should be called, wait? filepipe could also wait but the user can keep the pipe open to work around that. I don’t know an elegant way of not exiting in cmdpipe. And piping to cat is inefficient but?
Posting here since I see Patrick is the maintainer and the website listed in the script doesn’t exist.
Yes, tor-ctrl could use some love. Didn’t work on it much since no one ever cared about it. So even considered deprecation.
Documentation Control and Monitor Tor currently is needlessly complicated due to deficencies of the script
tor-ctrl -a /var/run/tor/control.authcookie -c "signal newnym"
-a /var/run/tor/control.authcookie could be avoided by using Tor auth cookie authentication by default.
-c could be the default (and then -c switch could be avoided).
-f file does not seem important. Could be removed. And if needed, better to invent a wrapper around it.
SLEEP_AFTER_CMD=1 - if we use sleep it means we didn’t figure out a proper event based flow.
Tor ControlPort support should be deprecated for code simplification. Debian tor package nowadays sets up Tor ControlSocket by default. No more need to use ControlPort. Then we could remove all references to telnet and the dependency on telnet.
I never tested SETEVENTS / long running sessions. If you can make that work, cool.
Help welcome.
Is ok.
I don’t think efficiency (as in speed) is important here?
I found the tor-ctrl an awesome tool and implemented most of the changes you wanted from above.
Also implemented a way to use setevents, see examples directory for listing circuit path or even streams organized and showing the final target.
my fork is on github and has the same name, I can’t include links.
-a is removed and the cookie file is discovered by sending PROTOCOLINFO to the controller. -c command be avoided not implemented -f removed sleep 1 if the program used is telnet, else for socat and nc/netcat sleep 0 by default. Sleep time can till be set with the -t [time] option. tor's socket can be used for tcp or for unix domain, format is [addr:]port or [unix:]path. If none is presented, fallback to 9051, which although debian default is /run/tor/control, I made the script to be portable and not depend on debian an bash, but this can be easily fixed on one line
setevents must be used with -w to await for user confirmation with an enter
Also, I don’t understand why it is not supported upstream just because it has a default path to ControlSocket? I see that as a gain to not specify one more thing on debian systems.
For now, I removed the deficencies on the script, so no need to specify anymore the cookie file path, the time and it is verbose by default, quiet -q only if you want.
The main thing is missing for me is SAFECOOKIE option to authenticate, which avoids leakage that happens with COOKIE and HASHEDPASSWORD.
Upstream by the usual definition as I understand it used in the Open Source ecosystem means the (original) developer / maintainer. There is no upstream for tor-ctrl. The original developer hasn’t been seen in years. Deprecated upstream means that the original developer vanished. There is no contact person upstream to accept bug reports, implement fixes or feature requests. It’s dead source code until a new maintainer steps up and becomes the new upstream.
Whonix by definition is a Linux distribution downstream from its upstream projects such as Debian. The Tor Project. tor-ctrl.
I did not remove it yet but to do that, I would not use getpots, I can make a hacky getopts for short options and assign the last $# to the command, so the command would be fixed as the last option to be shifted.
removed the -c, but command must be the last positional argument.
it cant be in the middle because getopts would stop parsing there and not consider arguments after it, and that is not desired.
happy to see some interest on tor-ctrl and if there is interest in long options without getopt which is limited, I can do that, or specify directly the benefits so I can make the changes.
The method above would have the same result as with getopts as -- would stop parsing every option after it, making the command the last option again. The only thing that would change is using long options?
With the changes made to the commit posted above, you don’t even need to put -- to stop option parsing, you can call tor-ctrl:
Maybe the benefit of the example you gave above is that shifting all the options and arguments and stop parsing with --, and getting the command with $@ would mean that the command does not have to be quoted anymore.
The benefit is that it would parse arguments that begin with - which should be reserved for options (that fix is not applied on the method posted above nor in getopts).
It also can interpret short and long options as well as --option=arg. -s socket, -s=socket, --socket socket, --socket=socket.
but it option requires an argument and the argument begins with - it will fail, example: -s -v
As for long vs short options, I don’t have a strong opinion. In the wiki and Whonix source code I try to use long options since these are usually easier to understand by users, “less magic”. Maybe it’s also because I don’t really understand yet why getopts would be needed at all. Ideally, none or needed or just in special cases.
Yes, I was thinking that. No more need for quotes. Which should it make a lot simpler if the command to be sent to Tor’s control protocol itself would include quotes. That avoid the need to have to double quote things.
Yes, I was thinking that. No more need for quotes. Which should it make a lot simpler if the command to be sent to Tor’s control protocol itself would include quotes. That avoid the need to have to double quote things.
tor_control_detection(){
## TODO: fix posix
## grep options '-o' and '-h' are not posix, but present on Debian and OpenBSD so making an excuse
## minimal check for tor service manager file
if test -f /lib/systemd/system/tor@default.service; then
tor_start_command="$(grep "ExecStart=" /lib/systemd/system/tor@default.service | sed s/ExecStart=//g)"
default_torrc="$(printf '%s\n' "${tor_start_command}" | grep -o -- "--defaults-torrc [^ ]*")"
f_torrc="$(printf '%s\n' "${tor_start_command}" | grep -o -- "-f [^ ]*")"
fi
## don't evaluate the result of verify-config as it will fail if not running as root or the tor user
## if 'f_torrc' is empty, tor will try: Default: @CONFDIR@/torrc, or $HOME/.torrc if that file is not found
## if 'default_torrc' is empty, tor will try: Default: @CONFDIR@/torrc-defaults.
# shellcheck disable=SC2086
result="$(tor --verify-config ${default_torrc} ${f_torrc} | grep -oE " Read configuration file [^ ]*| Including configuration file [^ ]*" | cut -d " " -f5 | sed "s/\"//;s/\"\.//")"
## only one option is needed, use head
# shellcheck disable=SC2086
tor_control_socket_alternative="$(grep -oEh "^ControlPort [^ ]*|^ControlSocket [^ ]*" ${result} | cut -d " " -f2 | sed "s/\"//g;s/unix\://" | head -n 1)"
## only modify 'tor_control_socket' if it is not empty, meaning not inserted as a cli option
: "${tor_control_socket:="${tor_control_socket_alternative}"}"
}
this function is called and it will detect tor_control_socket=/run/tor/control on Whonix and Debian of course, if the variable is still empty, last resort is 127.0.0.1:9051 later on the script.
Seems like could soon be worth it to re-vitalize tor-ctrl and re-install by default in Whonix now that there is a new upstream.