windkh / node-red-contrib-telegrambot

Telegram bot nodes for node-red.
Other
259 stars 116 forks source link

"TypeError: Agent is not a constructor" when using socks5 #244

Closed fgionghi closed 2 years ago

fgionghi commented 2 years ago

Hi, I would like to reach telegram servers through the tor network. I have a docker-compose that declare nodered v2.2.2 (nodejs v14.18.2) and another container with tor.

From the command line it seems to work:

bash-5.0$ curl --socks5 tor:9050 https://check.torproject.org/api/ip
{"IsTor":true,"IP":"141.136.0.117"}

I run this command inside the nodered container and tor is the name of the container running tor.

But if I try to activate the option SOCKS5 in the telegram bot on nodered, as soon as I hit deploy, i get the error "TypeError: Agent is not a constructor".

Does someone knows how to resolve? Thank you.

windkh commented 2 years ago

Hm and it works without using docker???

windkh commented 2 years ago

What version of the nodes do you use?

fgionghi commented 2 years ago

Without docker I really don't know, maybe I will try next week.

If you mean the version of telegram bot I'm using the latest one: 11.3.0

windkh commented 2 years ago

I never had the chance to test it with tor. My test system was a go-socks5 docker container running on my nas. Maybe I will try to check if it still works… Can you provide more info? Logfile, callstack?

fgionghi commented 2 years ago

Of course but can you explain to me how can i found those infos (a docs link is perfect)? I edited the log level to trace but I don't see anything relevant:

13 May 22:32:26 - [trace] [subflow:c391ed463dfbd2a7]  a01629cc9ceb5d92 | function     | e6973e17dfd20dd0
13 May 22:32:26 - [trace] [subflow:c391ed463dfbd2a7]  6a1249e5a23faafe | mongodb in   | 3da25d34cf7608ed
13 May 22:32:26 - [trace] [subflow:c391ed463dfbd2a7]  9cd194accb06a45b | debug        | 7d2a940658890bba
13 May 22:32:26 - [trace] [subflow:c391ed463dfbd2a7]  30a79a44d43dad9d | debug        | 8b98d5c5d9b9cd73
13 May 22:32:26 - [trace] [subflow:c391ed463dfbd2a7] ------------------|--------------|-----------------
13 May 22:32:26 - [error] [telegram sender:de5edebc39140c8d] TypeError: Agent is not a constructor
13 May 22:32:26 - [error] [telegram sender:cbd5ae53e566fd3a] TypeError: Agent is not a constructor
13 May 22:32:26 - [trace] [flow:35de765756cbf25d] ------------------|--------------|-----------------
13 May 22:32:26 - [trace] [flow:35de765756cbf25d]  id               | type         | alias
13 May 22:32:26 - [trace] [flow:35de765756cbf25d] ------------------|--------------|-----------------
13 May 22:32:26 - [trace] [flow:35de765756cbf25d]  2527dfb13be40be2 | function     | 
13 May 22:32:26 - [trace] [flow:35de765756cbf25d]  7dd1f2beb4f6b481 | http request | 
13 May 22:32:26 - [trace] [flow:35de765756cbf25d]  ba450b1fb8c24971 | json         | 
windkh commented 2 years ago

You can enable verbose logging in the node

fgionghi commented 2 years ago

Oh yeah I did that but in the debug window I have nothing more than the error in the title.

Other infos I omitted beacuse I think they are irrelevant but maybe..: as tor docker image I'm using https://hub.docker.com/r/dperson/torproxy beacuse it provides both socks and http. I am successfully using the http proxy with an HTTP request node.

windkh commented 2 years ago

Hm strange… I could help debugging if you manage to set up a debugger on your system…

fgionghi commented 2 years ago

Yes of course but I don't know what exactly do you mean

windkh commented 2 years ago

You should have some debugger installed on your computer which is able to remote debug the nodes inside the docker container. Then we try to step through the code to find the problem. I for myself use visual code under windows.

fgionghi commented 2 years ago

