apache guacamoleで自宅サーバにアクセスしたい Internetリバースプロキシ連携編 (original) (raw)

前回構築したapache guacamoleですが、インターネット経由でアクセスしてこそ目的を果たします。
自宅のルータから公開してもいいのですが客先から自分のプロバイダのIPにアクセスを続けていると怪しまれるかもしれません。

1つリバースプロキシをかまします。

Digital OceanのDroplets構築

昔作ったDigital Oceanのアカウントがあるのでここに作ります。
1台お遊びブログのWordpressが動いていますが、これに共用させるのはよろしくないので最小構成でお安くたてます。

メモリ512MBのストレージ10GBで月4ドルです。まあいいでしょう。
ここも512MBで動くDebianにします。
最小でもプロキシしかしないから大丈夫でしょ。困ったら増強すればよいのがクラウドのいいところです。

詳細までお見せできませんがルーティングのホップの関係で激遅になっても嫌なのでアジア地区のリージョンにデプロイしました。

リバースプロキシ構成

やりたい構成はこんな感じです。

インターネットの自端末からリバースプロキシを経由して自宅のguacamoleサーバにアクセス。
guacamoleのブラウザ経由で自宅サーバが触れるという寸法です。

ネットワーク上何やっているかバレないように暗号化する必要があるのでプロキシ前段はSSL、後段はVPNを張ります。
VPNはラボ内にOpenVPNの環境があるのでこれを流用します。

リバースプロキシソフトは何にするか悩みましたが以前ラボ内にも立てたhaproxyで作ることにします。

デプロイ後の構築

Dropletsデプロイ後はローカルPCからSSHで繋いで構築します。

❯ ssh root@xxx.xxx.231.148 The authenticity of host 'xxx.xxx.231.148 (xxx.xxx.231.148)' can't be established. ED25519 key fingerprint is SHA256:N4x/PTqqKyvJew+XImCBs2r6tM7vPvxE9qxWTk0PvLI. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes ...

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~#

パッケージインストールくらいまではansibleでやりたかったのですがなぜかrootユーザ以外の公開鍵のパーミッションエラーになります。

❯ ssh masashi@xxx.xxx.231.148 masashi@xxx.xxx.231.148: Permission denied (publickey).

どちらの設定が悪いかわからないので断念して手動で作ります。
もうrootで作っちゃいます。

パッケージインストール

リポジトリアップデートと使うものをインストール。
curl, vimは私の好みで入れます。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:# apt update root@debian-s-1vcpu-512mb-10gb-sgp1-01:# apt install curl vim haproxy openvpn

openvpnのドキュメントを見るとバージョン3が出ているようなのでremoveして入れなおします。

openvpn.net

root@debian-s-1vcpu-512mb-10gb-sgp1-01:# apt remove openvpn root@debian-s-1vcpu-512mb-10gb-sgp1-01:# mkdir -p /etc/apt/keyrings && curl -fsSL https://packages.openvpn.net/packages-repo.gpg | sudo tee /etc/apt/keyrings/openvpn.asc -----BEGIN PGP PUBLIC KEY BLOCK----- ...(略) -----END PGP PUBLIC KEY BLOCK----- root@debian-s-1vcpu-512mb-10gb-sgp1-01:# DISTRO=$(lsb_release -c | awk '{print $2}') root@debian-s-1vcpu-512mb-10gb-sgp1-01:# echo "deb [signed-by=/etc/apt/keyrings/openvpn.asc] https://packages.openvpn.net/openvpn3/debian $DISTRO main" | sudo tee /etc/apt/sources.list.d/openvpn-packages.list deb [signed-by=/etc/apt/keyrings/openvpn.asc] https://packages.openvpn.net/openvpn3/debian bookworm main root@debian-s-1vcpu-512mb-10gb-sgp1-01:# sudo apt update root@debian-s-1vcpu-512mb-10gb-sgp1-01:# apt install openvpn3 The following additional packages will be installed: gir1.2-glib-2.0 libgdbuspp2 libgirepository-1.0-1 libjsoncpp25 libprotobuf32 libtinyxml2-9 python3-gi python3-systemd Recommended packages: kmod-ovpn-dco The following NEW packages will be installed: gir1.2-glib-2.0 libgdbuspp2 libgirepository-1.0-1 libjsoncpp25 libprotobuf32 libtinyxml2-9 openvpn3 python3-gi python3-systemd 0 upgraded, 9 newly installed, 0 to remove and 36 not upgraded. Need to get 3398 kB of archives. After this operation, 13.6 MB of additional disk space will be used. Do you want to continue? [Y/n] y

