Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/198'
Browse files Browse the repository at this point in the history
* origin/pr/198:
  Fixed new firewall rule window
  • Loading branch information
marmarek committed Sep 23, 2019
2 parents 0dc73fd + 2e152b8 commit bdf0951
Showing 1 changed file with 67 additions and 57 deletions.
124 changes: 67 additions & 57 deletions qubesmanager/firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,66 @@ def __init__(self, parent=None):
self.populate_combos()
self.serviceComboBox.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)

self.model = None

def try_to_create_rule(self):
# return True if successful, False otherwise
address = str(self.addressComboBox.currentText())
service = str(self.serviceComboBox.currentText())

rule = qubesadmin.firewall.Rule(None, action='accept')

if address is not None and address != "*":
try:
rule.dsthost = address
except ValueError:
QtWidgets.QMessageBox.warning(
self, self.tr("Invalid address"),
self.tr("Address '{0}' is invalid.").format(address))
return False

if self.tcp_radio.isChecked():
rule.proto = 'tcp'
elif self.udp_radio.isChecked():
rule.proto = 'udp'

if self.model.port_range_pattern.fullmatch(service):
try:
rule.dstports = service
except ValueError:
QtWidgets.QMessageBox.warning(
self,
self.tr("Invalid port or service"),
self.tr("Port number or service '{0}' is "
"invalid.").format(service))
return False
elif service:
if self.model.service_port_pattern.fullmatch(service):
parsed_service = self.model.service_port_pattern.match(
service).groups()[0]
else:
parsed_service = service

try:
rule.dstports = parsed_service
except (TypeError, ValueError):
if self.model.get_service_port(parsed_service) is not None:
rule.dstports = self.model.get_service_port(parsed_service)
else:
QtWidgets.QMessageBox.warning(
self,
self.tr("Invalid port or service"),
self.tr(
"Port number or service '{0}' is "
"invalid.".format(parsed_service)))
return False

if self.model.current_row is not None:
self.model.set_child(self.model.current_row, rule)
else:
self.model.append_child(rule)
return True

def accept(self):
if self.tcp_radio.isChecked() or self.udp_radio.isChecked():
if not self.serviceComboBox.currentText():
Expand All @@ -99,7 +159,8 @@ def accept(self):
self.tr("You need to fill service "
"name/port for TCP/UDP rule"))
return
super().accept()
if self.try_to_create_rule():
super().accept()

def populate_combos(self):
example_addresses = [
Expand Down Expand Up @@ -145,6 +206,8 @@ class QubesFirewallRulesModel(QtCore.QAbstractItemModel):
def __init__(self, parent=None):
QtCore.QAbstractItemModel.__init__(self, parent)

self.current_row = None

self.__column_names = {0: "Address", 1: "Port/Service", 2: "Protocol", }
self.__services = list()

Expand Down Expand Up @@ -366,62 +429,9 @@ def populate_edit_dialog(self, dialog, row):
dialog.any_radio.setChecked(True)

def run_rule_dialog(self, dialog, row=None):
if dialog.exec_():

address = str(dialog.addressComboBox.currentText())
service = str(dialog.serviceComboBox.currentText())

rule = qubesadmin.firewall.Rule(None, action='accept')

if address is not None and address != "*":
try:
rule.dsthost = address
except ValueError:
QtWidgets.QMessageBox.warning(
dialog, self.tr("Invalid address"),
self.tr("Address '{0}' is invalid.").format(address))
return

if dialog.tcp_radio.isChecked():
rule.proto = 'tcp'
elif dialog.udp_radio.isChecked():
rule.proto = 'udp'

if self.port_range_pattern.fullmatch(service):
try:
rule.dstports = service
except ValueError:
QtWidgets.QMessageBox.warning(
dialog,
self.tr("Invalid port or service"),
self.tr("Port number or service '{0}' is "
"invalid.").format(service))
return
elif service:
if self.service_port_pattern.fullmatch(service):
parsed_service = self.service_port_pattern.match(
service).groups()[0]
else:
parsed_service = service

try:
rule.dstports = parsed_service
except (TypeError, ValueError):
if self.get_service_port(parsed_service) is not None:
rule.dstports = self.get_service_port(parsed_service)
else:
QtWidgets.QMessageBox.warning(
dialog,
self.tr("Invalid port or service"),
self.tr(
"Port number or service '{0}' is "
"invalid.".format(parsed_service)))
return

if row is not None:
self.set_child(row, rule)
else:
self.append_child(rule)
self.current_row = row
dialog.model = self
dialog.exec()

def index(self, row, column, parent=QtCore.QModelIndex()):
if not self.hasIndex(row, column, parent):
Expand Down

0 comments on commit bdf0951

Please sign in to comment.