xelaj / mtproto

🦋 Full-native go implementation of Telegram API
MIT License
1.24k stars 132 forks source link

Get PHONE_MIGRATE_5 after sending AuthSendCode #22

Closed xxxsen closed 3 years ago

xxxsen commented 4 years ago
setCode, err := client.AuthSendCode(&telegram.AuthSendCodeParams{
    "+86xxxx", 0000, "0000000", &telegram.CodeSettings{},
})

run auth example and get error msg like this.

panic: Panicking because of error: sedning AuthSendCode: PHONE_MIGRATE_5 (code 303)
quenbyako commented 4 years ago

Hey @xxxsen, thanks for question!

This is trouble of telegram api, all clients needs to automatically switch DCs between available. Also, this error is really specific, we don't know how to process it (code of dc is invluded in error id). Btw, we know about this problem since #12 was created. If you have any idea how to process this error code, let me know please.

xxxsen commented 4 years ago

https://itnext.io/source-code-walkthrough-of-telegram-ios-part-4-mtproto-connections-22ef2e5b339

"Enter Phone Number" part of this article shows how to switch datacenter, wish it would help you to solve this issue.

On Mon, Oct 12, 2020, 04:13 Richard Cooper notifications@github.com wrote:

Hey @xxxsen https://github.com/xxxsen, thanks for question!

This is trouble of telegram api, all clients needs to automatically switch DCs between available. Also, this error is really specific, we don't know how to process it (code of dc is invluded in error id). Btw, we know about this problem since #12 https://github.com/xelaj/mtproto/issues/12 was created. If you have any idea how to process this error code, let me know please.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/xelaj/mtproto/issues/22#issuecomment-706761541, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA5TKVFFJTLLZNEXRSSSLRLSKIGVRANCNFSM4SLZBXWQ .

quenbyako commented 4 years ago

@xxxsen thanks a lot! I'll research this issue, and answer here until closing it. This article will help

airani commented 4 years ago

@ololosha228 Also, I have this problem. you can fix it? or what can I do for fixing?

quenbyako commented 4 years ago

@airani i have some work in this case, i'll commit part of my solution to this issue in experimental branch and let you know)

In general, there is not so much stuff to do for dealing this error code, you need to say telegram.Client change few session settings: set special ip address and server salt, so i hope, we can fix it in a few days!

airani commented 4 years ago

@ololosha228 you saw this page? https://core.telegram.org/api/datacenter

quenbyako commented 4 years ago

@airani this branch is leads to fix this issue

@ololosha228 you saw this page?

Yeah, but i still don't understand their point of logic to do stuff like this (cause servers may proxying requests to another, but they are not). Anyway, i working on in, you can help too)

airani commented 4 years ago

I found solution to fix this bug, when you have got this error for a phone number you should change serverHost or DC id to X number in PHONE_MIGRATE_X and again call NewClient with new ClientConfig and note that delete the session file. and try again in a loop until you select the right DC for that phone number.

List of Telegram DC:

ID IP PORT
1 149.154.175.58 443
1 149.154.175.55 443
2 149.154.167.50 443
2 149.154.167.51 443
2 149.154.167.151 443
3 149.154.175.100 443
4 149.154.167.91 443
4 149.154.165.111 443
5 91.108.56.151 443
codenoid commented 4 years ago

this is major bug, break the whole library

quenbyako commented 4 years ago

@codenoid it's not a bug, it's a feature)))

Okay, without any jokes, we agree that library must change DC automatically (as many other stuff), i'll prioritize this feature to top level.

Unfortunately, i don't have much time to fix it, maybe someone could collect data about which usual telegram client do automatically without user requests (must dig in tdlib or android clients, they must have automatic jobs like change dc, recreate or repair sessions etc), and i'll just implement it. But i need info about this firstly.

codenoid commented 4 years ago

i don't know, but i still able to send code with https://github.com/sdidyk/mtproto

note: but after I input the confirmation code (with sdidyk/mtproto), my telegram account goes banned, i don't know, maybe it's uses old API

quenbyako commented 4 years ago

@codenoid i'm sorry to hear that :cry:

sdidyk/mtproto has bad credentials of app (app id 41994 is banned due to multiple cases spamming and doing malware stuff). Also, few parts of this (xelaj/mtproto) package was reimplemented, but i can say that sdidyk/mtproto doesnt have proper logic of automated things.

quenbyako commented 4 years ago

wait what? i didn't close it, its only WIP!

quenbyako commented 4 years ago

@xxxsen @airani @codenoid and also @patrikhermansson, i have some news!

So i had a few time to fix it, but it's looks so messy as i think. This commit will help. I hope.

@codenoid @xxxsen due to this error affects to specific phone numbers, can you please test this fix, and give us report?

patrikhermansson commented 4 years ago