バックエンド側構築

openvpnで自宅のpfSenseとSSLVPNを張ります。
スマホ用に作成済みのプロファイルを使いまわしてサクッとつなぎます。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:# cd pfSense1-TCP4-10443-masashi/ root@debian-s-1vcpu-512mb-10gb-sgp1-01:/pfSense1-TCP4-10443-masashi# ls pfSense1-TCP4-10443-masashi-tls.key pfSense1-TCP4-10443-masashi.ovpn pfSense1-TCP4-10443-masashi.p12

pfSenceからプロファイルをダウンロードして転送しました。
スマホに入っているものと設定は同じです。

ドキュメントに沿って接続。

ovpn configインポート

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# openvpn3 config-import --config pfSense1-TCP4-10443-masashi.ovpn --name homelab Configuration imported. Configuration path: /net/openvpn/v3/configuration/f058020ax67d6x41edxa50cxfe7e4018c7e6

ACL

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# openvpn3 config-acl --show --lock-down true --grant root --config homelab Granted access to root (uid 0) Configuration has been locked down

Configuration path: /net/openvpn/v3/configuration/f058020ax67d6x41edxa50cxfe7e4018c7e6
              Name: homelab
             Owner: (0)  root
         Read-only: no
       Locked down: yes
Ownership transfer: no
     Public access: no

Users granted access: 1 user - (0) root

サービス起動

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# systemctl enable --now openvpn3-session@homelab.service Job for openvpn3-session@homelab.service failed because the service did not take the steps required by its unit configuration. See "systemctl status openvpn3-session@homelab.service" and "journalctl -xeu openvpn3-session@homelab.service" for details.

うーむ。失敗。 ログを見ます。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# journalctl -xeu openvpn3-session@homelab.service No journal files were found. ~ ... ~ -- No entries --

何もなし。

ログレベルを変えてみます。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# openvpn3-admin log-service --log-level 6 Log method: journald Attached log subscriptions: 2 Log timestamps: enabled Log tag prefix enabled: enabled Log D-Bus details: enabled Current log level: 6 (was 3)

root@debian-s-1vcpu-512mb-10gb-sgp1-01:/pfSense1-TCP4-10443-masashi# openvpn3 session-start --config pfSense1-TCP4-10443-masashi.ovpn Using pre-loaded configuration profile 'pfSense1-TCP4-10443-masashi.ovpn' openvpn3/session-start: ** ERROR ** Configration profile is missing required options root@debian-s-1vcpu-512mb-10gb-sgp1-01:/pfSense1-TCP4-10443-masashi# openvpn3-admin journal Sun Oct 6 07:36:37 2024 Logger INFO: OpenVPN3/Linux v23 (openvpn3-service-log) OpenVPN core v3.10.1 linux x86_64 64-bit Copyright (C) 2012-2022 OpenVPN Inc. All rights reserved. Sun Oct 6 07:36:37 2024 Logger INFO: Log method: journald Wed Jan 19 08:01:49 586524 OpenVPN3/Linux v23 (openvpn3-service-log) Wed Jan 19 08:01:49 586524 OpenVPN core v3.10.1 linux x86_64 64-bit Wed Jan 19 08:01:49 586524 Copyright (C) 2012-2022 OpenVPN Inc. All rights reserved. Wed Jan 19 08:01:49 586524 Log method: journald Sun Oct 6 07:36:37 2024 Logger INFO: Idle exit set to 10 minutes Sun Oct 6 07:36:37 2024 {tag:15145234087389068337} Config Manager INFO: Parsed configuration 'homelab, owner: root Sun Oct 6 07:36:37 2024 {tag:15145234087389068337} Config Manager INFO: Setting configuration override 'persist-tun' to 'true' by UID 0 Sun Oct 6 07:37:13 2024 {tag:15661404060832673948} Session Manager INFO: OpenVPN 3 Session Manager started Sun Oct 6 07:38:48 2024 {tag:15145234087389068337} Config Manager INFO: Granted access to root on /net/openvpn/v3/configuration/f058020ax67d6x41edxa50cxfe7e4018c7e6 Sun Oct 6 07:40:24 2024 {tag:15210768373469198935} Session Manager INFO: OpenVPN 3 Session Manager started Sun Oct 6 07:42:39 2024 {tag:15145234087389068337} Config Manager INFO: Parsed single-use configuration 'pfSense1-TCP4-10443-masashi.ovpn, owner: root Sun Oct 6 07:42:39 2024 {tag:15145234087389068337} Config Manager INFO: Setting configuration override 'persist-tun' to 'true' by UID 0 Sun Oct 6 07:44:32 2024 Logger VERB1: Setting changed: log-level = 6

