在 Discourse 中使用 Protonmail Bridge

问题:a) Protonmail Bridge 无法监听除 127.0.0.1 以外的 IP,b) Docker 容器中的 Discourse 无法向主机机器的 127.0.0.1 发送 SMTP 请求。

解决方案:我们将其配置为发送到 172.17.0.1(主机上的 Docker 的桥接 IP),然后重定向到 Protonmail Bridge 正在监听的 127.0.0.1

我们使用 Socat 来实现这一点。

操作方法

首先安装 Socat:
apt install socat

我们将把 Socat 作为服务运行。创建服务文件:
nano /etc/systemd/system/protonsocat.service

…并在其中放入以下内容:

[Unit]
Description=Socat Bridge ProtonMail/Discourse
After=protonbridge.service,docker.service
ReloadPropagatedFrom=docker.service

[Service]
ExecStart=socat -d -d -lm TCP4-LISTEN:1026,fork,reuseaddr,so-bindtodevice=docker0,range=172.17.0.0/24 TCP4:127.0.0.1:1025
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target

备注:

  • 我们将 Socat 配置为监听所有 IP 172.17.0.0/24,端口 1026,并将流量重定向到 Protonmail Bridge 正在监听的 127.0.0.1:1025

  • Socat 文档示例:socat

  • -d -d 将打印致命、错误、警告和通知消息;您可能不希望记录这么多详细信息。

接下来,启动服务:
systemctl start protonsocat

检查它是否正在运行:
systemctl status protonsocat

并自动使其在启动时启动:
systemctl enable protonsocat

完成!现在,配置 Discourse app.yml 并重建:

  DISCOURSE_SMTP_ADDRESS: 172.17.0.1
  DISCOURSE_SMTP_PORT: 1026
  DISCOURSE_SMTP_USER_NAME: <Proton Bridge 命令行应用程序提供的 SMTP 用户名>
  DISCOURSE_SMTP_PASSWORD: "<SMTP 密码 - 加上引号>"
  DISCOURSE_SMTP_ENABLE_START_TLS: true
  DISCOURSE_SMTP_DOMAIN: <域名>
  DISCOURSE_NOTIFICATION_EMAIL: <在此处填写 Discourse 将发送事务性电子邮件的地址>
2 个赞

你好 @meglio
我找不到 protonbridge.service,所以我将其替换为 protonmail.service
但格式似乎有问题 After=protonbridge.service,docker.service

你好 @meglio 这似乎给了我一个错误

在初始 sysctl 命令之后

