【问题标题】:Why is my webhook code not accessible on HTTPS为什么我的 webhook 代码无法通过 HTTPS 访问
【发布时间】:2020-04-05 03:19:19
【问题描述】:

我在 AWS 上运行 RHEL 8 的机器上运行基于 apache2 的 SSL 服务器。我正在尝试在此服务器上部署一个 facebook webhook。我正在使用 curl 请求手动测试它。当我通过 HTTP 输入请求时,它的行为与预期一致。但是,当通过 HTTPS 发出请求时,我收到以下错误消息:

curl: (35) error:1408F10B:SSL routines:ssl3_get_record:wrong version number 我需要让它在 HTTPS 上工作,因为 facebook 不允许仅使用 HTTP 连接。

任何建议都会很棒,谢谢 - 如果我问得不好,我很抱歉,这是我的第一个问题。

webhook 的代码如下:


// Imports dependencies and set up http server
const
  express = require('express'),
  bodyParser = require('body-parser'),
  app = express().use(bodyParser.json()); // creates express http server

// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));

// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {

  let body = req.body;

  // Checks this is an event from a page subscription
  if (body.object === 'page') {

    // Iterates over each entry - there may be multiple if batched
    body.entry.forEach(function(entry) {

      // Gets the message. entry.messaging is an array, but
      // will only ever contain one message, so we get index 0
      let webhook_event = entry.messaging[0];
      console.log(webhook_event);
    });

    // Returns a '200 OK' response to all requests
    res.status(200).send('EVENT_RECEIVED');
  } else {
    // Returns a '404 Not Found' if event is not from a page subscription
    res.sendStatus(404);
  }

});

// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {

  // Your verify token. Should be a random string.
  let VERIFY_TOKEN = "duckgoesquack"

  // Parse the query params
  let mode = req.query['hub.mode'];
  let token = req.query['hub.verify_token'];
  let challenge = req.query['hub.challenge'];

  // Checks if a token and mode is in the query string of the request
  if (mode && token) {

    // Checks the mode and token sent is correct
    if (mode === 'subscribe' && token === VERIFY_TOKEN) {

      // Responds with the challenge token from the request
      console.log('WEBHOOK_VERIFIED');
      res.status(200).send(challenge);

    } else {
      // Responds with '403 Forbidden' if verify tokens do not match
      res.sendStatus(403);
    }
  }
});

我已尝试更新我的 apache conf 文件 - virtualhosts 部分如下:

NameVirtualHost *:80

<VirtualHost *:443>
ServerName lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/privkey.pem
</VirtualHost>

<VirtualHost *:80>
ServerName lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
</VirtualHost>

<VirtualHost *:443>
ServerName www.lloydarnoldtestapps.tk
ServerAlias *.lloydarnoldtestaps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
SSLCertificateFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

<VirtualHost *:1337>
ServerName lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
SSLCertificateFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>


<VirtualHost *:80>
ServerName www.lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
</VirtualHost>

<VirtualHost *:1337>
ServerName www.lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
SSLCertificateFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

【问题讨论】:

  • 你可以尝试运行openssl s_client -connect www.lloydarnoldtestapps.tk -port 443-prexit-debug
  • 嗨@Yan,这样做得到了这个响应:``` 139642581512640:error:0200206E:system library:connect:Connection timed out:../crypto/bio/b_sock2.c:110: 139642581512640 :error:2008A067:BIO 例程:BIO_connect:connect 错误:../crypto/bio/b_sock2.c:111:connect:errno=110 --- 没有可用的对等证书 --- 没有发送客户端证书 CA 名称 --- SSL 握手已读取 0 字节并写入 0 字节 验证:OK --- 新,(NONE),密码为 (NONE) 不支持安全重新协商 压缩:NONE 扩展:NONE 未协商 ALPN 未发送早期数据 验证返回码: 0(确定)```
  • 我的浏览器中显示了一个挂锁,并且证书在那里被标记为有效。我想知道我是否没有正确设置我的配置文件?如果您有任何很棒的想法 - 谢谢! :) @Yan
  • -servername www.lloydarnoldtestapps.tk 添加到命令中.. 似乎errno=110 the server rejects the connection. If this happens, you receive a message such as connect: Connection timed out or connect:errno=110.
  • 运行openssl s_client -connect lloydarnoldtestapps.tk -port 1337 -server lloydarnoldtestapps.tk -debug时会发生什么

标签: node.js apache express ssl webhooks


【解决方案1】:

我想我明白发生了什么。
curl -vv -X POST https://www.lloydarnoldtestapps.tk 响应来自 apache Server: Apache/2.4.37

运行时curl -vv -X POST http://www.lloydarnoldtestapps.tk:1337/webhook
回复来自快递X-Powered-By: Express

我认为,当您首先开始 express 并绑定到 port 1337 时,当您运行 apache 时,它​​实际上无法绑定,所以这就是您在向 port 1337 发送请求时收到 200 的原因

您不能从 apache 返回 index.js。您将使用 apache 作为反向代理和来自 AWS (HTTPS) -> Apache (HTTP) -> Express 的代理请求。这样apache就会终止https并通过http发出请求来表达。

查看 Proxy 和 ProxyPass for apache 以及关于反向代理到 nodejs 服务器的教程。

查看这篇帖子ProxyPass apache https to a node server

特别是这些指令

  ProxyPass / https://example.com:4433/
  ProxyPassReverse / https://example.com:4433 /

如果您有任何问题,请回复。

【讨论】:

  • 非常感谢 - 我明天会尝试第一件事并告诉你。谢谢你的帮助!
  • 我研究了使用反向代理,并找到了解决方案。非常感谢您的帮助 - 您为我节省了大量时间并提高了我对 Web 应用程序的理解!
  • @LloydArnold 太好了!编码愉快!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 2011-12-23
  • 2016-03-16
  • 1970-01-01
  • 2018-09-23
  • 2014-04-29
相关资源
最近更新 更多