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

the HTTP(S) Agent invalid #379

Open
qisuwan opened this issue Mar 26, 2024 · 7 comments
Open

the HTTP(S) Agent invalid #379

qisuwan opened this issue Mar 26, 2024 · 7 comments

Comments

@qisuwan
Copy link

qisuwan commented Mar 26, 2024

Environment

demo package.json

{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "node src/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "http-proxy-agent": "^7.0.2",
    "https-proxy-agent": "^7.0.4",
    "ofetch": "^1.3.4"
  }
}

Reproduction

The demo code

import { HttpProxyAgent } from 'http-proxy-agent'
import { ofetch } from 'ofetch'

const res1 = await ofetch('http://ip-api.com/json')

const res2 = await ofetch('http://ip-api.com/json', {
  agent: new HttpProxyAgent('http://127.0.0.1:7890'),
})

console.log('No Proxy IP: ' + res1.query)
console.log('Proxy IP: ' + res2.query)

Describe the bug

console print

No Proxy IP: 125.70.78.162
Proxy IP: 125.70.78.162

result is same, the set proxy is invalid

Additional context

No response

Logs

No response

@kevindaizj
Copy link

Same
nuxt #24799

@blowsie
Copy link
Contributor

blowsie commented Apr 23, 2024

Same for me, reproduction here:
Im not sure how we can make a reproduction on Codesandbox for proxy issues? Maybe there is a public proxy we can use?

a node project

import fetch from 'node-fetch';
import { HttpsProxyAgent } from 'https-proxy-agent';

(async () => {
    const targetUrl = 'https://ident.me/ip';
    {
        const response = await fetch(targetUrl);
        const text = await response.text();
        console.log(text);
    }
    {
        const response = await fetch(targetUrl, {
            agent: new HttpsProxyAgent('http://127.0.0.1:7890')
        });
        const text = await response.text();
        console.log(text);
    }
})();

result: 113.248.xxx.xxx 5.252.198.49 two ips are not the same

but if use nuxt server\api\test\google.get.ts

import {HttpsProxyAgent} from "https-proxy-agent";
import {$fetch, ofetch} from "ofetch";
import fetch from 'node-fetch'

export default defineEventHandler(async (event) => {
    const ip1 = await $fetch('https://ident.me/ip');
    console.log(ip1);

    const ip2 = await $fetch('https://ident.me/ip', {
        agent: new HttpsProxyAgent('http://127.0.0.1:7890')
    })
    console.log(ip2);

    const ip3 = await ofetch('https://ident.me/ip', {
        agent: new HttpsProxyAgent('http://127.0.0.1:7890')
    })
    console.log(ip3);

    const response = await fetch('https://ident.me/ip', {
        agent: new HttpsProxyAgent('http://127.0.0.1:7890')
    })
    const ip4 = await response.text();
    console.log(ip4);

    return {
        ip1,
        ip2,
        ip3,
        ip4
    }
})

{ "ip1": "113.248.xxx.xxx", "ip2": "113.248.xxx.xxx", "ip3": "113.248.xxx.xxx", "ip4": "113.248.xxx.xxx" } the ips are the same, http proxy it's not work

Originally posted by @289997171 in nuxt/nuxt#24799 (comment)

@blowsie
Copy link
Contributor

blowsie commented Apr 23, 2024

In addition to this, there is no types for agent in oftech


Edit this has been reported here

@blowsie
Copy link
Contributor

blowsie commented Apr 23, 2024

Could it be because ofetch takes preference on nodes native fetch, which in newer versions of node uses undici which does not use agent instead it uses dispatcher.

So you can try this.

import { ofetch } from 'ofetch'
import { ProxyAgent } from 'undici'

const res1 = await ofetch('http://ip-api.com/json')

const res2 = await ofetch('http://ip-api.com/json', {
  dispatcher: new ProxyAgent('http://127.0.0.1:7890'),
})

console.log('No Proxy IP: ' + res1.query)
console.log('Proxy IP: ' + res2.query)

@qisuwan
Copy link
Author

qisuwan commented Apr 24, 2024

Could it be because ofetch takes preference on nodes native fetch, which in newer versions of node uses undici which does not use agent instead it uses dispatcher.

So you can try this.

import { ofetch } from 'ofetch'
import { ProxyAgent } from 'undici'

const res1 = await ofetch('http://ip-api.com/json')

const res2 = await ofetch('http://ip-api.com/json', {
  dispatcher: new ProxyAgent('http://127.0.0.1:7890'),
})

console.log('No Proxy IP: ' + res1.query)
console.log('Proxy IP: ' + res2.query)

This is also a solution. Currently, I'm using node-fetch instead of fetch in Nuxt, it work well. However, there's a usage example in the README involving an agent. If it's not supported, then shouldn't mention this feature.

@blowsie
Copy link
Contributor

blowsie commented Apr 24, 2024

Could it be because ofetch takes preference on nodes native fetch, which in newer versions of node uses undici which does not use agent instead it uses dispatcher.

So you can try this.

import { ofetch } from 'ofetch'
import { ProxyAgent } from 'undici'

const res1 = await ofetch('http://ip-api.com/json')

const res2 = await ofetch('http://ip-api.com/json', {
  dispatcher: new ProxyAgent('http://127.0.0.1:7890'),
})

console.log('No Proxy IP: ' + res1.query)
console.log('Proxy IP: ' + res2.query)

I can confirm this is working perfectly fine.

@niicojs
Copy link

niicojs commented Apr 28, 2024

Using proxyagent from undici works fine for HTTP proxy. I didn't find a way for HTTPS proxies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants