fix checkClient() all position equal not passed

This commit is contained in:
xiaomlove
2022-03-04 17:39:19 +08:00
parent 0665770ca5
commit c1c511b9fe
2 changed files with 22 additions and 11 deletions
+4 -1
View File
@@ -60,7 +60,10 @@ class Test extends Command
*/ */
public function handle() public function handle()
{ {
echo date('Y-m-d H:i:s', 1623820546); $peerId = '-TR3000-uff7q3z5126z';
$agent = 'Transmission/3.00';
$rep = new AgentAllowRepository();
$r = $rep->checkClient($peerId, $agent, true);
} }
} }
+18 -10
View File
@@ -73,6 +73,7 @@ class AgentAllowRepository extends BaseRepository
$versionTooLowStr = ''; $versionTooLowStr = '';
foreach ($allows as $agentAllow) { foreach ($allows as $agentAllow) {
$agentAllowId = $agentAllow->id; $agentAllowId = $agentAllow->id;
$logPrefix = "[ID: $agentAllowId]";
$isPeerIdAllowed = $isAgentAllowed = $isPeerIdTooLow = $isAgentTooLow = false; $isPeerIdAllowed = $isAgentAllowed = $isPeerIdTooLow = $isAgentTooLow = false;
//check peer_id //check peer_id
if ($agentAllow->peer_id_pattern == '') { if ($agentAllow->peer_id_pattern == '') {
@@ -83,15 +84,15 @@ class AgentAllowRepository extends BaseRepository
$matchType = $agentAllow->peer_id_matchtype; $matchType = $agentAllow->peer_id_matchtype;
$matchNum = $agentAllow->peer_id_match_num; $matchNum = $agentAllow->peer_id_match_num;
try { try {
$peerIdResult = $this->isAllowed($pattern, $start, $matchNum, $matchType, $peerId, $debug); $peerIdResult = $this->isAllowed($pattern, $start, $matchNum, $matchType, $peerId, $debug, $logPrefix);
if ($debug) { if ($debug) {
do_log( do_log(
"agentAllowId: $agentAllowId, peerIdResult: $peerIdResult, with parameters: " "$logPrefix, peerIdResult: $peerIdResult, with parameters: "
. nexus_json_encode(compact('pattern', 'start', 'matchNum', 'matchType', 'peerId')) . nexus_json_encode(compact('pattern', 'start', 'matchNum', 'matchType', 'peerId'))
); );
} }
} catch (\Exception $exception) { } catch (\Exception $exception) {
do_log("agent allow: {$agentAllow->id} check peer_id error: " . $exception->getMessage(), 'error'); do_log("$logPrefix, check peer_id error: " . $exception->getMessage(), 'error');
throw new NexusException("regular expression err for peer_id: " . $start . ", please ask sysop to fix this"); throw new NexusException("regular expression err for peer_id: " . $start . ", please ask sysop to fix this");
} }
if ($peerIdResult == 1) { if ($peerIdResult == 1) {
@@ -111,15 +112,15 @@ class AgentAllowRepository extends BaseRepository
$matchType = $agentAllow->agent_matchtype; $matchType = $agentAllow->agent_matchtype;
$matchNum = $agentAllow->agent_match_num; $matchNum = $agentAllow->agent_match_num;
try { try {
$agentResult = $this->isAllowed($pattern, $start, $matchNum, $matchType, $agent, $debug); $agentResult = $this->isAllowed($pattern, $start, $matchNum, $matchType, $agent, $debug, $logPrefix);
if ($debug) { if ($debug) {
do_log( do_log(
"agentAllowId: $agentAllowId, agentResult: $agentResult, with parameters: " "$logPrefix, agentResult: $agentResult, with parameters: "
. nexus_json_encode(compact('pattern', 'start', 'matchNum', 'matchType', 'agent')) . nexus_json_encode(compact('pattern', 'start', 'matchNum', 'matchType', 'agent'))
); );
} }
} catch (\Exception $exception) { } catch (\Exception $exception) {
do_log("agent allow: {$agentAllow->id} check agent error: " . $exception->getMessage(), 'error'); do_log("$logPrefix, check agent error: " . $exception->getMessage(), 'error');
throw new NexusException("regular expression err for agent: " . $start . ", please ask sysop to fix this"); throw new NexusException("regular expression err for agent: " . $start . ", please ask sysop to fix this");
} }
if ($agentResult == 1) { if ($agentResult == 1) {
@@ -199,16 +200,20 @@ class AgentAllowRepository extends BaseRepository
* @param $matchType * @param $matchType
* @param $value * @param $value
* @param bool $debug * @param bool $debug
* @param string $logPrefix
* @return int * @return int
* @throws NexusException * @throws NexusException
*/ */
private function isAllowed($pattern, $start, $matchNum, $matchType, $value, $debug = false): int private function isAllowed($pattern, $start, $matchNum, $matchType, $value, $debug = false, $logPrefix = ''): int
{ {
$matchBench = $this->getPatternMatches($pattern, $start, $matchNum); $matchBench = $this->getPatternMatches($pattern, $start, $matchNum);
if ($debug) { if ($debug) {
do_log("matchBench: " . nexus_json_encode($matchBench)); do_log("$logPrefix, matchBench: " . nexus_json_encode($matchBench));
} }
if (!preg_match($pattern, $value, $matchTarget)) { if (!preg_match($pattern, $value, $matchTarget)) {
if ($debug) {
do_log(sprintf("$logPrefix, pattern: (%s) not match: (%s)", $pattern, $value));
}
return 0; return 0;
} }
if ($matchNum <= 0) { if ($matchNum <= 0) {
@@ -216,7 +221,7 @@ class AgentAllowRepository extends BaseRepository
} }
$matchTarget = array_slice($matchTarget, 1); $matchTarget = array_slice($matchTarget, 1);
if ($debug) { if ($debug) {
do_log("matchTarget: " . nexus_json_encode($matchTarget)); do_log("$logPrefix, matchTarget: " . nexus_json_encode($matchTarget));
} }
for ($i = 0; $i < $matchNum; $i++) { for ($i = 0; $i < $matchNum; $i++) {
if (!isset($matchBench[$i]) || !isset($matchTarget[$i])) { if (!isset($matchBench[$i]) || !isset($matchTarget[$i])) {
@@ -232,12 +237,15 @@ class AgentAllowRepository extends BaseRepository
throw new NexusException(sprintf("Invalid match type: %s", $matchType)); throw new NexusException(sprintf("Invalid match type: %s", $matchType));
} }
if ($matchTarget[$i] > $matchBench[$i]) { if ($matchTarget[$i] > $matchBench[$i]) {
//higher, pass directly
return 1; return 1;
} elseif ($matchTarget[$i] < $matchBench[$i]) { } elseif ($matchTarget[$i] < $matchBench[$i]) {
return 2; return 2;
} }
} }
return 0;
//NOTE: at last, after all position checked, not [NOT_MATCH] or lower, it is passed!
return 1;
} }