Files
CyberPanel/plogical/firewallUtilities.py

89 lines
3.0 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
import subprocess
import CyberCPLogFileWriter as logging
import shlex
2018-11-16 14:41:40 +05:00
from processUtilities import ProcessUtilities
2017-10-24 19:16:36 +05:00
class FirewallUtilities:
2018-11-16 14:41:40 +05:00
@staticmethod
def resFailed(res):
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu and res != 0:
return True
elif ProcessUtilities.decideDistro() == ProcessUtilities.centos and res == 1:
return True
return False
2017-10-24 19:16:36 +05:00
@staticmethod
def doCommand(command):
2017-10-24 19:16:36 +05:00
try:
cmd = shlex.split(command)
2019-03-21 23:26:42 +05:00
res = ProcessUtilities.executioner(cmd)
2018-11-16 14:41:40 +05:00
if FirewallUtilities.resFailed(res):
logging.CyberCPLogFileWriter.writeToFile("Failed to apply rule: " + command + " Error #" + str(res))
return 0
except OSError, msg:
2018-11-16 14:41:40 +05:00
logging.CyberCPLogFileWriter.writeToFile("Failed to apply rule: " + command + " Error: " + str(msg))
return 0
except ValueError, msg:
2018-11-16 14:41:40 +05:00
logging.CyberCPLogFileWriter.writeToFile("Failed to apply rule: " + command + " Error: " + str(msg), 1)
return 0
return 1
2017-10-24 19:16:36 +05:00
@staticmethod
def addRule(proto,port,ipAddress):
ruleFamily = 'rule family="ipv4"'
sourceAddress = 'source address="' + ipAddress + '"'
ruleProtocol = 'port protocol="' + proto + '"'
rulePort = 'port="' + port + '"'
2017-10-24 19:16:36 +05:00
command = "sudo firewall-cmd --permanent --zone=public --add-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
2017-10-24 19:16:36 +05:00
2018-11-16 14:41:40 +05:00
if not FirewallUtilities.doCommand(command):
2017-10-24 19:16:36 +05:00
return 0
ruleFamily = 'rule family="ipv6"'
sourceAddress = ''
command = "sudo firewall-cmd --permanent --zone=public --add-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
2018-11-16 14:41:40 +05:00
if not FirewallUtilities.doCommand(command):
return 0
command = 'sudo firewall-cmd --reload'
2018-11-16 14:41:40 +05:00
if not FirewallUtilities.doCommand(command):
2017-10-24 19:16:36 +05:00
return 0
return 1
@staticmethod
2018-11-14 12:17:24 -05:00
def deleteRule(proto, port, ipAddress):
ruleFamily = 'rule family="ipv4"'
sourceAddress = 'source address="' + ipAddress + '"'
ruleProtocol = 'port protocol="' + proto + '"'
rulePort = 'port="' + port + '"'
2018-11-14 13:45:03 -05:00
command = "sudo firewall-cmd --permanent --zone=public --remove-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
2017-10-24 19:16:36 +05:00
2018-11-16 14:41:40 +05:00
if not FirewallUtilities.doCommand(command):
return 0
2017-10-24 19:16:36 +05:00
ruleFamily = 'rule family="ipv6"'
sourceAddress = ''
2017-10-24 19:16:36 +05:00
command = "sudo firewall-cmd --permanent --zone=public --remove-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
2017-10-24 19:16:36 +05:00
2018-11-16 14:41:40 +05:00
if not FirewallUtilities.doCommand(command):
return 0
2017-10-24 19:16:36 +05:00
command = 'sudo firewall-cmd --reload'
2017-10-24 19:16:36 +05:00
2018-11-16 14:41:40 +05:00
if not FirewallUtilities.doCommand(command):
2017-10-24 19:16:36 +05:00
return 0
2018-11-16 14:41:40 +05:00
return 1