Session managerが開始した形跡はあるが… listenもしてない。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# ss -ta State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 20 127.0.0.1:smtp 0.0.0.0:*
LISTEN 0 4096 127.0.0.54:domain 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:domain 0.0.0.0:*
LISTEN 0 4096 0.0.0.0:5355 0.0.0.0:*
LISTEN 0 128 0.0.0.0:ssh 0.0.0.0:*
ESTAB 0 52 xxx.xxx.231.148:ssh xxx.xxx.187.83:38748
LISTEN 0 4096 [::]:5355 [::]:*
LISTEN 0 20 [::1]:smtp [::]:*
LISTEN 0 128 [::]:ssh [::]:*

自宅のグローバルIPにはping届くと。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# ping xxx.xxx.187.83 PING xxx.xxx.187.83 (xxx.xxx.187.83) 56(84) bytes of data. 64 bytes from xxx.xxx.187.83: icmp_seq=1 ttl=45 time=93.7 ms 64 bytes from xxx.xxx.187.83: icmp_seq=2 ttl=45 time=97.9 ms ^C --- xxx.xxx.187.83 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 93.729/95.817/97.905/2.088 ms

他のステータス系も見てみたけど繋がっていませんね。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# openvpn3 sessions-list No sessions available

openvpnバージョンダウン

openvpn3でどうしてもセッションが張れない。どうしよう。
と思っていたらopenvpn2コマンドもインストールされていました。
Debian12なのですがopenvpn3のドキュメントにも12はなかったので12はまだ対応していないのかもしれません。
openvpn2で試してみます。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# openvpn2 --config pfSense1-TCP4-10443-masashi.ovpn

** ERROR ** pyOpenSSL library is not installed. Cannot parse PKCS#12 files.

パッケージが不足しているので追加でインストール。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# apt search pyOpenSSL Sorting... Done Full Text Search... Done python3-aioopenssl/stable 0.6.0-1 all Asyncio TLS Transport using PyOpenSSL

python3-ndg-httpsclient/stable 0.5.1-6 all enhanced HTTPS support for httplib and urllib2 using PyOpenSSL for Python3

python3-openssl/stable 23.0.0-1 all Python 3 wrapper around the OpenSSL library

python3-service-identity/stable 18.1.0-8 all Service identity verification for pyOpenSSL (Python 3 module)

pyOpenSSLと書いてある2つをインストールします。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:/pfSense1-TCP4-10443-masashi# apt install python3-service-identity root@debian-s-1vcpu-512mb-10gb-sgp1-01:/pfSense1-TCP4-10443-masashi# apt install python3-ndg-httpsclient

再接続。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# openvpn2 --config pfSense1-TCP4-10443-masashi.ovpn Credentials needed Auth User name: masashi Auth Password: Press CTRL-C to stop the connection

2024-10-06 08:17:49.585043 [STATUS] (StatusMajor.CONNECTION, StatusMinor.CFG_OK) config_path=/net/openvpn/v3/configuration/165ba2f5xe123x4f5axa2e2xef75ec5f4bb7 2024-10-06 08:17:49.585139 [LOG] Starting connection 2024-10-06 08:17:49.586488 [STATUS] (StatusMajor.CONNECTION, StatusMinor.CONN_CONNECTING) 2024-10-06 08:17:49.820450 [LOG] Connecting 2024-10-06 08:17:49.820829 [STATUS] (StatusMajor.CONNECTION, StatusMinor.CONN_CONNECTING) 2024-10-06 08:17:51.673568 [LOG] Connected: masashi@xxx.xxx.187.83:10443 (xxx.xxx.187.83) via /TCPv4 on tun/10.10.0.2/ gw=[10.10.0.1/] mtu=1500 2024-10-06 08:17:51.674058 [STATUS] (StatusMajor.CONNECTION, StatusMinor.CONN_CONNECTED)

OKです。接続できたようです!

openvpnをバックグラウンドで起動します。
ovpnファイルにユーザ名書いてあるのに聞かれるのなんでだろう?
一旦放置。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# openvpn2 --config pfSense1-TCP4-10443-masashi.ovpn --daemon Credentials needed Auth User name: masashi Auth Password: Connecting ... Connected VPN session is running in the background. Session path: /net/openvpn/v3/sessions/9018767asc7a1s4433sbb3as7a57fc4cdcaf