root@ip-172-31-0-166:/var/discourse# systemctl status protonsocat.service
● protonsocat.service - Socat Bridge ProtonMail/Discourse
     Loaded: loaded (/etc/systemd/system/protonsocat.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-12-18 20:58:18 UTC; 25s ago
   Main PID: 2020962 (socat)
      Tasks: 1 (limit: 1143)
     Memory: 2.0M
        CPU: 4ms
     CGroup: /system.slice/protonsocat.service
             └─2020962 socat -d -d -lm TCP4-LISTEN:1026,fork,reuseaddr,so-bindtodevice=docker0,range=172.17.0.0/24 TCP4:127.0.0.1:1025

Dec 18 20:58:18 ip-172-31-0-166 systemd[1]: Started Socat Bridge ProtonMail/Discourse.
Dec 18 20:58:18 ip-172-31-0-166 socat[2020962]: 2022/12/18 20:58:18 socat[2020962] W ioctl(5, IOCTL_VM_SOCKETS_GET_LOCAL_CID, ...): Inappropriate ioctl for device
Dec 18 20:58:18 ip-172-31-0-166 socat[2020962]: N listening on AF=2 0.0.0.0:1026
**Dec 18 20:58:42 ip-172-31-0-166 systemd[1]: /etc/systemd/system/protonsocat.service:3: Failed to add dependency on protonbridge.service,docker.service, ignoring: Invalid argument**

另外,在运行 ./discourse-doctor 时,我也收到错误


==================== MAIL TEST ====================
For a robust test, get an address from http://www.mail-tester.com/
Or just send a test message to yourself.
Email address for mail test? ('n' to skip) [[REDACTED]@proton.me]:
Sending mail to [REDACTED]@proton.me. . .
SAM: sudo docker exec -w /var/www/discourse -i app rake emails:test[[REDACTED]@proton.me]
Testing sending to [REDACTED]@proton.me using 172.17.0.1:1026, username:[REDACTED]@proton.me with plain auth.
======================================== ERROR ========================================
                                    UNEXPECTED ERROR

**end of file reached**

====================================== SOLUTION =======================================
This is not a common error. No recommended solution exists!

Please report the exact error message above to https://meta.discourse.org/
(And a solution, if you find one!)

任何提示都将不胜感激,并将有所帮助!
谢谢。

你们自从那以后解决那些问题了吗?

我实际上有一个更严重的问题,我的 socat 运行正常,但我无法 telnet 172.17.0.1 1026172.17.0.2 1026。在 Discourse 的 Docker 容器内,我也尝试了 127.0.0.1 10261025,但都失败了。

在主机上,我使用 127.0.0.1 1025 可以正常 telnet 连接,Discourse 更改了什么吗?

root@raspberry:/var/discourse# sudo systemctl status protonsocat.service 
● protonsocat.service - Socat Bridge ProtonMail/Discourse
     Loaded: loaded (/etc/systemd/system/protonsocat.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-02-25 20:37:34 CET; 4min 37s ago
   Main PID: 1530542 (socat)
      Tasks: 1 (limit: 9236)
     Memory: 816.0K
        CPU: 10ms
     CGroup: /system.slice/protonsocat.service
             └─1530542 socat -d -d -lm TCP4-LISTEN:1026,fork,reuseaddr,so-bindtodevice=docker0,range=172.17.0.0/24 TCP4:127.0.0.1:1025

Feb 25 20:37:34 raspberry.local systemd[1]: Started Socat Bridge ProtonMail/Discourse.
Feb 25 20:37:34 raspberry.local socat[1530542]: 2023/02/25 20:37:34 socat[1530542] W ioctl(5, IOCTL_VM_SOCKETS_GET_LOCAL_CID, ...): Inappropriate ioctl for device
Feb 25 20:37:34 raspberry.local socat[1530542]: N listening on AF=2 0.0.0.0:1026

主机:

root@raspberry:/var/discourse# telnet 127.0.0.1 1025
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 127.0.0.1 ESMTP Service Ready
root@raspberry:/var/discourse# nmap 127.0.0.1 1025
Starting Nmap 7.80 ( https://nmap.org ) at 2023-02-25 21:03 CET
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000045s latency).
Not shown: 993 closed ports
PORT     STATE SERVICE
80/tcp   open  http
443/tcp  open  https
1025/tcp open  NFS-or-IIS
3306/tcp open  mysql
5432/tcp open  postgresql
8080/tcp open  http-proxy
8088/tcp open  radan-http

Nmap done: 2 IP addresses (1 host up) scanned in 3.36 seconds

主机上的接口:

root@raspberry:/var/discourse# ifconfig
br-eb8fd6a0d930: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::42:bdff:fe5c:8dbf  prefixlen 64  scopeid 0x20<link>
        ether 02:42:bd:5c:8d:bf  txqueuelen 0  (Ethernet)
        RX packets 1336  bytes 94913 (94.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2254  bytes 3098019 (3.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:60ff:feaf:784c  prefixlen 64  scopeid 0x20<link>
        ether 02:42:60:af:78:4c  txqueuelen 0  (Ethernet)
        RX packets 295920  bytes 21547471 (21.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1049936  bytes 1487623390 (1.4 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether dc:a6:32:c2:70:f8  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 97179  bytes 22606251 (22.6 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 97179  bytes 22606251 (22.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth5a26573: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::428:bfff:febf:cf5c  prefixlen 64  scopeid 0x20<link>
        ether 06:28:bf:bf:cf:5c  txqueuelen 0  (Ethernet)
        RX packets 1336  bytes 113617 (113.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2279  bytes 3099825 (3.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethe0f8588: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::a04d:adff:feb1:351a  prefixlen 64  scopeid 0x20<link>
        ether a2:4d:ad:b1:35:1a  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2488 (2.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethfe706a1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::74b3:3aff:feea:89ec  prefixlen 64  scopeid 0x20<link>
        ether 76:b3:3a:ea:89:ec  txqueuelen 0  (Ethernet)
        RX packets 4459  bytes 287499 (287.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5221  bytes 7330834 (7.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.4  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::dea6:32ff:fec2:70f9  prefixlen 64  scopeid 0x20<link>
        ether dc:a6:32:c2:70:f9  txqueuelen 1000  (Ethernet)
        RX packets 2789702  bytes 3447696344 (3.4 GB)
        RX errors 0  dropped 7  overruns 0  frame 0
        TX packets 1082661  bytes 213831559 (213.8 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Docker

root@raspberry-app:/var/www/discourse# telnet 172.17.0.1 1026
Trying 172.17.0.1...
telnet: Unable to connect to remote host: Connection refused