0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub Actions の Job から WireGuard で VPN アクセス

Posted at

背景

GitHub Actions の Job で家のネットワークにアクセスさせたいことがあり、いったんは Squid を認証付きで publilc に公開するというのをやっていたのですが、やっぱり嬉しくないのでどうしたものかと思っていたのですが WireGuard が使えるというドキュメントがあったのでこれを実装することにしました。

WireGuard はすでに Raspberry Pi で構築済みで、家族での DAZN 視聴に使っていたのを使います。

DietPi を使っているのですが WireGuard (PiVPN) セットアップは非常に簡単です、外からアクセスできるようにするのはおうちのネットワーク機器・構成依存ですが WireGuard で使う UDP port をひとつ port mapping だか port forwarding で転送すれば完了です。DDNS サービスを使えば Public IP アドレスの割り当てが変わっても大丈夫。

WireGuard の設定確認

WireGuard のサーバー側の wg0.conf は次のようになっています。

[Interface]
PrivateKey = (秘密:サーバーの秘密鍵)
Address = 10.220.182.1/24
MTU = 1420
ListenPort = 51820
### begin github-actions ###
[Peer]
# friendly_name = github-actions
PublicKey = (秘密:公開鍵)
PresharedKey = (秘密:事前共有鍵)
AllowedIPs = 10.220.182.9/32
### end github-actions ###

GitHub Actions の設定

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
      (中略)
      - run: sudo apt install wireguard

      - name: Setup VPN
        run: |
          echo "${{ secrets.WIREGUARD_PRIVATE_KEY }}" > privatekey # (1)
          echo "${{ secrets.WIREGUARD_PRESHARED_KEY }}" > presharedkey # (2)
          sudo ip link add dev wg0 type wireguard # (3)
          sudo ip address add dev wg0 10.220.182.9/24 peer 10.220.182.1/24 # (4)
          sudo wg set wg0 listen-port 48123 private-key privatekey peer ${{ secrets.WIREGUARD_SERVER_PUBLIC_KEY }} preshared-key presharedkey allowed-ips 0.0.0.0/0 endpoint ${{ secrets.WIREGUARD_ENDPOINT }} # (5)
          sudo ip link set up dev wg0 # (6)
          sudo ip route add 192.168.0.0/24 dev wg0 # (7)

      - name: Connectivity test
        run: ping -w 3 192.168.0.100
  • (1)、(2) ではクライアント向けに発行されている秘密鍵と事前共有鍵をファイルに保存しています
  • (3) で VPN 用の Network interface を作成しています
  • (4) では (3) で作成した Network interface に IP アドレスを設定しています
    10.220.182.9/24 がクライアントに発行されているアドレスで、10.220.182.1/24 がサーバー側のアドレスです
  • (5) で WireGuard 接続のための設定を行っています
    • (1)、(2) で保存していたファイルで private-key、preshared-key を指定しています
    • サーバーの公開鍵を peer の引数で指定しています
    • endpoint でサーバーのホスト名 or IP アドレスと port を指定しています (my-wg-server.example.com:51820 など)
  • (6) で VPN 用 Network interface を up させることで VPN 接続させています
  • (7) で 192.168.0.0/24 宛てを VPN にルーティングするようにルーティングテーブルに追加しています

Self-hosted runner ではダメなのか

家に十分なリソースをもったサーバーがあれば Self-hosted runner として使えば VPN 接続などせずともそもそも家のネットワーク内で Job を実行可能なのですが、メモリが 2GB しかない Raspberry Pi 4 しかないのであきらめました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?