2017-06-07 13:48:54 +03:00
|
|
|
import json
|
2016-09-16 14:35:14 +03:00
|
|
|
import os
|
2017-12-26 18:45:47 +03:00
|
|
|
import sqlite3
|
|
|
|
from base64 import b64decode
|
2016-11-30 00:29:42 +03:00
|
|
|
from os.path import isfile as file_exists
|
2018-01-26 11:59:49 +03:00
|
|
|
from threading import Lock, RLock
|
2016-11-30 00:29:42 +03:00
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
from .memory import MemorySession, _SentFileType
|
|
|
|
from ..crypto import AuthKey
|
|
|
|
from ..tl.types import (
|
2018-01-18 11:52:39 +03:00
|
|
|
InputPhoto, InputDocument
|
2017-12-27 02:50:09 +03:00
|
|
|
)
|
2016-08-28 15:16:52 +03:00
|
|
|
|
2017-12-26 18:45:47 +03:00
|
|
|
EXTENSION = '.session'
|
2018-01-18 11:52:39 +03:00
|
|
|
CURRENT_VERSION = 3 # database version
|
|
|
|
|
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
class SQLiteSession(MemorySession):
|
2017-06-07 13:48:54 +03:00
|
|
|
"""This session contains the required information to login into your
|
|
|
|
Telegram account. NEVER give the saved JSON file to anyone, since
|
|
|
|
they would gain instant access to all your messages and contacts.
|
|
|
|
|
|
|
|
If you think the session has been compromised, close all the sessions
|
|
|
|
through an official Telegram client to revoke the authorization.
|
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
|
2018-03-02 13:36:39 +03:00
|
|
|
def __init__(self, session_id=None):
|
2018-03-02 00:34:32 +03:00
|
|
|
super().__init__()
|
2017-06-17 23:04:29 +03:00
|
|
|
"""session_user_id should either be a string or another Session.
|
|
|
|
Note that if another session is given, only parameters like
|
|
|
|
those required to init a connection will be copied.
|
|
|
|
"""
|
2017-06-10 14:15:04 +03:00
|
|
|
# These values will NOT be saved
|
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
|
|
|
|
2017-12-26 18:59:30 +03:00
|
|
|
# Cross-thread safety
|
|
|
|
self._seq_no_lock = Lock()
|
|
|
|
self._msg_id_lock = Lock()
|
2018-01-26 11:59:49 +03:00
|
|
|
self._db_lock = RLock()
|
2017-12-26 18:59:30 +03:00
|
|
|
|
2017-12-26 18:45:47 +03:00
|
|
|
# Migrating from .json -> SQL
|
2017-12-27 02:50:09 +03:00
|
|
|
entities = self._check_migrate_json()
|
2017-12-26 18:45:47 +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]
|
|
|
|
if version != CURRENT_VERSION:
|
|
|
|
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()
|
|
|
|
|
|
|
|
# These values will be saved
|
|
|
|
c.execute('select * from sessions')
|
2017-12-28 16:58:42 +03:00
|
|
|
tuple_ = c.fetchone()
|
|
|
|
if tuple_:
|
|
|
|
self._dc_id, self._server_address, self._port, key, = tuple_
|
|
|
|
self._auth_key = AuthKey(data=key)
|
2017-12-26 18:45:47 +03:00
|
|
|
|
|
|
|
c.close()
|
|
|
|
else:
|
|
|
|
# Tables don't exist, create new ones
|
2018-01-06 21:35:24 +03:00
|
|
|
self._create_table(
|
|
|
|
c,
|
|
|
|
"version (version integer primary key)"
|
|
|
|
,
|
|
|
|
"""sessions (
|
2017-12-28 03:04:11 +03:00
|
|
|
dc_id integer primary key,
|
2017-12-26 18:45:47 +03:00
|
|
|
server_address text,
|
|
|
|
port integer,
|
2017-12-28 03:04:11 +03:00
|
|
|
auth_key blob
|
2018-01-06 21:35:24 +03:00
|
|
|
)"""
|
|
|
|
,
|
|
|
|
"""entities (
|
2017-12-27 02:50:09 +03:00
|
|
|
id integer primary key,
|
|
|
|
hash integer not null,
|
2017-12-26 18:45:47 +03:00
|
|
|
username text,
|
|
|
|
phone integer,
|
|
|
|
name text
|
2018-01-06 21:35:24 +03:00
|
|
|
)"""
|
|
|
|
,
|
|
|
|
"""sent_files (
|
2018-01-05 17:33:25 +03:00
|
|
|
md5_digest blob,
|
|
|
|
file_size integer,
|
2018-01-18 11:52:39 +03:00
|
|
|
type integer,
|
|
|
|
id integer,
|
|
|
|
hash integer,
|
|
|
|
primary key(md5_digest, file_size, type)
|
2018-01-06 21:35:24 +03:00
|
|
|
)"""
|
2018-01-05 17:33:25 +03:00
|
|
|
)
|
2018-01-06 21:35:24 +03:00
|
|
|
c.execute("insert into version values (?)", (CURRENT_VERSION,))
|
2017-12-27 02:50:09 +03:00
|
|
|
# Migrating from JSON -> new table and may have entities
|
|
|
|
if entities:
|
|
|
|
c.executemany(
|
|
|
|
'insert or replace into entities values (?,?,?,?,?)',
|
|
|
|
entities
|
|
|
|
)
|
2018-01-31 23:01:53 +03:00
|
|
|
self._update_session_table()
|
2017-12-26 18:45:47 +03:00
|
|
|
c.close()
|
|
|
|
self.save()
|
2017-06-10 14:15:04 +03:00
|
|
|
|
2018-03-02 14:20:11 +03:00
|
|
|
def clone(self, to_instance=None):
|
|
|
|
cloned = super().clone(to_instance)
|
2018-03-02 13:36:39 +03:00
|
|
|
cloned.save_entities = self.save_entities
|
|
|
|
return cloned
|
2018-03-02 00:34:32 +03:00
|
|
|
|
2017-12-26 18:45:47 +03:00
|
|
|
def _check_migrate_json(self):
|
|
|
|
if file_exists(self.filename):
|
|
|
|
try:
|
|
|
|
with open(self.filename, encoding='utf-8') as f:
|
|
|
|
data = json.load(f)
|
2017-12-27 02:50:09 +03:00
|
|
|
self.delete() # Delete JSON file to create database
|
|
|
|
|
2017-12-26 18:45:47 +03:00
|
|
|
self._port = data.get('port', self._port)
|
|
|
|
self._server_address = \
|
|
|
|
data.get('server_address', self._server_address)
|
|
|
|
|
|
|
|
if data.get('auth_key_data', None) is not None:
|
|
|
|
key = b64decode(data['auth_key_data'])
|
|
|
|
self._auth_key = AuthKey(data=key)
|
|
|
|
|
2017-12-27 02:50:09 +03:00
|
|
|
rows = []
|
|
|
|
for p_id, p_hash in data.get('entities', []):
|
2018-01-10 12:46:43 +03:00
|
|
|
if p_hash is not None:
|
|
|
|
rows.append((p_id, p_hash, None, None, None))
|
2017-12-27 02:50:09 +03:00
|
|
|
return rows
|
2017-12-28 19:06:14 +03:00
|
|
|
except UnicodeDecodeError:
|
2017-12-27 02:50:09 +03:00
|
|
|
return [] # No entities
|
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-01-18 11:52:39 +03:00
|
|
|
# old == 1 doesn't have the old sent_files so no need to drop
|
|
|
|
if old == 2:
|
|
|
|
# Old cache from old sent_files lasts then a day anyway, drop
|
|
|
|
c.execute('drop table sent_files')
|
|
|
|
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)
|
|
|
|
)""")
|
2018-01-06 21:35:24 +03:00
|
|
|
c.close()
|
|
|
|
|
2018-01-18 11:52:39 +03:00
|
|
|
@staticmethod
|
|
|
|
def _create_table(c, *definitions):
|
2018-01-06 21:35:24 +03:00
|
|
|
"""
|
|
|
|
Creates a table given its definition 'name (columns).
|
|
|
|
If the sqlite version is >= 3.8.2, it will use "without rowid".
|
|
|
|
See http://www.sqlite.org/releaselog/3_8_2.html.
|
|
|
|
"""
|
|
|
|
required = (3, 8, 2)
|
|
|
|
sqlite_v = tuple(int(x) for x in sqlite3.sqlite_version.split('.'))
|
|
|
|
extra = ' without rowid' if sqlite_v >= required else ''
|
|
|
|
for definition in definitions:
|
|
|
|
c.execute('create table {}{}'.format(definition, extra))
|
2017-12-26 18:45:47 +03:00
|
|
|
|
|
|
|
# Data from sessions should be kept as properties
|
|
|
|
# not to fetch the database every time we need it
|
2017-12-28 03:04:11 +03:00
|
|
|
def set_dc(self, dc_id, server_address, port):
|
2018-03-02 14:20:11 +03:00
|
|
|
super().set_dc(dc_id, server_address, port)
|
2017-12-28 03:04:11 +03:00
|
|
|
self._update_session_table()
|
|
|
|
|
2017-12-29 21:41:12 +03:00
|
|
|
# Fetch the auth_key corresponding to this data center
|
2018-01-26 11:59:49 +03:00
|
|
|
c = self._cursor()
|
2017-12-29 21:41:12 +03:00
|
|
|
c.execute('select auth_key from sessions')
|
|
|
|
tuple_ = c.fetchone()
|
2018-02-23 15:13:39 +03:00
|
|
|
if tuple_ and tuple_[0]:
|
2017-12-29 21:41:12 +03:00
|
|
|
self._auth_key = AuthKey(data=tuple_[0])
|
|
|
|
else:
|
|
|
|
self._auth_key = None
|
|
|
|
c.close()
|
|
|
|
|
2018-03-02 19:39:04 +03:00
|
|
|
@MemorySession.auth_key.setter
|
2017-12-26 18:45:47 +03:00
|
|
|
def auth_key(self, value):
|
|
|
|
self._auth_key = value
|
|
|
|
self._update_session_table()
|
|
|
|
|
|
|
|
def _update_session_table(self):
|
|
|
|
with self._db_lock:
|
2018-01-26 11:59:49 +03:00
|
|
|
c = self._cursor()
|
2018-01-07 02:38:30 +03:00
|
|
|
# While we can save multiple rows into the sessions table
|
|
|
|
# currently we only want to keep ONE as the tables don't
|
|
|
|
# tell us which auth_key's are usable and will work. Needs
|
|
|
|
# some more work before being able to save auth_key's for
|
|
|
|
# multiple DCs. Probably done differently.
|
|
|
|
c.execute('delete from sessions')
|
2017-12-29 21:41:12 +03:00
|
|
|
c.execute('insert or replace into sessions values (?,?,?,?)', (
|
2018-03-03 16:59:30 +03:00
|
|
|
self._dc_id,
|
2017-12-26 18:45:47 +03:00
|
|
|
self._server_address,
|
|
|
|
self._port,
|
2017-12-28 03:04:11 +03:00
|
|
|
self._auth_key.key if self._auth_key else b''
|
2017-12-26 18:45:47 +03:00
|
|
|
))
|
|
|
|
c.close()
|
2017-06-07 13:48:54 +03:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
"""Saves the current session object as session_user_id.session"""
|
2017-12-26 18:45:47 +03:00
|
|
|
with self._db_lock:
|
|
|
|
self._conn.commit()
|
2017-06-07 13:48:54 +03:00
|
|
|
|
2018-01-26 11:59:49 +03:00
|
|
|
def _cursor(self):
|
|
|
|
"""Asserts that the connection is open and returns a cursor"""
|
|
|
|
with self._db_lock:
|
|
|
|
if self._conn is None:
|
|
|
|
self._conn = sqlite3.connect(self.filename,
|
|
|
|
check_same_thread=False)
|
|
|
|
return self._conn.cursor()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Closes the connection unless we're working in-memory"""
|
|
|
|
if self.filename != ':memory:':
|
|
|
|
with self._db_lock:
|
|
|
|
if self._conn is not None:
|
|
|
|
self._conn.close()
|
|
|
|
self._conn = None
|
|
|
|
|
2017-06-07 13:48:54 +03:00
|
|
|
def delete(self):
|
|
|
|
"""Deletes the current session file"""
|
2017-12-26 18:45:47 +03:00
|
|
|
if self.filename == ':memory:':
|
|
|
|
return True
|
2017-06-07 13:48:54 +03:00
|
|
|
try:
|
2017-12-26 18:45:47 +03:00
|
|
|
os.remove(self.filename)
|
2017-06-07 13:48:54 +03:00
|
|
|
return True
|
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
@classmethod
|
|
|
|
def list_sessions(cls):
|
2017-06-14 14:56:42 +03:00
|
|
|
"""Lists all the sessions of the users who have ever connected
|
|
|
|
using this client and never logged out
|
|
|
|
"""
|
|
|
|
return [os.path.splitext(os.path.basename(f))[0]
|
2017-12-26 18:45:47 +03:00
|
|
|
for f in os.listdir('.') if f.endswith(EXTENSION)]
|
2017-06-07 13:48:54 +03:00
|
|
|
|
2017-12-27 02:50:09 +03:00
|
|
|
# Entity processing
|
|
|
|
|
|
|
|
def process_entities(self, tlo):
|
|
|
|
"""Processes all the found entities on the given TLObject,
|
|
|
|
unless .enabled is False.
|
|
|
|
|
|
|
|
Returns True if new input entities were added.
|
|
|
|
"""
|
|
|
|
if not self.save_entities:
|
|
|
|
return
|
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
rows = self._entities_to_rows(tlo)
|
2017-12-27 02:50:09 +03:00
|
|
|
if not rows:
|
|
|
|
return
|
|
|
|
|
|
|
|
with self._db_lock:
|
2018-01-26 11:59:49 +03:00
|
|
|
self._cursor().executemany(
|
2017-12-27 02:50:09 +03:00
|
|
|
'insert or replace into entities values (?,?,?,?,?)', rows
|
|
|
|
)
|
2018-01-26 11:59:49 +03:00
|
|
|
self.save()
|
2017-12-27 02:50:09 +03:00
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
def _fetchone_entity(self, query, args):
|
|
|
|
c = self._cursor()
|
|
|
|
c.execute(query, args)
|
|
|
|
return c.fetchone()
|
2017-12-27 02:50:09 +03:00
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
def get_entity_rows_by_phone(self, phone):
|
|
|
|
return self._fetchone_entity(
|
|
|
|
'select id, hash from entities where phone=?', (phone,))
|
2017-12-27 13:54:08 +03:00
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
def get_entity_rows_by_username(self, username):
|
2018-03-02 14:20:11 +03:00
|
|
|
return self._fetchone_entity(
|
2018-03-03 14:51:35 +03:00
|
|
|
'select id, hash from entities where username=?', (username,))
|
2017-12-27 02:50:09 +03:00
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
def get_entity_rows_by_name(self, name):
|
2018-03-02 14:20:11 +03:00
|
|
|
return self._fetchone_entity(
|
2018-03-03 14:51:35 +03:00
|
|
|
'select id, hash from entities where name=?', (name,))
|
2017-12-27 02:50:09 +03:00
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
def get_entity_rows_by_id(self, id):
|
2018-03-02 14:20:11 +03:00
|
|
|
return self._fetchone_entity(
|
2018-03-03 14:51:35 +03:00
|
|
|
'select id, hash from entities where id=?', (id,))
|
2018-01-05 17:33:25 +03:00
|
|
|
|
|
|
|
# File processing
|
|
|
|
|
2018-01-18 11:52:39 +03:00
|
|
|
def get_file(self, md5_digest, file_size, cls):
|
2018-01-26 11:59:49 +03:00
|
|
|
tuple_ = self._cursor().execute(
|
2018-01-18 11:52:39 +03:00
|
|
|
'select id, hash from sent_files '
|
|
|
|
'where md5_digest = ? and file_size = ? and type = ?',
|
2018-01-18 21:35:46 +03:00
|
|
|
(md5_digest, file_size, _SentFileType.from_type(cls).value)
|
2018-01-05 17:33:25 +03:00
|
|
|
).fetchone()
|
2018-01-18 11:52:39 +03:00
|
|
|
if tuple_:
|
|
|
|
# Both allowed classes have (id, access_hash) as parameters
|
|
|
|
return cls(tuple_[0], tuple_[1])
|
2018-01-05 17:33:25 +03:00
|
|
|
|
2018-01-18 11:52:39 +03:00
|
|
|
def cache_file(self, md5_digest, file_size, instance):
|
|
|
|
if not isinstance(instance, (InputDocument, InputPhoto)):
|
|
|
|
raise TypeError('Cannot cache %s instance' % type(instance))
|
2018-01-15 14:36:46 +03:00
|
|
|
|
|
|
|
with self._db_lock:
|
2018-01-26 11:59:49 +03:00
|
|
|
self._cursor().execute(
|
2018-01-18 21:36:47 +03:00
|
|
|
'insert or replace into sent_files values (?,?,?,?,?)', (
|
2018-01-18 11:52:39 +03:00
|
|
|
md5_digest, file_size,
|
2018-01-18 21:35:46 +03:00
|
|
|
_SentFileType.from_type(type(instance)).value,
|
2018-01-18 11:52:39 +03:00
|
|
|
instance.id, instance.access_hash
|
|
|
|
))
|
2018-01-26 11:59:49 +03:00
|
|
|
self.save()
|