达芙妮拒绝所有远程请求

Daphne rejects all remote requests

我有一个项目,在该项目中我使用通道并且在本地一切正常,但是当我部署在 Heroku 上时,每次我尝试连接时都会收到 403。起初我以为问题出在 Heroku 上,因为我在本地对其进行了测试,甚至在本地使用了 Heroku 中的数据库和 redis 实例,一切正常。

但是,当我使用 ngrok 打开到我的本地主机的 public 隧道时,我发现结果与 Heroku 中的结果相同。对于每个请求,我都会收到 403 并尝试调试它并没有多大帮助,因为事件循环有时会突然控制或者我会收到超时错误。除了一个在本地访问而另一个在远程访问之外,设置完全相同。 这就是我开始达芙妮的方式: daphne weout.asgi:application --port 8000 --bind 0.0.0.0 -v 3.

我的库版本:

将 Daphne 的 versbosity 设置为最大,这是我尝试连接时得到的结果:

Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,489 [asyncio] DEBUG: poll 101.195 ms took 0.023 ms: 1 events
Nov 22 07:16:34 weout-staging app/web.1 10.12.43.130:10299 - - [22/Nov/2019:15:16:33] "WSCONNECTING /api/v1/ws/" - -
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,513 [daphne.http_protocol] DEBUG: Upgraded connection ['10.x.x.x', 10299] to WebSocket
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,648 [asyncio] WARNING: Executing <Task pending coro=<AsyncConsumer.__call__() running at /app/.heroku/python/lib/python3.6/site-packages/channels/consumer.py:59> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fe01e6979d8>()] created at /app/.heroku/python/lib/python3.6/asyncio/base_events.py:276> created at /app/.heroku/python/lib/python3.6/site-packages/daphne/server.py:209> took 0.131 seconds
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,655 [daphne.server] INFO: failing WebSocket opening handshake ('Access denied')
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,656 [daphne.server] WARNING: dropping connection to peer tcp4:10.12.43.130:10299 with abort=False: Access denied
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,656 [daphne.ws_protocol] DEBUG: WebSocket ['10.x.x.x', 10299] rejected by application
Nov 22 07:16:34 weout-staging app/web.1 10.12.43.130:10299 - - [22/Nov/2019:15:16:33] "WSREJECT /api/v1/ws/" - -
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,660 [aioredis] DEBUG: Parsing Redis URI 'redis://xxxx@xxxxx'
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,660 [aioredis] DEBUG: Creating tcp connection to ('xxx.compute.amazonaws.com', 14059)
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,663 [asyncio] DEBUG: Get address info xxx, type=<SocketKind.SOCK_STREAM: 1>
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,667 [asyncio] DEBUG: Getting address info xxx.compute.amazonaws.com:14059, type=<SocketKind.SOCK_STREAM: 1> took 3.777 ms: [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('x.x.x.x', 14059))]
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,669 [daphne.ws_protocol] DEBUG: WebSocket closed for ['10.x.x.x', 10299]
Nov 22 07:16:34 weout-staging app/web.1 10.12.43.130:10299 - - [22/Nov/2019:15:16:33] "WSDISCONNECT /api/v1/ws/" - -
Nov 22 07:16:34 weout-staging app/web.1 2019-11-22 15:16:33,671 [asyncio] DEBUG: connect <socket.socket fd=16, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('0.0.0.0', 0)> to ('x.x.x.x', 14059)

我使用 Daphne 为我的普通 Django 视图和 websocket 消费者提供服务。 Django 视图一切正常,所以只有在连接到消费者时才会出现问题

有没有人在远程访问Dapnhe时遇到类似的问题? 起初我尝试用 uvicorn 搭配 gunicorn 但他们有 bug when the consumer is closed during the initial connection phase 所以我换回达芙妮

原来是AllowedHostsOriginValidator!那肯定浪费了我很多时间。顺便说一下,如果没有 Origin header,你知道中间件如何处理来自移动应用程序或其他来源的请求吗?

无论如何我暂时删除它并且当前问题已解决