dimdenGD / chrome-lens-ocr

Library to use Google Lens OCR for free, via API used in Chromium.
167 stars 6 forks source link

Problem in setting up and using SOCKS #22

Closed NabiKAZ closed 2 weeks ago

NabiKAZ commented 2 weeks ago

The main problem is with the set the SOCKS. It's even strange that HttpsProxyAgent doesn't work even though they are of the same type as ProxyAgent. I tested different modes which are listed below:

import { ProxyAgent } from 'undici';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';

const lens = new Lens({
  fetchOptions: {
    // dispatcher: new ProxyAgent('http://127.0.0.1:10809') // OK
    // dispatcher: new ProxyAgent('socks://127.0.0.1:10808') // Invalid URL protocol: the URL must start with `http:` or `https:`.
    // dispatcher: new HttpsProxyAgent('http://127.0.0.1:10809') // TypeError: fetch failed
    // dispatcher: new SocksProxyAgent('socks://127.0.0.1:10808') // TypeError: fetch failed
  }
});

While all of these work well in the fetch agent.

In the guide, it is mentioned that the core can be used with fetch, so I tested the following codes, but I was unsuccessful.

async function customFetch(url, options) {
  console.log('Custom fetch called with URL:', url);
  return fetch(url, options);
}

const lens = new Lens({
  fetchOptions: {
    agent: new SocksProxyAgent('socks://127.0.0.1:10808')
  }
}, customFetch);

Or:

import fetch from 'node-fetch';
const lens = new Lens({...}, fetch);

My suggestion is to migrate from undici to node-fetch as the default library.

dimdenGD commented 2 weeks ago

Fixed setting fetch in 4.0.3

NabiKAZ commented 2 weeks ago

Thank you for your quick response. It worked well.

import Lens from 'chrome-lens-ocr'; //4.0.3
import fetch from 'node-fetch';
import { SocksProxyAgent } from 'socks-proxy-agent';

const proxyAgent = new SocksProxyAgent('socks://127.0.0.1:10808');

const lens = new Lens({
  fetchOptions: {
    agent: proxyAgent
  },
}, fetch);