Skip to content

Commit

Permalink
Merge branch 'ui2.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-iniguez-goya committed Jul 28, 2020
2 parents 08245a0 + d137a50 commit eff9c5c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
37 changes: 28 additions & 9 deletions ui/opensnitch/dialogs/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,17 @@ def _render_connection(self, con):

if con.dst_host != "" and con.dst_host != con.dst_ip:
try:
dst_host = re.search("(.*)\s\((.*)\)", con.dst_host)
# get the domain that a process is trying to resolve. format: 1.1.1.1 (host.example.com)
dst_host_regexp = re.search("(.*)\s\((.*)\)", con.dst_host)
except Exception:
pass

if dst_host != None and len(dst_host.groups()) == 2:
self._add_dsthost_to_combo(dst_host.group(2))
else:
dst_host = con.dst_host
self.whatCombo.addItem("%s" % con.dst_host, "simple_host")
self._add_dsthost_to_combo(con.dst_host)
dst_host = con.dst_host
if dst_host_regexp != None and len(dst_host_regexp.groups()) == 2:
dst_host = dst_host_regexp.group(2)
print("host regexp: " + dst_host)

self._add_dsthost_to_combo(dst_host)

self.whatIPCombo.addItem("to %s" % con.dst_ip, "dst_ip")

Expand Down Expand Up @@ -300,6 +301,9 @@ def closeEvent(self, e):
e.ignore()

def _add_dsthost_to_combo(self, dst_host):
self.whatCombo.addItem("%s" % dst_host, "simple_host")
self.whatIPCombo.addItem("%s" % dst_host, "simple_host")

parts = dst_host.split('.')[1:]
nparts = len(parts)
for i in range(0, nparts - 1):
Expand Down Expand Up @@ -345,7 +349,7 @@ def _get_combo_operator(self, combo, what_idx):
return "simple", "dest.ip", self._con.dst_ip

elif combo.itemData(what_idx) == "simple_host":
return "simple", "dest.host", self._con.dst_host
return "simple", "dest.host", combo.currentText()

elif combo.itemData(what_idx) == "regex_host":
return "regexp", "dest.host", "%s" % '\.'.join(combo.currentText().split('.')).replace("*", ".*")[3:]
Expand All @@ -361,6 +365,16 @@ def _on_apply_clicked(self):
self._default_action = self.ACTION_ALLOW
self._send_rule()

def _get_rule_name(self):
rule_temp_name = slugify("%s %s" % (self._rule.action, self._rule.duration))
if self._ischeckAdvanceded:
rule_temp_name = "%s-list" % rule_temp_name
else:
rule_temp_name = "%s-simple" % rule_temp_name
rule_temp_name = slugify("%s %s" % (rule_temp_name, self._rule.operator.data))

return rule_temp_name

def _send_rule(self):
self._cfg.setSettings("promptDialog/geometry", self.saveGeometry())
self._rule = ui_pb2.Rule(name="user.choice")
Expand All @@ -373,25 +387,30 @@ def _send_rule(self):
what_idx = self.whatCombo.currentIndex()
self._rule.operator.type, self._rule.operator.operand, self._rule.operator.data = self._get_combo_operator(self.whatCombo, what_idx)

rule_temp_name = self._get_rule_name()

# TODO: move to a method
data=[]
if self._ischeckAdvanceded and self.checkDstIP.isChecked() and self.whatCombo.itemData(what_idx) != "dst_ip":
_type, _operand, _data = self._get_combo_operator(self.whatIPCombo, self.whatIPCombo.currentIndex())
data.append({"type": _type, "operand": _operand, "data": _data})
rule_temp_name = slugify("%s %s" % (rule_temp_name, _data))

if self._ischeckAdvanceded and self.checkDstPort.isChecked() and self.whatCombo.itemData(what_idx) != "dst_port":
data.append({"type": "simple", "operand": "dest.port", "data": str(self._con.dst_port)})
rule_temp_name = slugify("%s %s" % (rule_temp_name, str(self._con.dst_port)))

if self._ischeckAdvanceded and self.checkUserID.isChecked() and self.whatCombo.itemData(what_idx) != "user_id":
data.append({"type": "simple", "operand": "user.id", "data": str(self._con.user_id)})
rule_temp_name = slugify("%s %s" % (rule_temp_name, str(self._con.user_id)))

if self._ischeckAdvanceded:
data.append({"type": self._rule.operator.type, "operand": self._rule.operator.operand, "data": self._rule.operator.data})
self._rule.operator.data = json.dumps(data)
self._rule.operator.type = "list"
self._rule.operator.operand = ""

self._rule.name = slugify("%s %s %s" % (self._rule.action, self._rule.operator.type, self._rule.operator.data))
self._rule.name = rule_temp_name

self.hide()
if self._ischeckAdvanceded:
Expand Down
2 changes: 2 additions & 0 deletions ui/opensnitch/dialogs/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ def _cb_main_table_double_clicked(self, row):
elif idx == StatsDialog.COL_RULES:
cur_idx = 2
self._set_rules_tab_active(row, cur_idx)
else:
return

self._set_active_widgets(True, str(data))

Expand Down
2 changes: 1 addition & 1 deletion ui/opensnitch/res/preferences.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
<string>Preferences</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
Expand Down
9 changes: 7 additions & 2 deletions ui/opensnitch/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ def _async_worker(self):
self._status_change_trigger.emit()
was_connected = self._connected

def _check_versions(self, daemon_version):
lMayor, lMinor, lPatch = version.split(".")
rMayor, rMinor, rPatch = daemon_version.split(".")
if lMayor != rMayor or (lMayor == rMayor and lMinor != rMinor):
self._version_warning_trigger.emit(daemon_version, version)

def _is_local_request(self, proto, addr):
if proto == "unix":
return True
Expand Down Expand Up @@ -372,8 +378,7 @@ def _populate_stats_events(self, db, addr, stats, table, colnames, cols, items):
def Ping(self, request, context):
try:
self._last_ping = datetime.now()
if request.stats.daemon_version != version:
self._version_warning_trigger.emit(request.stats.daemon_version, version)
self._check_versions(request.stats.daemon_version)

proto, addr = self._get_peer(context.peer())
# do not update db here, do it on the main thread
Expand Down

0 comments on commit eff9c5c

Please sign in to comment.