Skip to content

Commit 339c660

Browse files
ui,popups: allow to select snap paths automatically
In the same way that we automatically select AppImages paths, now we'll also select snap paths. Usually these paths start with /snap, followed by the name of the app, a revision number and the path to the executable: /snap/firefox/12345/usr/lib/firefox/firefox Internally the rule will create this regular expression, to match the the snap regardless of the revision: ^\/snap\/firefox\/[0-9]+\/usr\/lib\/firefox\/firefox$ Closes: #1439.
1 parent dff1283 commit 339c660

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

ui/opensnitch/dialogs/prompt/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ def _render_connection(self, con):
475475
self._add_fixed_options_to_combo(self.whatCombo, con, uid)
476476
if con.process_path.startswith(_constants.APPIMAGE_PREFIX):
477477
self._add_appimage_pattern_to_combo(self.whatCombo, con)
478+
elif con.process_path.startswith(_constants.SNAP_PREFIX):
479+
self._add_snap_pattern_to_combo(self.whatCombo, con)
478480
self._add_dst_networks_to_combo(self.whatCombo, con.dst_ip)
479481

480482
if con.dst_host != "" and con.dst_host != con.dst_ip:
@@ -556,6 +558,22 @@ def _add_appimage_pattern_to_combo(self, combo, con):
556558
_constants.FIELD_APPIMAGE
557559
)
558560

561+
def _add_snap_pattern_to_combo(self, combo, con):
562+
"""snap absolute path usually starts with /snap/, followed by a revision
563+
number which changes after every installation or update.
564+
"""
565+
snap_path = con.process_path
566+
snap_parts = snap_path.split('/')
567+
app = snap_parts[2]
568+
app_path = "/".join(snap_parts[4:])
569+
from_field = "from {0}/{1}/*/{2}".format(_constants.SNAP_PREFIX, app, app_path)
570+
571+
combo.addItem(
572+
QC.translate("popups", from_field),
573+
_constants.FIELD_SNAP
574+
)
575+
576+
559577
def _add_dst_networks_to_combo(self, combo, dst_ip):
560578
alias = NetworkAliases.get_alias(dst_ip)
561579
if alias:

ui/opensnitch/dialogs/prompt/_constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
FIELD_DST_NETWORK = "dst_network"
1919
FIELD_DST_HOST = "simple_host"
2020
FIELD_APPIMAGE = "appimage_path"
21+
FIELD_SNAP = "snap_path"
2122

2223
DURATION_30s = "30s"
2324
DURATION_5m = "5m"
@@ -28,6 +29,7 @@
2829
# don't translate
2930

3031
APPIMAGE_PREFIX = "/tmp/.mount_"
32+
SNAP_PREFIX = "/snap"
3133

3234
# label displayed in the pop-up combo
3335
DURATION_session = QC.translate("popups", "until reboot")

ui/opensnitch/dialogs/prompt/_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ def set_default_target(combo, con, cfg, app_name, app_args):
157157
if idx != -1:
158158
combo.setCurrentIndex(idx)
159159
return
160+
elif con.process_path.startswith(_constants.SNAP_PREFIX):
161+
idx = combo.findData(_constants.FIELD_SNAP)
162+
if idx != -1:
163+
combo.setCurrentIndex(idx)
164+
return
160165

161166
if int(con.process_id) > 0 and app_name != "" and app_args != "":
162167
combo.setCurrentIndex(int(cfg.getSettings(cfg.DEFAULT_TARGET_KEY)))
@@ -213,3 +218,12 @@ def get_combo_operator(data, comboText, con):
213218
appimage_path = os.path.dirname(con.process_path).replace('.', r'\.')
214219
appimage_path = appimage_path[0:len(_constants.APPIMAGE_PREFIX)+7]
215220
return Config.RULE_TYPE_REGEXP, Config.OPERAND_PROCESS_PATH, r'^{0}[0-9A-Za-z]{{6,7}}\/.*{1}$'.format(appimage_path, appimage_bin)
221+
222+
elif data == _constants.FIELD_SNAP:
223+
snap_path = con.process_path
224+
snap_parts = snap_path.split('/')
225+
snap_prefix = snap_parts[1]
226+
app = snap_parts[2]
227+
app_path = r'\/'.join(snap_parts[4:])
228+
regexp = r'^\/{0}\/{1}\/[0-9]+\/{2}$'.format(snap_prefix, app, app_path)
229+
return Config.RULE_TYPE_REGEXP, Config.OPERAND_PROCESS_PATH, regexp

0 commit comments

Comments
 (0)