Skip to content

Commit c5f14c1

Browse files
authored
Merge pull request #1646 from hashtopolis/getCracksOfTask
Added helper endpoint to get the cracks of a task
2 parents 955dbfe + d67c3dc commit c5f14c1

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

src/api/v2/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ public static function addCORSheaders(Request $request, $response) {
306306
require __DIR__ . "/../../inc/apiv2/helper/exportWordlist.routes.php";
307307
require __DIR__ . "/../../inc/apiv2/helper/getAccessGroups.routes.php";
308308
require __DIR__ . "/../../inc/apiv2/helper/getAgentBinary.routes.php";
309+
require __DIR__ . "/../../inc/apiv2/helper/getCracksOfTask.routes.php";
309310
require __DIR__ . "/../../inc/apiv2/helper/getFile.routes.php";
310311
require __DIR__ . "/../../inc/apiv2/helper/getUserPermission.routes.php";
311312
require __DIR__ . "/../../inc/apiv2/helper/importCrackedHashes.routes.php";
@@ -319,5 +320,4 @@ public static function addCORSheaders(Request $request, $response) {
319320
require __DIR__ . "/../../inc/apiv2/helper/taskExtraDetails.routes.php";
320321
require __DIR__ . "/../../inc/apiv2/helper/unassignAgent.routes.php";
321322

322-
323323
$app->run();
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
use DBA\Chunk;
4+
use DBA\ContainFilter;
5+
use JetBrains\PhpStorm\NoReturn;
6+
use Psr\Http\Message\ResponseInterface as Response;
7+
use Psr\Http\Message\ServerRequestInterface as Request;
8+
use DBA\Factory;
9+
use DBA\Hash;
10+
use DBA\Hashlist;
11+
use DBA\QueryFilter;
12+
use DBA\Task;
13+
use Middlewares\Utils\HttpErrorException;
14+
15+
require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php");
16+
17+
class GetCracksOfTaskHelper extends AbstractHelperAPI {
18+
public static function getBaseUri(): string {
19+
return "/api/v2/helper/getCracksOfTask";
20+
}
21+
22+
public static function getAvailableMethods(): array {
23+
return ['GET'];
24+
}
25+
26+
public function getRequiredPermissions(string $method): array {
27+
return [Hashlist::PERM_READ, Hash::PERM_READ, Task::PERM_READ];
28+
}
29+
30+
public static function getResponse(): null {
31+
return null;
32+
}
33+
34+
35+
#[NoReturn] public function actionPost(array $data): object|array|null {
36+
assert(False, "getCracksOfTask has no POST");
37+
}
38+
39+
/**
40+
* Description of get params for swagger.
41+
*/
42+
public function getParamsSwagger(): array {
43+
return [
44+
[
45+
"in" => "query",
46+
"name" => "task",
47+
"schema" => [
48+
"type" => "integer",
49+
"format" => "int32"
50+
],
51+
"required" => true,
52+
"example" => 1,
53+
"description" => "The ID of the task."
54+
]
55+
];
56+
}
57+
58+
/**
59+
* Endpoint to get the cracked hashes of a certain task
60+
* @param Request $request
61+
* @param Response $response
62+
* @return Response
63+
* @throws HttpErrorException
64+
*/
65+
public function handleGet(Request $request, Response $response): Response {
66+
$this->preCommon($request);
67+
$task = Factory::getTaskFactory()->get($request->getQueryParams()['task']);
68+
if ($task == null) {
69+
throw new HttpError("No task has been found with provided task id");
70+
}
71+
$hashlists = Util::checkSuperHashlist(Factory::getHashlistFactory()->get(Factory::getTaskWrapperFactory()->get($task->getTaskWrapperId())->getHashlistId()));
72+
if ($hashlists[0]->getFormat() == DHashlistFormat::PLAIN) {
73+
$hashFactory = Factory::getHashFactory();
74+
}
75+
else {
76+
$hashFactory = Factory::getHashBinaryFactory();
77+
}
78+
$qF = new QueryFilter(Chunk::TASK_ID, $task->getId(), "=");
79+
$chunks = Factory::getChunkFactory()->filter([Factory::FILTER => $qF]);
80+
$chunkIds = array();
81+
foreach ($chunks as $chunk) {
82+
$chunkIds[] = $chunk->getId();
83+
}
84+
$queryFilters[] = new ContainFilter(Hash::CHUNK_ID, $chunkIds);
85+
$queryFilters[] = new QueryFilter(Hash::IS_CRACKED, 1, "=");
86+
$hashes = $hashFactory->filter([Factory::FILTER => $queryFilters]);
87+
$converted = [];
88+
89+
foreach ($hashes as $hash) {
90+
$converted[] = self::obj2Resource($hash);
91+
}
92+
$ret = self::createJsonResponse(data: $converted);
93+
94+
$body = $response->getBody();
95+
$body->write($this->ret2json($ret));
96+
97+
return $response->withStatus(200)
98+
->withHeader("Content-Type", 'application/vnd.api+json;');
99+
}
100+
101+
static public function register($app): void {
102+
$baseUri = GetCracksOfTaskHelper::getBaseUri();
103+
104+
/* Allow CORS preflight requests */
105+
$app->options($baseUri, function (Request $request, Response $response): Response {
106+
return $response;
107+
});
108+
$app->get($baseUri, "getCracksOfTaskHelper:handleGet");
109+
}
110+
}
111+
112+
GetCracksOfTaskHelper::register($app);

0 commit comments

Comments
 (0)