mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-15 09:42:57 -07:00
Check for pycryptodome library on import
This commit is contained in:
parent
2952d1360a
commit
d031d74bc6
1 changed files with 52 additions and 30 deletions
|
@ -17,10 +17,6 @@ import base64
|
||||||
import bleach
|
import bleach
|
||||||
import json
|
import json
|
||||||
import cherrypy
|
import cherrypy
|
||||||
from Cryptodome.Protocol.KDF import PBKDF2
|
|
||||||
from Cryptodome.Cipher import AES
|
|
||||||
from Cryptodome.Random import get_random_bytes
|
|
||||||
from Cryptodome.Hash import HMAC, SHA256
|
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
import email.utils
|
import email.utils
|
||||||
|
@ -39,6 +35,22 @@ import urllib2
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
try:
|
||||||
|
from Cryptodome.Protocol.KDF import PBKDF2
|
||||||
|
from Cryptodome.Cipher import AES
|
||||||
|
from Cryptodome.Random import get_random_bytes
|
||||||
|
from Cryptodome.Hash import HMAC, SHA256
|
||||||
|
CRYPTODOME = True
|
||||||
|
except ImportError:
|
||||||
|
try:
|
||||||
|
from Crypto.Protocol.KDF import PBKDF2
|
||||||
|
from Crypto.Cipher import AES
|
||||||
|
from Crypto.Random import get_random_bytes
|
||||||
|
from Crypto.Hash import HMAC, SHA256
|
||||||
|
CRYPTODOME = True
|
||||||
|
except ImportError:
|
||||||
|
CRYPTODOME = False
|
||||||
|
|
||||||
import gntp.notifier
|
import gntp.notifier
|
||||||
import facebook
|
import facebook
|
||||||
import twitter
|
import twitter
|
||||||
|
@ -689,45 +701,55 @@ class ANDROIDAPP(Notifier):
|
||||||
if not subject or not body:
|
if not subject or not body:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Data to encrypt
|
|
||||||
plaintext_data = {'subject': subject.encode("utf-8"),
|
plaintext_data = {'subject': subject.encode("utf-8"),
|
||||||
'body': body.encode("utf-8")}
|
'body': body.encode("utf-8")}
|
||||||
|
|
||||||
logger.debug("Plaintext data: {}".format(plaintext_data))
|
logger.debug("Plaintext data: {}".format(plaintext_data))
|
||||||
|
|
||||||
# Key generation
|
if CRYPTODOME:
|
||||||
salt = get_random_bytes(16)
|
# Key generation
|
||||||
passphrase = plexpy.CONFIG.API_KEY
|
salt = get_random_bytes(16)
|
||||||
key_length = 32 # AES256
|
passphrase = plexpy.CONFIG.API_KEY
|
||||||
iterations = 1000
|
key_length = 32 # AES256
|
||||||
key = PBKDF2(passphrase, salt, dkLen=key_length, count=iterations,
|
iterations = 1000
|
||||||
prf=lambda p, s: HMAC.new(p, s, SHA256).digest())
|
key = PBKDF2(passphrase, salt, dkLen=key_length, count=iterations,
|
||||||
|
prf=lambda p, s: HMAC.new(p, s, SHA256).digest())
|
||||||
|
|
||||||
logger.debug("Encryption key (base64): {}".format(base64.b64encode(key)))
|
logger.debug("Encryption key (base64): {}".format(base64.b64encode(key)))
|
||||||
|
|
||||||
# Encrypt using AES GCM
|
# Encrypt using AES GCM
|
||||||
nonce = get_random_bytes(16)
|
nonce = get_random_bytes(16)
|
||||||
cipher = AES.new(key, AES.MODE_GCM, nonce)
|
cipher = AES.new(key, AES.MODE_GCM, nonce)
|
||||||
encrypted_data, gcm_tag = cipher.encrypt_and_digest(json.dumps(plaintext_data))
|
encrypted_data, gcm_tag = cipher.encrypt_and_digest(json.dumps(plaintext_data))
|
||||||
|
|
||||||
logger.debug("Encrypted data (base64): {}".format(base64.b64encode(encrypted_data)))
|
logger.debug("Encrypted data (base64): {}".format(base64.b64encode(encrypted_data)))
|
||||||
logger.debug("GCM tag (base64): {}".format(base64.b64encode(gcm_tag)))
|
logger.debug("GCM tag (base64): {}".format(base64.b64encode(gcm_tag)))
|
||||||
logger.debug("Nonce (base64): {}".format(base64.b64encode(nonce)))
|
logger.debug("Nonce (base64): {}".format(base64.b64encode(nonce)))
|
||||||
logger.debug("Salt (base64): {}".format(base64.b64encode(salt)))
|
logger.debug("Salt (base64): {}".format(base64.b64encode(salt)))
|
||||||
|
|
||||||
headers = {'Content-Type': 'application/json'}
|
payload = {'app_id': self.ONESIGNAL_APP_ID,
|
||||||
|
'include_player_ids': [self.config['device_id']],
|
||||||
|
'contents': {'en': 'PlexPy Notification'},
|
||||||
|
'data': {'cipher_text': base64.b64encode(encrypted_data),
|
||||||
|
'gcm_tag': base64.b64encode(gcm_tag),
|
||||||
|
'nonce': base64.b64encode(nonce),
|
||||||
|
'salt': base64.b64encode(salt)},
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
logger.warn(u"PlexPy Notifiers :: PyCryptodome library is missing. "
|
||||||
|
"Install this library to encrypt Android app notifications. "
|
||||||
|
"Android app notifications will be sent unecrypted.")
|
||||||
|
|
||||||
payload = {'app_id': self.ONESIGNAL_APP_ID,
|
payload = {'app_id': self.ONESIGNAL_APP_ID,
|
||||||
'include_player_ids': [self.config['device_id']],
|
'include_player_ids': [self.config['device_id']],
|
||||||
'contents': {'en': 'PlexPy Notification'},
|
'contents': {'en': 'PlexPy Notification'},
|
||||||
'data': {'cipher_text': base64.b64encode(encrypted_data),
|
'data': plaintext_data,
|
||||||
'gcm_tag': base64.b64encode(gcm_tag),
|
}
|
||||||
'nonce': base64.b64encode(nonce),
|
|
||||||
'salt': base64.b64encode(salt)}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug("OneSignal payload: {}".format(payload))
|
logger.debug("OneSignal payload: {}".format(payload))
|
||||||
|
|
||||||
|
headers = {'Content-Type': 'application/json'}
|
||||||
|
|
||||||
r = requests.post("https://onesignal.com/api/v1/notifications", headers=headers, json=payload)
|
r = requests.post("https://onesignal.com/api/v1/notifications", headers=headers, json=payload)
|
||||||
request_status = r.status_code
|
request_status = r.status_code
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue