Skip to content

Commit f2734b4

Browse files
committed
Implement flag hashing in Challenge model and remove duplicate port field
Signed-off-by: Soumyaditya Batabyal <soumyaditya2021@gmail.com>
1 parent 4ff80b4 commit f2734b4

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import json
2+
import os
3+
from django.core.management.base import BaseCommand
4+
from challenge.models import Challenge
5+
6+
class Command(BaseCommand):
7+
help = "Populate challenge data from challenge/challenge.json"
8+
9+
def handle(self, *args, **options):
10+
file_path = os.path.join('challenge', 'challenge.json')
11+
12+
try:
13+
with open(file_path, 'r') as json_file:
14+
challenges_data = json.load(json_file)
15+
except FileNotFoundError:
16+
self.stderr.write(self.style.ERROR(f"JSON file not found: {file_path}"))
17+
return
18+
except json.JSONDecodeError as e:
19+
self.stderr.write(self.style.ERROR(f"Error decoding JSON: {e}"))
20+
return
21+
22+
for item in challenges_data:
23+
challenge, created = Challenge.objects.get_or_create(
24+
name=item.get("name"),
25+
defaults={
26+
"description": item.get("description", ""),
27+
"docker_image": item.get("docker_image", ""),
28+
"docker_port": item.get("docker_port", 0),
29+
"start_port": item.get("start_port", 0),
30+
"end_port": item.get("end_port", 0),
31+
"flag": item.get("flag", ""),
32+
"point": item.get("point", 0)
33+
}
34+
)
35+
if created:
36+
self.stdout.write(self.style.SUCCESS(f"Challenge '{challenge.name}' created."))
37+
else:
38+
self.stdout.write(f"Challenge '{challenge.name}' already exists.")
39+
40+
self.stdout.write(self.style.SUCCESS("Challenge data has been populated successfully."))

challenge/models.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django.db import models
22
from django.contrib.auth.models import User
3+
import hashlib # Import hashlib
4+
from django.core.exceptions import ValidationError # Import ValidationError
35

4-
# Create your models here
56
class Challenge(models.Model):
67
id = models.AutoField(primary_key=True, unique=True)
78
name = models.CharField(max_length=100, unique=True)
@@ -18,11 +19,13 @@ class Challenge(models.Model):
1819
def __str__(self):
1920
return self.name
2021

21-
# overwriting default save method
22+
# Overriding default save method
2223
def save(self, *args, **kwargs):
2324
if self.start_port > self.end_port:
24-
raise Exception("Start port should be less than end port")
25-
# Here flag need to be hashed
25+
raise ValidationError("Start port should be less than end port") # Raise ValidationError if start_port is greater than end_port
26+
if self.flag:
27+
if not self.flag.startswith("hashed_"):
28+
self.flag = "hashed_" + hashlib.sha256(self.flag.encode('utf-8')).hexdigest()
2629
super(Challenge, self).save(*args, **kwargs)
2730

2831
class UserChallenge(models.Model):
@@ -40,7 +43,6 @@ class UserChallenge(models.Model):
4043
is_live = models.BooleanField(default=False)
4144
no_of_attempt = models.IntegerField(default=0)
4245
is_solved = models.BooleanField(default=False)
43-
port = models.IntegerField()
4446

4547
def __str__(self):
46-
return f"{self.user.username} - {self.challenge.name}"
48+
return f"{self.user.username} - {self.challenge.name}"

0 commit comments

Comments
 (0)