2018-04-27 21:51:23 +03:00
|
|
|
import datetime
|
2016-09-16 14:35:14 +03:00
|
|
|
import os
|
2020-10-01 14:20:29 +03:00
|
|
|
import time
|
2021-09-19 14:45:19 +03:00
|
|
|
import ipaddress
|
|
|
|
from typing import Optional, List
|
2016-11-30 00:29:42 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
from .abstract import Session
|
2021-09-12 14:27:13 +03:00
|
|
|
from .._misc import utils
|
|
|
|
from .. import _tl
|
2021-09-19 14:45:19 +03:00
|
|
|
from .types import DataCenter, ChannelState, SessionState, Entity
|
2016-08-28 15:16:52 +03:00
|
|
|
|
2018-08-02 16:17:44 +03:00
|
|
|
try:
|
|
|
|
import sqlite3
|
2019-02-13 10:51:26 +03:00
|
|
|
sqlite3_err = None
|
|
|
|
except ImportError as e:
|
2018-08-02 16:17:44 +03:00
|
|
|
sqlite3 = None
|
2019-02-13 10:51:26 +03:00
|
|
|
sqlite3_err = type(e)
|
2018-08-02 16:17:44 +03:00
|
|
|
|
2017-12-26 18:45:47 +03:00
|
|
|
EXTENSION = '.session'
|
2021-09-19 14:45:19 +03:00
|
|
|
CURRENT_VERSION = 8 # database version
|
2018-01-18 11:52:39 +03:00
|
|
|
|
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
class SQLiteSession(Session):
|
|
|
|
"""
|
|
|
|
This session contains the required information to login into your
|
|
|
|
Telegram account. NEVER give the saved session file to anyone, since
|
|
|
|
they would gain instant access to all your messages and contacts.
|
2017-06-07 13:48:54 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
If you think the session has been compromised, close all the sessions
|
|
|
|
through an official Telegram client to revoke the authorization.
|
2017-06-07 13:48:54 +03:00
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
|
2018-03-02 13:36:39 +03:00
|
|
|
def __init__(self, session_id=None):
|
2018-08-02 16:17:44 +03:00
|
|
|
if sqlite3 is None:
|
2019-02-13 10:51:26 +03:00
|
|
|
raise sqlite3_err
|
2018-08-02 16:17:44 +03:00
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
super().__init__()
|
2017-12-26 18:45:47 +03:00
|
|
|
self.filename = ':memory:'
|
2018-03-02 13:36:39 +03:00
|
|
|
self.save_entities = True
|
2017-06-10 14:15:04 +03:00
|
|
|
|
2018-03-02 13:36:39 +03:00
|
|
|
if session_id:
|
|
|
|
self.filename = session_id
|
|
|
|
if not self.filename.endswith(EXTENSION):
|
|
|
|
self.filename += EXTENSION
|
2017-06-19 00:34:23 +03:00
|
|
|
|
2018-01-26 11:59:49 +03:00
|
|
|
self._conn = None
|
|
|
|
c = self._cursor()
|
2017-12-26 18:45:47 +03:00
|
|
|
c.execute("select name from sqlite_master "
|
|
|
|
"where type='table' and name='version'")
|
|
|
|
if c.fetchone():
|
|
|
|
# Tables already exist, check for the version
|
|
|
|
c.execute("select version from version")
|
|
|
|
version = c.fetchone()[0]
|
2019-05-03 00:20:39 +03:00
|
|
|
if version < CURRENT_VERSION:
|
2017-12-26 18:45:47 +03:00
|
|
|
self._upgrade_database(old=version)
|
2018-01-05 17:33:25 +03:00
|
|
|
c.execute("delete from version")
|
|
|
|
c.execute("insert into version values (?)", (CURRENT_VERSION,))
|
2017-12-26 18:45:47 +03:00
|
|
|
self.save()
|
|
|
|
else:
|
|
|
|
# Tables don't exist, create new ones
|
2021-09-19 14:45:19 +03:00
|
|
|
self._mk_tables(c)
|
2018-01-06 21:35:24 +03:00
|
|
|
c.execute("insert into version values (?)", (CURRENT_VERSION,))
|
2017-12-26 18:45:47 +03:00
|
|
|
c.close()
|
|
|
|
self.save()
|
2017-06-10 14:15:04 +03:00
|
|
|
|
2017-12-26 18:45:47 +03:00
|
|
|
def _upgrade_database(self, old):
|
2018-01-26 11:59:49 +03:00
|
|
|
c = self._cursor()
|
2018-04-23 22:16:09 +03:00
|
|
|
if old == 1:
|
|
|
|
old += 1
|
|
|
|
# old == 1 doesn't have the old sent_files so no need to drop
|
2018-01-18 11:52:39 +03:00
|
|
|
if old == 2:
|
2018-04-23 22:16:09 +03:00
|
|
|
old += 1
|
2018-01-18 11:52:39 +03:00
|
|
|
# Old cache from old sent_files lasts then a day anyway, drop
|
|
|
|
c.execute('drop table sent_files')
|
2018-04-23 22:16:09 +03:00
|
|
|
self._create_table(c, """sent_files (
|
|
|
|
md5_digest blob,
|
|
|
|
file_size integer,
|
|
|
|
type integer,
|
|
|
|
id integer,
|
|
|
|
hash integer,
|
|
|
|
primary key(md5_digest, file_size, type)
|
|
|
|
)""")
|
|
|
|
if old == 3:
|
|
|
|
old += 1
|
|
|
|
self._create_table(c, """update_state (
|
|
|
|
id integer primary key,
|
|
|
|
pts integer,
|
|
|
|
qts integer,
|
|
|
|
date integer,
|
|
|
|
seq integer
|
|
|
|
)""")
|
2019-02-10 13:10:41 +03:00
|
|
|
if old == 4:
|
|
|
|
old += 1
|
|
|
|
c.execute("alter table sessions add column takeout_id integer")
|
2019-09-12 20:17:32 +03:00
|
|
|
if old == 5:
|
|
|
|
# Not really any schema upgrade, but potentially all access
|
|
|
|
# hashes for User and Channel are wrong, so drop them off.
|
|
|
|
old += 1
|
|
|
|
c.execute('delete from entities')
|
2020-10-01 14:20:29 +03:00
|
|
|
if old == 6:
|
|
|
|
old += 1
|
|
|
|
c.execute("alter table entities add column date integer")
|
2021-09-19 14:45:19 +03:00
|
|
|
if old == 7:
|
|
|
|
self._mk_tables(c)
|
|
|
|
c.execute('''
|
|
|
|
insert into datacenter (id, ip, port, auth)
|
|
|
|
select dc_id, server_address, port, auth_key
|
|
|
|
from sessions
|
|
|
|
''')
|
|
|
|
c.execute('''
|
|
|
|
insert into session (user_id, dc_id, bot, pts, qts, date, seq, takeout_id)
|
|
|
|
select
|
|
|
|
0,
|
|
|
|
s.dc_id,
|
|
|
|
0,
|
|
|
|
coalesce(u.pts, 0),
|
|
|
|
coalesce(u.qts, 0),
|
|
|
|
coalesce(u.date, 0),
|
|
|
|
coalesce(u.seq, 0),
|
|
|
|
s.takeout_id
|
|
|
|
from sessions s
|
|
|
|
left join update_state u on u.id = 0
|
|
|
|
limit 1
|
|
|
|
''')
|
|
|
|
c.execute('''
|
|
|
|
insert into entity (id, access_hash, ty)
|
|
|
|
select
|
|
|
|
case
|
|
|
|
when id < -1000000000000 then -(id + 1000000000000)
|
|
|
|
when id < 0 then -id
|
|
|
|
else id
|
|
|
|
end,
|
|
|
|
hash,
|
|
|
|
case
|
|
|
|
when id < -1000000000000 then 67
|
|
|
|
when id < 0 then 71
|
|
|
|
else 85
|
|
|
|
end
|
|
|
|
from entities
|
|
|
|
''')
|
|
|
|
c.execute('drop table sessions')
|
|
|
|
c.execute('drop table entities')
|
|
|
|
c.execute('drop table sent_files')
|
|
|
|
c.execute('drop table update_state')
|
|
|
|
|
|
|
|
def _mk_tables(self, c):
|
|
|
|
self._create_table(
|
|
|
|
c,
|
|
|
|
'''version (
|
|
|
|
version integer primary key
|
|
|
|
)''',
|
|
|
|
'''datacenter (
|
|
|
|
id integer primary key,
|
|
|
|
ip text not null,
|
|
|
|
port integer not null,
|
|
|
|
auth blob not null
|
|
|
|
)''',
|
|
|
|
'''session (
|
|
|
|
user_id integer primary key,
|
|
|
|
dc_id integer not null,
|
|
|
|
bot integer not null,
|
|
|
|
pts integer not null,
|
|
|
|
qts integer not null,
|
|
|
|
date integer not null,
|
|
|
|
seq integer not null,
|
|
|
|
takeout_id integer
|
|
|
|
)''',
|
|
|
|
'''channel (
|
|
|
|
channel_id integer primary key,
|
|
|
|
pts integer not null
|
|
|
|
)''',
|
|
|
|
'''entity (
|
|
|
|
id integer primary key,
|
|
|
|
access_hash integer not null,
|
|
|
|
ty integer not null
|
|
|
|
)''',
|
|
|
|
)
|
2019-09-12 20:17:32 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
async def insert_dc(self, dc: DataCenter):
|
|
|
|
self._execute(
|
|
|
|
'insert or replace into datacenter values (?,?,?,?)',
|
|
|
|
dc.id,
|
|
|
|
str(ipaddress.ip_address(dc.ipv6 or dc.ipv4)),
|
|
|
|
dc.port,
|
|
|
|
dc.auth
|
|
|
|
)
|
2017-12-28 03:04:11 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
async def get_all_dc(self) -> List[DataCenter]:
|
|
|
|
c = self._cursor()
|
|
|
|
res = []
|
|
|
|
for (id, ip, port, auth) in c.execute('select * from datacenter'):
|
|
|
|
ip = ipaddress.ip_address(ip)
|
|
|
|
res.append(DataCenter(
|
|
|
|
id=id,
|
|
|
|
ipv4=int(ip) if ip.version == 4 else None,
|
|
|
|
ipv6=int(ip) if ip.version == 6 else None,
|
|
|
|
port=port,
|
|
|
|
auth=auth,
|
|
|
|
))
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def set_state(self, state: SessionState):
|
|
|
|
self._execute(
|
|
|
|
'insert or replace into session values (?,?,?,?,?,?,?,?)',
|
|
|
|
state.user_id,
|
|
|
|
state.dc_id,
|
|
|
|
int(state.bot),
|
|
|
|
state.pts,
|
|
|
|
state.qts,
|
|
|
|
state.date,
|
|
|
|
state.seq,
|
|
|
|
state.takeout_id,
|
|
|
|
)
|
2017-12-29 21:41:12 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
async def get_state(self) -> Optional[SessionState]:
|
|
|
|
row = self._execute('select * from session')
|
|
|
|
return SessionState(*row) if row else None
|
2017-12-26 18:45:47 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
async def insert_channel_state(self, state: ChannelState):
|
|
|
|
self._execute(
|
|
|
|
'insert or replace into channel values (?,?)',
|
|
|
|
state.channel_id,
|
|
|
|
state.pts,
|
|
|
|
)
|
2019-02-10 13:10:41 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
async def get_all_channel_states(self) -> List[ChannelState]:
|
2018-06-14 18:04:15 +03:00
|
|
|
c = self._cursor()
|
2021-09-19 14:45:19 +03:00
|
|
|
try:
|
|
|
|
return [
|
|
|
|
ChannelState(*row)
|
|
|
|
for row in c.execute('select * from channel')
|
|
|
|
]
|
|
|
|
finally:
|
|
|
|
c.close()
|
2017-06-07 13:48:54 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
async def insert_entities(self, entities: List[Entity]):
|
|
|
|
c = self._cursor()
|
|
|
|
try:
|
|
|
|
c.executemany(
|
|
|
|
'insert or replace into entity values (?,?,?)',
|
|
|
|
[(e.id, e.access_hash, e.ty) for e in entities]
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
c.close()
|
2018-04-25 14:37:29 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
async def get_entity(self, ty: int, id: int) -> Optional[Entity]:
|
|
|
|
row = self._execute('select ty, id, access_hash from entity where id = ?', id)
|
|
|
|
return Entity(*row) if row else None
|
2018-04-25 14:37:29 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
async def save(self):
|
2018-06-24 13:21:58 +03:00
|
|
|
# This is a no-op if there are no changes to commit, so there's
|
|
|
|
# no need for us to keep track of an "unsaved changes" variable.
|
2018-09-04 12:28:01 +03:00
|
|
|
if self._conn is not None:
|
|
|
|
self._conn.commit()
|
2017-06-07 13:48:54 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
@staticmethod
|
|
|
|
def _create_table(c, *definitions):
|
|
|
|
for definition in definitions:
|
|
|
|
c.execute('create table {}'.format(definition))
|
|
|
|
|
2018-01-26 11:59:49 +03:00
|
|
|
def _cursor(self):
|
|
|
|
"""Asserts that the connection is open and returns a cursor"""
|
2018-06-14 18:04:15 +03:00
|
|
|
if self._conn is None:
|
|
|
|
self._conn = sqlite3.connect(self.filename,
|
|
|
|
check_same_thread=False)
|
|
|
|
return self._conn.cursor()
|
2018-01-26 11:59:49 +03:00
|
|
|
|
2018-06-25 21:11:48 +03:00
|
|
|
def _execute(self, stmt, *values):
|
|
|
|
"""
|
|
|
|
Gets a cursor, executes `stmt` and closes the cursor,
|
|
|
|
fetching one row afterwards and returning its result.
|
|
|
|
"""
|
|
|
|
c = self._cursor()
|
|
|
|
try:
|
|
|
|
return c.execute(stmt, values).fetchone()
|
|
|
|
finally:
|
|
|
|
c.close()
|