diff --git a/server/helper.py b/server/helper.py index 1663fc5a..54140b21 100755 --- a/server/helper.py +++ b/server/helper.py @@ -860,6 +860,7 @@ def collect_lang_strings(json, pref, stringSqlParams): # return decrypted_data # pycryptodome ------------------------------------------------------------------------- + def prepare_key(encryption_key): key = hashlib.sha256(encryption_key.encode()).digest() return key diff --git a/server/plugin.py b/server/plugin.py index d34ddce9..ecba96fc 100755 --- a/server/plugin.py +++ b/server/plugin.py @@ -11,7 +11,7 @@ from collections import namedtuple import conf from const import pluginsPath, logPath, applicationPath, reportTemplatesPath from logger import mylog -from helper import timeNowTZ, updateState, get_file_content, write_file, get_setting, get_setting_value +from helper import timeNowTZ, updateState, get_file_content, write_file, get_setting, get_setting_value, decrypt_data from api import update_api from plugin_utils import logEventStatusCounts, get_plugin_string, get_plugin_setting_obj, print_plugin_info, list_to_csv, combine_plugin_objects, resolve_wildcards_arr, handle_empty, custom_plugin_decoder from notification import Notification_obj @@ -225,47 +225,86 @@ def execute_plugin(db, all_plugins, plugin, pluginsState = plugins_state() ): newLines = [] # Create the file path - file_path = os.path.join(pluginsPath, plugin["code_name"], 'last_result.log') + file_dir = os.path.join(pluginsPath, plugin["code_name"]) + file_prefix = 'last_result' - # Check if the file exists - if os.path.exists(file_path): - # File exists, open it and read its contents - with open(file_path, 'r+') as f: - newLines = f.read().split('\n') + # key to decrypt data if available + encryption_key = get_setting_value('SYNC_encryption_key') - # if the script produced some outpout, clean it up to ensure it's the correct format - # cleanup - select only lines containing a separator to filter out unnecessary data - newLines = list(filter(lambda x: '|' in x, newLines)) + # Check for files starting with the specified prefix + matching_files = [f for f in os.listdir(file_dir) if f.startswith(file_prefix)] + + for filename in matching_files: + # Create the full file path + file_path = os.path.join(file_dir, filename) - for line in newLines: - columns = line.split("|") - # There has to be always 9 columns - if len(columns) == 9: - # Create a tuple containing values to be inserted into the database. - # Each value corresponds to a column in the table in the order of the columns. - # must match the Plugins_Objects and Plugins_Events database tables and can be used as input for the plugin_object_class. - sqlParams.append( - ( - 0, # "Index" placeholder - plugin["unique_prefix"], # "Plugin" column value from the plugin dictionary - columns[0], # "Object_PrimaryID" value from columns list - columns[1], # "Object_SecondaryID" value from columns list - 'null', # Placeholder for "DateTimeCreated" column - columns[2], # "DateTimeChanged" value from columns list - columns[3], # "Watched_Value1" value from columns list - columns[4], # "Watched_Value2" value from columns list - columns[5], # "Watched_Value3" value from columns list - columns[6], # "Watched_Value4" value from columns list - 'not-processed', # "Status" column (placeholder) - columns[7], # "Extra" value from columns list - 'null', # Placeholder for "UserData" column - columns[8] # "ForeignKey" value from columns list - ) - ) + # Check if the file exists + if os.path.exists(file_path): + + # Check if the file name contains "encoded" + if '.encoded.' in filename and encryption_key != '': + + # Decrypt the entire file + with open(file_path, 'r+') as f: + encrypted_data = f.read() + decrypted_data = decrypt_data(encrypted_data, encryption_key) + + # Write the decrypted data back to the file + f.seek(0) + f.write(decrypted_data) + f.truncate() + + # Rename the file + new_filename = filename.replace('.encoded.', '.decoded.') + os.rename(file_path, os.path.join(file_dir, new_filename)) + + elif filename == 'last_result.log' : + new_filename = filename else: - mylog('none', ['[Plugins] Skipped invalid line in the output: ', line]) - else: - mylog('debug', [f'[Plugins] The file {file_path} does not exist']) + # skipping decoded and other files + continue + + # Open the decrypted file and process its contents + with open(os.path.join(file_dir, new_filename), 'r') as f: + newLines = f.read().split('\n') + + # if the script produced some outpout, clean it up to ensure it's the correct format + # cleanup - select only lines containing a separator to filter out unnecessary data + newLines = list(filter(lambda x: '|' in x, newLines)) + + for line in newLines: + columns = line.split("|") + # There has to be always 9 columns + if len(columns) == 9: + # Create a tuple containing values to be inserted into the database. + # Each value corresponds to a column in the table in the order of the columns. + # must match the Plugins_Objects and Plugins_Events database tables and can be used as input for the plugin_object_class. + sqlParams.append( + ( + 0, # "Index" placeholder + plugin["unique_prefix"], # "Plugin" column value from the plugin dictionary + columns[0], # "Object_PrimaryID" value from columns list + columns[1], # "Object_SecondaryID" value from columns list + 'null', # Placeholder for "DateTimeCreated" column + columns[2], # "DateTimeChanged" value from columns list + columns[3], # "Watched_Value1" value from columns list + columns[4], # "Watched_Value2" value from columns list + columns[5], # "Watched_Value3" value from columns list + columns[6], # "Watched_Value4" value from columns list + 'not-processed', # "Status" column (placeholder) + columns[7], # "Extra" value from columns list + 'null', # Placeholder for "UserData" column + columns[8] # "ForeignKey" value from columns list + ) + ) + else: + mylog('none', ['[Plugins] Skipped invalid line in the output: ', line]) + else: + mylog('debug', [f'[Plugins] The file {file_path} does not exist']) + + # TODO: delete processed files + # os.rename(file_path, os.path.join(file_dir, new_filename)) + # app-db-query if plugin['data_source'] == 'app-db-query':