Use the 'openvpn3 session-manage' command line tool to manage this session. See 'openvpn3 session-manage --help' and 'openvpn3 sessions-list' for more details

To disconnect and stop this VPN session run:

$ openvpn3 session-manage --session-path /net/openvpn/v3/sessions/9018767asc7a1s4433sbb3as7a57fc4cdcaf --disconnect

sessions pathファイルも作成されているし大丈夫そう。
コネクションもEstablishになってます。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# ss -ant State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
(略) LISTEN 0 20 127.0.0.1:25 0.0.0.0:*
ESTAB 0 52 xxx.xxx.231.148:22 xxx.xxx.187.83:38748
TIME-WAIT 0 0 xxx.xxx.231.148:37502 xxx.xxx.187.83:10443
ESTAB 0 0 xxx.xxx.231.148:59116 xxx.xxx.187.83:10443
(略)

プライベートアドレスへの疎通OK。
バックエンドは無事構築完了です。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~/pfSense1-TCP4-10443-masashi# ping 192.168.0.36 PING 192.168.0.36 (192.168.0.36) 56(84) bytes of data. 64 bytes from 192.168.0.36: icmp_seq=1 ttl=63 time=95.3 ms 64 bytes from 192.168.0.36: icmp_seq=2 ttl=63 time=93.9 ms ^C --- 192.168.0.36 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 93.938/94.604/95.270/0.666 ms

フロントエンド側構築

フロント側を作ります。

HTTPS用の証明書を配置。 後でパーミッション変えないと。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~# ls -l /etc/ssl/for_internet_gateway.pem -rw-rw-r-- 1 root root 3095 Oct 6 08:35 /etc/ssl/for_internet_gateway.pem

Let's encryptあたりで発行したかったのですが、ドメイン取ってないしなるはやで作りたかったのでいったん自己証明書にしました。
ちょうどラボのpfSenseが自己CAを作成済みで、サーバ証明書だけあればいいのでこちらで発行しました。
また、haproxyに配置する際は証明書と鍵を一緒にしないといけない様なので両方を結合して入れてます。

haproxy.cfgをssl用に修正。
証明のパスを書くのと自宅guacamoleサーバへの転送設定を書きました。
ヘルスチェックは無しでいいです。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~# tail /etc/haproxy/haproxy.cfg errorfile 502 /etc/haproxy/errors/502.http errorfile 503 /etc/haproxy/errors/503.http errorfile 504 /etc/haproxy/errors/504.http

frontend http-in bind *:443 ssl crt /etc/ssl/for_internet_gateway.pem default_backend to_homelab

backend to_homelab server homelab 192.168.0.36:8090

サービス再起動。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~# systemctl restart haproxy

listenもOKです。

root@debian-s-1vcpu-512mb-10gb-sgp1-01:~# ss -lntp | grep :443 LISTEN 0 4096 0.0.0.0:443 0.0.0.0:* users:(("haproxy",pid=4935,fd=6)) ""))

グローバルIPアクセス

準備が整いましたのでDigital OceanのグローバルIPへアクセスしてみます。

OKです。証明書検証エラーは仕方ないとして無事に自宅ラボへ転送されているようです。
これでどこからでもブラウザ経由でアクセスできるようになりました。

guacamoleのsshubuntu desktopへ入って接続状態を見るとローカルIPで接続できてます。
ちゃんとguacamoleサーバ踏んでますね。
zshのプロンプトが文字化けしてますが、フォントの対応はまた今度。(guacamoleの時だけだししなくてもいいかも)

まとめ

前後編に分けてインターネット経由の自宅ラボアクセス環境を構築しました。
apache guacamole結構いい感じです。構築もコンテナで簡単でしたし、接続設定も難しくありません。
また、ブラウザだけでリモート操作できるのはありがたいし楽ですね。
プロキシは最小スペックですが、GUIはちょっと遅延するもののターミナルの操作なら問題なくできます。(回線の問題かもしれません)
記事には書いていませんが、一応スマホからもアクセス可能です。
ドメインとか不正アクセス対応とかもう少しやらないといけないことはあるのですが本件は土日の半分くらい使って構築できました。
一部完全でないものの、証明書とか新仮想マシン等ありもの環境を流用できたのが時間短縮になりよかったです。

クライアント端末になんのアプリも入れること無く、httpsアクセスで自宅環境のアクセスできる用になったので、どこでも怪しまれずに自宅サーバで遊べますw