Skip to content

Commit 05e58b3

Browse files
committed
backgroundtask
1 parent 4573c27 commit 05e58b3

File tree

1 file changed

+125
-113
lines changed

1 file changed

+125
-113
lines changed

__init__.py

Lines changed: 125 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
2222
# delete file in temp
2323
# if latest_hash != downloaded plugin hash -> alert to update (with link to latest release)
2424

25+
# Plugin details
26+
plugin_name = 'sigscan'
27+
28+
# Repository details
29+
repo_owner = 'rikodot'
30+
repo_name = 'binja_native_sigscan'
31+
file_url = 'https://github.com/{}/{}/releases/latest/download'.format(repo_owner, repo_name)
32+
33+
# Name of files in release section on github (leave blank if platform not supported)
34+
win_file = 'sigscan.dll'
35+
linux_file = ''
36+
darwin_file = ''
37+
2538
# Function that determines whether system is supported
2639
def is_system_supported(file_name):
2740
return file_name != None and len(file_name) > 0
@@ -31,25 +44,25 @@ def data_folder_exists():
3144
return os.path.isdir(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data'))
3245

3346
# Function that determines whether current_native_plugin_data file exists
34-
def data_file_exists(plugin_name):
47+
def data_file_exists():
3548
return os.path.isfile(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data'))
3649

3750
# Function that determines whether temp folder exists
3851
def temp_folder_exists():
3952
return os.path.isdir(os.path.join(binaryninja.user_plugin_path(), 'temp'))
4053

4154
# Function that reads current_native_plugin_data file
42-
def read_data_file(plugin_name):
55+
def read_data_file():
4356
with open(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data'), 'r') as f:
4457
return f.read().splitlines()
4558

4659
# Function that writes to current_native_plugin_data file
47-
def write_data_file(plugin_name, version, hash):
60+
def write_data_file(version, hash):
4861
with open(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data'), 'w') as f:
4962
f.write(version + '\n' + hash)
5063

5164
# Function that deletes file from current_native_plugin_data
52-
def delete_data_file(plugin_name):
65+
def delete_data_file():
5366
os.remove(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data'))
5467

5568
# Function that calculates hash of file
@@ -94,7 +107,7 @@ def is_plugin_installed(file_name):
94107
return os.path.isfile(os.path.join(binaryninja.user_plugin_path(), file_name))
95108

96109
# Function that determines whether plugin is outdated
97-
def is_plugin_outdated(plugin_name, version, hash):
110+
def is_plugin_outdated(version, hash):
98111
if data_folder_exists():
99112
if data_file_exists(plugin_name):
100113
data = read_data_file(plugin_name)
@@ -108,124 +121,123 @@ def is_plugin_outdated(plugin_name, version, hash):
108121
return True
109122

110123
# Function that alerts user
111-
def alert_user(plugin_name, description):
124+
def alert_user(description):
112125
binaryninja.interaction.show_message_box('{} (Native plugin loader)'.format(plugin_name), description, binaryninja.enums.MessageBoxButtonSet.OKButtonSet, binaryninja.enums.MessageBoxIcon.InformationIcon)
113126

114-
# Plugin details
115-
plugin_name = 'sigscan'
116-
117-
# Repository details
118-
repo_owner = 'rikodot'
119-
repo_name = 'binja_native_sigscan'
120-
file_url = 'https://github.com/{}/{}/releases/latest/download'.format(repo_owner, repo_name)
127+
# Function that does the actual work
128+
def check_for_updates(repo_owner, repo_name, file_url, win_file, linux_file, darwin_file):
129+
# Retrieve the HTML of the release page
130+
release_url = 'https://github.com/{}/{}/releases/latest'.format(repo_owner, repo_name)
131+
response = requests.get(release_url)
132+
html = response.content
133+
134+
# Parse the HTML to extract the release version
135+
soup = bs4.BeautifulSoup(html, 'html.parser')
136+
latest_version_tag = getattr(soup.find('span', {'class': 'css-truncate-target'}), 'text', None)
137+
latest_version = latest_version_tag.strip() if latest_version_tag else None
138+
139+
# Determine OS we are running on
140+
platform = sys.platform.lower()
141+
142+
# Windows
143+
if platform.startswith('win'):
144+
file_url = '{}/{}'.format(file_url, win_file)
145+
file = win_file
146+
# Linux
147+
elif platform.startswith('linux'):
148+
file_url = '{}/{}'.format(file_url, linux_file)
149+
file = linux_file
150+
# Mac
151+
elif platform.startswith('darwin'):
152+
file_url = '{}/{}'.format(file_url, darwin_file)
153+
file = darwin_file
154+
else:
155+
alert_user(plugin_name, 'Unsupported platform')
121156

122-
# Name of files in release section on github (leave blank if platform not supported)
123-
win_file = 'sigscan.dll'
124-
linux_file = ''
125-
darwin_file = ''
157+
# Make sure we have data folder
158+
if not data_folder_exists():
159+
os.mkdir(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data'))
126160

127-
# Retrieve the HTML of the release page
128-
release_url = 'https://github.com/{}/{}/releases/latest'.format(repo_owner, repo_name)
129-
response = requests.get(release_url)
130-
html = response.content
131-
132-
# Parse the HTML to extract the release version
133-
soup = bs4.BeautifulSoup(html, 'html.parser')
134-
latest_version_tag = getattr(soup.find('span', {'class': 'css-truncate-target'}), 'text', None)
135-
latest_version = latest_version_tag.strip() if latest_version_tag else None
136-
137-
# Determine OS we are running on
138-
platform = sys.platform.lower()
139-
140-
# Windows
141-
if platform.startswith('win'):
142-
file_url = '{}/{}'.format(file_url, win_file)
143-
file = win_file
144-
# Linux
145-
elif platform.startswith('linux'):
146-
file_url = '{}/{}'.format(file_url, linux_file)
147-
file = linux_file
148-
# Mac
149-
elif platform.startswith('darwin'):
150-
file_url = '{}/{}'.format(file_url, darwin_file)
151-
file = darwin_file
152-
else:
153-
alert_user(plugin_name, 'Unsupported platform')
154-
155-
# Make sure we have data folder
156-
if not data_folder_exists():
157-
os.mkdir(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data'))
158-
159-
# Make sure we have temp folder
160-
if not temp_folder_exists():
161-
os.mkdir(os.path.join(binaryninja.user_plugin_path(), 'temp'))
162-
else:
163-
# Delete all files in temp folder
164-
for file in os.listdir(os.path.join(binaryninja.user_plugin_path(), 'temp')):
165-
delete_file_from_temp(file)
166-
167-
# Do the thing
168-
if (is_system_supported(file) and latest_version != None):
169-
plugin_data = (read_data_file(plugin_name) if data_file_exists(plugin_name) else None) if data_folder_exists() else None
170-
# Check if we have both version and hash of the plugin
171-
if plugin_data == None or len(plugin_data) != 2 or plugin_data[0] == None or plugin_data[1] == None:
172-
delete_data_file(plugin_name) if data_file_exists(plugin_name) else None
173-
plugin_data = None
174-
175-
data_version = plugin_data[0] if plugin_data != None else None
176-
data_hash = plugin_data[1] if plugin_data != None else None
177-
if not is_plugin_installed(file):
178-
# Plugin not installed, just download it
179-
if download_file(file_url, file):
180-
# Register plugin in data directory
181-
write_data_file(plugin_name, latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
182-
alert_user(plugin_name, 'Plugin downloaded successfully, please restart Binary Ninja to load it')
183-
else:
184-
alert_user(plugin_name, 'Failed to download plugin')
161+
# Make sure we have temp folder
162+
if not temp_folder_exists():
163+
os.mkdir(os.path.join(binaryninja.user_plugin_path(), 'temp'))
185164
else:
186-
# Plugin installed, no data about the plugin
187-
if (data_version == None and data_hash == None):
188-
# Download latest version of the plugin and check if we have that version
189-
download_file_to_temp(file_url, file)
190-
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
191-
# We have the latest version, register it in data directory
192-
write_data_file(plugin_name, latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
193-
delete_file_from_temp(file)
165+
# Delete all files in temp folder
166+
for file in os.listdir(os.path.join(binaryninja.user_plugin_path(), 'temp')):
167+
delete_file_from_temp(file)
168+
169+
# Do the thing
170+
if (is_system_supported(file) and latest_version != None):
171+
plugin_data = (read_data_file() if data_file_exists() else None) if data_folder_exists() else None
172+
# Check if we have both version and hash of the plugin
173+
if plugin_data == None or len(plugin_data) != 2 or plugin_data[0] == None or plugin_data[1] == None:
174+
delete_data_file() if data_file_exists() else None
175+
plugin_data = None
176+
177+
data_version = plugin_data[0] if plugin_data != None else None
178+
data_hash = plugin_data[1] if plugin_data != None else None
179+
if not is_plugin_installed(file):
180+
# Plugin not installed, just download it
181+
if download_file(file_url, file):
182+
# Register plugin in data directory
183+
write_data_file(latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
184+
alert_user('Plugin downloaded successfully, please restart Binary Ninja to load it')
194185
else:
195-
# We don't have the latest version, alert user
196-
alert_user(plugin_name, 'You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
197-
# Plugin installed, but data shows it's outdated
198-
elif (data_version != latest_version):
199-
# Make sure the version in the data directory is actually the version we have installed (we compare hashes - hash now and hash when we downloaded the plugin)
200-
if (data_hash == calculate_hash(os.path.join(binaryninja.user_plugin_path(), file))):
201-
# Yep, version noted in data corresponds to the hash of currently installed plugin
202-
alert_user(plugin_name, 'You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
203-
else:
204-
# Nope, version noted in data doesn't correspond to the hash of currently installed plugin (user probably replaced the plugin)
205-
delete_data_file(plugin_name)
186+
alert_user('Failed to download plugin')
187+
else:
188+
# Plugin installed, no data about the plugin
189+
if (data_version == None and data_hash == None):
206190
# Download latest version of the plugin and check if we have that version
207191
download_file_to_temp(file_url, file)
208192
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
209-
# We have the latest version, register it in data directory so user is not prompted to update as he probably already did
210-
write_data_file(plugin_name, latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
193+
# We have the latest version, register it in data directory
194+
write_data_file(latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
211195
delete_file_from_temp(file)
212196
else:
213197
# We don't have the latest version, alert user
214-
alert_user(plugin_name, 'You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
215-
# Plugin installed, data shows it's up to date, but let's make sure
216-
elif (data_version == latest_version):
217-
# Make sure the version in the data directory is actually the version we have installed (we compare hashes - hash now and hash when we downloaded the plugin)
218-
if (data_hash != calculate_hash(os.path.join(binaryninja.user_plugin_path(), file))):
219-
# Nope, hash noted in data doesn't correspond to the hash of currently installed plugin (user probably replaced the plugin with different version)
220-
delete_data_file(plugin_name)
221-
# Let's check if our plugin matches the hash in the latest github release (developer could have replaced file in the github release and user updated to it)
222-
download_file_to_temp(file_url, file)
223-
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
224-
# Yep, hash of the plugin in the github release corresponds to the hash of currently installed plugin so we have the latest one
225-
write_data_file(plugin_name, latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
226-
delete_file_from_temp(file)
198+
alert_user('You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
199+
# Plugin installed, but data shows it's outdated
200+
elif (data_version != latest_version):
201+
# Make sure the version in the data directory is actually the version we have installed (we compare hashes - hash now and hash when we downloaded the plugin)
202+
if (data_hash == calculate_hash(os.path.join(binaryninja.user_plugin_path(), file))):
203+
# Yep, version noted in data corresponds to the hash of currently installed plugin
204+
alert_user('You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
227205
else:
228-
# Not the latest one (according to the hash in the github release), but user might be intending to test different version of the plugin, add ignore option
229-
alert_user(plugin_name, 'You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
230-
else:
231-
alert_user(plugin_name, 'This plugin is not supported on current platform or plugin not found or its github releases not found')
206+
# Nope, version noted in data doesn't correspond to the hash of currently installed plugin (user probably replaced the plugin)
207+
delete_data_file()
208+
# Download latest version of the plugin and check if we have that version
209+
download_file_to_temp(file_url, file)
210+
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
211+
# We have the latest version, register it in data directory so user is not prompted to update as he probably already did
212+
write_data_file(latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
213+
delete_file_from_temp(file)
214+
else:
215+
# We don't have the latest version, alert user
216+
alert_user('You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
217+
# Plugin installed, data shows it's up to date, but let's make sure
218+
elif (data_version == latest_version):
219+
# Make sure the version in the data directory is actually the version we have installed (we compare hashes - hash now and hash when we downloaded the plugin)
220+
if (data_hash != calculate_hash(os.path.join(binaryninja.user_plugin_path(), file))):
221+
# Nope, hash noted in data doesn't correspond to the hash of currently installed plugin (user probably replaced the plugin with different version)
222+
delete_data_file()
223+
# Let's check if our plugin matches the hash in the latest github release (developer could have replaced file in the github release and user updated to it)
224+
download_file_to_temp(file_url, file)
225+
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
226+
# Yep, hash of the plugin in the github release corresponds to the hash of currently installed plugin so we have the latest one
227+
write_data_file(latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
228+
delete_file_from_temp(file)
229+
else:
230+
# Not the latest one (according to the hash in the github release), but user might be intending to test different version of the plugin, add ignore option
231+
alert_user('You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
232+
else:
233+
alert_user('This plugin is not supported on current platform or plugin not found or its github releases not found')
234+
235+
class Updater(binaryninja.BackgroundTaskThread):
236+
def __init__(self):
237+
binaryninja.BackgroundTaskThread.__init__(self, 'Native plugin loader - checking for updates on: {}'.format(plugin_name), True)
238+
239+
def run(self):
240+
check_for_updates(repo_owner, repo_name, file_url, win_file, linux_file, darwin_file)
241+
242+
obj = Updater()
243+
obj.start()

0 commit comments

Comments
 (0)