hydrus/include/HydrusSessions.py

314 lines
11 KiB
Python
Raw Normal View History

2013-03-15 02:38:12 +00:00
import collections
2013-11-27 18:27:11 +00:00
import hashlib
2013-03-15 02:38:12 +00:00
import httplib
import HydrusConstants as HC
2013-07-24 20:26:00 +00:00
import HydrusExceptions
2013-03-15 02:38:12 +00:00
import os
import Queue
import re
import sqlite3
import sys
import threading
import time
import traceback
2014-05-28 21:03:24 +00:00
import urllib
2013-03-15 02:38:12 +00:00
import wx
import yaml
2013-12-04 22:44:16 +00:00
from twisted.internet.threads import deferToThread
2015-03-25 22:04:19 +00:00
import HydrusData
import HydrusGlobals
2013-03-15 02:38:12 +00:00
2013-12-04 22:44:16 +00:00
HYDRUS_SESSION_LIFETIME = 30 * 86400
2013-10-02 22:06:06 +00:00
2014-07-23 21:21:37 +00:00
class HydrusMessagingSessionManagerServer( object ):
2013-11-27 18:27:11 +00:00
def __init__( self ):
2015-03-25 22:04:19 +00:00
existing_sessions = wx.GetApp().Read( 'messaging_sessions' )
2013-11-27 18:27:11 +00:00
2014-09-10 22:37:38 +00:00
self._service_keys_to_sessions = collections.defaultdict( dict )
2013-11-27 18:27:11 +00:00
2014-08-27 22:15:22 +00:00
for ( service_key, session_tuples ) in existing_sessions:
2013-11-27 18:27:11 +00:00
2015-02-25 19:34:30 +00:00
self._service_keys_to_sessions[ service_key ] = { session_key : ( account, name, expires ) for ( session_key, account, name, expires ) in session_tuples }
2013-11-27 18:27:11 +00:00
2013-12-04 22:44:16 +00:00
self._lock = threading.Lock()
2013-11-27 18:27:11 +00:00
2014-08-27 22:15:22 +00:00
def GetIdentityAndName( self, service_key, session_key ):
2013-11-27 18:27:11 +00:00
2013-12-04 22:44:16 +00:00
with self._lock:
2013-11-27 18:27:11 +00:00
2014-09-10 22:37:38 +00:00
if session_key not in self._service_keys_to_sessions[ service_key ]: raise HydrusExceptions.SessionException( 'Did not find that session!' )
2013-12-04 22:44:16 +00:00
else:
2013-11-27 18:27:11 +00:00
2014-10-01 22:58:32 +00:00
( account, name, expires ) = self._service_keys_to_sessions[ service_key ][ session_key ]
2013-11-27 18:27:11 +00:00
2015-06-24 22:10:14 +00:00
if HydrusData.TimeHasPassed( expires ):
2013-12-04 22:44:16 +00:00
2014-09-10 22:37:38 +00:00
del self._service_keys_to_sessions[ service_key ][ session_key ]
2013-12-04 22:44:16 +00:00
2015-05-06 20:26:18 +00:00
raise HydrusExceptions.SessionException( 'Session expired! Try again!' )
2013-12-04 22:44:16 +00:00
2014-10-01 22:58:32 +00:00
return ( account.GetAccountKey(), name )
2013-11-27 18:27:11 +00:00
2014-08-27 22:15:22 +00:00
def AddSession( self, service_key, access_key, name ):
2013-11-27 18:27:11 +00:00
2015-07-01 22:02:07 +00:00
session_key = HydrusData.GenerateKey()
2013-11-27 18:27:11 +00:00
2015-03-25 22:04:19 +00:00
account_key = wx.GetApp().Read( 'account_key_from_access_key', service_key, access_key )
2013-11-27 18:27:11 +00:00
2015-03-25 22:04:19 +00:00
account = wx.GetApp().Read( 'account', service_key, account_key )
2013-11-27 18:27:11 +00:00
2015-03-25 22:04:19 +00:00
now = HydrusData.GetNow()
2013-11-27 18:27:11 +00:00
2014-10-01 22:58:32 +00:00
expires = now + HYDRUS_SESSION_LIFETIME
2013-11-27 18:27:11 +00:00
2014-10-01 22:58:32 +00:00
with self._lock: self._service_keys_to_sessions[ service_key ][ session_key ] = ( account, name, expires )
2013-11-27 18:27:11 +00:00
2015-03-25 22:04:19 +00:00
wx.GetApp().Write( 'messaging_session', service_key, session_key, account_key, name, expires )
2013-11-27 18:27:11 +00:00
return session_key
2014-07-23 21:21:37 +00:00
class HydrusSessionManagerClient( object ):
2013-03-15 02:38:12 +00:00
def __init__( self ):
2015-03-25 22:04:19 +00:00
existing_sessions = wx.GetApp().Read( 'hydrus_sessions' )
2013-03-15 02:38:12 +00:00
2014-10-01 22:58:32 +00:00
self._service_keys_to_sessions = { service_key : ( session_key, expires ) for ( service_key, session_key, expires ) in existing_sessions }
2013-03-15 02:38:12 +00:00
self._lock = threading.Lock()
2014-08-27 22:15:22 +00:00
def DeleteSessionKey( self, service_key ):
2013-03-15 02:38:12 +00:00
with self._lock:
2015-03-25 22:04:19 +00:00
wx.GetApp().Write( 'delete_hydrus_session_key', service_key )
2013-03-15 02:38:12 +00:00
2014-09-10 22:37:38 +00:00
if service_key in self._service_keys_to_sessions: del self._service_keys_to_sessions[ service_key ]
2013-03-15 02:38:12 +00:00
2014-08-27 22:15:22 +00:00
def GetSessionKey( self, service_key ):
2013-03-15 02:38:12 +00:00
2015-03-25 22:04:19 +00:00
now = HydrusData.GetNow()
2013-03-15 02:38:12 +00:00
with self._lock:
2014-09-10 22:37:38 +00:00
if service_key in self._service_keys_to_sessions:
2013-03-15 02:38:12 +00:00
2014-10-01 22:58:32 +00:00
( session_key, expires ) = self._service_keys_to_sessions[ service_key ]
2013-03-15 02:38:12 +00:00
2014-10-01 22:58:32 +00:00
if now + 600 > expires: del self._service_keys_to_sessions[ service_key ]
2013-03-15 02:38:12 +00:00
else: return session_key
# session key expired or not found
2015-07-01 22:02:07 +00:00
service = wx.GetApp().GetServicesManager().GetService( service_key )
2013-03-15 02:38:12 +00:00
2014-01-29 21:59:42 +00:00
( response, cookies ) = service.Request( HC.GET, 'session_key', return_cookies = True )
2013-03-15 02:38:12 +00:00
try: session_key = cookies[ 'session_key' ].decode( 'hex' )
except: raise Exception( 'Service did not return a session key!' )
2014-10-01 22:58:32 +00:00
expires = now + HYDRUS_SESSION_LIFETIME
2013-03-15 02:38:12 +00:00
2014-10-01 22:58:32 +00:00
self._service_keys_to_sessions[ service_key ] = ( session_key, expires )
2013-03-15 02:38:12 +00:00
2015-03-25 22:04:19 +00:00
wx.GetApp().Write( 'hydrus_session', service_key, session_key, expires )
2013-03-15 02:38:12 +00:00
return session_key
2014-07-23 21:21:37 +00:00
class HydrusSessionManagerServer( object ):
2013-03-15 02:38:12 +00:00
def __init__( self ):
self._lock = threading.Lock()
2014-08-06 20:29:17 +00:00
self.RefreshAllAccounts()
2015-03-25 22:04:19 +00:00
HydrusGlobals.pubsub.sub( self, 'RefreshAllAccounts', 'update_all_session_accounts' )
2013-11-06 18:22:07 +00:00
2013-03-15 02:38:12 +00:00
2014-08-27 22:15:22 +00:00
def AddSession( self, service_key, access_key ):
2013-10-30 22:28:06 +00:00
2013-11-06 18:22:07 +00:00
with self._lock:
2015-03-25 22:04:19 +00:00
account_key = wx.GetApp().Read( 'account_key_from_access_key', service_key, access_key )
2013-10-30 22:28:06 +00:00
2014-10-01 22:58:32 +00:00
if account_key not in self._account_keys_to_accounts:
2014-09-10 22:37:38 +00:00
2015-03-25 22:04:19 +00:00
account = wx.GetApp().Read( 'account', account_key )
2014-07-23 21:21:37 +00:00
2014-10-01 22:58:32 +00:00
self._account_keys_to_accounts[ account_key ] = account
2014-07-23 21:21:37 +00:00
2014-10-01 22:58:32 +00:00
account = self._account_keys_to_accounts[ account_key ]
2014-07-23 21:21:37 +00:00
2015-07-01 22:02:07 +00:00
session_key = HydrusData.GenerateKey()
2013-10-30 22:28:06 +00:00
2014-10-01 22:58:32 +00:00
self._account_keys_to_session_keys[ account_key ].add( session_key )
2013-11-06 18:22:07 +00:00
2015-03-25 22:04:19 +00:00
now = HydrusData.GetNow()
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
expires = now + HYDRUS_SESSION_LIFETIME
2013-11-06 18:22:07 +00:00
2015-03-25 22:04:19 +00:00
wx.GetApp().Write( 'session', session_key, service_key, account_key, expires )
2013-10-30 22:28:06 +00:00
2014-10-01 22:58:32 +00:00
self._service_keys_to_sessions[ service_key ][ session_key ] = ( account, expires )
2013-11-06 18:22:07 +00:00
2013-10-02 22:06:06 +00:00
2014-10-01 22:58:32 +00:00
return ( session_key, expires )
2013-10-02 22:06:06 +00:00
2014-08-27 22:15:22 +00:00
def GetAccount( self, service_key, session_key ):
2013-03-15 02:38:12 +00:00
2013-10-02 22:06:06 +00:00
with self._lock:
2014-09-10 22:37:38 +00:00
service_sessions = self._service_keys_to_sessions[ service_key ]
2014-05-28 21:03:24 +00:00
2014-09-10 22:37:38 +00:00
if session_key in service_sessions:
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
( account, expires ) = service_sessions[ session_key ]
2013-11-06 18:22:07 +00:00
2015-06-24 22:10:14 +00:00
if HydrusData.TimeHasPassed( expires ): del service_sessions[ session_key ]
2013-11-06 18:22:07 +00:00
else: return account
2015-05-06 20:26:18 +00:00
raise HydrusExceptions.SessionException( 'Did not find that session! Try again!' )
2013-10-02 22:06:06 +00:00
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
def RefreshAccounts( self, service_key, account_keys ):
2013-11-06 18:22:07 +00:00
with self._lock:
2014-10-01 22:58:32 +00:00
for account_key in account_keys:
2013-11-06 18:22:07 +00:00
2015-03-25 22:04:19 +00:00
account = wx.GetApp().Read( 'account', account_key )
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
self._account_keys_to_accounts[ account_key ] = account
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
if account_key in self._account_keys_to_session_keys:
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
session_keys = self._account_keys_to_session_keys[ account_key ]
2013-11-06 18:22:07 +00:00
for session_key in session_keys:
2014-10-01 22:58:32 +00:00
( old_account, expires ) = self._service_keys_to_sessions[ service_key ][ session_key ]
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
self._service_keys_to_sessions[ service_key ][ session_key ] = ( account, expires )
2013-11-06 18:22:07 +00:00
2013-03-15 02:38:12 +00:00
2013-11-06 18:22:07 +00:00
def RefreshAllAccounts( self ):
2014-08-06 20:29:17 +00:00
with self._lock:
2013-11-06 18:22:07 +00:00
2014-09-10 22:37:38 +00:00
self._service_keys_to_sessions = collections.defaultdict( dict )
2013-11-06 18:22:07 +00:00
2015-03-25 22:04:19 +00:00
self._account_keys_to_session_keys = HydrusData.default_dict_set()
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
self._account_keys_to_accounts = {}
2014-08-06 20:29:17 +00:00
2014-09-10 22:37:38 +00:00
#
2014-08-06 20:29:17 +00:00
2015-03-25 22:04:19 +00:00
existing_sessions = wx.GetApp().Read( 'sessions' )
2014-09-10 22:37:38 +00:00
2014-10-01 22:58:32 +00:00
for ( session_key, service_key, account, expires ) in existing_sessions:
account_key = account.GetAccountKey()
2014-08-06 20:29:17 +00:00
2014-10-01 22:58:32 +00:00
self._service_keys_to_sessions[ service_key ][ session_key ] = ( account, expires )
2014-08-06 20:29:17 +00:00
2014-10-01 22:58:32 +00:00
self._account_keys_to_session_keys[ account_key ].add( session_key )
2014-08-06 20:29:17 +00:00
2014-10-01 22:58:32 +00:00
self._account_keys_to_accounts[ account_key ] = account
2014-08-06 20:29:17 +00:00
2013-11-06 18:22:07 +00:00
2014-07-23 21:21:37 +00:00
class WebSessionManagerClient( object ):
2013-11-06 18:22:07 +00:00
def __init__( self ):
2015-03-25 22:04:19 +00:00
existing_sessions = wx.GetApp().Read( 'web_sessions' )
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
self._names_to_sessions = { name : ( cookies, expires ) for ( name, cookies, expires ) in existing_sessions }
2013-11-06 18:22:07 +00:00
self._lock = threading.Lock()
def GetCookies( self, name ):
2013-03-15 02:38:12 +00:00
2015-03-25 22:04:19 +00:00
now = HydrusData.GetNow()
2013-03-15 02:38:12 +00:00
with self._lock:
2014-09-10 22:37:38 +00:00
if name in self._names_to_sessions:
2013-03-15 02:38:12 +00:00
2014-10-01 22:58:32 +00:00
( cookies, expires ) = self._names_to_sessions[ name ]
2013-03-15 02:38:12 +00:00
2014-10-01 22:58:32 +00:00
if now + 300 > expires: del self._names_to_sessions[ name ]
2013-11-06 18:22:07 +00:00
else: return cookies
2013-03-15 02:38:12 +00:00
2013-11-06 18:22:07 +00:00
# name not found, or expired
if name == 'hentai foundry':
2015-04-01 20:44:54 +00:00
( response_gumpf, cookies ) = wx.GetApp().DoHTTP( HC.GET, 'http://www.hentai-foundry.com/?enterAgree=1', return_cookies = True )
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
expires = now + 60 * 60
2013-11-06 18:22:07 +00:00
elif name == 'pixiv':
2015-03-25 22:04:19 +00:00
( id, password ) = wx.GetApp().Read( 'pixiv_account' )
2013-11-06 18:22:07 +00:00
if id == '' and password == '':
raise Exception( 'You need to set up your pixiv credentials in services->manage pixiv account.' )
form_fields = {}
form_fields[ 'mode' ] = 'login'
form_fields[ 'pixiv_id' ] = id
form_fields[ 'pass' ] = password
form_fields[ 'skip' ] = '1'
body = urllib.urlencode( form_fields )
headers = {}
headers[ 'Content-Type' ] = 'application/x-www-form-urlencoded'
2015-04-01 20:44:54 +00:00
( response_gumpf, cookies ) = wx.GetApp().DoHTTP( HC.POST, 'http://www.pixiv.net/login.php', request_headers = headers, body = body, return_cookies = True )
2013-11-06 18:22:07 +00:00
# _ only given to logged in php sessions
2014-01-29 21:59:42 +00:00
if 'PHPSESSID' not in cookies or '_' not in cookies[ 'PHPSESSID' ]: raise Exception( 'Pixiv login credentials not accepted!' )
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
expires = now + 30 * 86400
2013-11-06 18:22:07 +00:00
2014-10-01 22:58:32 +00:00
self._names_to_sessions[ name ] = ( cookies, expires )
2013-11-06 18:22:07 +00:00
2015-03-25 22:04:19 +00:00
wx.GetApp().Write( 'web_session', name, cookies, expires )
2013-11-06 18:22:07 +00:00
return cookies
2013-03-15 02:38:12 +00:00