简体   繁体   English

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

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

I'm using fastify and fastify-http-proxy on a VPS (Ubuntu 19.x) that has three IPv4 addresses.我在具有三个 IPv4 地址的 VPS(Ubuntu 19.x)上使用fastifyfastify-http-proxy I can verify their functionality with (example IPs):我可以使用(示例 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

I see here that you can supply an interface by supplying a localAddress to Node's http requests.在这里看到您可以通过向 Node 的http请求提供localAddress来提供接口。 Has anyone done this with fastify or fastify-http-proxy ?有没有人用fastifyfastify-http-proxy做到这一点? I've searched both and can't find support in each package for it.我已经搜索了两者,但在每个 package 中都找不到支持。

I run an API which forwards traffic to a host.我运行 API 将流量转发到主机。 Recently, it received a lot of (legitimate) traffic and the host's (Sony) DDoS prevention flagged and blocked it due to high traffic from a single address.最近,它收到了大量(合法)流量,并且由于来自单个地址的高流量,主机(索尼)的 DDoS 防护标记并阻止了它。 When contacted, they claimed to not be able to remove the block, but that I was free to change my VPS' IP.联系时,他们声称无法删除该块,但我可以自由更改我的 VPS 的 IP。 To prevent this in the future, I'd like to almost randomize which interface it uses.为了防止将来发生这种情况,我想几乎随机化它使用的接口。

Thanks!谢谢!

You can do it by setting:您可以通过设置来做到这一点:

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

In this way, all your requests will have that IP.这样,您的所有请求都将具有该 IP。 All the request options are supported using this settings , since fastify-reply-from is used under the hood.使用此设置支持所有请求选项,因为fastify-reply-from是在后台使用的。

But you have 3 ips to use, so you need to register three times the plugin per different routes, or build a "round getter" like this should work as well:但是你有 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