Версия 0.8.0 #4

Merged
kor-elf merged 8 commits from develop into main 2024-04-28 14:26:47 +05:00
412 changed files with 3998 additions and 1598 deletions

View File

@ -1,68 +1,9 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_FORCE_HTTPS=false
APP_DEMO_MODE=false
APP_DEMO_EMAIL=
APP_DEMO_PASSWORD=
APP_DEFAULT_USER_TIMEZONE=UTC
# Valid languages: ru | en
APP_DEFAULT_LOCALE=ru
LOG_CHANNEL=daily
LOG_DEPRECATIONS_CHANNEL=deprecations
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
DOCKER_NGINX_PORT=8080
DOCKER_WEBSOCKET_PORT=8081
DOCKER_DB_PORT=3306
MYSQL_ROOT_PASSWORD=root_pass
DB_DATABASE=captcha
DB_USERNAME=captcha
DB_PASSWORD=captcha_pass
UID=1000
GID=1000

21
.gitignore vendored
View File

@ -1,19 +1,6 @@
/.phpunit.cache
/node_modules
/public/build
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.env.production
.phpunit.result.cache
Homestead.json
Homestead.yaml
auth.json
npm-debug.log
yarn-error.log
/.fleet
/.idea
/.vscode
Homestead.json
/.vagrant
.phpunit.result.cache

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2023 Leonid Nikitin (kor-elf)
Copyright (c) 2023 - 2024 Leonid Nikitin (kor-elf)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -2,9 +2,11 @@
Захотелось написать свой независимый сервис защиты от роботов. Сервис каптча написан на фреймворке Laravel. Вдохновлялся, а так же брал картинки с проекта <a href="https://github.com/wenlng/go-captcha" target="_blank">Go Captcha</a>.
[Сайт проекта](https://service-captcha.projects.kor-elf.net/)
## Зависимости
php 8.2 (модули: redis, gd)
php 8.3 (модули: redis, gd)
redis
@ -26,7 +28,7 @@ https://captcha-admin-demo.tut-site.net/api-docs/
https://git.kor-elf.net/kor-elf/service-captcha-gui
## Как проверять со стороны бэкенда
Для Laravel 10 есть готовый пакет: https://git.kor-elf.net/kor-elf/captcha-rule-for-laravel
Для Laravel 10, 11 есть готовый пакет: https://git.kor-elf.net/kor-elf/captcha-rule-for-laravel
Можно установить этот пакет так: composer require kor-elf/captcha-rule-for-laravel

View File

@ -1,13 +0,0 @@
<?php declare(strict_types=1);
namespace App\Http\Controllers\Private;
use Illuminate\View\View;
final class DashboardController extends Controller
{
public function index(): View
{
return view('private/dashboard/index');
}
}

View File

@ -1,35 +0,0 @@
<?php declare(strict_types=1);
namespace App\Repositories;
use App\Enums\CaptchaLogType;
use App\Models\CaptchaLog;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
final class CaptchaLogRepository
{
public function countByType(CaptchaLogType $type, ?int $captchaId = null): int
{
return CaptchaLog::query()
->when($captchaId, function (Builder $query, int $captchaId) {
$query->where('captcha_id', $captchaId);
})
->where('type', '=', $type)
->count();
}
public function getCaptchaLogsByTypes(array $types, ?int $captchaId = null, ?int $limit = null): Collection
{
return CaptchaLog::query()
->when($captchaId, function (Builder $query, int $captchaId) {
$query->where('captcha_id', $captchaId);
})
->when($limit, function (Builder $query, int $limit) {
$query->limit($limit);
})
->whereIn('type', $types)
->latest()
->get();
}
}

View File

@ -0,0 +1,92 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_FORCE_HTTPS=false
APP_CAPTCHA=false
CAPTCHA_API_DOMAIN=http://your-domain-captcha-or-IP:8081
CAPTCHA_PRIVATE_TOKEN=
CAPTCHA_STATIC_PATH=http://your-domain-captcha-or-IP:8081/captcha
CAPTCHA_PUBLIC_TOKEN=
APP_DEMO_MODE=false
APP_DEMO_EMAIL=
APP_DEMO_PASSWORD=
APP_DEFAULT_USER_TIMEZONE=UTC
# Valid languages: ru | en
APP_DEFAULT_LOCALE=ru
LOG_CHANNEL=daily
LOG_DEPRECATIONS_CHANNEL=deprecations
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=captcha
DB_USERNAME=captcha
DB_PASSWORD=captcha_pass
BROADCAST_DRIVER=reverb
CACHE_DRIVER=redis
FILESYSTEM_DISK=local
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
REVERB_APP_ID=
REVERB_APP_KEY=
REVERB_APP_SECRET=
REVERB_HOST="reverb"
REVERB_PORT=9000
REVERB_SCHEME=http
# * or localhost.com or localhost.com, localhost.net
REVERB_ALLOWED_ORIGINS="*"
REVERB_HOST_CLIENT="localhost"
REVERB_PORT_CLIENT=8081
REVERB_SCHEME_CLIENT=http
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}"
VITE_REVERB_PORT="${REVERB_PORT}"
VITE_REVERB_SCHEME="${REVERB_SCHEME}"

19
app/application/.gitignore vendored Normal file
View File