Can confirm that it works well in my testing! Thanks for fixing and sorry for late reply.

quenbyako commented 4 years ago

@patrikhermansson check out also this branch

We had reworked literally all package include new tl parser, which doesn't panic every second, also we reimplemented codegen tool, now personally me don't cry when i'm scrolling telegram package in godoc))))

patrikhermansson commented 4 years ago

Hi! Sorry about this but clearly didn't test enough earlier. It doesn't work yet I'm afraid. Have not solved it but a few issues I found:

1) tryToProcessErr() in mtproto.go doesn't match with the PHONE_MIGRATE error. The switch statement matches against "PHONE_MIGRATE_X" but the e.Message is "PHONE_MIGRATE_4" (and assumign _1, _2, _3 etc.). This is an easy fix with somethign like if strings.Contains(e.Message, "PHONE_MIGRATE_") {} instead.

2) e.AdditionalInfo does not contain DC id, it's nil which means newIP, found := m.dclist[e.AdditionalInfo.(int)] panics. Might be that we have to parse the e.Message and get the DC id from there instead.

3) However, even changing these things, it doesn't work. I get the following error:

[]uint8{ 0x00, 0x00, 0x00, 0x00, } &errors.errorString{ s: "context canceled", }

Apologies again for being mistaken in the previous message.

quenbyako commented 4 years ago

@patrikhermansson errors with additional data (like EMAIL_UNCONFIRMED_X PASSWORD_TOO_FRESH_X etc.) extracting data from simple text and giving you extended struct with custom data and native error name (you can research TryExpandError implementation)

But i'm worried, that you didn't solve this issue, could you please post here github gist with testing code? Maybe we need to add more docs of how to solve errors with additional data...

patrikhermansson commented 4 years ago

Ok had a look. Yeah you're right, there's an issue in TryExpandError on line 77. It should be if choosedPrefixSuffix == nil not if choosedPrefixSuffix != nil.

But even if I fix this (which sorts point 1 and 2 in my previous comment). I still get the error:

[]uint8{ 0x00, 0x00, 0x00, 0x00, } &errors.errorString{ s: "context canceled", }

So this is separate. Gonna see if i can sort example code

patrikhermansson commented 4 years ago

TryExpandError error is fixed in new_decoder_refactoring branch so not gonna do a pull request but will try to figure out the context issue. Clearly one that gets cancelled somewhere

quenbyako commented 3 years ago

Due to multiple responses, this bug was fixed in release candidate branch, so i'll close it

VityaSchel commented 2 years ago

о боже ничего не работает об оже

Mookiees commented 2 years ago

эээ чо PHONE_MIGRATE_2

VityaSchel commented 2 years ago

эээ чо PHONE_MIGRATE_2

а ничо нормально общайся

Salman-2523 commented 3 days ago

getting this error, can please someone help

Error calling Telegram API: auth.sendCode { : 'mtrpc_error', error_code: 303, error_message: 'PHONE_MIGRATE_5' }


const createMTProtoInstance = (dcId = null, serverHost = null) => {
  return new MTProto({
    api_id: parseInt(process.env.TELEGRAM_API_ID),
    api_hash: process.env.TELEGRAM_API_HASH,
    ...(dcId ? { dcId } : {}),
    ...(serverHost ? { server_address: serverHost } : {}),
    storageOptions: {
      path: path.resolve(
        __dirname,
        dcId ? `./data/dc${dcId}.json` : "./data/default.json"
      ),
    },
  });
};

let mtproto = createMTProtoInstance();

app.post("/auth/start", async (req, res) => {
  const { phone } = req.body;
  console.log("PHONE", phone);

  if (!phone) {
    return res.status(400).json({ error: "Phone number is required" });
  }

  // Ensure international format
  const formattedPhone = phone.startsWith("+") ? phone : `+${phone}`;

  enhancedLog("Auth Start", `Attempting to send code to: ${formattedPhone}`);

  try {
    mtproto = createMTProtoInstance();
    const { phone_code_hash } = await handleDCMigration(
      mtproto,
      "auth.sendCode",
      {
        phone_number: formattedPhone,
        settings: {
          _: "codeSettings",
        },
      }
    );

    enhancedLog("Auth Start", "Code sent successfully", { phone_code_hash });

    res.status(200).json({
      phone_code_hash,
      message: "Code sent to Telegram!",
      phone: formattedPhone,
    });
  } catch (error) {
    logError("Auth Start", error);

    res.status(500).json({
      error: "Failed to send code",
      details: error.message,
      fullError: error,
    });
  }
});
quenbyako commented 3 days ago

Hey @Salman-2523, you should connect to DC 5 if you getting this error. Information about data centers and ip addresses you can fetch by calling auth.getConfig request (or similar name, i honestly don't remember exact method name), so just reconnect to that dc — and there you go, easy peasy 😉