diff --git a/.env.example b/.env.example
index cc54ed2f..5f48a793 100644
--- a/.env.example
+++ b/.env.example
@@ -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
+
diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php
index 7d5313ce..f12530d6 100644
--- a/app/Console/Commands/Test.php
+++ b/app/Console/Commands/Test.php
@@ -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);
}
}
diff --git a/app/Models/Exam.php b/app/Models/Exam.php
index 11fc524d..ccdc1259 100644
--- a/app/Models/Exam.php
+++ b/app/Models/Exam.php
@@ -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:
%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:
%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(', '));
}
diff --git a/app/Repositories/AgentAllowRepository.php b/app/Repositories/AgentAllowRepository.php
index 1b261d56..7512ddf4 100644
--- a/app/Repositories/AgentAllowRepository.php
+++ b/app/Repositories/AgentAllowRepository.php
@@ -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;
+
+ }
+
}
diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php
index 0db572af..0fc5c270 100644
--- a/app/Repositories/ExamRepository.php
+++ b/app/Repositories/ExamRepository.php
@@ -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());
diff --git a/include/constants.php b/include/constants.php
index f9c4fa02..365c8bd7 100644
--- a/include/constants.php
+++ b/include/constants.php
@@ -1,6 +1,6 @@