Try   HackMD

前言

趁著openwrt推出了新版本23.05,最近也開始學習如何使用cloudflare,把服務做了一次整理。先前的服務大致上有:

  • Blog: GitHub Pages (jekyll)
    • Staging (jekyll build, static files)
  • resume: GitHub Pages (static files)
  • WireGuard: 使用openwrt上的luci設定

並使用Let's encrypt取得TLS憑證。而先前的網域xnum.tw快到期了,順便換成xnum.in

cloudflare的使用方式

本來以為cdn就是個加速服務,可以託管一些靜態檔案,沒想到功能越做越多,實際嘗試後也發現在自行架設服務時方便很多。目前使用的幾種方式如下:

DNS-Only

這就和正常的dns server一樣,新增一條A record上去。用來指可以從公網ssh連線的主機,就不用記IP了。

Proxied

採取這種方式,所有流量都會經過cloudflare。第一段是從browser端連線到cloudflare,這會使用cloudflare簽發的憑證。第二段是從cloudflare連線到server,這段有不同加密模式可以設定。在Domain裡面的SSL/TLS可以設定加密模式,如果設定成Full就會使用TLS連線,但在server上也需要一張憑證才行。

在SSL/TLS裡面有一個Origin Server可以用來產生cloudflare自己簽發的憑證,用來加密這段流量,因此在server端就不需要再透過acme拿另外一張憑證了。

Tunnel

使用Tunnel的好處是不需要在防火牆上面放行INPUT,而是由內部連向cloudflare來通訊。藉由減少可攻擊面積,這很大程度的提升了伺服器的安全性。

透過Tunnel連線時,可以指定要使用HTTP或是HTTPS連線,並且可以在網址直接使用IP和Port的方式,例如https://127.0.0.1:8000。

另外一個好處是可以搭配Zero Trust使用,所有連線的使用者都需要利用OAuth或TOTP等方式先進行認證,並通過規則檢查,例如:特定的email清單或email domain。一般的內網服務通常是利用Basic Auth或OIDC來進行認證,然而設定Zero Trust以後可以增加攻擊的困難度,等於多上了一道保險。

HTTP

這個方式是最常見的,用來暴露內網的服務。不需要額外設定就能使用。

HTTPS (IP, self-signed)

有些服務會透過這種方式提供,最常見的是proxmox ve和各家NAS的web ui。他們沒有VirtualHost,而是直接listen一個特定的port。

這種情況下需要將No TLS Verify啟用來放行自己簽署的憑證

此頁面在Zero Trust > Access > Tunnel > Config > Public Hostnames > Edit > Additional application settings。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

HTTPS

有一些服務透過正統的方式取得acme憑證並架設在網際網路上,但為了增加安全性,設定了防火牆來放行白名單IP。通常這個架設方式會使用VirtualHost,也就是判斷request上的Host Header來切換不同的service。

如果我們想要在內部架設tunnel並順利連線,就需要額外設定HTTP Host Header指到對應的domain上。

此頁面也在Tunnel上

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

服務

linktree

littlelink是一個開源的linktree。沒有前後端,屬於純粹的靜態檔案。

對於靜態檔案可以直接透過cloudflare Proxied的方式部署。

Blog

雖然最後部署在GitHub Pages上面,在編寫時仍有可能需要一個Staging站來預覽結果,透過以下指令可以生成Staging用的靜態網站。

jekyll build --watch --config _config.local.yml

Shaarli

shaarli是一個開源的書籤管理服務,他也可以進行公開分享。對於大量在網路上看到的文章,就不需要儲存在瀏覽器內,可以藉由這個服務管理並搜尋歷史。

由於它需要儲存資料,我使用docker將他架設在NAS上,並利用cloudflare Tunnel暴露到公網上。

VPN

WireGuard

在openwrt上使用wireguard需要安裝kmod-wireguard

最近出現了一個有趣的專案wg-easy。為了簡化我將他使用host network來提供服務。docker-compose.yaml如下:

version: "3.8"
services:
  wg-easy:
    environment:
      - WG_HOST=example.com
      - WG_PERSISTENT_KEEPALIVE=20
      - WG_DEVICE=br-lan
    image: weejewel/wg-easy
    container_name: wg-easy
    network_mode: "host"
    volumes:
      - .:/etc/wireguard
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
      - SYS_MODULE

wg-easy提供了一個web-ui,這個ui可以使用cloudflare tunnel來暴露。

另外需要將他的interface wg0加入到openwrt的介面中,懶惰的做法是直接把wg0當成LAN zone。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

並且在防火牆允許通過

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Future Works

Cloudflare Pages

類似GitHub Pages,但他可以編譯前端程式碼。此外在設定custom domain也比較方便。