<div>                David, Roger, et al.,<br><br>I just got back from holidays and really enjoyed this thread!<br><br>I run my Loadbalanced Tor Relay as a Guard/Middle Relay, very similar to David's topology diagram, without the Snoflake-Server proxy. I'm using Nginx (which forks a child process per core) instead of HAProxy. My Backend Tor Relay Nodes are running on several, different Physical Servers; thus, I'm using Private Address Space instead of Loopback Address Space.<br><br>In this configuration, I discovered that I had to configure Nginix/HAProxy to use Transparent Streaming Mode, use Source IP Address Sticky Sessions (Pinning), configure the Loadbalancer to send the Backend Tor Relay Nodes' traffic back to Nginx/HAProxy (Kernel & IPTables), configure all Backend Tor Relay Nodes to use a copy of the same .tordb (I wasn't able to get the Backend Tor Relay Nodes working with the same .tordb (over NFS) without the DirectoryAuthorities complaining), and configure the Backend Tor Relay Nodes to use the same DirectoryAuthority (to ensure each Backend Tor Relay Node sends Meta-Data to the same DirectoryAuthority). Moreover, I've enabled logging to a central Syslog Server for each Backend Tor Relay Node and created a number of Shell Scripts to help remotely manage each Backend Tor Relay Node.<br><br>Here are some sample configurations for reference.<br><br>Nginx Config:<br><br>upstream orport_tornodes {<br>#least_conn;<br>hash $remote_addr consistent;<br>#server 192.168.0.1:9001 weight=1 max_fails=1 fail_timeout=10s;<br>#server 192.168.0.1:9001 down;<br>server 192.168.0.11:9001 weight=4 max_fails=0 fail_timeout=0s;<br>server 192.168.0.21:9001 weight=4 max_fails=0 fail_timeout=0s;<br>#server 192.168.0.31:9001 weight=4 max_fails=3 fail_timeout=300s;<br>server 192.168.0.41:9001 weight=4 max_fails=0 fail_timeout=0s;<br>server 192.168.0.51:9001 weight=4 max_fails=0 fail_timeout=0s;<br>#zone orport_torfarm 64k;<br><br><br>HAProxy Config (Alternate):<br><br>frontend tornodes<br># Log to global config<br>log global<br><br># Bind to port 443 on a specified interface<br>bind 0.0.0.0:9001 transparent<br><br># We're proxying TCP here...<br>mode tcp<br><br>default_backend orport_tornodes<br><br># Simple TCP source consistent over several servers using the specified<br># source 0.0.0.0 usesrc clientip<br>backend orport_tornodes<br><br>balance source<br>hash-type consistent<br>#server tornode1 192.168.0.1:9001 check disabled<br>#server tornode11 192.168.0.11:9001 source 192.168.0.1<br>server tornode11 192.168.0.11:9001 source 0.0.0.0 usesrc clientip check disabled<br>server tornode21 192.168.0.21:9001 source 0.0.0.0 usesrc clientip check disabled<br>#server tornode31 192.168.0.31:9001 source 0.0.0.0 usesrc clientip check disabled<br>server tornode41 192.168.0.41:9001 source 0.0.0.0 usesrc clientip check disabled<br>server tornode51 192.168.0.51:9001 source 0.0.0.0 usesrc clientip check disabled<br><br><br>Linux Kernel & IPTables Config:<br><br>modprobe xt_socket<br>modprobe xt_TPROXY<br><br>echo 1 > /proc/sys/net/ipv4/ip_forward; cat /proc/sys/net/ipv4/ip_forward<br>echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind; cat /proc/sys/net/ipv4/ip_nonlocal_bind<br>echo 15000 64000 > /proc/sys/net/ipv4/ip_local_port_range; cat /proc/sys/net/ipv4/ip_local_port_range<br><br>ip rule del fwmark 1 lookup 100 2>/dev/null # Ensure Duplicate Rule is not Created<br>ip rule add fwmark 1 lookup 100 # ip rule show<br>ip route add local 0.0.0.0/0 dev lo table 100 # ip route show table wan0; ip route show table 100<br><br>iptables -I INPUT -p tcp --dport 9001 -j ACCEPT<br>iptables -t mangle -N TOR<br>iptables -t mangle -A PREROUTING -p tcp -m socket -j TOR<br>iptables -t mangle -A TOR -j MARK --set-mark 1<br>iptables -t mangle -A TOR -j ACCEPT<br>#iptables -t mangle -A PREROUTING -p tcp -s 192.168.0.0/24 --sport 9001 -j MARK --set-xmark 0x1/0xffffffff<br>#iptables -t mangle -A PREROUTING -p tcp --dport 9001 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 9001 --on-ip 127.0.0.1<br><br><br>Backend Tor Relay Node Configs:<br><br># cat /tmp/torrc <br>Nickname xxxxxxxxxxxxxxxxxx<br>ORPort xxx.xxx.xxx.xxx:9001 NoListen<br>ORPort 192.168.0.11:9001 NoAdvertise<br>SocksPort 9050<br>SocksPort 192.168.0.11:9050<br>ControlPort 9051<br>DirAuthority longclaw orport=443 no-v2 v3ident=23D15D965BC35114467363C165C4F724B64B4F66 199.58.81.140:80 74A910646BCEEFBCD2E874FC1DC997430F968145<br>FallbackDir 193.23.244.244:80 orport=443 id=7BE683E65D48141321C5ED92F075C55364AC7123<br>DirCache 0<br>ExitRelay 0<br>MaxMemInQueues 192 MB<br>GeoIPFile /opt/share/tor/geoip<br>Log notice file /tmp/torlog<br>Log notice syslog<br>VirtualAddrNetwork 10.192.0.0/10<br>AutomapHostsOnResolve 1<br>TransPort 192.168.0.11:9040<br>DNSPort 192.168.0.11:9053<br>RunAsDaemon 1<br>DataDirectory /tmp/tor/torrc.d/.tordb<br>AvoidDiskWrites 1<br>User tor<br>ContactInfo tor-operator@your-emailaddress-domain<br><br># cat /tmp/torrc <br>Nickname xxxxxxxxxxxxxxxxxx<br>ORPort xxx.xxx.xxx.xxx:9001 NoListen<br>ORPort 192.168.0.41:9001 NoAdvertise<br>SocksPort 9050<br>SocksPort 192.168.0.41:9050<br>ControlPort 9051<br>DirAuthority longclaw orport=443 no-v2 v3ident=23D15D965BC35114467363C165C4F724B64B4F66 199.58.81.140:80 74A910646BCEEFBCD2E874FC1DC997430F968145<br>FallbackDir 193.23.244.244:80 orport=443 id=7BE683E65D48141321C5ED92F075C55364AC7123<br>DirCache 0<br>ExitRelay 0<br>MaxMemInQueues 192 MB<br>GeoIPFile /opt/share/tor/geoip<br>Log notice file /tmp/torlog<br>Log notice syslog<br>VirtualAddrNetwork 10.192.0.0/10<br>AutomapHostsOnResolve 1<br>TransPort 192.168.0.41:9040<br>DNSPort 192.168.0.41:9053<br>RunAsDaemon 1<br>DataDirectory /tmp/tor/torrc.d/.tordb<br>AvoidDiskWrites 1<br>User tor<br>ContactInfo tor-operator@your-emailaddress-domain<br><br><br>Shell Scripts to Remotely Manage Tor Relay Nodes:<br><br># cat /usr/sbin/stat-tor-nodes <br>#!/bin/sh<br>uptime-all-nodes; memfree-all-nodes; netstat-tor-nodes<br><br># cat /usr/sbin/uptime-all-nodes <br>#!/bin/sh<br>/usr/bin/ssh -t admin@192.168.0.11 'hostname; uptime'<br>/usr/bin/ssh -t admin@192.168.0.21 'hostname; uptime'<br>/usr/bin/ssh -t admin@192.168.0.31 'hostname; uptime'<br>/usr/bin/ssh -t admin@192.168.0.41 'hostname; uptime'<br>/usr/bin/ssh -t admin@192.168.0.51 'hostname; uptime'<br><br># cat /usr/sbin/memfree-all-nodes <br>#!/bin/sh<br>/usr/bin/ssh -t admin@192.168.0.11 'hostname; grep MemFree /proc/meminfo'<br>/usr/bin/ssh -t admin@192.168.0.21 'hostname; grep MemFree /proc/meminfo'<br>/usr/bin/ssh -t admin@192.168.0.31 'hostname; grep MemFree /proc/meminfo'<br>/usr/bin/ssh -t admin@192.168.0.41 'hostname; grep MemFree /proc/meminfo'<br>/usr/bin/ssh -t admin@192.168.0.51 'hostname; grep MemFree /proc/meminfo'<br><br># cat /usr/sbin/netstat-tor-nodes <br>#!/bin/sh<br>/usr/bin/ssh -t admin@192.168.0.11 'hostname; netstat -anp | grep -i tor | grep -v 192.168.0.1: | wc -l'<br>/usr/bin/ssh -t admin@192.168.0.21 'hostname; netstat -anp | grep -i tor | grep -v 192.168.0.1: | wc -l'<br>/usr/bin/ssh -t admin@192.168.0.31 'hostname; netstat -anp | grep -i tor | grep -v 192.168.0.1: | wc -l'<br>/usr/bin/ssh -t admin@192.168.0.41 'hostname; netstat -anp | grep -i tor | grep -v 192.168.0.1: | wc -l'<br>/usr/bin/ssh -t admin@192.168.0.51 'hostname; netstat -anp | grep -i tor | grep -v 192.168.0.1: | wc -l'<br><br># cat /jffs/sbin/ps-tor-nodes <br>#!/bin/sh<br>/usr/bin/ssh -t admin@192.168.0.11 'hostname; ps w | grep -i tor'<br>/usr/bin/ssh -t admin@192.168.0.21 'hostname; ps w | grep -i tor'<br>/usr/bin/ssh -t admin@192.168.0.31 'hostname; ps w | grep -i tor'<br>/usr/bin/ssh -t admin@192.168.0.41 'hostname; ps w | grep -i tor'<br>/usr/bin/ssh -t admin@192.168.0.51 'hostname; ps w | grep -i tor'<br><br># cat /usr/sbin/killall-tor-nodes <br>#!/bin/sh<br>read -r -p "Are you sure? [y/N] " input<br>case "$input" in<br>[yY])<br>/usr/bin/ssh -t admin@192.168.0.11 'killall tor'<br>/usr/bin/ssh -t admin@192.168.0.21 'killall tor'<br>#/usr/bin/ssh -t admin@192.168.0.31 'killall tor'<br>/usr/bin/ssh -t admin@192.168.0.41 'killall tor'<br>/usr/bin/ssh -t admin@192.168.0.51 'killall tor'<br>return 0<br>;;<br>*)<br>return 1<br>;;<br>esac<br><br># cat /usr/sbin/restart-tor-nodes <br>#!/bin/sh<br>read -r -p "Are you sure? [y/N] " input<br>case "$input" in<br>[yY])<br>/usr/bin/ssh -t admin@192.168.0.11 '/usr/sbin/tor -f /tmp/torrc --quiet'<br>/usr/bin/ssh -t admin@192.168.0.21 '/usr/sbin/tor -f /tmp/torrc --quiet'<br>#/usr/bin/ssh -t admin@192.168.0.31 '/usr/sbin/tor -f /tmp/torrc --quiet'<br>/usr/bin/ssh -t admin@192.168.0.41 '/usr/sbin/tor -f /tmp/torrc --quiet'<br>/usr/bin/ssh -t admin@192.168.0.51 '/usr/sbin/tor -f /tmp/torrc --quiet'<br>return 0<br>;;<br>*)<br>return 1<br>;;<br>esac<br><br>I've been meaning to put together a tutorial on Loadbalancing Tor Relays, but haven't found the time as of yet. Perhaps, this will help, until I am able to find the time.<br><br>I appreciate your knowledge sharing and for furthering the topic of Loadbalancing Tor Relays; especially, with regard to Bridging and Exit Relays.<br><br>Keep up the Great Work!<br><br>Respectfully,<br><br><br>Gary<br>            </div>            <div class="yahoo_quoted" style="margin:10px 0px 0px 0.8ex;border-left:1px solid #ccc;padding-left:1ex;">                        <div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;color:#26282a;">                                <div>                    On Tuesday, January 4, 2022, 09:57:52 PM MST, Roger Dingledine <arma@torproject.org> wrote:                </div>                <div><br></div>                <div><br></div>                <div>[I'm about to go off-line for some days, so I am sending my current<br clear="none">suboptimally-organized reply, which I hope is better than waiting another<br clear="none">week to respond :)]<br clear="none"><br clear="none">On Thu, Dec 30, 2021 at 10:42:51PM -0700, David Fifield wrote:<br clear="none">> Let's make a distinction between the "frontend" snowflake-server<br clear="none">> pluggable transport process, and the "backend" tor process. These don't<br clear="none">> necessarily have to be 1:1; either one could be run in multiple<br clear="none">> instances. Currently, the "backend" tor is the limiting factor, because<br clear="none">> it uses only 1 CPU core. The "frontend" snowflake-server can scale to<br clear="none">> multiple cores in a single process and is comparatively unrestrained.<br clear="none"><br clear="none">Excellent point, and yes this simplifies. Great.<br clear="none"><br clear="none">> I believe that the "pinning" of a client session to particular tor<br clear="none">> instance will work automatically by the fact that snowflake-server keeps<br clear="none">> an outgoing connection alive (i.e., through the load balancer) as long<br clear="none">> as a KCP session exists.<br clear="none">>[...]<br clear="none">> But before starting the second instance the first time, copy keys from<br clear="none">> the first instance:<br clear="none"><br clear="none">Hm. It looks promising! But we might still have a Tor-side problem<br clear="none">remaining. I think it boils down to how long the KCP sessions last.<br clear="none"><br clear="none">The details on how exactly these bridge instances will diverge over time:<br clear="none"><br clear="none">The keys directory will start out the same, but after four weeks<br clear="none">(DEFAULT_ONION_KEY_LIFETIME_DAYS, used to be one week but in Tor<br clear="none">0.3.1.1-alpha, proposal 274, we bumped it up to four weeks) each<br clear="none">bridge will rotate its onion key (the one clients use for circuit-level<br clear="none">crypto). That is, each instance will generate its own fresh onion key.<br clear="none"><br clear="none">The two bridge instances actually haven't diverged completely at that<br clear="none">point, since Tor remembers the previous onion key (i.e. the onion key<br clear="none">from the previous period) and is willing to receive create cells that<br clear="none">use it for one further week (DEFAULT_ONION_KEY_GRACE_PERIOD_DAYS). So it<br clear="none">is after 5 weeks that the original (shared) onion key will no longer work.<br clear="none"><br clear="none">Where this matters is (after this 5 weeks have passed) if the client<br clear="none">connects to the bridge, fetches and caches the bridge descriptor of<br clear="none">instance A, and then later it connects to the bridge again and gets<br clear="none">passed to instance B. In this case, the create cell that the client<br clear="none">generates will use the onion key for instance A, and instance B won't<br clear="none">know how to decrypt it so it will send a destroy cell back.<br clear="none"><br clear="none">If this is an issue, we can definitely work around it, by e.g. disabling<br clear="none">the onion key rotation on the bridges, or setting up a periodic rsync+hup<br clear="none">between the bridges, or teaching clients to use createfast cells in this<br clear="none">situation (this type of circuit crypto doesn't use the onion key at all,<br clear="none">and just relies on TLS for security -- which can only be done for the<br clear="none">first hop of the circuit but that's the one we're talking about here).<br clear="none"><br clear="none">But before we think about workarounds, maybe we don't need one: how long<br clear="none">does "the KCP session" last?<br clear="none"><br clear="none">Tor clients try to fetch a fresh bridge descriptor every three-ish<br clear="none">hours, and once they fetch a bridge descriptor from their "current"<br clear="none">bridge instance, they should know the onion key that it wants to use. So<br clear="none">it is that up-to-three-hour window where I think things could go wrong.<br clear="none">And that timeframe sounds promising.<br clear="none"><br clear="none">(I also want to double-check that clients don't try to use the onion<br clear="none">key from the current cached descriptor while fetching the updated<br clear="none">descriptor. That could become an ugly bug in the wrong circumstances,<br clear="none">and would be something we want to fix if it's happening.)<br clear="none"><br clear="none">Here's how you can simulate a pair of bridge instances that have diverged<br clear="none">after five weeks, so you can test how things would work with them:<br clear="none"><br clear="none">Copy the keys directory as before, but "rm secret_onion_key*" in the<br clear="none">keys directory on n-1 of the instances, before starting them.)<br clear="none"><br clear="none">Thanks!<div class="yqt3348672060" id="yqtfd90653"><br clear="none">--Roger<br clear="none"><br clear="none">_______________________________________________<br clear="none">tor-relays mailing list<br clear="none"><a shape="rect" ymailto="mailto:tor-relays@lists.torproject.org" href="mailto:tor-relays@lists.torproject.org">tor-relays@lists.torproject.org</a><br clear="none"><a shape="rect" href="https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-relays" target="_blank">https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-relays</a><br clear="none"></div></div>            </div>                </div>