Commit 1528042f authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

enhance proxy

parent 648f7301
...@@ -34,7 +34,7 @@ public abstract class Task ...@@ -34,7 +34,7 @@ public abstract class Task
{ {
} }
public abstract String getTextForUser(User user) throws IOException; public abstract String getTextForUser(User user, String path) throws IOException;
public String getName() public String getName()
{ {
......
...@@ -23,7 +23,7 @@ public class TextTask extends Task ...@@ -23,7 +23,7 @@ public class TextTask extends Task
{ {
} }
public String getTextForUser(User user) public String getTextForUser(User user, String path)
{ {
return text; return text;
} }
......
package com.telephoners.krakyournet.ctf.beans.tasks; package com.telephoners.krakyournet.ctf.beans.tasks;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.base.Joiner;
import com.telephoners.krakyournet.ctf.beans.Flag; import com.telephoners.krakyournet.ctf.beans.Flag;
import com.telephoners.krakyournet.ctf.beans.User; import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.helpers.StreamUtils; import com.telephoners.krakyournet.ctf.helpers.StreamUtils;
...@@ -20,6 +21,7 @@ import java.util.List; ...@@ -20,6 +21,7 @@ import java.util.List;
public class WebTask extends Task public class WebTask extends Task
{ {
private String url; private String url;
private final Joiner urlJoiner = Joiner.on("/");
public WebTask(String name, int level, List<Flag> flags, String url) public WebTask(String name, int level, List<Flag> flags, String url)
{ {
...@@ -31,9 +33,10 @@ public class WebTask extends Task ...@@ -31,9 +33,10 @@ public class WebTask extends Task
{ {
} }
public String getTextForUser(User user) throws IOException public String getTextForUser(User user, String path) throws IOException
{ {
return StreamUtils.readStream(proxyRequest(getUrl(), user)); String url = urlJoiner.join(getUrl(), path);
return StreamUtils.readStream(proxyRequest(url, user));
} }
public String getUrl() public String getUrl()
......
package com.telephoners.krakyournet.ctf.resources;
import com.telephoners.krakyournet.ctf.beans.User;
import io.dropwizard.auth.Auth;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.InputStream;
@Path(value = "/proxy/task")
@Produces(MediaType.APPLICATION_JSON)
public class ProxyResource {
@GET
@Path("{task_id}/{path: .*}")
@Produces("text/html; charset=UTF-8")
public InputStream task1(@Auth User user,
final @PathParam("task_id") String taskId,
final @PathParam("path") String path) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://192.168.56.101/");
httpget.setHeader("CTF-User", user.getName());
CloseableHttpResponse execute = httpClient.execute(httpget);
HttpEntity entity = execute.getEntity();
return entity.getContent();
}
}
package com.telephoners.krakyournet.ctf.resources; package com.telephoners.krakyournet.ctf.resources;
import com.google.common.base.Joiner;
import com.telephoners.krakyournet.ctf.beans.User; import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.beans.tasks.Task; import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository; import com.telephoners.krakyournet.ctf.repositories.TasksRepository;
...@@ -25,12 +26,14 @@ public class TaskResource ...@@ -25,12 +26,14 @@ public class TaskResource
this.tasksRepository = tasksRepository; this.tasksRepository = tasksRepository;
} }
@Path("{task_level}") @Path("{task_level}/{path: .*}")
@GET @GET
public Response getTask(@Auth User user, public Response getTask(@Auth User user,
final @PathParam("task_level") int taskLevel) throws IOException final @PathParam("task_level") int taskLevel,
final @PathParam("path") String path) throws IOException
{ {
Task task = tasksRepository.getByLevel(taskLevel); Task task = tasksRepository.getByLevel(taskLevel);
return Response.ok().entity(task.getTextForUser(user)).build(); //todo: refactor, path not necessary in textTasks
return Response.ok().entity(task.getTextForUser(user, path)).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