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

@@ -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());