sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.02k stars 917 forks source link

HTTP2 with proxy doesn't work? (http2-wrapper) #2348

Open wildwind3325 opened 2 months ago

wildwind3325 commented 2 months ago

` var http2 = require('http2-wrapper');

(async () => { let { got } = await import('got'); let url = 'https://whatever.com'; let result = await got.get(url, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0' }, maxRedirects: 0, retry: { limit: 0 }, request: http2.auto, http2: true, agent: { http2: new http2.proxies.Http2OverHttp({ proxyOptions: { url: 'http://127.0.0.1:10809', rejectUnauthorized: false } }) } }).json(); console.log(result); })(); `

The http2 agent doesn't work, proxy log is empty, I'm using v2ray as proxy server, it supports both http and socks.

johnsmith0209 commented 1 month ago

Same here, but get error messages

got@14.2.1 http2-wrapper@2.2.1

const agent = new http2Wrapper.proxies.HttpOverHttp2({
    proxyOptions: {
        url: `http://127.0.0.1:10809`,
        rejectUnauthorized: false,
    }
});
const options = {
    url: 'https://httpbin.org/ip',
    http2: true,
    agent: {
        http2: agent,
    }
};
const result = await got(options).json();

The error message is:

RequestError: The "options.agent" property must be one of type http2wrapper.Agent-like Object, undefined or false. Received object

This indicates that the return value of http2Wrapper.proxies.HttpOverHttp2 is not http2wrapper.Agent like. But the original post of http2-wrapper agent is confusing, and there is not an argument can pass in something like 'url', for the end-point of the proxy server.

johnsmith0209 commented 1 month ago

So, I used the wrong proxy class.

The followed worked for me:

const agent = new http2Wrapper.proxies.Http2OverHttp({
    proxyOptions: {
        url: `http://127.0.0.1:10809`,
        rejectUnauthorized: false,
    }
});
const options = {
    url: 'https://httpbin.org/ip',
    http2: true,
    agent: {
        http2: agent,
    }
};
const result = await got(options).json();

@wildwind3325