Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy Support #43

Closed
throwaway29345 opened this issue Apr 20, 2021 · 8 comments
Closed

Proxy Support #43

throwaway29345 opened this issue Apr 20, 2021 · 8 comments
Labels
Status: Needs info Full requirements are not yet known, so implementation should not be started Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects

Comments

@throwaway29345
Copy link

throwaway29345 commented Apr 20, 2021

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 throwaway29345 added the Type: Feature New feature or request label Apr 20, 2021
@ghost ghost added this to Features in JS Apr 20, 2021
@gr2m gr2m added Type: Support Any questions, information, or general needs around the SDK or GitHub APIs and removed Type: Feature New feature or request labels Apr 22, 2021
@ghost ghost moved this from Features to Support in JS Apr 22, 2021
@gr2m
Copy link
Contributor

gr2m commented Apr 22, 2021

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 gr2m added the Status: Needs info Full requirements are not yet known, so implementation should not be started label Apr 22, 2021
@ghost ghost moved this from Support to Awaiting response in JS Apr 22, 2021
@gr2m
Copy link
Contributor

gr2m commented May 28, 2021

closing due to inactivity

@gr2m gr2m closed this as completed May 28, 2021
JS automation moved this from Awaiting response to Done May 28, 2021
@etnperlong
Copy link

etnperlong commented Aug 31, 2022

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 },
});

@timrogers
Copy link

@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 timrogers reopened this Sep 2, 2022
@rajbos
Copy link

rajbos commented Aug 4, 2023

@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,
    },
});

@wolfy1339
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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 },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs info Full requirements are not yet known, so implementation should not be started Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
No open projects
JS
  
Done
Development

No branches or pull requests

6 participants