Ejemplo n.º 1
0
def buildIndexComponents(request):
	bestScore = BestScore.objects.filter(user=request.user)
	instances = EC2Instance.objects.filter(user=request.user)
	config = manageEC2.readConfig()

	context = {}

	if instances:
		instance = instances[0]

		# If we haven't tried to get the IP yet, or we've failed, try again
		if instance.ip == "0.0.0.0":
			instance.ip = manageEC2.getInstIP(instance.instanceID)
			instance.save()

		timeDelta = datetime.timedelta(hours=int(config["image_hours"]),
				minutes=int(config["image_minutes"]))
		curTime = datetime.datetime.now(datetime.timezone.utc)
		timeLeft = (timeDelta - (curTime - instance.startTime))
		niceTimeLeft = datetime.timedelta(seconds=int(timeLeft.total_seconds()))

		context["timeLeft"] = niceTimeLeft
		context["currentScore"] = instance.score
		context["IPAddr"] = instance.ip
		context["username"] = config["ami_username"]
		context["password"] = config["ami_password"]

	if bestScore:
		context["bestScore"] = bestScore[0].score
		context["bestScoreTime"] = bestScore[0].time

	return context
Ejemplo n.º 2
0
def startInstance(request):
	if EC2Instance.objects.filter(user=request.user):
		# The user already has an instance started, so redirect to the index
		return HttpResponseRedirect(reverse("index"))

	else:
		# There's no existing instance, so start one up!
		config = manageEC2.readConfig()

		user_data=config["score_server_address"] + ";" + str(request.user.id) 

		instanceID = manageEC2.startInstance(user_data)

		instance = EC2Instance(instanceID=instanceID, user=request.user)

		instance.save()

		return render(request, "pgmanager/startInstance.html")
Ejemplo n.º 3
0
def registerScore(request):
	config = manageEC2.readConfig()

	validScoreLims = [int(config["image_scoremin"]), int(config["image_scoremax"])]

	try:
		userID = int(request.POST["userID"])
		score = int(request.POST["score"])
	except KeyError:	# Should happen when someone doesn't provide a POST field
		return HttpResponse("")
	except ValueError:	# Should happen when the field isn't an integer
		return HttpResponse("")

	try:
		user = User.objects.get(id=userID)
		instance = EC2Instance.objects.get(user=user)
	except: # Likely errors: django DoesNotExist, OverflowError
		return HttpResponse("")

	if score > max(validScoreLims) or score < min(validScoreLims):
		return HttpResponse("")

	instance.score=score
	instance.save()

	# If this is the best score, set it and save it
	try:
		bestScore = BestScore.objects.get(user=user)
	except: # Likely exception: django DoesNotExist
		bestScore = BestScore(user=user, score=-1)

	if score > bestScore.score:
		bestScore.score=score
		bestScore.save()

	return HttpResponse("") # Don't render anything on success or failure
Ejemplo n.º 4
0
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "SecurityTrainingGround.settings")

import django
django.setup()


import datetime

from pgmanager.models import EC2Instance

import SecurityTrainingGround.manageEC2 as manageEC2


config = manageEC2.readConfig()


# Find all the instances that are running that aren't assigned a user
allInstances = manageEC2.listInstances()
knownInstances = [inst.instanceID for inst in EC2Instance.objects.all()]
unknownInstances = [inst for inst in allInstances
		if inst not in knownInstances]

print("Instances that do not match those in use by a user:")
print(unknownInstances)


# Which instances in the DB aren't still running?
deadInstances = [inst for inst in knownInstances
		if inst not in allInstances]