<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Gateway on Mini Fish</title>
    <link>https://blog.minifish.org/tags/gateway/</link>
    <description>Recent content in Gateway on Mini Fish</description>
    <image>
      <title>Mini Fish</title>
      <url>https://blog.minifish.org/android-chrome-512x512.png</url>
      <link>https://blog.minifish.org/android-chrome-512x512.png</link>
    </image>
    <generator>Hugo -- 0.154.5</generator>
    <language>en-US</language>
    <copyright>Mini Fish 2014-present. Licensed under CC-BY-NC</copyright>
    <lastBuildDate>Wed, 12 Oct 2022 21:07:00 +0800</lastBuildDate>
    <atom:link href="https://blog.minifish.org/tags/gateway/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>How to Deploy a Secure Transparent Gateway</title>
      <link>https://blog.minifish.org/posts/how-to-deploy-a-secure-transparent-gateway/</link>
      <pubDate>Wed, 12 Oct 2022 21:07:00 +0800</pubDate>
      <guid>https://blog.minifish.org/posts/how-to-deploy-a-secure-transparent-gateway/</guid>
      <description>&lt;h2 id=&#34;background&#34;&gt;Background&lt;/h2&gt;
&lt;p&gt;After moving house, there are many more devices at home that need internet access. However, I don&amp;rsquo;t want to configure a proxy on each device, so I thought of using a transparent gateway.&lt;/p&gt;
&lt;h2 id=&#34;transparent-gateway&#34;&gt;Transparent Gateway&lt;/h2&gt;
&lt;p&gt;After some research, I found that the easiest way is to use the premium version of Clash, although I didn&amp;rsquo;t know when Clash released a premium version. I mainly referred to &lt;a href=&#34;https://www.cfmem.com/2022/05/clash.html&#34;&gt;this article&lt;/a&gt;. It&amp;rsquo;s much simpler than setting up iptables.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<h2 id="background">Background</h2>
<p>After moving house, there are many more devices at home that need internet access. However, I don&rsquo;t want to configure a proxy on each device, so I thought of using a transparent gateway.</p>
<h2 id="transparent-gateway">Transparent Gateway</h2>
<p>After some research, I found that the easiest way is to use the premium version of Clash, although I didn&rsquo;t know when Clash released a premium version. I mainly referred to <a href="https://www.cfmem.com/2022/05/clash.html">this article</a>. It&rsquo;s much simpler than setting up iptables.</p>
<h3 id="network-topology">Network Topology</h3>
<p>I have a 10-year-old Thinkpad x230 at home, which is perfect for this purpose. Here is a simple topology diagram.</p>
<p>Router1 is a fiber-optic modem with routing capabilities, Router2 is a regular router, with the gateway and DNS pointing to the Thinkpad, where Linux is running to act as a transparent gateway with Clash on top.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-txt" data-lang="txt"><span style="display:flex;"><span>                                 +------------+
</span></span><span style="display:flex;"><span>                                 |            |
</span></span><span style="display:flex;"><span>                                 |  Internet  |
</span></span><span style="display:flex;"><span>                                 |            |
</span></span><span style="display:flex;"><span>                                 +-----+------+
</span></span><span style="display:flex;"><span>                                       |
</span></span><span style="display:flex;"><span>                                 +-----+------+
</span></span><span style="display:flex;"><span>                                 |            |
</span></span><span style="display:flex;"><span>                      +----------+  Router1   +-----------+
</span></span><span style="display:flex;"><span>                      |          |            |           |
</span></span><span style="display:flex;"><span>                      |          +------------+           |
</span></span><span style="display:flex;"><span>                      |                                   |
</span></span><span style="display:flex;"><span>                      |                                   |
</span></span><span style="display:flex;"><span>                +-----+-----+                       +-----+------+
</span></span><span style="display:flex;"><span>                |           |                       |            |
</span></span><span style="display:flex;"><span>     +----------+  Router2  +----------+            |  Thinkpad  |
</span></span><span style="display:flex;"><span>     |          |           |          |            |            |
</span></span><span style="display:flex;"><span>     |          +-----+-----+          |            +------------+
</span></span><span style="display:flex;"><span>     |                |                |
</span></span><span style="display:flex;"><span>     |                |                |
</span></span><span style="display:flex;"><span>     |                |                |
</span></span><span style="display:flex;"><span>+----+-----+     +----+-----+    +-----+-----+
</span></span><span style="display:flex;"><span>|          |     |          |    |           |
</span></span><span style="display:flex;"><span>|   Mac    |     |  iPad    |    |  iPhone   |
</span></span><span style="display:flex;"><span>|          |     |          |    |           |
</span></span><span style="display:flex;"><span>+----------+     +----------+    +-----------+
</span></span></code></pre></div><h3 id="add-dns-section-in-clash-configuration">Add DNS Section in Clash Configuration</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">dns</span>:
</span></span><span style="display:flex;"><span><span style="color:#f92672">enable</span>: <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">listen</span>: <span style="color:#ae81ff">0.0.0.0</span>:<span style="color:#ae81ff">53</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">enhanced-mode</span>: <span style="color:#ae81ff">fake-ip</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">nameserver</span>:
</span></span><span style="display:flex;"><span>  - <span style="color:#ae81ff">114.114.114.114</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">fallback</span>:
</span></span><span style="display:flex;"><span>  - <span style="color:#ae81ff">8.8.8.8</span>
</span></span></code></pre></div><h3 id="clash-tun-feature-section">Clash tun Feature Section</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">tun</span>:
</span></span><span style="display:flex;"><span><span style="color:#f92672">enable</span>: <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">stack</span>: <span style="color:#ae81ff">system</span> <span style="color:#75715e"># or gvisor</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">dns-hijack</span>:
</span></span><span style="display:flex;"><span>  - <span style="color:#ae81ff">any:53</span>
</span></span><span style="display:flex;"><span>  - <span style="color:#ae81ff">tcp://any:53</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">auto-route</span>: <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">auto-detect-interface</span>: <span style="color:#66d9ef">true</span>
</span></span></code></pre></div><p>For traffic forwarding, simply edit <code>/etc/sysctl.conf</code> on the Thinkpad and add <code>net.ipv4.ip_forward=1</code>, then execute <code>sysctl -p</code> to apply it. After that, point the gateway and DNS of Router2 to the Thinkpad, and you&rsquo;re done.</p>
<h2 id="network-protocols">Network Protocols</h2>
<p>Initially, I used native HTTP2 for unblocking, but it cannot proxy UDP. When only a few devices need unblocking, it doesn&rsquo;t matter whether UDP is used, but with many devices at home, some of them can only use UDP. I considered socks + tls, but it didn&rsquo;t feel secure and required opening odd ports like UDP 443. It felt like giving away my intentions. Eventually, I chose Trojan, which essentially mimics native HTTPS. Trojan has two versions; I used Trojan-go simply because I didn&rsquo;t want to manage dependencies. Also, I&rsquo;m more familiar with Go.</p>
<p>Trojan-go has a requirement for a genuinely accessible HTTP server, so I used the simplest Python <code>http.server</code>. Back in Python 2, it was called <code>simplehttp</code>. You can simply use <code>python3 -m http.server 80</code> and optionally add <code>--directory</code> to specify a directory.</p>
<p>Additionally, Trojan-go requires the client to fill in the SNI, which means using the domain used during key application. Therefore, prerequisites like applying for the <a href="https://github.com/haoel/haoel.github.io">domain</a>, applying for Let&rsquo;s Encrypt certificates, and configuring crontab must all be completed. There&rsquo;s a learning curve, but I had done it before, so I just skipped that part.</p>
<p>For the client part, you can use Clash directly, and refer to <a href="https://github.com/Dreamacro/clash/wiki/configuration">here</a> for guidance.</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
