fix exam exit

This commit is contained in:
xiaomlove
2023-04-29 03:46:14 +08:00
parent 10cd69852c
commit bc926b798f
7 changed files with 114 additions and 32 deletions

View File

@@ -87,3 +87,6 @@ MEILISEARCH_HOST=127.0.0.1
MEILISEARCH_PORT=7700
MEILISEARCH_MASTER_KEY=
CACHE_KEY_AGENT_ALLOW=all_agent_allows
CACHE_KEY_AGENT_DENY=all_agent_denies

View File

@@ -97,8 +97,9 @@ class Test extends Command
*/
public function handle()
{
$rep = new MeiliSearchRepository();
$rep->import();
$rep = new ExamRepository();
$r = $rep->listMatchExam(1);
dd($r);
}
}

View File

@@ -12,7 +12,7 @@ class Exam extends NexusModel
public $timestamps = true;
protected $casts = [
'filters' => 'object',
'filters' => 'array',
'indexes' => 'array',
];
@@ -116,25 +116,27 @@ class Exam extends NexusModel
$currentFilters = $this->filters;
$arr = [];
$filter = self::FILTER_USER_CLASS;
if (!empty($currentFilters->{$filter})) {
$classes = collect(User::$classes)->only($currentFilters->{$filter});
if (!empty($currentFilters[$filter])) {
$classes = collect(User::$classes)->only($currentFilters[$filter]);
$arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $classes->pluck('text')->implode(', '));
}
$filter = self::FILTER_USER_REGISTER_TIME_RANGE;
if (!empty($currentFilters->{$filter})) {
$range = $currentFilters->{$filter};
$arr[] = sprintf(
"%s: <br/>%s ~ %s",
nexus_trans("exam.filters.$filter"),
$range[0] ? Carbon::parse($range[0])->toDateTimeString() : '--',
$range[1] ? Carbon::parse($range[1])->toDateTimeString() : '--'
);
if (!empty($currentFilters[$filter])) {
$range = $currentFilters[$filter];
if (!empty($range[0]) || !empty($range[1])) {
$arr[] = sprintf(
"%s: <br/>%s ~ %s",
nexus_trans("exam.filters.$filter"),
$range[0] ? Carbon::parse($range[0])->toDateTimeString() : '--',
$range[1] ? Carbon::parse($range[1])->toDateTimeString() : '--'
);
}
}
$filter = self::FILTER_USER_DONATE;
if (!empty($currentFilters->{$filter})) {
$donateStatus = $classes = collect(User::$donateStatus)->only($currentFilters->{$filter});
if (!empty($currentFilters[$filter])) {
$donateStatus = collect(User::$donateStatus)->only($currentFilters[$filter]);
$arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $donateStatus->pluck('text')->implode(', '));
}

View File

@@ -74,7 +74,8 @@ class AgentAllowRepository extends BaseRepository
public function checkClient($peerId, $agent, $debug = false)
{
//check from high version to low version, if high version allow, stop!
$allows = NexusDB::remember("all_agent_allows", 3600, function () {
$cacheKey = nexus_env("CACHE_KEY_AGENT_ALLOW", "all_agent_allows") . ":php";
$allows = NexusDB::remember($cacheKey, 3600, function () {
return AgentAllow::query()
->orderBy('peer_id_start', 'desc')
->orderBy('agent_start', 'desc')
@@ -190,8 +191,9 @@ class AgentAllowRepository extends BaseRepository
private function checkIsDenied($peerId, $agent, $familyId)
{
$cacheKey = nexus_env("CACHE_KEY_AGENT_DENY", "all_agent_denies") . ":php";
/** @var Collection $allDenies */
$allDenies = NexusDB::remember("all_agent_denies", 3600, function () {
$allDenies = NexusDB::remember($cacheKey, 3600, function () {
return AgentDeny::query()->get()->groupBy('family_id');
});
$agentDenies = $allDenies->get($familyId, []);
@@ -264,4 +266,71 @@ class AgentAllowRepository extends BaseRepository
}
public function checkClientSimple($peerId, $agent, $debug = false)
{
//check from high version to low version, if high version allow, stop!
$cacheKey = nexus_env("CACHE_KEY_AGENT_ALLOW", "all_agent_allows") . ":php";
$allows = NexusDB::remember($cacheKey, 3600, function () {
return AgentAllow::query()
->orderBy('peer_id_start', 'desc')
->orderBy('agent_start', 'desc')
->get();
});
$agentAllowPassed = null;
foreach ($allows as $agentAllow) {
$agentAllowId = $agentAllow->id;
$agentAllowLogPrefix = "[ID: $agentAllowId], peerId: $peerId";
$pattern = $agentAllow->peer_id_pattern;
//check peer_id, when handle scrape request, no peer_id, so let it pass
$isPeerIdAllowed = empty($pattern) || preg_match($pattern, $peerId);
$agentAllowLogPrefix .= ", peer_id pattern: $pattern, isPeerIdAllowed: $isPeerIdAllowed";
//check agent, agent must have both announce + scrape
$pattern = $agentAllow->agent_pattern;
$isAgentAllowed = !empty($pattern) && preg_match($pattern, $agent);
$agentAllowLogPrefix .= ", agent pattern: $pattern, isAgentAllowed: $isAgentAllowed";
//both OK, passed, client is allowed
if ($isPeerIdAllowed && $isAgentAllowed) {
$agentAllowPassed = $agentAllow;
do_log("$agentAllowLogPrefix, PASSED", 'debug');
break;
}
if ($debug) {
do_log("$agentAllowLogPrefix, NOT PASSED", 'debug');
}
}
if (!$agentAllowPassed) {
throw new ClientNotAllowedException("Banned Client, Please goto " . getSchemeAndHttpHost() . "/faq.php#id29 for a list of acceptable clients");
}
if ($debug) {
do_log("agentAllowPassed: " . $agentAllowPassed->toJson(), 'debug');
}
// check if exclude
if ($agentAllowPassed->exception == 'yes') {
$agentDeny = $this->checkIsDenied($peerId, $agent, $agentAllowPassed->id);
if ($agentDeny) {
if ($debug) {
do_log("agentDeny: " . $agentDeny->toJson());
}
throw new ClientNotAllowedException(sprintf(
"[%s-%s]Client: %s is banned due to: %s",
$agentAllowPassed->id, $agentDeny->id, $agentDeny->name, $agentDeny->comment
));
}
}
if (isHttps() && $agentAllowPassed->allowhttps != 'yes') {
throw new ClientNotAllowedException(sprintf(
"[%s]This client does not support https well, Please goto %s/faq.php#id29 for a list of proper clients",
$agentAllowPassed->id, getSchemeAndHttpHost()
));
}
return $agentAllowPassed;
}
}

View File

@@ -251,21 +251,24 @@ class ExamRepository extends BaseRepository
$filters = $exam->filters;
$filter = Exam::FILTER_USER_CLASS;
if (!empty($filters->{$filter}) && !in_array($user->class, $filters->{$filter})) {
do_log("$logPrefix, user class: {$user->class} not in: " . json_encode($filters->{$filter}));
$filterValues = $filters[$filter];
if (!empty($filterValues) && !in_array($user->class, $filterValues)) {
do_log("$logPrefix, user class: {$user->class} not in: " . json_encode($filterValues));
return false;
}
$filter = Exam::FILTER_USER_DONATE;
if (!empty($filters->{$filter}) && !in_array($user->donate_status, $filters->{$filter})) {
do_log("$logPrefix, user donate status: {$user->donate_status} not in: " . json_encode($filters->{$filter}));
$filterValues = $filters[$filter];
if (!empty($filterValues) && !in_array($user->donate_status, $filterValues)) {
do_log("$logPrefix, user donate status: {$user->donate_status} not in: " . json_encode($filterValues));
return false;
}
$filter = Exam::FILTER_USER_REGISTER_TIME_RANGE;
$filterValues = $filters[$filter];
$added = $user->added->toDateTimeString();
$registerTimeBegin = isset($filters->{$filter}[0]) ? Carbon::parse($filters->{$filter}[0])->toDateTimeString() : '';
$registerTimeEnd = isset($filters->{$filter}[1]) ? Carbon::parse($filters->{$filter}[1])->toDateTimeString() : '';
$registerTimeBegin = isset($filterValues[0]) ? Carbon::parse($filterValues[0])->toDateTimeString() : '';
$registerTimeEnd = isset($filterValues[1]) ? Carbon::parse($filterValues[1])->toDateTimeString() : '';
if (!empty($registerTimeBegin) && $added < $registerTimeBegin) {
do_log("$logPrefix, user added: $added not bigger than begin: " . $registerTimeBegin);
return false;
@@ -839,13 +842,13 @@ class ExamRepository extends BaseRepository
->orderBy("$userTable.id", "asc");
$filter = Exam::FILTER_USER_CLASS;
if (!empty($filters->$filter)) {
$baseQuery->whereIn("$userTable.class", $filters->$filter);
if (!empty($filters[$filter])) {
$baseQuery->whereIn("$userTable.class", $filters[$filter]);
}
$filter = Exam::FILTER_USER_DONATE;
if (!empty($filters->$filter) && count($filters->$filter) == 1) {
$donateStatus = $filters->$filter[0];
if (!empty($filters[$filter]) && count($filters[$filter]) == 1) {
$donateStatus = $filters[$filter][0];
if ($donateStatus == User::DONATE_YES) {
$baseQuery->where(function (Builder $query) {
$query->where('donor', 'yes')->where(function (Builder $query) {
@@ -865,7 +868,7 @@ class ExamRepository extends BaseRepository
}
$filter = Exam::FILTER_USER_REGISTER_TIME_RANGE;
$range = $filters->$filter;
$range = $filters[$filter];
if (!empty($range)) {
if (!empty($range[0])) {
$baseQuery->where("$userTable.added", ">=", Carbon::parse($range[0])->toDateTimeString());

View File

@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.1');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-04-21');
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.2');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-04-29');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");

View File

@@ -1070,8 +1070,12 @@ function clear_inbox_count_cache($uid)
function clear_agent_allow_deny_cache()
{
do_log("clear_agent_allow_deny_cache");
\Nexus\Database\NexusDB::cache_del("all_agent_allows");
\Nexus\Database\NexusDB::cache_del("all_agent_denies");
$allowCacheKey = nexus_env("CACHE_KEY_AGENT_ALLOW", "all_agent_allows");
$denyCacheKey = nexus_env("CACHE_KEY_AGENT_DENY", "all_agent_denies");
foreach (["", ":php", ":go"] as $suffix) {
\Nexus\Database\NexusDB::cache_del($allowCacheKey . $suffix);
\Nexus\Database\NexusDB::cache_del($denyCacheKey . $suffix);
}
}