h2non/toxy

Question about abort poisons

tonygaetani opened this issue · 6 comments

I'd like to implement a poison on a resource that will disrupt the connection after some time. It looks like the abort poison is what I want, but I was not getting the functionality I wanted with the following configuration:

const toxy = require('..')
const poisons = toxy.poisons

const proxy = toxy()
const serverhost = 'mysite.de'
const timeout = 1000

proxy
  .all('/*')
  .forward('http://' + serverhost)

proxy
  .all('/myresource/*')
  .poison(toxy.poisons.abort(1000))
  .withRule(rules.method('GET'))
  .outgoingPoison(toxy.poisons.abort(1000))
  .withRule(rules.method('GET'))
  .forward('http://' + serverhost)

const port = 4567
proxy.listen(port)
console.log('Server listening on port:', port)

My landscape is:

[client] => [nginx] => [toxy] => [webapp]

Is this the right approach, or am I misunderstanding something?

h2non commented

Please, see abort.js example

h2non commented

@tonygaetani I believe that this example is what you're looking for. Essentially, in this example, toxy will abort the connection if the target server takes more than 2 seconds to respond.
Note that you can also use the probability rule to enable/disable the abort poison randomly. In some scenarios like this it's useful.

const toxy = require('..')
const poisons = toxy.poisons
const rules = toxy.rules

const proxy = toxy()
const serverhost = 'httpbin.org'
const timeout = 1500

proxy
  .forward('http://' + serverhost)

proxy
  .all('/delay/*')
  // In this case, the socket will be closed if
  // the target server doesn't replies with
  // a response after 2 seconds
  .poison(toxy.poisons.abort({ delay: timeout, next: true }))
  .withRule(rules.method('GET'))

const port = 4567
proxy.listen(port)
console.log('Server listening on port:', port)
console.log('Success: http://localhost:' + port + '/delay/1')
console.log('Aborted: http://localhost:' + port + '/delay/3')

I see, I misunderstood the use case for the abort poison. What I'm looking for is something different. I want the proxy to refuse to continue routing traffic, or make the server's response appear to be something invalid, after a certain period of time.

Imagine you have a single-page application that you are interacting with in your browser. Your browser is talking to toxy which is proxying the traffic to and from the SPA webserver. After a period of time, toxy stops routing the traffic from your browser to the webapp so the connection appears broken. Is this something I can achieve with toxy?

h2non commented

The scenarios you're looking can be perfectly covered with toxy in multiple ways. Let's separate things:

For the first scenario, if you want to refuse certain traffic or even all the traffic received by the proxy, you can simply plug in the abort poison as you've seen in the examples whenever you want, and also you can disable it later. You can do that programmatically or via HTTP API, so for instance you can talk with toxy directly from your browser app via XHR in order to configure the proxy dynamically enabling/disabling poisons/rules.

For the second one, which is what we tried to cover initially, you can use abort poison with the following options: { delay: 1000, next: true }. That means toxy will close the connection if the target server takes more than 1 second to respond. I guess that's what you described before.

Note that you can use multiple rules, and write your own rules to limit the execution scope of the poisons. For instance, you can write a custom rule which enables a poison only for a certain amount of time (e.g: 1 min), and then disables the poisoning.

h2non commented

BTW, if want to use the HTTP API directly from a web browser, toxy should ideally implement CORS, but unfortunately it isn't supported yet. Just let me know and I can provide support for it.

h2non commented

Closing due to inactivity.

btw toxy now supports CORS in the admin HTTP API. See docs.