繁体   English   中英

使用 Fastify 和 Fastify-HTTP-Proxy 绑定接口

[英]Binding Interfaces With Fastify and Fastify-HTTP-Proxy

我在具有三个 IPv4 地址的 VPS(Ubuntu 19.x)上使用fastifyfastify-http-proxy 我可以使用(示例 IP)验证它们的功能:

curl --interface 45.63.23.63 api.ipify.org
curl --interface 45.63.72.48 api.ipify.org
curl --interface 45.63.40.39 api.ipify.org

在这里看到您可以通过向 Node 的http请求提供localAddress来提供接口。 有没有人用fastifyfastify-http-proxy做到这一点? 我已经搜索了两者,但在每个 package 中都找不到支持。

我运行 API 将流量转发到主机。 最近,它收到了大量(合法)流量,并且由于来自单个地址的高流量,主机(索尼)的 DDoS 防护标记并阻止了它。 联系时,他们声称无法删除该块,但我可以自由更改我的 VPS 的 IP。 为了防止将来发生这种情况,我想几乎随机化它使用的接口。

谢谢!

您可以通过设置来做到这一点:

server.register(require('fastify-http-proxy'), {
  upstream: 'http://my-api.example.com',
  prefix: '/api',
  http: {
    requestOptions: {
      localAddress: '45.63.72.48'
    }
  }
})

这样,您的所有请求都将具有该 IP。 使用此设置支持所有请求选项,因为fastify-reply-from是在后台使用的。

但是你有 3 个 ips 可以使用,所以你需要为每个不同的路由注册 3 次插件,或者像这样构建一个“round getter”应该也可以:

const ips = [
  '45.63.23.63',
  '45.63.72.48',
  '45.63.40.39'
]
let endlessIterator = ips[Symbol.iterator]()

const roundIps = {
  get localAddress () {
    let v = endlessIterator.next().value
    if (!v) {
      endlessIterator = ips[Symbol.iterator]()
      v = endlessIterator.next().value
    }
    return v
  }
}

server.register(require('fastify-http-proxy'), {
  upstream: 'http://my-api.example.com',
  prefix: '/api',
  http: {
    requestOptions: roundIps
  }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM