Skip to content
This repository has been archived by the owner on Oct 29, 2022. It is now read-only.

Commit

Permalink
attempt at making the IP user search allow wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
cene-co-za committed Nov 9, 2019
1 parent f23e411 commit 7402db7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
31 changes: 21 additions & 10 deletions lib/Destiny/Chat/ChatRedisService.php
Expand Up @@ -36,11 +36,7 @@ function afterConstruct() {
$this->redis = Application::instance()->getRedis();
}

/**
* @throws Exception
*/
public function findUserIdsByUsersIp(int $userid): array {
$keys = RedisUtils::callScript('check-sameip-users', [$userid]);
private function stripRedisUserIpPrefixes(array $keys) {
return array_filter(array_map(function($n) {
return intval(substr($n, strlen('CHAT:userips-')));
}, $keys), function($n){
Expand All @@ -49,15 +45,30 @@ public function findUserIdsByUsersIp(int $userid): array {
}

/**
* Finds all users who share the same IP
* @throws Exception
*/
public function findUserIdsByUsersIp(int $userid): array {
$keys = RedisUtils::callScript('check-sameip-users', [$userid]);
return $this->stripRedisUserIpPrefixes($keys);
}

/**
* Find all users by ip
* @throws Exception
*/
public function findUserIdsByIP(string $ipaddress): array {
$keys = RedisUtils::callScript('check-ip', [$ipaddress]);
return array_filter(array_map(function($n) {
return intval(substr($n, strlen('CHAT:userips-')));
}, $keys), function($n){
return $n != null && $n > 0;
});
return $this->stripRedisUserIpPrefixes($keys);
}

/**
* Find all users by ip (wildcard)
* @throws Exception
*/
public function findUserIdsByIPWildcard(string $ipaddress): array {
$keys = RedisUtils::callScript('check-ip-wildcard', [$ipaddress]);
return $this->stripRedisUserIpPrefixes($keys);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/Destiny/Controllers/ChatAdminController.php
Expand Up @@ -47,8 +47,13 @@ public function adminChatBroadcast(array $params, ViewModel $model): string {
*/
public function adminChatIp(array $params, ViewModel $model): string {
$model->title = 'Chat';
$max = 100;
FilterParams::required($params, 'ip');
$ids = ChatRedisService::instance()->findUserIdsByIP($params['ip']);
$ids = ChatRedisService::instance()->findUserIdsByIPWildcard($params['ip']);
$ids = array_unique($ids);
if (count($ids) > $max) {
$ids = array_slice($ids, 0, $max);
}
$model->usersByIp = UserService::instance()->getUsersByUserIds($ids);
$model->searchIp = $params ['ip'];
return 'admin/chat';
Expand Down
17 changes: 17 additions & 0 deletions scripts/redis/check-ip-wildcard.lua
@@ -0,0 +1,17 @@
if not ARGV[1] then
return {err = "INVALID ARGUMENTS"}
end

local ip = ARGV[1]
local foundusers = {}
local keys = redis.call("KEYS", "CHAT:userips-*")
for _, key in ipairs(keys) do
local rawData = redis.call('ZRANGE', key, 0, -1);
for idx = 1, #rawData, 2 do
if string.match(rawData[idx], ip) then
table.insert(foundusers, key)
end
end
end

return foundusers
2 changes: 1 addition & 1 deletion views/admin/chat.php
Expand Up @@ -39,7 +39,7 @@
<div class="ds-block">
<div class="form-group">
<label>IP Address:
<br /><small>Search for users by an IP address</small>
<br /><small>Search for users by an IP address. Use * to indicate a wildcard search e.g 192.168.*</small>
</label>
<input name="ip" type="text" class="form-control" value="<?=Tpl::out($this->searchIp)?>" placeholder="192.168.0.1" />
</div>
Expand Down

0 comments on commit 7402db7

Please sign in to comment.