|
@@ -1,17 +1,19 @@
|
|
-const log4js = require('log4js')
|
|
|
|
|
|
+const logger = require('log4js').getLogger()
|
|
const { sleep } = require('../Helpers')
|
|
const { sleep } = require('../Helpers')
|
|
const errors = require('../errors')
|
|
const errors = require('../errors')
|
|
|
|
+const MemorySession = require('../sessions/Memory')
|
|
const { addKey } = require('../crypto/RSA')
|
|
const { addKey } = require('../crypto/RSA')
|
|
const { TLRequest } = require('../tl/tlobject')
|
|
const { TLRequest } = require('../tl/tlobject')
|
|
const utils = require('../Utils')
|
|
const utils = require('../Utils')
|
|
-const Session = require('../sessions/Session')
|
|
|
|
|
|
+const Session = require('../sessions/Abstract')
|
|
|
|
+const JSONSession = require('../sessions/JSONSession')
|
|
const os = require('os')
|
|
const os = require('os')
|
|
const { GetConfigRequest } = require('../tl/functions/help')
|
|
const { GetConfigRequest } = require('../tl/functions/help')
|
|
const { LAYER } = require('../tl/AllTLObjects')
|
|
const { LAYER } = require('../tl/AllTLObjects')
|
|
const { functions, types } = require('../tl')
|
|
const { functions, types } = require('../tl')
|
|
const { computeCheck } = require('../Password')
|
|
const { computeCheck } = require('../Password')
|
|
const MTProtoSender = require('../network/MTProtoSender')
|
|
const MTProtoSender = require('../network/MTProtoSender')
|
|
-const { ConnectionTCPFull } = require('../network/connection/TCPFull')
|
|
|
|
|
|
+const { ConnectionTCPObfuscated } = require('../network/connection/TCPObfuscated')
|
|
const DEFAULT_DC_ID = 4
|
|
const DEFAULT_DC_ID = 4
|
|
const DEFAULT_IPV4_IP = '149.154.167.51'
|
|
const DEFAULT_IPV4_IP = '149.154.167.51'
|
|
const DEFAULT_IPV6_IP = '[2001:67c:4e8:f002::a]'
|
|
const DEFAULT_IPV6_IP = '[2001:67c:4e8:f002::a]'
|
|
@@ -19,7 +21,7 @@ const DEFAULT_PORT = 443
|
|
|
|
|
|
class TelegramClient {
|
|
class TelegramClient {
|
|
static DEFAULT_OPTIONS = {
|
|
static DEFAULT_OPTIONS = {
|
|
- connection: ConnectionTCPFull,
|
|
|
|
|
|
+ connection: ConnectionTCPObfuscated,
|
|
useIPV6: false,
|
|
useIPV6: false,
|
|
proxy: null,
|
|
proxy: null,
|
|
timeout: 10,
|
|
timeout: 10,
|
|
@@ -38,7 +40,7 @@ class TelegramClient {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- constructor(sessionName, apiId, apiHash, opts = TelegramClient.DEFAULT_OPTIONS) {
|
|
|
|
|
|
+ constructor(session, apiId, apiHash, opts = TelegramClient.DEFAULT_OPTIONS) {
|
|
if (apiId === undefined || apiHash === undefined) {
|
|
if (apiId === undefined || apiHash === undefined) {
|
|
throw Error('Your API ID or Hash are invalid. Please read "Requirements" on README.md')
|
|
throw Error('Your API ID or Hash are invalid. Please read "Requirements" on README.md')
|
|
}
|
|
}
|
|
@@ -48,11 +50,21 @@ class TelegramClient {
|
|
this._useIPV6 = args.useIPV6
|
|
this._useIPV6 = args.useIPV6
|
|
this._entityCache = new Set()
|
|
this._entityCache = new Set()
|
|
if (typeof args.baseLogger == 'string') {
|
|
if (typeof args.baseLogger == 'string') {
|
|
- this._log = log4js.getLogger(args.baseLogger)
|
|
|
|
|
|
+ this._log = logger
|
|
} else {
|
|
} else {
|
|
this._log = args.baseLogger
|
|
this._log = args.baseLogger
|
|
}
|
|
}
|
|
- const session = Session.tryLoadOrCreateNew(sessionName)
|
|
|
|
|
|
+ // Determine what session we will use
|
|
|
|
+ if (typeof session === 'string' || !session) {
|
|
|
|
+ try {
|
|
|
|
+ session = JSONSession.tryLoadOrCreateNew(session)
|
|
|
|
+ } catch (e) {
|
|
|
|
+ console.log(e)
|
|
|
|
+ session = new MemorySession()
|
|
|
|
+ }
|
|
|
|
+ } else if (!(session instanceof Session)) {
|
|
|
|
+ throw new Error('The given session must be str or a session instance')
|
|
|
|
+ }
|
|
if (!session.serverAddress || (session.serverAddress.includes(':') !== this._useIPV6)) {
|
|
if (!session.serverAddress || (session.serverAddress.includes(':') !== this._useIPV6)) {
|
|
session.setDC(DEFAULT_DC_ID, this._useIPV6 ? DEFAULT_IPV6_IP : DEFAULT_IPV4_IP, DEFAULT_PORT)
|
|
session.setDC(DEFAULT_DC_ID, this._useIPV6 ? DEFAULT_IPV6_IP : DEFAULT_IPV4_IP, DEFAULT_PORT)
|
|
}
|
|
}
|
|
@@ -196,7 +208,6 @@ class TelegramClient {
|
|
if (!(request instanceof TLRequest)) {
|
|
if (!(request instanceof TLRequest)) {
|
|
throw new Error('You can only invoke MTProtoRequests')
|
|
throw new Error('You can only invoke MTProtoRequests')
|
|
}
|
|
}
|
|
- console.log('sending request..', request)
|
|
|
|
await request.resolve(this, utils)
|
|
await request.resolve(this, utils)
|
|
|
|
|
|
if (request.CONSTRUCTOR_ID in this._floodWaitedRequests) {
|
|
if (request.CONSTRUCTOR_ID in this._floodWaitedRequests) {
|
|
@@ -278,8 +289,9 @@ class TelegramClient {
|
|
if (await this.isUserAuthorized()) {
|
|
if (await this.isUserAuthorized()) {
|
|
return this
|
|
return this
|
|
}
|
|
}
|
|
|
|
+ let phone
|
|
if (!args.botToken) {
|
|
if (!args.botToken) {
|
|
- args.phone = utils.parsePhone(args.phone) || args.phone
|
|
|
|
|
|
+ phone = utils.parsePhone(args.phone) || args.phone
|
|
}
|
|
}
|
|
if (args.botToken) {
|
|
if (args.botToken) {
|
|
await this.signIn({
|
|
await this.signIn({
|
|
@@ -292,7 +304,7 @@ class TelegramClient {
|
|
let attempts = 0
|
|
let attempts = 0
|
|
let twoStepDetected = false
|
|
let twoStepDetected = false
|
|
|
|
|
|
- await this.sendCodeRequest(args.phone, args.forceSMS)
|
|
|
|
|
|
+ await this.sendCodeRequest(phone, args.forceSMS)
|
|
console.log('you got sent the code')
|
|
console.log('you got sent the code')
|
|
|
|
|
|
let signUp = false
|
|
let signUp = false
|
|
@@ -314,22 +326,19 @@ class TelegramClient {
|
|
} else {
|
|
} else {
|
|
// this throws SessionPasswordNeededError if 2FA enabled
|
|
// this throws SessionPasswordNeededError if 2FA enabled
|
|
me = await this.signIn({
|
|
me = await this.signIn({
|
|
- phone: args.phone,
|
|
|
|
|
|
+ phone: phone,
|
|
code: value,
|
|
code: value,
|
|
})
|
|
})
|
|
}
|
|
}
|
|
break
|
|
break
|
|
} catch (e) {
|
|
} catch (e) {
|
|
-
|
|
|
|
if (e instanceof errors.SessionPasswordNeededError) {
|
|
if (e instanceof errors.SessionPasswordNeededError) {
|
|
twoStepDetected = true
|
|
twoStepDetected = true
|
|
break
|
|
break
|
|
} else if (e instanceof errors.PhoneNumberOccupiedError) {
|
|
} else if (e instanceof errors.PhoneNumberOccupiedError) {
|
|
signUp = true
|
|
signUp = true
|
|
-
|
|
|
|
} else if (e instanceof errors.PhoneNumberUnoccupiedError) {
|
|
} else if (e instanceof errors.PhoneNumberUnoccupiedError) {
|
|
signUp = true
|
|
signUp = true
|
|
-
|
|
|
|
} else if (e instanceof errors.PhoneCodeEmptyError ||
|
|
} else if (e instanceof errors.PhoneCodeEmptyError ||
|
|
e instanceof errors.PhoneCodeExpiredError ||
|
|
e instanceof errors.PhoneCodeExpiredError ||
|
|
e instanceof errors.PhoneCodeHashEmptyError ||
|
|
e instanceof errors.PhoneCodeHashEmptyError ||
|
|
@@ -345,18 +354,16 @@ class TelegramClient {
|
|
throw new Error(`${args.maxAttempts} consecutive sign-in attempts failed. Aborting`)
|
|
throw new Error(`${args.maxAttempts} consecutive sign-in attempts failed. Aborting`)
|
|
}
|
|
}
|
|
if (twoStepDetected) {
|
|
if (twoStepDetected) {
|
|
- console.log('two steps baby')
|
|
|
|
if (!args.password) {
|
|
if (!args.password) {
|
|
throw new Error('Two-step verification is enabled for this account. ' +
|
|
throw new Error('Two-step verification is enabled for this account. ' +
|
|
'Please provide the \'password\' argument to \'start()\'.')
|
|
'Please provide the \'password\' argument to \'start()\'.')
|
|
}
|
|
}
|
|
me = await this.signIn({
|
|
me = await this.signIn({
|
|
- phone: args.phone,
|
|
|
|
|
|
+ phone: phone,
|
|
password: args.password,
|
|
password: args.password,
|
|
})
|
|
})
|
|
}
|
|
}
|
|
const name = utils.getDisplayName(me)
|
|
const name = utils.getDisplayName(me)
|
|
- console.log(me)
|
|
|
|
console.log('Signed in successfully as', name)
|
|
console.log('Signed in successfully as', name)
|
|
return this
|
|
return this
|
|
}
|
|
}
|
|
@@ -495,7 +502,6 @@ class TelegramClient {
|
|
}
|
|
}
|
|
|
|
|
|
_handleUpdate(update) {
|
|
_handleUpdate(update) {
|
|
-
|
|
|
|
this.session.processEntities(update)
|
|
this.session.processEntities(update)
|
|
this._entityCache.add(update)
|
|
this._entityCache.add(update)
|
|
|
|
|
|
@@ -781,7 +787,6 @@ class TelegramClient {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
return false
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
async signUp() {
|
|
async signUp() {
|