@ -0,0 +1,19 @@
/.phpunit.cache
/node_modules
/public/build
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.env.production
.phpunit.result.cache
Homestead.json
Homestead.yaml
auth.json
npm-debug.log
yarn-error.log
/.fleet
/.idea
/.vscode

View File

@ -0,0 +1,17 @@
<?php declare(strict_types=1);
namespace App\Broadcasting;
use App\Models\CaptchaToken;
use App\Models\User;
final readonly class CreatedCaptchaLog
{
/**
* Authenticate the user's access to the channel.
*/
public function join(User $user): bool
{
return $user->can('viewAny', CaptchaToken::class);
}
}

View File

@ -0,0 +1,38 @@
<?php declare(strict_types=1);
namespace App\Dto\Repository\CaptchaLogRepository;
use App\Enums\CaptchaLogType;
final class QuantityByDays
{
private array $days = [];
/**
* @param string $day ("Y-m-d")
* @param CaptchaLogType $type
* @param int $count
* @return void
*/
public function add(string $day, CaptchaLogType $type, int $count): void
{
if (!isset($this->days[$day])) {
$this->days[$day] = [];
}
$this->days[$day][$type->value] = $count;
}
/**
* @param string $day ("Y-m-d")
* @return array
*/
public function getDay(string $day): array
{
return $this->days[$day] ?? [];
}
public function getDays(): array
{
return $this->days;
}
}

View File

@ -8,4 +8,9 @@ enum CaptchaLogType: int
case Error = 2;
case Verified = 3;
case ReadVerified = 4;
public function getTitle(): string
{
return __('captcha_log_type.' . $this->name);
}
}

View File

@ -0,0 +1,55 @@
<?php declare(strict_types=1);
namespace App\Events;
use App\Helpers\Helpers;
use App\Models\CaptchaLog;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
final class CreatedCaptchaLog implements ShouldBroadcast
{
use SerializesModels, Dispatchable;
public array $chartCaptchaActivity = [];
public array $captchaLog = [];
public function __construct(CaptchaLog $captchaLog) {
$this->chartCaptchaActivity = [
'date' => $captchaLog->created_at->format('Y-m-d'),
'meta' => $captchaLog->type->getTitle(),
'type' => $captchaLog->type->value,
];
$link = '';
$title = '';
if ($captchaLog->captcha && $captchaLog->captcha->captchaToken) {
$link = route('captcha-tokens.edit', ['captcha_token' => $captchaLog->captcha->captcha_token_id], false);
$title = $captchaLog->captcha->captchaToken->title;
}
$ip = $captchaLog->ip;
$userAgent = $captchaLog->user_agent;
if (Helpers::isDemoMode()) {
$ip = __('Demo Mode');
$userAgent = __('Demo Mode');
}
$this->captchaLog = [
'created_at' => $captchaLog->created_at->format("d.m.Y H:i:s"),
'link' => $link,
'title' => $title,
'type' => $captchaLog->type->getTitle(),
'ip' => $ip,
'user_agent' => $userAgent,
'referer' => $captchaLog->referer,
];
}
public function broadcastOn(): Channel
{
return new PrivateChannel('chart-captcha-activity');
}
}

View File

@ -19,7 +19,9 @@ public function __construct(
public function login(): View
{
return view('public/login');
return view('public/login', [
'captcha' => config('app.captcha', false)
]);
}
public function authorization(AuthorizationRequest $request): RedirectResponse

View File

@ -0,0 +1,43 @@
<?php declare(strict_types=1);
namespace App\Http\Controllers\Private;
use App\Http\Resources\Private\Dashboard\ChartCaptchaActivity;
use App\Services\Private\DashboardService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\View\View;
final class DashboardController extends Controller
{
public function __construct(
private readonly DashboardService $dashboardService,
) { }
public function index(Request $request): View
{
$user = $request->user();
$limit = 30;
$with = ['captcha', 'captcha.captchaToken'];
$result = $this->dashboardService->captchaLog($user, $limit, $with);
if ($result->isError()) {
$this->errors($result);
}
return view('private/dashboard/index', $result->getData());
}
public function chartCaptchaActivity(Request $request): JsonResponse
{
$user = $request->user();
$from = Carbon::now()->subDays(7);
$to = Carbon::now();
$result = $this->dashboardService->chartCaptchaActivity($user, $from, $to);
if (!$result->isSuccess()) {
return response()->json($result->getData())->setStatusCode($result->getCode());
}
return response()->json(new ChartCaptchaActivity($result));
}
}

View File

@ -11,7 +11,7 @@
use Illuminate\Http\Request;
use Illuminate\View\View;
class UsersController extends Controller
final class UsersController extends Controller
{
public function __construct(
private readonly UserService $userService

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
namespace App\Http\Controllers\Private;
use App\Http\Resources\Private\Websockets\Setting;
use App\Services\Private\WebsocketService;
use Illuminate\Http\JsonResponse;
final class WebsocketsController extends Controller
{
public function __construct(
private readonly WebsocketService $websocketService,
) { }
public function settings(): JsonResponse
{
$result = $this->websocketService->settings();
if (!$result->isSuccess()) {
return response()->json($result->getData())->setStatusCode($result->getCode());
}
return response()->json(new Setting($result));
}
}

Some files were not shown because too many files have changed in this diff Show More