Commit 40c3495c by x

firewall: code formatting

parent 114e301a
...@@ -42,5 +42,5 @@ def val_domain(value): ...@@ -42,5 +42,5 @@ def val_domain(value):
raise ValidationError(u'%s - helytelen domain' % value) raise ValidationError(u'%s - helytelen domain' % value)
def ipv4_2_ipv6(ipv4): def ipv4_2_ipv6(ipv4):
m = ipv4_re.match(ipv4) m = ipv4_re.match(ipv4)
return "2001:738:2001:4031:%s:%s:%s:0" % (m.group(1), m.group(2), m.group(3)) return "2001:738:2001:4031:%s:%s:%s:0" % (m.group(1), m.group(2), m.group(3))
...@@ -6,43 +6,43 @@ from firewall.fw import * ...@@ -6,43 +6,43 @@ from firewall.fw import *
def reload_firewall_lock(): def reload_firewall_lock():
acquire_lock = lambda: cache.add("reload_lock1", "true", 9) acquire_lock = lambda: cache.add("reload_lock1", "true", 9)
if acquire_lock(): if acquire_lock():
print "megszereztem" print "megszereztem"
ReloadTask.delay() ReloadTask.delay()
else: else:
print "nem szereztem meg" print "nem szereztem meg"
class ReloadTask(Task): class ReloadTask(Task):
def run(self, **kwargs): def run(self, **kwargs):
acquire_lock = lambda: cache.add("reload_lock1", "true", 90) acquire_lock = lambda: cache.add("reload_lock1", "true", 90)
release_lock = lambda: cache.delete("reload_lock1") release_lock = lambda: cache.delete("reload_lock1")
if not acquire_lock(): if not acquire_lock():
print "mar folyamatban van egy reload" print "mar folyamatban van egy reload"
return return
print "indul" print "indul"
time.sleep(10) time.sleep(10)
try: try:
print "ipv4" print "ipv4"
ipv4 = firewall() ipv4 = firewall()
ipv4.reload() ipv4.reload()
# print ipv4.show() # print ipv4.show()
print "ipv6" print "ipv6"
ipv6 = firewall(True) ipv6 = firewall(True)
ipv6.reload() ipv6.reload()
print "dns" print "dns"
dns() dns()
print "dhcp" print "dhcp"
dhcp() dhcp()
print "vege" print "vege"
except: except:
raise raise
print "nem sikerult :(" print "nem sikerult :("
print "leall" print "leall"
release_lock() release_lock()
...@@ -15,63 +15,63 @@ import sys ...@@ -15,63 +15,63 @@ import sys
def reload_firewall(request): def reload_firewall(request):
if request.user.is_authenticated(): if request.user.is_authenticated():
if(request.user.is_superuser): if(request.user.is_superuser):
html = u"Be vagy jelentkezve es admin is vagy, kedves %s!" % request.user.username html = u"Be vagy jelentkezve es admin is vagy, kedves %s!" % request.user.username
html += "<br> 10 masodperc mulva ujratoltodik" html += "<br> 10 masodperc mulva ujratoltodik"
ReloadTask.delay() ReloadTask.delay()
else: else:
html = u"Be vagy jelentkezve, csak nem vagy admin, kedves %s!" % request.user.username html = u"Be vagy jelentkezve, csak nem vagy admin, kedves %s!" % request.user.username
else: else:
html = u"Nem vagy bejelentkezve, kedves ismeretlen!" html = u"Nem vagy bejelentkezve, kedves ismeretlen!"
return HttpResponse(html) return HttpResponse(html)
@csrf_exempt @csrf_exempt
def firewall_api(request): def firewall_api(request):
if request.method == 'POST': if request.method == 'POST':
try: try:
data=json.loads(base64.b64decode(request.POST["data"])) data=json.loads(base64.b64decode(request.POST["data"]))
command = request.POST["command"] command = request.POST["command"]
if(data["password"] != "bdmegintelrontottaanetet"): if(data["password"] != "bdmegintelrontottaanetet"):
raise Exception("rossz jelszo") raise Exception("rossz jelszo")
if(not(data["vlan"] == "vm-net" or data["vlan"] == "war")): if(not(data["vlan"] == "vm-net" or data["vlan"] == "war")):
raise Exception("csak vm-net es war-re mukodik") raise Exception("csak vm-net es war-re mukodik")
data["hostname"] = re.sub(r' ','_', data["hostname"]) data["hostname"] = re.sub(r' ','_', data["hostname"])
if(command == "create"): if(command == "create"):
data["owner"] = "opennebula" data["owner"] = "opennebula"
owner = auth.models.User.objects.get(username=data["owner"]) owner = auth.models.User.objects.get(username=data["owner"])
host = models.Host(hostname=data["hostname"], vlan=models.Vlan.objects.get(name=data["vlan"]), mac=data["mac"], ipv4=data["ip"], owner=owner, description=data["description"], pub_ipv4=models.Vlan.objects.get(name=data["vlan"]).snat_ip, shared_ip=True) host = models.Host(hostname=data["hostname"], vlan=models.Vlan.objects.get(name=data["vlan"]), mac=data["mac"], ipv4=data["ip"], owner=owner, description=data["description"], pub_ipv4=models.Vlan.objects.get(name=data["vlan"]).snat_ip, shared_ip=True)
host.full_clean() host.full_clean()
host.save() host.save()
host.enable_net() host.enable_net()
for p in data["portforward"]: for p in data["portforward"]:
host.add_port(proto=p["proto"], public=int(p["public_port"]), private=int(p["private_port"])) host.add_port(proto=p["proto"], public=int(p["public_port"]), private=int(p["private_port"]))
elif(command == "destroy"): elif(command == "destroy"):
data["owner"] = "opennebula" data["owner"] = "opennebula"
print data["hostname"] print data["hostname"]
owner = auth.models.User.objects.get(username=data["owner"]) owner = auth.models.User.objects.get(username=data["owner"])
host = models.Host.objects.get(hostname=data["hostname"], owner=owner) host = models.Host.objects.get(hostname=data["hostname"], owner=owner)
host.del_rules() host.del_rules()
host.delete() host.delete()
else: else:
raise Exception("rossz parancs") raise Exception("rossz parancs")
reload_firewall_lock() reload_firewall_lock()
except (ValidationError, IntegrityError, AttributeError, Exception) as e: except (ValidationError, IntegrityError, AttributeError, Exception) as e:
return HttpResponse(u"rosszul hasznalod! :(\n%s\n" % e); return HttpResponse(u"rosszul hasznalod! :(\n%s\n" % e);
except: except:
# raise # raise
return HttpResponse(u"rosszul hasznalod! :(\n"); return HttpResponse(u"rosszul hasznalod! :(\n");
return HttpResponse(u"ok"); return HttpResponse(u"ok");
return HttpResponse(u"ez kerlek egy api lesz!\n"); return HttpResponse(u"ez kerlek egy api lesz!\n");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment