This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| fuss:tor [2023/03/03 03:48] – [Monitoring Tor Instances with Monit] office | fuss:tor [2025/10/21 23:26] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 232: | Line 232: | ||
| The previous configuration line will turn on debug logging and will send all messages to the system log. | The previous configuration line will turn on debug logging and will send all messages to the system log. | ||
| + | |||
| + | |||
| + | ====== Monitoring tor Health Status ====== | ||
| + | |||
| + | When scaling up and using load balancing and high availability it is important to have a decisive way to determine whether a backend service is alive or not. Usually checks vary from " | ||
| + | |||
| + | In case of tor, given the inner workings, the strongest way perhaps to know whether tor is available, is to check whether a circuit has been established, | ||
| + | |||
| + | In order to do that, the tor control port and password have to be defined in the tor configuration file (usually at ''/ | ||
| + | <code bash> | ||
| + | tor --hash-password " | ||
| + | </ | ||
| + | which will result in a password generated on the standard output: | ||
| + | < | ||
| + | 16: | ||
| + | </ | ||
| + | |||
| + | The password will then be added to the tor configuration: | ||
| + | < | ||
| + | ControlPort 8050 | ||
| + | HashedControlPassword 16: | ||
| + | </ | ||
| + | |||
| + | After restarting tor the control port can be accessed via TCP and the status of the circuit can be checked. Here is a full TCP transcript using netcat: | ||
| + | < | ||
| + | $ nc -nv 192.168.1.1 8050 | ||
| + | Connection to 172.16.1.81 8050 port [tcp/*] succeeded! | ||
| + | AUTHENTICATE " | ||
| + | 250 OK | ||
| + | GETINFO status/ | ||
| + | 250-status/ | ||
| + | 250 OK | ||
| + | |||
| + | </ | ||
| + | where: | ||
| + |   * '' | ||
| + |   * '' | ||
| + | |||
| + | The useful string to look for in the responses is '' | ||
| + | |||
| + | ===== Monitoring Tor Instances with Monit ===== | ||
| + | |||
| + | With the configuration in place, tor is restarted and the following monit configuration is created: | ||
| + | < | ||
| + | ########################################################################### | ||
| + | ## Copyright (C) Wizardry and Steamworks 2023 - License: GNU GPLv3 ## | ||
| + | ########################################################################### | ||
| + | |||
| + | check process tor-01 with pidfile / | ||
| + |    start program  | ||
| + |    stop program  | ||
| + | if failed host 127.0.0.1 port 9050 type tcp then restart | ||
| + | if failed host 127.0.0.1 port 8050 type tcp and | ||
| + | # password is: tor surrounded by quotes 0x22 | ||
| + |       send " | ||
| + |           | ||
| + |       send " | ||
| + |           | ||
| + | retry 1 | ||
| + | timeout 5 seconds | ||
| + | then restart | ||
| + | |||
| + | </ | ||
| + | that will restart tor in case a circuit is not built within two minutes (60 seconds standard monit check time and times two for one more retry). | ||
| + | |||
| + | ===== Monitoring tor Instances using Expect ===== | ||
| + | |||
| + | A more versatile variation of the [[/ | ||
| + | |||
| + | < | ||
| + | # | ||
| + | ########################################################################### | ||
| + | ## Copyright (C) Wizardry and Steamworks 2024 - License: MIT ## | ||
| + | ########################################################################### | ||
| + | # This is an " | ||
| + | # circuit and sets the return status depending on whether it has or not. # | ||
| + | # # | ||
| + | # In other words, iff. the script returns 0, then tor has an established  | ||
| + | # circuit; otherwise no circuit has been established.  | ||
| + | # # | ||
| + | # Requirements:  | ||
| + | #   * expect (TCL program)  | ||
| + | #   * tor must expose a control port and must have a control password  | ||
| + | # # | ||
| + | # In order to generate a control password, issue: tor --hash-password PWD # | ||
| + | # where PWD is the desired control port password. After that, amend the # | ||
| + | # tor configuration file to set the control port address, port and pass: # | ||
| + | # # | ||
| + | # ControlPort 0.0.0.0: | ||
| + | # HashedControlPassword 16: | ||
| + | # # | ||
| + | # Running: ./ | ||
| + | # where:  | ||
| + | #   * ADDRESS is the tor listening control address,  | ||
| + | # * PORT is the tor listening control port, # | ||
| + | #   * PASSWORD is the plaintext control password  | ||
| + | # # | ||
| + | # after which the return status can be checked on the shell with: # | ||
| + | # echo $? # | ||
| + | ########################################################################### | ||
| + | |||
| + | set address [lindex $argv 0]; | ||
| + | set port [lindex $argv 1]; | ||
| + | set password [lindex $argv 2]; | ||
| + | |||
| + | set timeout 5 | ||
| + | spawn telnet $address $port | ||
| + | |||
| + | send " | ||
| + | expect "250 OK\r\n" | ||
| + | send " | ||
| + | expect { | ||
| + | timeout { | ||
| + | exit 1 | ||
| + | } | ||
| + |     -ex " | ||
| + | } | ||
| + | |||
| + | </ | ||
| ====== Load-Balancing Multiple Tor Instances via HAProxy ====== | ====== Load-Balancing Multiple Tor Instances via HAProxy ====== | ||
| Line 262: | Line 381: | ||
|         bind    0.0.0.0: |         bind    0.0.0.0: | ||
| balance leastconn | balance leastconn | ||
| - | + |          | |
| - | server socks5-1 127.0.0.1: | + |         server socks5-2 127.0.0.1: | 
| - | server socks5-2 127.0.0.1: | + | |
| </ | </ | ||
| Line 274: | Line 392: | ||
| Finally, by pointing an application at the HAProxy port '' | Finally, by pointing an application at the HAProxy port '' | ||
| - | ====== Monitoring Tor Instances with Monit ====== | + | ===== Availability  | 
| - | Tor can be elaborately monitored and restarted automatically in case it is necessary  | + | Based on [[/ | 
| - | First, a tor password  | + | The tor configuration  | 
| - | <code bash> | + | |
| - | tor --hash-password  | + | |
| - | </ | + | |
| - | which will result in a password generated on the standard output: | + | |
| < | < | ||
| - | 16: | + | ControlPort 8050 | 
| + | HashedControlPassword  | ||
| </ | </ | ||
| - | The password  | + | For each additional tor instance, a program  | 
| - | < | + | < | 
| - | ControlPort 0.0.0.0:8051 | + | socat TCP-LISTEN: | 
| - | HashedControlPassword 16: | + | |
| </ | </ | ||
| + | where: | ||
| + |   * '' | ||
| + |   * '' | ||
| + |   * '' | ||
| - | With the configuration  | + | Finally  | 
| < | < | ||
| - | ########################################################################### | + | defaults | 
| - | ## Copyright (C) Wizardry and Steamworks 2023 - License: GNU GPLv3 ## | + | mode tcp | 
| - | ########################################################################### | + |          | 
| - | check process tor-01 with pidfile / | + | listen socks5-balance | 
| - |    start program  | + | bind 0.0.0.0:9050 | 
| - |    stop program  | + |          | 
| - |    if failed host 127.0.0.1  | + |          | 
| - |    if failed host 127.0.0.1  | + |          | 
| - | # password is: tor surrounded by quotes 0x22 | + | |
| - |       send " | + | |
| - |           | + | |
| - |       send " | + | |
| - |           | + | |
| - | retry 1 | + | |
| - | timeout 5 seconds | + | |
| - | then restart | + | |
| </ | </ | ||
| - | that will restart tor in case a circuit is not built within two minutes (60 seconds standard monit check time and times two for one more retry). | + | |
| + | The mechanism will ensure  | ||
| + | |||
| + | Incidentally, | ||
For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.