91 lines
No EOL
3.8 KiB
Markdown
91 lines
No EOL
3.8 KiB
Markdown
# 20240211141946 Cloudflareでトンネルを作る
|
|
#raspi4 #raspi5 #cloudflare #server
|
|
|
|
Raspberry Pi 4, 5, N100サーバーなどで実施。
|
|
|
|
|
|
## 注意
|
|
色々なサイトを参考にしたけど、変化が激しいので100%参考になることは無かった。おそらくこのメモもすぐに陳腐化すると思う。私はこの分野の専門ではないので、全然わからないで適当にやっている。
|
|
|
|
## トンネルとは
|
|
[トンネリングとは?| ネットワークにおけるトンネリング | Cloudflare](https://www.cloudflare.com/ja-jp/learning/network-layer/what-is-tunneling/)
|
|
|
|
## 前提
|
|
- ドメインを持っている
|
|
- ネームサーバーをcloudflareに移管できる
|
|
- ブラウザでがんばってやろうとしたが挫折。 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/) に書いてある内容を基本的にフォローする感じ
|
|
|
|
## cloudflareでアカウントを作る
|
|
|
|
[Cloudflare | Web Performance & Security](https://dash.cloudflare.com/sign-up)
|
|
ここからサインアップしてアカウントを作る。
|
|
|
|
## cloudflaredをインストールする
|
|
raspi4にcloudflaredをインストールする。
|
|
[Downloads · Cloudflare Zero Trust docs](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/) から、 raspiならARM64をダウンロードしてインストール。intelでubuntuやdebianの場合 amd64 / x86-64 の方の.deb
|
|
|
|
```
|
|
$ sudo dpkg -i coudflared-linux-amd64.deb
|
|
```
|
|
|
|
|
|
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 *****
|
|
```
|
|
`*****` の所のtokenをメモしておく [[20240212183159 Cloudflare トンネルのtoken|Cloudflare トンネルのtoken(private)]]
|
|
|
|
以上でトンネル作成の準備が完了。具体的な、何をトンネル経由でやりとりするか? の設定は、
|
|
`~/.cloudflared/config.yaml` に書いていく。
|
|
例は以下
|
|
- [[20240211185359 Cloudflareでsshアクセスできるようにする|Raspi4 Cloudflareでsshアクセスできるようにする]]
|
|
- [[20240211202623 Raspi4 Cloudflare tunnel経由でhttpsサーバーを公開する|Raspi4 Cloudflare tunnel経由でhttpsサーバーを公開する]] |