@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
318 lines (317 loc) • 12.2 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.siginBot = siginBot;
exports.siginUser = siginUser;
exports.sendCode = sendCode;
exports.sigin = sigin;
exports.recoverPassword = recoverPassword;
exports.sendRecoveryCode = sendRecoveryCode;
exports.checkPassword = checkPassword;
exports.acceptTOS = acceptTOS;
exports.getPasswordHint = getPasswordHint;
exports.signup = signup;
exports.getMe = getMe;
const index_js_1 = require("../raw/index.js");
const index_js_2 = require("../session/index.js");
const Password_js_1 = require("../crypto/Password.js");
const Logger_js_1 = require("../Logger.js");
const Errors = __importStar(require("../errors/index.js"));
async function siginBot(botToken) {
while (true) {
let user;
try {
user = await this.invoke(new index_js_1.Raw.auth.ImportBotAuthorization({
botAuthToken: botToken,
apiId: this._apiId,
apiHash: this._apiHash,
flags: 0,
}), 0);
}
catch (error) {
if (error instanceof Errors.Exceptions.SeeOther.UserMigrate) {
error;
await this._session.stop();
const [ip, port] = await index_js_2.DataCenter.DataCenter(error.value, this._testMode, this._ipv6, false);
const auth = new index_js_2.Auth(error.value, this._testMode, this._ipv6);
this._storage.setAddress(error.value, ip, port, this._testMode);
this._storage.setApiId(this._apiId);
this._storage.setAuthKey(await auth.create(), this._storage.dcId);
this._session = new index_js_2.Session(this, this._storage.dcId, this._storage.authKey, this._storage.testMode);
await this._session.start();
}
else {
throw error;
}
}
finally {
if (user && 'user' in user) {
await this._storage.setUserId(user.user.id);
await this._storage.setIsBot(true);
}
}
if (user && 'user' in user) {
return user.user;
}
}
}
async function siginUser(auth) {
let _phoneNumber;
let _sendCode;
let _signedIn;
let _signedUp;
while (true) {
try {
_phoneNumber = await auth.phoneNumber();
_sendCode = await sendCode.call(this, _phoneNumber);
break;
}
catch (error) {
if (error instanceof Errors.Exceptions.BadRequest.BadRequest) {
Logger_js_1.Logger.error(error);
if (auth.authError) {
await auth.authError(error);
}
}
else {
throw error;
}
}
}
Logger_js_1.Logger.info('[101] The confirmation code has been sent.');
while (true) {
const code = await auth.code();
try {
_signedIn = await sigin.call(this, _phoneNumber, _sendCode.phoneCodeHash, code);
break;
}
catch (error) {
if (error instanceof Errors.Exceptions.BadRequest.BadRequest) {
Logger_js_1.Logger.error(error);
if (auth.authError) {
await auth.authError(error);
}
}
else if (error instanceof Errors.Exceptions.Unauthorized.SessionPasswordNeeded) {
let trying = 1;
while (true) {
try {
if (trying <= 3) {
if (!auth.password) {
throw new Error('2FA password required');
}
return await checkPassword.call(this, await auth.password(await getPasswordHint.call(this)));
}
else {
Logger_js_1.Logger.info('[102] Look you are forgotten the password');
if (auth.recoveryCode) {
const emailPattern = await sendRecoveryCode.call(this);
Logger_js_1.Logger.info(`[103] The recovery code has been sent to ${emailPattern}`);
while (true) {
const recoveryCode = await auth.recoveryCode();
try {
return await recoverPassword.call(this, recoveryCode);
}
catch (error) {
if (error instanceof Errors.Exceptions.BadRequest.BadRequest) {
Logger_js_1.Logger.error(error);
if (auth.authError) {
await auth.authError(error);
}
}
else {
throw error;
}
}
}
}
else {
break;
}
}
}
catch (error) {
if (error instanceof Errors.Exceptions.BadRequest.BadRequest) {
Logger_js_1.Logger.error(error);
if (auth.authError) {
await auth.authError(error);
}
trying++;
}
else {
throw error;
}
}
}
}
else {
throw error;
}
}
}
if (_signedIn && _signedIn instanceof index_js_1.Raw.User) {
return _signedIn;
}
while (true) {
try {
_signedUp = await signup.call(this, _phoneNumber, _sendCode.phoneCodeHash, auth.firstname ? await auth.firstname() : String(Date.now()), auth.lastname ? await auth.lastname() : '');
break;
}
catch (error) {
if (error instanceof Errors.Exceptions.BadRequest.BadRequest) {
Logger_js_1.Logger.error(error);
if (auth.authError) {
await auth.authError(error);
}
}
else {
throw error;
}
}
}
if (_signedIn instanceof index_js_1.Raw.help.TermsOfService) {
Logger_js_1.Logger.info(`\n${_signedIn.text}\n`);
await acceptTOS.call(this, _signedIn.id.data);
}
return _signedUp;
}
async function sendCode(phoneNumber) {
phoneNumber = phoneNumber.replace(/\+/g, '').trim();
while (true) {
try {
const r = await this.invoke(new index_js_1.Raw.auth.SendCode({
phoneNumber: phoneNumber,
apiId: this._apiId,
apiHash: this._apiHash,
settings: new index_js_1.Raw.CodeSettings({}),
}), 0);
return r;
}
catch (error) {
if (error instanceof Errors.Exceptions.SeeOther.NetworkMigrate ||
error instanceof Errors.Exceptions.SeeOther.PhoneMigrate) {
await this._session.stop();
const [ip, port] = await index_js_2.DataCenter.DataCenter(error.value, this._testMode, this._ipv6, false);
const auth = new index_js_2.Auth(error.value, this._testMode, this._ipv6);
this._storage.setAddress(error.value, ip, port, this._testMode);
this._storage.setApiId(this._apiId);
this._storage.setAuthKey(await auth.create(), this._storage.dcId);
this._session = new index_js_2.Session(this, this._storage.dcId, this._storage.authKey, this._storage.testMode);
await this._session.start();
}
else if (error instanceof Errors.ClientError.ClientDisconnected) {
await this.connect();
}
else {
throw error;
}
}
}
}
async function sigin(phoneNumber, phoneCodeHash, phoneCode) {
const r = await this.invoke(new index_js_1.Raw.auth.SignIn({
phoneNumber: phoneNumber.replace(/\+/g, '').trim(),
phoneCodeHash,
phoneCode,
}), 0);
if (r instanceof index_js_1.Raw.auth.AuthorizationSignUpRequired) {
if (r.termsOfService) {
return r.termsOfService;
}
return false;
}
else {
await this._storage.setUserId(r.user.id);
await this._storage.setIsBot(false);
return r.user;
}
}
async function recoverPassword(code) {
const r = await this.invoke(new index_js_1.Raw.auth.RecoverPassword({
code: code,
}), 0);
if ('user' in r) {
await this._storage.setUserId(r.user.id);
await this._storage.setIsBot(false);
return r.user;
}
return;
}
async function sendRecoveryCode() {
const r = await this.invoke(new index_js_1.Raw.auth.RequestPasswordRecovery(), 0);
return r.emailPattern;
}
async function checkPassword(password) {
const r = await this.invoke(new index_js_1.Raw.auth.CheckPassword({
password: (0, Password_js_1.computePasswordCheck)(await this.invoke(new index_js_1.Raw.account.GetPassword(), 0), password),
}), 0);
if ('user' in r) {
await this._storage.setUserId(r.user.id);
await this._storage.setIsBot(false);
return r.user;
}
return;
}
async function acceptTOS(id) {
const r = await this.invoke(new index_js_1.Raw.help.AcceptTermsOfService({
id: new index_js_1.Raw.DataJSON({
data: id,
}),
}));
return Boolean(r);
}
async function getPasswordHint() {
const r = await this.invoke(new index_js_1.Raw.account.GetPassword(), 0);
return r.hint ?? '';
}
async function signup(phoneNumber, phoneCodeHash, firstname, lastname = '') {
const r = await this.invoke(new index_js_1.Raw.auth.SignUp({
phoneNumber: phoneNumber.replace(/\+/g, '').trim(),
phoneCodeHash,
firstName: firstname,
lastName: lastname,
}));
if ('user' in r) {
await this._storage.setUserId(r.user.id);
await this._storage.setIsBot(false);
return r.user;
}
return;
}
async function getMe() {
return await this.invoke(new index_js_1.Raw.users.GetFullUser({
id: new index_js_1.Raw.InputUserSelf(),
}));
}