ok for vscode, is something like this ok? https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_remote-debugging However I don't think I can do it today.

windkh commented 2 years ago

Let‘s try it … when we have both some time…

fgionghi commented 2 years ago

Perfect, probably is obvious but I have the same error using an online socks5 proxy

skrashevich commented 2 years ago

Have the same problem with node-red running as home-assistance add-on from official repo

windkh commented 2 years ago

@fgionghi I just tested with my local go-socks5 proxy running on my NAS. Here it works. Can you provide me the IP, username, password of the socks5 server you are using so that I can test it with that one?

windkh commented 2 years ago

@fgionghi I installed the docker image from https://hub.docker.com/r/dperson/torproxy. Got it running on port 9050 without issues. It does not crash when connecting.

Looking at the source code I came to a theory: Agent is used here

class SocksProxyAgent extends agent_base_1.Agent

so can you check what version of agent-base is on your machine?

const agent_base_1 = require("agent-base");

fgionghi commented 2 years ago

I just tried with 192.252.211.197:14921 and got the same error.

From inside the nodered docker container:

bash-5.0$ npm list | grep agent
  | +-- hpagent@0.1.2
  | +-- https-proxy-agent@5.0.0
  | | +-- agent-base@6.0.2
  | | +-- https-proxy-agent@5.0.1
  | | | +-- agent-base@6.0.2 deduped

Shouldn't socks-proxy-agent be there also? (I now installed it and nothing changed). Is your nodered running in a docker container?

fgionghi commented 2 years ago

Could it be an arm problem? I've tried different version, both in a docker container and without it, but nothing. One thing I notice is that when I install node-red-contrib-telegrambot it doesn't install dependencies like https-proxy-agent (on arm).

I've also tried to use the docker image on a x86_64 arch and socks5 functionality works.

windkh commented 2 years ago

@fgionghi that makes sense but don't ask me why the dependencies are not installed on a arm. At least it explains the error that Agent is not a constructor.

Is it possible to install https-proxy-agent from command line on a arm platform?

npm install https-proxy-agent --save

from within the node-red-contrib-telegrambot folder

fgionghi commented 2 years ago

While I was trying a completely different approach I found the solution. I just had to set the HTTP_PROXY env variable. No need to manually install any dependencies.

environment:
   - HTTP_PROXY=tor:8118

tor:8118 is my other container with an http proxy on 8118.

But WHY adding this var let me be able to use SOCKS5 proxy? I'm not even using an http_proxy and I when I was using it, it was working even without this var. Even more: I can set HTTP_PROXY to something that doesn't exist and it solve my SOCKS5 problem anyway!

Hope you find some sense and can explain to me.

windkh commented 2 years ago

I had a look at the source code and also in the code of the dependencies... no one makes use of HTTP_PROXY. The question is: why is it behaving in a different way and not throwing an exception.

If I could debug it, then probably I could have an idea, but from here I can only speculate.

--> so you do not get a TypeError by just setting this variable????

fgionghi commented 2 years ago

--> so you do not get a TypeError by just setting this variable????

exactly. After the deploy the bot node says connected and in the debug window i don't have any errors. The only change I made was setting HTTP_PROXY var.

Now I have other errors! After I click "deploy" I got:

Either using my socks5 proxy or with one on the internet (both works with curl).

windkh commented 2 years ago

same here https://github.com/windkh/node-red-contrib-telegrambot/issues/247

fgionghi commented 2 years ago

Solved adding http:// to the HTTP_PROXY value.

The only problem is that now I am using an http proxy by default. To solve this I am using the env var NO_PROXY=.net,.orgbut I can't add .com otherwise telegram socks option break everything again (because telegram api are on .com I image).

windkh commented 2 years ago

Today I faced the same problem. The Agent could not be created due to permission problems. Without knowing it exactly I thing that during initialization some elevated rights are needed. Under windows you can run node-red using administrator rights and then it works.

windkh commented 2 years ago

Make sure to set HTTP_PROXY and NO_PROXY to exclude 127.0.0.1