1

我想使用处理 example.com 的任何子域的 express 创建一个本地 https 服务器。理想情况下,我想像这样修改我的主机文件: 127.0.0.2 localhost *.example.com 但这是不允许的。
所以我想创建自己的 dns 服务器来将 *.example.com 的 IP 解析为 127.0.0.2。我查看了https://github.com/tjfontaine/node-dnshttps://github.com/iriscouch/dnsd但我不明白如何将它们应用于我的场景。

4

3 回答 3

2

xip.io可能不适合你,但我发现它非常有用。“xip.io 是一个神奇的域名,可为任何 IP 地址提供通配符 DNS。” “xip.io 在公共 Internet 上运行自定义 DNS 服务器。当您的计算机查找 xip.io 域时,xip.io DNS 服务器会从域中提取 IP 地址并将其发送回响应中。”

因此,以下所有域都将解析为 127.0.0.1:

127.0.0.1.xip.io
www.127.0.0.1.xip.io
example.com.127.0.0.1.xip.io
something.example.com.127.0.0.1.xip.io

守护进程恰好是用 Coffeescript 编写的,以便在 node.js 上运行。)

于 2013-09-25T01:17:01.733 回答
1

有些事情让我绊倒,我已经解决了。

  1. 我没有在我机器的网络设置中设置 DNS 服务器 IP 地址。
  2. 我在启动 DNS 服务器和启动 https 服务器之间遇到了时间问题。
  3. 我没有在 DNS 响应中包含端口号

这是我的解决方案:

var fs = require('fs');
var path = require('path');
var dns = require('native-dns');
var https = require('https');
var express = require('express');

String.prototype.endsWith = function(s) {
  return this.length >= s.length && this.substr(this.length - s.length) == s;
};

var startDns = function(example_port, callback) {
  var server = dns.createServer();

  server.on('request', function(request, response) {
    var found = false;

    for (var q = 0; q < request.question.length; q++)
    {
      var name = request.question[q].name;
      if (name.endsWith("example.com"))
      {
        response.answer.push(dns.A({
          name:name,
          address:'127.0.0.2',
          port:example_port,
          ttl:600
        }));
        found = true;
      }
    }
    if (found)
    {
      response.send();
    }
  });

  server.on('error', function(err, buff, req, res) {
    console.log(JSON.stringify(err));
  });

  server.on('listening', function() {
    console.log("DNS server started on port 53");
    if (callback)
    {
      callback();
    }
  });

   server.serve(53);
   return server;
};

var startHttps = function(serverPort) {
  // Set up secure server
  var options = {
    key:fs.readFileSync(path.join(__dirname, 'certificates/example.com-key.pem')),
    cert:fs.readFileSync(path.join(__dirname, 'certificates/example.com-cert.pem'))
  };

  var app = express();
  var server = https.createServer(options, app);
  app.get('*', function(req, res, next) {
    res.send('Hello from ' + req.headers.host);
  });
  server.listen(serverPort, 'example.com');
  console.log('https server started on port ' + serverPort);
  return server;
};

var server_port = parseInt(process.argv[2] || 8082);
var httpsServer;

var dnsServer = startDns(server_port, function() {
  httpsServer = startHttps(server_port)
});

process.on('SIGINT', function() {
  console.log("shutting down");
  if (httpsServer)
  {
    httpsServer.close();
  }
  if (dnsServer)
  {
    dnsServer.close();
  }
});

注意:这适用于我的 Windows 机器。我还在其他平台上测试。我不确定我是否正确处理了我的 dns 服务器中无法识别的域。

于 2013-09-26T21:28:23.960 回答
1

您将需要运行本地 DNS 服务器来拦截请求。

我发现dnsproxy.py工作得很好。它是用 python 编写的,需要在您打算使用它时运行。

您将需要编辑您的hosts文件并添加如下一行:

127.0.0.1    *.example.com

之后,您将需要启动 DNS 代理服务器:

$ sudo python dnsproxy.py -s 8.8.8.8

8.8.8.8是 Google 的 DNS 服务器的 IP 地址,如果在 hosts 文件中找不到记录,将用作备用。

完成此操作后,您应该能够在端口上启动快速服务器80并处理对*.example.com.

于 2013-09-24T23:53:49.077 回答