rest.js icon indicating copy to clipboard operation
rest.js copied to clipboard

Proxy Support

Open throwaway29345 opened this issue 3 years ago • 4 comments

What’s missing?

Proxy support. I googled "octokit proxy", and found 2-3 github issues describing adding proxy support to Octokit, but all of them led to 404 links (example 1 & 2).

Why?

In general, any HTTP client should support proxies.

Alternatives you tried

Here's what I tried with a Socks5 proxy:

	import { SocksProxyAgent } from 'socks-proxy-agent';

	const agent = new SocksProxyAgent({/* credentials */});
	const requestWithProxy = request.defaults({ request: { agent } });

	const o = new Octokit({
		request: requestWithProxy,
	});

But this doesn't work, the requests are still sent from my local machine's IP address.

throwaway29345 avatar Apr 20 '21 16:04 throwaway29345

Also the request constructor option is an object, not a request instance. Can you try this?

	import { SocksProxyAgent } from 'socks-proxy-agent';

	const agent = new SocksProxyAgent({/* credentials */});

	const o = new Octokit({
		request: { agent },
	});

Octokit definitely works with proxies, see our test at https://github.com/octokit/core.js/blob/master/test/agent-proxy/agent-proxy-test.test.ts

gr2m avatar Apr 22 '21 21:04 gr2m

closing due to inactivity

gr2m avatar May 28 '21 19:05 gr2m

The same issue. Not working at all. tested with socks-proxy-agent and http-proxy-agent

But I found a solution, it seems working in nodejs environment

import { SocksProxyAgent } from 'socks-proxy-agent';
import fetch from 'node-fetch';  // import node-fetch

const agent = new SocksProxyAgent({/* credentials */});

const o = new Octokit({
	request: { agent, fetch },
});

etnperlong avatar Aug 31 '22 14:08 etnperlong

@etnperlong Sorry to hear that you're having issues with proxies in Octokit! Have you tried this without customising fetch? I wonder if that could be part of the issue.

timrogers avatar Sep 02 '22 12:09 timrogers

@timrogers, we have been needing to use the workaround below to be able to use the proxy settings, but with the breaking changes in 8.0.0 this now fails as well, preventing an upgrade to 8.0.0.

const { request } = require("@octokit/request");
const { Octokit } = require("@octokit/rest");
const { HttpsProxyAgent } = require('https-proxy-agent');
// configure the proxy if needed
if (process.env.HTTPS_PROXY) {
    agent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
} else if (process.env.https_proxy) {
    agent = new HttpsProxyAgent(process.env.https_proxy);
}

// get a new client
const githubClient = new Octokit({
    baseUrl: "https://api.github.com",
    auth: process.env.GITHUB_TOKEN,
    request: {
      agent: agent,
      timeout: 5000,
    },
});

rajbos avatar Aug 04 '23 10:08 rajbos

Yes, in v8 of @octokit/request we switched to the Fetch API instead of node-fetch.

Please see our test for how to implement proxy usage https://github.com/octokit/core.js/blob/73ce7fba8ec4184025c3359744935bed0b020bdf/test/agent-proxy/agent-proxy-test.test.ts#L18

You have to use the ProxyAgent from undici

All usages of NodeJS based http(s).Agent will not work as they are incompatible with the fetch API.

Proxies definitely do work with Octokit, it is a functionality that we account for in our tests

wolfy1339 avatar Aug 04 '23 14:08 wolfy1339

Closing as this is not an issue. Proxies are supported, but due to the removal of node-fetch require a little bit more code on the client side. See my comment above on how to do so with undici

wolfy1339 avatar Nov 25 '23 00:11 wolfy1339

For reference, here is how this is done with native fetch in NodeJS:

import { ProxyAgent, fetch as undiciFetch } from "undici";
import { Octokit } from "@octokit/rest";

const myFetch = (url, opts) => {
  return undiciFetch(url, {
    ...opts,
    dispatcher: new ProxyAgent({
      uri: process.env.HTTPS_PROXY,
      keepAliveTimeout: 10,
      keepAliveMaxTimeout: 10,
    }),
  });
};

const octokit = new Octokit({
  baseUrl: serverUrl,
  request: { fetch: myFetch },
});

wolfy1339 avatar Nov 25 '23 00:11 wolfy1339