<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: onyks</title>
    <description>The latest articles on DEV Community by onyks (@onyks).</description>
    <link>https://dev.to/onyks</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3918706%2Fdde3cf5b-8429-4bf1-a37c-915d6180e9d2.jpeg</url>
      <title>DEV Community: onyks</title>
      <link>https://dev.to/onyks</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/onyks"/>
    <language>en</language>
    <item>
      <title>How I fixed network state corruption in my Linux Tor proxy</title>
      <dc:creator>onyks</dc:creator>
      <pubDate>Mon, 11 May 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/onyks/how-i-fixed-network-state-corruption-in-my-linux-tor-proxy-lmn</link>
      <guid>https://dev.to/onyks/how-i-fixed-network-state-corruption-in-my-linux-tor-proxy-lmn</guid>
      <description>&lt;p&gt;Most transparent proxy scripts share a fatal flaw: if they crash, they take your system's network down with them. Firewall rules are left lingering, DNS is routed to a dead port, and you're forced to manually flush routing tables just to get back online.&lt;/p&gt;

&lt;p&gt;When building &lt;strong&gt;TTP (Transparent Tor Proxy)&lt;/strong&gt;, I wanted to engineer a resilient system that survives crashes and prevents leaks, especially on modern distros dealing with &lt;code&gt;systemd-resolved&lt;/code&gt; and &lt;code&gt;firewalld&lt;/code&gt; conflicts.&lt;/p&gt;

&lt;p&gt;Here is the engineering approach I used to handle the three biggest network failure points.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Atomic Cleanup with Stateless nftables
&lt;/h3&gt;

&lt;p&gt;Modifying the system's default firewall rules requires complex backup and restore logic. It's fragile and prone to race conditions. &lt;/p&gt;

&lt;p&gt;Instead of touching existing rules, TTP creates a strictly isolated &lt;code&gt;inet ttp&lt;/code&gt; table. I set the &lt;code&gt;output&lt;/code&gt; hook to &lt;code&gt;priority -150&lt;/code&gt; to ensure TTP's redirection executes &lt;em&gt;before&lt;/em&gt; any standard NAT or &lt;code&gt;firewalld&lt;/code&gt; rules. &lt;/p&gt;

&lt;p&gt;Because the ruleset is completely isolated, network restoration is a single, atomic operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nft destroy table inet ttp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the proxy stops, the table is nuked. No system state backups are required, meaning zero risk of permanent corruption.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Auto-Recovering Orphaned Sessions
&lt;/h3&gt;

&lt;p&gt;If the main process is killed abruptly (e.g., &lt;code&gt;SIGKILL&lt;/code&gt; or power loss), the network remains trapped in Tor-routing mode. &lt;/p&gt;

&lt;p&gt;To solve this, TTP uses a persistent JSON lock file (&lt;code&gt;/var/lib/ttp/ttp.lock&lt;/code&gt;) to track the session state. On startup, the CLI checks for this file. If it exists, it verifies if the recorded PID is actually alive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_orphan&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# ...
&lt;/span&gt;    &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pid&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;kill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Sends no signal, just checks for existence
&lt;/span&gt;    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;OSError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt; &lt;span class="c1"&gt;# Process is dead, session is orphaned
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If an orphaned session is detected, TTP automatically triggers the rollback logic (destroying the &lt;code&gt;nftables&lt;/code&gt; table and restoring DNS) before starting a new, clean circuit.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The DNS Hard-Reset Strategy
&lt;/h3&gt;

&lt;p&gt;Routing DNS through Tor (&lt;code&gt;127.0.0.1:9053&lt;/code&gt;) is straightforward. Restoring it reliably is hard because &lt;code&gt;systemd-resolved&lt;/code&gt; aggressively caches states. &lt;/p&gt;

&lt;p&gt;A simple &lt;code&gt;revert&lt;/code&gt; command often leaves the system unable to resolve domains. To guarantee cleartext connectivity upon exit, the restoration uses a "Hard-Reset" sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Revert interface-specific overrides (&lt;code&gt;resolvectl revert &amp;lt;interface&amp;gt;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Force-restart the daemon to purge lingering memory states (&lt;code&gt;systemctl restart systemd-resolved&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Wipe the cache entirely (&lt;code&gt;resolvectl flush-caches&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Result
&lt;/h3&gt;

&lt;p&gt;The result is a proxy that guarantees a safe return to cleartext routing, even after a fatal crash. I've also added an emergency &lt;code&gt;--restore-only&lt;/code&gt; flag and active leak audits (&lt;code&gt;ttp check-leak&lt;/code&gt;) to verify exit IPs and DNS routes dynamically.&lt;/p&gt;

&lt;p&gt;If you're interested in low-level Linux networking, &lt;code&gt;nftables&lt;/code&gt; routing, or want to audit the code, you can check the repository &lt;a href="https://github.com/onyks-os/TransparentTorProxy" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I am currently an undergraduate CS student, so any feedback is profoundly appreciated.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>python</category>
      <category>networking</category>
      <category>security</category>
    </item>
  </channel>
</rss>
