gram-js / gramjs

NodeJS/Browser MTProto API Telegram client library,
MIT License
1.28k stars 179 forks source link

Help solve the problem with connecting proxy IPv6 #369

Open atomexpert opened 2 years ago

atomexpert commented 2 years ago

Hello! Unfortunately, the library does not work correctly with Ipv6 proxy. I kindly ask you to help me to solve this problem.

The crux of the problem is this:

2022-08-02_13-00-15

Connection is via Ipv6 data center: 2001:067c:04e8:f004:0000:0000:0000:000a:80

2022-08-02_13-06-55

Connection is via Ipv6 data center: 2001:067c:04e8:f002:0000:0000:0000:000a:80

2022-08-02_13-12-59

Data centers that I have tried:

  1. 2001:b28:f23d:f001::a
  2. 2001:b28:f23d:f003::a
  3. 2001:67c:4e8:f004::a
  4. 2001:b28:f23f:f005::a

But in the end the error was the same as described above.

I used different proxies, checked them in the desktop version of telegram - they all work fine. If anyone has any solution, I would be very grateful if you share it.

Code I used (with working proxy):

const { Api, TelegramClient } = require("telegram"); const { StringSession } = require("telegram/sessions"); const input = require("input"); const apiId = 1; const apiHash = "b6b154c3707471f5339bd661645ed3d6"; const session = new StringSession("");

const client = new TelegramClient(session, apiId, apiHash, { connectionRetries: 2, useIPV6: true, langCode: 'en', systemLangCode: 'en', proxy: { ip: "45.89.19.41", port: 7711, username: "FFhf0l", password: "bBlM1c6JSG", MTProxy: false, socksType: 5, timeout: 20, } })

let start = async()=>{ await client.connect() .then(response => { console.log("Connect"); }) .catch(error => { console.log("Error: "+error); })

await client.start({
    phoneNumber: async () => await input.text("number ?"),
    password: async () => await input.text("password?"),
    phoneCode: async () => await input.text("Code ?"),
    onError: (err) => {
        throw new Error(err)
    },
})
.then(response => {
    console.log("Ok!");
    console.log(client.session.save());
})
.catch(error => {
    console.log("Error: "+error); 
})

} start();

atomexpert commented 2 years ago

In general, the problem turned out to be in data centers. Each number has its own region, and depending on it, Telegram allocates a specific Data Center. And unfortunately not every proxy ipV6 can connect to this DC.