<?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>Building a Stateless, RAM-Only Transparent Tor Proxy for Linux (TTP v0.3.0)</title>
      <dc:creator>onyks</dc:creator>
      <pubDate>Fri, 15 May 2026 10:51:30 +0000</pubDate>
      <link>https://dev.to/onyks/building-a-stateless-ram-only-transparent-tor-proxy-for-linux-ttp-v030-gfi</link>
      <guid>https://dev.to/onyks/building-a-stateless-ram-only-transparent-tor-proxy-for-linux-ttp-v030-gfi</guid>
      <description>&lt;p&gt;Transparent Tor Proxy (TTP) is a Linux CLI tool designed to intercept all outgoing network traffic and force it through the Tor network using &lt;code&gt;nftables&lt;/code&gt;. Version 0.3.0 introduces a major architectural shift focused on system integrity and forensic invisibility, transitioning the application to a fully volatile runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Challenge
&lt;/h2&gt;

&lt;p&gt;Classic transparent proxy implementations often suffer from severe state management issues. System configurations, such as &lt;code&gt;/etc/resolv.conf&lt;/code&gt;, are typically overwritten directly. Furthermore, the default system Tor daemon (&lt;code&gt;tor.service&lt;/code&gt;) is usually hijacked, leading to permission conflicts and disrupting any pre-existing background Tor relays.&lt;/p&gt;

&lt;h2&gt;
  
  
  The v0.3.0 Solution: Amnesia Core
&lt;/h2&gt;

&lt;p&gt;The application core has been redesigned to operate almost entirely in volatile memory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Volatile Standard Core&lt;/strong&gt;: All runtime metadata, lock files, and logs are now stored in &lt;code&gt;/run/ttp&lt;/code&gt;, which utilizes a &lt;code&gt;tmpfs&lt;/code&gt; mount. This ensures zero physical traces are left on the host's physical disk after a power cycle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Native Service Management&lt;/strong&gt;: Tor is executed as a dedicated &lt;code&gt;ttp-tor.service&lt;/code&gt; systemd unit. The volatile unit file is dynamically generated in &lt;code&gt;/run/systemd/system/&lt;/code&gt;, avoiding interference with the system's own &lt;code&gt;tor.service&lt;/code&gt; and bypassing its sandboxing restrictions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conflict Resolution&lt;/strong&gt;: Daemon management is handled via a private Unix ControlSocket located at &lt;code&gt;/run/tor/ttp/control.sock&lt;/code&gt;. &lt;code&gt;SocksPort&lt;/code&gt; is set to &lt;code&gt;0&lt;/code&gt; by default, allowing TTP to coexist alongside other Tor instances without triggering "Address already in use" errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stateless DNS Overlay&lt;/strong&gt;: Physical overwrites of &lt;code&gt;/etc/resolv.conf&lt;/code&gt; have been replaced with a kernel-level &lt;code&gt;mount --bind&lt;/code&gt; strategy. This achieves non-destructive redirection of DNS queries. Stale DNS mount overlays are automatically cleared to ensure absolute idempotency, and a lazy unmount fallback (&lt;code&gt;umount -l&lt;/code&gt;) ensures successful removal during teardown even if the resource is busy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Network Safety and Memory Trade-offs
&lt;/h2&gt;

&lt;p&gt;Operating exclusively in RAM introduces specific constraints that require careful resource and state management.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Graceful Teardown&lt;/strong&gt;: A cryptographic &lt;code&gt;SHUTDOWN&lt;/code&gt; signal is sent to the Tor daemon before &lt;code&gt;nftables&lt;/code&gt; firewall rules are removed. This guarantees all circuits close cleanly and strictly prevents cleartext &lt;code&gt;RST&lt;/code&gt; packet leaks on the physical interface.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Persistent Entry Guards&lt;/strong&gt;: Bootstrapping Tor entirely from scratch on every execution introduces severe latency. To mitigate this, &lt;code&gt;DataDirectory /var/lib/tor/ttp/&lt;/code&gt; is utilized to preserve Entry Guards across runs, maintaining a fast bootstrap time of approximately 3 seconds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource Management&lt;/strong&gt;: Logs have been moved to &lt;code&gt;/run/ttp/ttp.log&lt;/code&gt; and are capped at a 1MB limit to prevent memory exhaustion in the RAM disk. Additionally, a pre-flight safety check is performed to verify sufficient &lt;code&gt;tmpfs&lt;/code&gt; space is available before execution, preventing out-of-memory crashes mid-setup.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The networking logic utilizes a dedicated &lt;code&gt;inet ttp&lt;/code&gt; table for &lt;code&gt;nftables&lt;/code&gt;, ensuring all firewall modifications are isolated and cleanup is executed atomically via an &lt;code&gt;nft destroy&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;: Entry guards are maintained in a persistent cache to facilitate rapid initialization. Although a setting to modify this behavior is currently absent, its introduction is expected in a subsequent version. It must be noted that standard OS-level logging, including systemd journals, remains active, meaning the tool is not forensically invisible or reliable for high-stakes anonymity. For scenarios necessitating absolute forensic protection, the use of TailsOS and the TOR Browser is advised.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repository:&lt;/strong&gt; &lt;a href="https://github.com/onyks-os/TransparentTorProxy" rel="noopener noreferrer"&gt;TransparentTorProxy on GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>networking</category>
      <category>security</category>
      <category>showdev</category>
    </item>
    <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>
