public_notes/content/20240211141946 Cloudflareでトンネルを作る.md

91 lines
3.8 KiB
Markdown
Raw Normal View History

2024-04-09 17:45:20 +09:00
# 20240211141946 Cloudflareでトンネルを作る
2024-04-07 14:59:43 +09:00
#raspi4 #raspi5 #cloudflare #server
2024-02-13 20:10:02 +09:00
2024-04-09 17:45:20 +09:00
Raspberry Pi 4, 5, N100サーバーなどで実施。
2024-02-13 20:10:02 +09:00
## 注意
色々なサイトを参考にしたけど、変化が激しいので100%参考になることは無かった。おそらくこのメモもすぐに陳腐化すると思う。私はこの分野の専門ではないので、全然わからないで適当にやっている。
## トンネルとは
[トンネリングとは?| ネットワークにおけるトンネリング | Cloudflare](https://www.cloudflare.com/ja-jp/learning/network-layer/what-is-tunneling/)
## 前提
- ドメインを持っている
- ネームサーバーをcloudflareに移管できる
2024-02-28 21:00:23 +09:00
- ブラウザでがんばってやろうとしたが挫折。 cliの方法を試す。つまり、 [Create a locally-managed tunnel (CLI) · Cloudflare Zero Trust docs](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/) に書いてある内容を基本的にフォローする感じ
2024-02-13 20:10:02 +09:00
## cloudflareでアカウントを作る
[Cloudflare | Web Performance & Security](https://dash.cloudflare.com/sign-up)
ここからサインアップしてアカウントを作る。
## cloudflaredをインストールする
raspi4にcloudflaredをインストールする。
2024-04-09 17:45:20 +09:00
[Downloads · Cloudflare Zero Trust docs](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/) から、 raspiならARM64をダウンロードしてインストール。intelでubuntuやdebianの場合 amd64 / x86-64 の方の.deb
2024-02-13 20:10:02 +09:00
```
$ sudo dpkg -i coudflared-linux-amd64.deb
```
2024-04-07 18:09:47 +09:00
2024-02-13 20:10:02 +09:00
sshのproxycommandとして使うので、wsl2(のubuntu) amd64/x86-64をダウンロードしてそちらにもインストールした。
### 専用ユーザーを作成
```
$ sudo groupadd --system cloudflare
$ sudo useradd --system \
--gid cloudflare \
--create-home \
--home-dir /var/lib/cloudflare \
--comment "Cloudflaire" \
cloudflare
$ sudo passwd cloudflare
New password:
Retype new password:
passwd: password updated successfully
$ sudo visudo
cloudflare ALL=(ALL:ALL) ALL
を追加
kazbo@saipi4:~ $ su cloudflare
Password:
cloudflare@saipi4:/home/kazbo $ cd
cloudflare@saipi4:~ $ pwd
/var/lib/cloudflare
```
以降作業はこのユーザー(cloudflare)で実施
### ログイン
```
cloudflare@saipi4:~ $ cloudflared login
Please open the following URL and log in with your Cloudflare account:
https://dash.cloudflare.com/argotunnel?aud=&callback=https%3A%2F%2Flogin.cloudflareaccess.org%...
Leave cloudflared running to download the cert automatically.
[URLをクリックして、使いたいドメインをクリック、承認を押す]
You have successfully logged in.
If you wish to copy your credentials to a server, they have been saved to:
/var/lib/cloudflare/.cloudflared/cert.pem
```
## トンネルの作成
```
cloudflare@saipi4:~ $ cloudflared tunnel create saipi4(ここは自分が設定したい名前)
Tunnel credentials written to /var/lib/cloudflare/.cloudflared/******.json. cloudflared chose this file based on where your origin certificate was found. Keep this file secret. To revoke these credentials, delete the tunnel.
Created tunnel saipi4 with id *****
```
2024-02-28 21:00:23 +09:00
`*****` の所のtokenをメモしておく [[20240212183159 Cloudflare トンネルのtoken|Cloudflare トンネルのtoken(private)]]
2024-02-13 20:10:02 +09:00
2024-02-28 21:00:23 +09:00
以上でトンネル作成の準備が完了。具体的な、何をトンネル経由でやりとりするか? の設定は、
`~/.cloudflared/config.yaml` に書いていく。
例は以下
2024-04-09 17:45:20 +09:00
- [[20240211185359 Cloudflareでsshアクセスできるようにする|Raspi4 Cloudflareでsshアクセスできるようにする]]
2024-02-28 21:00:23 +09:00
- [[20240211202623 Raspi4 Cloudflare tunnel経由でhttpsサーバーを公開する|Raspi4 Cloudflare tunnel経由でhttpsサーバーを公開する]]