probot / smee-client

🔴 Receives payloads then sends them to your local server
https://smee.io
ISC License
435 stars 137 forks source link

Proxy support for smee client #194

Closed ojoggerst closed 2 weeks ago

ojoggerst commented 2 years ago

Problem description

We would like to use the smee client behind a proxy. Currently, there is no possibility to add a proxy URL to the smee client, so that all outgoing connections would be proxied through a specific server.

Solution

  1. Use superagent proxy (https://www.npmjs.com/package/superagent-proxy)
  2. Use global agent (https://www.npmjs.com/package/global-agent)

Option one needs code changes and seems a bit more cumbersome.

Option two is fairly simple and only needs global agent dependencies and one line in index.js to import the package.

After that you can do: GLOBAL_AGENT_HTTP_PROXY=http://<proxy-host>:<proxy-port> node ./bin/smee.js -u https://<smee-server>/<token>

Please check PR: https://github.com/probot/smee-client/pull/195

jakobkolb commented 1 year ago

I need this feature to be able to use smee in our environment. Would love to see it become reality ❤️

wolfy1339 commented 3 months ago

You can pass in your own custom fetch wrapper. NodeJS doesn't support Proxies out of the box with the Fetch API currently

import { fetch as undiciFetch, ProxyAgent } from 'undici';
import SmeeClient from "smee-client";

const myFetch = (url, options) => {
  return undiciFetch(url, {
    ...options,
    dispatcher: new ProxyAgent(<your_proxy_url>)
  })
}
const smee = new SmeeClient({
  source: 'https://smee.io/abc123',
  target: 'http://localhost:3000/events',
  logger: console,
  fetch: myFetch
})

This should be added to the README

ftrahman commented 2 months ago

where are we meant to pass the custom fetch wrapper to? smee.js?

i tried the previous solution and ended up with a sea of errors.

wolfy1339 commented 2 months ago

where are we meant to pass the custom fetch wrapper to? smee.js?

Yes. That is what is indicated in the code example I shared

i tried the previous solution and ended up with a sea of errors.

Those packages are probably not compatible with the native Fetch API available in Node and instead use the Node HTTP API

Uzlopak commented 2 months ago

Actually the examply is not quite the recommended way by undici.

You would use setGlobalDispatcher to set the proxy agent globally. Also there is EnvProxyAgent, which can handle the HTTP_PROXY and HTTPS_PROXY env variables. I have to look up if it is enabled by default now on node.js, but that was the desired behavior.

wolfy1339 commented 2 months ago

Actually the examply is not quite the recommended way by undici.

You would use setGlobalDispatcher to set the proxy agent globally. Also there is EnvProxyAgent, which can handle the HTTP_PROXY and HTTPS_PROXY env variables. I have to look up if it is enabled by default now on node.js, but that was the desired behavior.

Undici probably updated since then, but my solution is backwards compatible to all NodeJS versions we support.

At the time of Node 18.0, there was no support for the HTTP_PROXY and HTTPS_PROXY env variables, nor was there an EnvProxyAgent

Besides, do we really want to include undici (which is a fairly large package) just to be able to have the proxy env variables?

ftrahman commented 2 months ago

even having tried your solution @wolfy1339, i am still receiving connection timeouts. this has been hindering me for a few days now

wolfy1339 commented 2 months ago

What version are you running?

ftrahman commented 2 months ago

node is 18.20.2, smee is 2.0.2

wolfy1339 commented 2 months ago

There is also another place you need to specify the proxy: https://www.npmjs.com/package/eventsource#httphttps-proxy

npm
eventsource
W3C compliant EventSource client for Node.js and browser (polyfill). Latest version: 2.0.2, last published: 2 years ago. Start using eventsource in your project by running `npm i eventsource`. There are 805 other projects in the npm registry using eventsource.
github-actions[bot] commented 2 months ago

:tada: This issue has been resolved in version 3.0.0-beta.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 2 weeks ago

:tada: This issue has been resolved in version 2.0.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket: