Commit b1f18eb9 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

huge tasks refactoring

parent d6b0bd3e
......@@ -3,21 +3,15 @@ package com.telephoners.krakyournet.ctf.commands;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import com.telephoners.krakyournet.ctf.core.TaskType;
import com.telephoners.krakyournet.ctf.helpers.TextTaskConfig;
import com.telephoners.krakyournet.ctf.helpers.WebTaskConfig;
import com.telephoners.krakyournet.ctf.modules.ApplicationModule;
import com.telephoners.krakyournet.ctf.objects.Flag;
import com.telephoners.krakyournet.ctf.objects.tasks.Task;
import com.telephoners.krakyournet.ctf.objects.tasks.TaskCryptoContent;
import com.telephoners.krakyournet.ctf.objects.tasks.TaskWebContent;
import com.telephoners.krakyournet.ctf.objects.tasks.TextTask;
import com.telephoners.krakyournet.ctf.objects.tasks.WebTask;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository;
import io.dropwizard.cli.ConfiguredCommand;
import io.dropwizard.setup.Bootstrap;
import net.sourceforge.argparse4j.inf.Namespace;
import java.util.List;
import java.util.stream.Collectors;
public class RegisterTasksCommand extends ConfiguredCommand<ApplicationConfiguration>
{
......@@ -33,36 +27,13 @@ public class RegisterTasksCommand extends ConfiguredCommand<ApplicationConfigura
{
TasksRepository tasksRepository = injector.getInstance(TasksRepository.class);
List<TextTaskConfig> cryptoTasks = applicationConfiguration.getTextTasks();
List<WebTaskConfig> webTasks = applicationConfiguration.getWebTasks();
List<TextTask> cryptoTasks = applicationConfiguration.getTextTasks();
List<WebTask> webTasks = applicationConfiguration.getWebTasks();
tasksRepository.clean();
cryptoTasks.forEach(cryptoTaskConfig -> {
cryptoTaskConfig.getFlags().stream()
.map(Flag::new)
.collect(Collectors.toList());
tasksRepository.add(new Task(
cryptoTaskConfig.getName(),
cryptoTaskConfig.getLevel(),
TaskType.TEXT,
cryptoTaskConfig.getFlags().stream()
.map(Flag::new)
.collect(Collectors.toList()),
new TaskCryptoContent(cryptoTaskConfig.getText())
));
});
webTasks.forEach(webTaskConfig -> tasksRepository.add(new Task(
webTaskConfig.getName(),
webTaskConfig.getLevel(),
TaskType.WEB,
webTaskConfig.getFlags().stream()
.map(Flag::new)
.collect(Collectors.toList()),
new TaskWebContent(webTaskConfig.getUrl())
)));
cryptoTasks.forEach(tasksRepository::add);
webTasks.forEach(tasksRepository::add);
}
@Override
......
package com.telephoners.krakyournet.ctf.core;
import com.telephoners.krakyournet.ctf.helpers.TextTaskConfig;
import com.telephoners.krakyournet.ctf.helpers.WebTaskConfig;
import com.telephoners.krakyournet.ctf.objects.Team;
import com.telephoners.krakyournet.ctf.objects.User;
import com.telephoners.krakyournet.ctf.objects.tasks.Task;
import com.telephoners.krakyournet.ctf.objects.tasks.TextTask;
import com.telephoners.krakyournet.ctf.objects.tasks.WebTask;
import io.dropwizard.Configuration;
import java.util.List;
......@@ -15,8 +16,8 @@ public class ApplicationConfiguration extends Configuration
private String dbName;
private String flagHashMethod;
private List<Team> teams;
private List<TextTaskConfig> textTasks;
private List<WebTaskConfig> webTasks;
private List<TextTask> textTasks;
private List<WebTask> webTasks;
private List<User> admins;
public List<User> getAdmins()
......@@ -29,26 +30,6 @@ public class ApplicationConfiguration extends Configuration
this.admins = admins;
}
public List<TextTaskConfig> getTextTasks()
{
return textTasks;
}
public void setTextTasks(List<TextTaskConfig> textTasks)
{
this.textTasks = textTasks;
}
public List<WebTaskConfig> getWebTasks()
{
return webTasks;
}
public void setWebTasks(List<WebTaskConfig> webTasks)
{
this.webTasks = webTasks;
}
public String getFlagHashMethod()
{
return flagHashMethod;
......@@ -98,4 +79,24 @@ public class ApplicationConfiguration extends Configuration
{
this.dbName = dbName;
}
public List<TextTask> getTextTasks()
{
return textTasks;
}
public void setTextTasks(List<TextTask> textTasks)
{
this.textTasks = textTasks;
}
public List<WebTask> getWebTasks()
{
return webTasks;
}
public void setWebTasks(List<WebTask> webTasks)
{
this.webTasks = webTasks;
}
}
package com.telephoners.krakyournet.ctf.helpers;
import java.util.List;
public class TextTaskConfig
{
private String name;
private int level;
private String text;
private List<String> flags;
//todo: refactor, map directly to Flag class?
public String getName() {
return name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public void setName(String name) {
this.name = name;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<String> getFlags() {
return flags;
}
public void setFlags(List<String> flags) {
this.flags = flags;
}
}
package com.telephoners.krakyournet.ctf.helpers;
import java.util.List;
/**
* Created by gpietrus on 23.02.2016.
*/
public class WebTaskConfig {
private String name;
private String url;
private int level;
private List<String> flags;
public String getName() {
return name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<String> getFlags() {
return flags;
}
public void setFlags(List<String> flags) {
this.flags = flags;
}
}
package com.telephoners.krakyournet.ctf.objects;
import java.util.Random;
/**
* Created by gpietrus on 20.02.2016.
*/
public class Flag {
private String value;
private static int flagLength = 32;
private String description;
public Flag() {
}
public Flag(String value) {
public Flag(String value, String description) {
this.value = value;
}
public static Flag newRandomFlag() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("AGH_CTF_");
for(int i = 0; i < flagLength; i++) {
char c = (char) (new Random().nextInt(128 - 32) + 32);
stringBuilder.append(c);
}
return new Flag(stringBuilder.toString());
this.description = description;
}
public String getValue() {
......@@ -33,4 +19,14 @@ public class Flag {
public void setValue(String value) {
this.value = value;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
......@@ -12,11 +12,13 @@ public class Solution
private ObjectId id;
private Task task;
private Team team;
private Flag flag;
public Solution(Team team, Task task)
public Solution(Team team, Task task, Flag flag)
{
this.team = team;
this.task = task;
this.flag = flag;
}
public Solution()
......@@ -42,4 +44,14 @@ public class Solution
{
return team;
}
public Flag getFlag()
{
return flag;
}
public void setFlag(Flag flag)
{
this.flag = flag;
}
}
......@@ -12,25 +12,21 @@ import java.util.List;
@Entity("tasks")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Task
public abstract class Task
{
@Id
private ObjectId id;
protected ObjectId id;
@PublicProperty
private String name;
protected String name;
@PublicProperty
private int level;
private TaskType taskType;
private List<Flag> flags;
private TaskContent taskContent;
protected int level;
protected List<Flag> flags;
public Task(String name, int level, TaskType type, List<Flag> flags, TaskContent taskContent)
public Task(String name, int level, List<Flag> flags)
{
this.flags = flags;
this.taskType = type;
this.level = level;
this.name = name;
this.taskContent = taskContent;
this.level = level;
this.flags = flags;
}
public Task()
......@@ -57,16 +53,6 @@ public class Task
this.level = level;
}
public TaskType getTaskType()
{
return taskType;
}
public void setTaskType(TaskType taskType)
{
this.taskType = taskType;
}
public List<Flag> getFlags()
{
return flags;
......@@ -76,14 +62,4 @@ public class Task
{
this.flags = flags;
}
public TaskContent getTaskContent()
{
return taskContent;
}
public void setTaskContent(TaskContent taskContent)
{
this.taskContent = taskContent;
}
}
package com.telephoners.krakyournet.ctf.objects.tasks;
/**
* Created by gpietrus on 23.02.2016.
*/
public abstract class TaskContent {
}
package com.telephoners.krakyournet.ctf.objects.tasks;
/**
* Created by gpietrus on 23.02.2016.
*/
public class TaskCryptoContent extends TaskContent
{
private String text;
public TaskCryptoContent(String text) {
this.text = text;
}
public TaskCryptoContent() {}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
package com.telephoners.krakyournet.ctf.objects.tasks;
/**
* Created by gpietrus on 23.02.2016.
*/
public class TaskWebContent extends TaskContent
{
private String url;
public TaskWebContent(String url) {
this.url = url;
}
public TaskWebContent() {}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
package com.telephoners.krakyournet.ctf.objects.tasks;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.telephoners.krakyournet.ctf.objects.Flag;
import org.mongodb.morphia.annotations.Entity;
import java.util.List;
@Entity("tasks")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TextTask extends Task
{
private String text;
public TextTask(String name, int level, List<Flag> flags, String text)
{
super(name, level, flags);
this.text = text;
}
public TextTask()
{
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
}
package com.telephoners.krakyournet.ctf.objects.tasks;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.telephoners.krakyournet.ctf.objects.Flag;
import org.mongodb.morphia.annotations.Entity;
import java.util.List;
@Entity("tasks")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class WebTask extends Task
{
private String url;
public WebTask(String name, int level, List<Flag> flags, String url)
{
super(name, level, flags);
this.url = url;
}
public WebTask()
{
}
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
}
......@@ -2,8 +2,10 @@ package com.telephoners.krakyournet.ctf.repositories;
import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import com.telephoners.krakyournet.ctf.helpers.DBObjectUtils;
import com.telephoners.krakyournet.ctf.objects.Flag;
import com.telephoners.krakyournet.ctf.objects.Solution;
import com.telephoners.krakyournet.ctf.objects.Team;
import com.telephoners.krakyournet.ctf.objects.User;
import com.telephoners.krakyournet.ctf.objects.tasks.Task;
import org.apache.commons.codec.binary.Hex;
import org.mongodb.morphia.Datastore;
......@@ -15,6 +17,8 @@ import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@Singleton
......@@ -25,7 +29,7 @@ public class TasksRepository implements Repository
private Datastore datastore;
private TeamsRepository teamsRepository;
private SolutionsRepository solutionsRepository;
private String salt = "SECURE_SALT"; //todo
private String salt = "SECURE_SALT"; //todo: move to configuration!
@Inject
public TasksRepository(ApplicationConfiguration applicationConfiguration, Datastore datastore,
......@@ -83,6 +87,44 @@ public class TasksRepository implements Repository
));
}
private Optional<Task> getTaskFlagByHashValue(User user, String userHash)
{
String username = user.getName();
//todo: inline
//todo: collapse lambdas
List<Task> tasks = this.getAll();
Optional<Task> task = tasks.stream()
.filter(new Predicate<Task>()
{
@Override
public boolean test(Task task)
{
return task.getFlags()
.stream()
.map(new Function<Flag, String>()
{
@Override
public String apply(Flag flag)
{
return calculateHashValue(username, flag.getValue());
}
})
.filter(new Predicate<String>()
{
@Override
public boolean test(String flagValue)
{
return flagValue.equals(userHash);
}
})
.findFirst()
.isPresent();
}
})
.findFirst();
return task;
}
public String calculateHashValue(String username, String flagValue)
{ //todo
String combinedStrings = salt + username + flagValue; //todo
......@@ -96,12 +138,14 @@ public class TasksRepository implements Repository
return encodedHash;
}
public boolean checkFlag(String username, String flagValue)
public boolean checkFlag(User user, String hashValue)
{
Optional<Task> task = getByUserFlag(username, flagValue);
Optional<Team> team = teamsRepository.getTeamByUserName(username);
Optional<Task> task = getTaskFlagByHashValue(user, hashValue);
// Optional<Task> task = getByUserFlag(username, hashValue);
Optional<Team> team = teamsRepository.getTeamByUser(user);
if (task.isPresent() && team.isPresent()) {
solutionsRepository.add(new Solution(team.get(), task.get()));
solutionsRepository.add(new Solution(team.get(), task.get(), null)); //todo
return true;
}
return false;
......
......@@ -37,7 +37,7 @@ public class SolutionsResource {
public Response submitSolution(@Auth User user,
String flag) throws Exception
{
if (tasksRepository.checkFlag(user.getName(), flag)) {
if (tasksRepository.checkFlag(user, flag)) {
return Response.ok().build();
}
return Response.status(Response.Status.NOT_ACCEPTABLE).build();
......
......@@ -2,8 +2,6 @@ package com.telephoners.krakyournet.ctf.resources;
import com.telephoners.krakyournet.ctf.objects.User;
import com.telephoners.krakyournet.ctf.objects.tasks.Task;
import com.telephoners.krakyournet.ctf.objects.tasks.TaskCryptoContent;
import com.telephoners.krakyournet.ctf.objects.tasks.TaskWebContent;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository;
import io.dropwizard.auth.Auth;
import org.apache.http.HttpEntity;
......@@ -49,14 +47,15 @@ public class TaskResource
String taskText = null;
switch (task.getTaskType()) {
/*switch (task.getTaskType()) {
case TEXT:
taskText = ((TaskCryptoContent) task.getTaskContent()).getText();
break;
case WEB:
taskText = readStream(proxyRequest(((TaskWebContent) task.getTaskContent()).getUrl(), user));
break;
}
}*/
//todo!!!!!
return Response.ok().entity(taskText).build();
}
......
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 to comment