Proxy support
Feature Request
Wanted Feature
Is there a convenient way to get the library to use a HTTP or SOCKS5 proxy?
I figure I could be able to get that to work by editing the source code, but that's pretty nasty.
Is there any "official" way to get that going?
Or any recommendation of how you'd do it?
I could even do a PR if I'm told the "right" way to implement this. Like passing a custom axios setup, or something like that?
Any pointers or help would be greatly appreciated.
Thanks!
Would this work?
import yahoo_finance from "yahoo-finance2"; // import the yahoo-finance2 module
import { ProxyAgent } from "undici-proxy-agent"; // import an undici-compatible proxy agent
// note: install with `npm i undici-proxy-agent`
const socks_url: string = "socks5h://127.0.0.1:9050"; // define socks proxy url; use socks5h to proxy DNS too
const dispatcher = new ProxyAgent(socks_url); // create a dispatcher that routes requests via the proxy
const result = await yahoo_finance.quote( // call a yahoo-finance method of your choice
"AAPL", // pass the symbol or query
{}, // pass module-specific query options
{ fetchOptions: { dispatcher } } // pass the dispatcher via fetchOptions
); // end the call
console.log(result);
Hey @arthurwolf, sorry for the late reply.
There's not currently any "official" way to do this, but I'd love for there to be one. Did the above work for you?
I looked into this briefly in February and also suggested some things in https://github.com/gadicc/yahoo-finance2/issues/872#issuecomment-2636045576, which I'll quote here for convenience:
Mmm that's a really good question to which I don't actually know the answer to 😅
From a quick look online - but haven't tried this to myself - courtesy of lselden in >https://github.com/node-fetch/node-fetch/issues/1770#issuecomment-1854439586:
I've been using global-agent with node-fetch for a while now (though on v2.7 and only for node < v18)
import * as globalAgent from 'global-agent'; process.env["GLOBAL_AGENT_HTTP_PROXY"] = process.env.HTTP_PROXY || "http://my.proxy.address:8888"; globalAgent.bootstrap();or maybe with proxy-agent, something like:
const proxy = require('proxy-agent'); const agent = proxy({ protocol: "http:", port: "1024", hostname: "proxy.corporate.com", ciphers: 'TLS_AES_256_GCM_SHA384:ECDHE-RSA-AES256-GCM-SHA384', minVersion: "TLSv1.2", }); const result = yahooFinance.quote('AAPL', {}, { fetchOptions: { agent }});If either of those work let me know so I can add it to the docs 😅
In short, yes, you can pass fetchOptions per request, and, I guess it would be nice to be able to set it once too with new YahooFinance(), right? We could definitely add that.
Yes the above code worked for me.
I guess it might be a good idea to add that as an example somewhere in the documentation, so people who want to use it with a proxy can find it easily?
Adding a proxy "option" is a lot more involved I guess?
Thanks for reporting back! Added to my list:
- [ ] Official proxy docs
- [ ] Ability to specify
fetchOptionsinnew YahooFinance()too.
Yeah I don't think we'd provide our own proxy code ourselves... better for peope to understand how proxies work with fetch() and to use a library that definitely works with their own proxy, and to know they can easily re-use it with anything else that uses fetch() too.