Browse Source

[Web] Show country of sasl ips

andryyy 4 years ago
parent
commit
a89fe53e4a
5 changed files with 72 additions and 10 deletions
  1. 7 0
      data/web/css/site/user.css
  2. 43 3
      data/web/inc/functions.inc.php
  3. 16 6
      data/web/js/site/user.js
  4. 6 1
      data/web/json_api.php
  5. 0 0
      data/web/user.php

+ 7 - 0
data/web/css/site/user.css

@@ -119,4 +119,11 @@ border-bottom-width: 3px;
   font-style: italic;
   color: #158cba;
   user-select:none;
+}
+.ip-location-flag {
+  border-radius: 4px;
+  top: 3px;
+}
+.xmpp-logo-user {
+  width:64px;
 }

+ 43 - 3
data/web/inc/functions.inc.php

@@ -251,8 +251,10 @@ function password_check($password1, $password2) {
 
   return true;
 }
-function last_login($action, $username) {
+function last_login($action, $username, $sasl_limit = 10) {
   global $pdo;
+	global $redis;
+  $sasl_limit = intval($sasl_limit);
   switch ($action) {
     case 'get':
       if (filter_var($username, FILTER_VALIDATE_EMAIL) && hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
@@ -261,13 +263,51 @@ function last_login($action, $username) {
             AND `success` = 1
               GROUP BY `real_rip`, `service`
               ORDER BY `datetime` DESC
-              LIMIT 5;');
-        $stmt->execute(array(':username' => $username));
+              LIMIT :sasl_limit;');
+        $stmt->execute(array(':username' => $username, ':sasl_limit' => $sasl_limit));
         $sasl = $stmt->fetchAll(PDO::FETCH_ASSOC);
         foreach ($sasl as $k => $v) {
           if (!filter_var($sasl[$k]['real_rip'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
             $sasl[$k]['real_rip'] = 'Web/EAS/Internal (' . $sasl[$k]['real_rip'] . ')';
           }
+          elseif (filter_var($sasl[$k]['real_rip'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
+            try {
+              $sasl[$k]['location'] = $redis->hGet('IP_LOCATIONS', $sasl[$k]['real_rip']);
+            }
+            catch (RedisException $e) {
+              $_SESSION['return'][] = array(
+                'type' => 'danger',
+                'log' => array(__FUNCTION__, $_action, $_data_log),
+                'msg' => array('redis_error', $e)
+              );
+              return false;
+            }
+            if (!$sasl[$k]['location']) {
+              $curl = curl_init();
+              curl_setopt($curl, CURLOPT_URL,"https://dfdata.bella.network/lookup/" . $sasl[$k]['real_rip']);
+              curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+              $ip_data = curl_exec($curl);
+              if (!curl_errno($curl)) {
+                $ip_data_array = json_decode($ip_data, true);
+                if ($ip_data_array !== false and !empty($ip_data_array['location']['shortcountry'])) {
+                  $sasl[$k]['location'] = $ip_data_array['location']['shortcountry'];
+                    try {
+                      $redis->hSet('IP_LOCATIONS', $sasl[$k]['real_rip'], $sasl[$k]['location']);
+                    }
+                    catch (RedisException $e) {
+                      $_SESSION['return'][] = array(
+                        'type' => 'danger',
+                        'log' => array(__FUNCTION__, $_action, $_data_log),
+                        'msg' => array('redis_error', $e)
+                      );
+                      curl_close($curl);
+                      return false;
+                    }
+                }
+              }
+              curl_close($curl);
+            }
+          }
         }
       }
       else {

+ 16 - 6
data/web/js/site/user.js

@@ -78,11 +78,11 @@ jQuery(function($){
     }
   })
 
-  function last_logins(action) {
+  function last_logins(action, lines = 5) {
     if (action == 'get') {
       $.ajax({
         dataType: 'json',
-        url: '/api/v1/get/last-login/' + encodeURIComponent(mailcow_cc_username),
+        url: '/api/v1/get/last-login/' + encodeURIComponent(mailcow_cc_username) + '/' + lines,
         jsonp: false,
         error: function () {
           console.log('error reading last logins');
@@ -99,14 +99,24 @@ jQuery(function($){
             $.each(data.sasl, function (i, item) {
               var datetime = new Date(item.datetime.replace(/-/g, "/"));
               var local_datetime = datetime.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"});
-              if (item.service == "smtp") { service = '<div class="label label-default">' + item.service.toUpperCase() + '<i class="bi bi-chevron-compact-right"></i></div>'; }
-              else if (item.service == "imap") { service = '<div class="label label-default"><i class="bi bi-chevron-compact-left"></i> ' + item.service.toUpperCase() + '</div>'; }
-              else { service = '<div class="label label-default">' + item.service.toUpperCase() + '</div>'; }
+              if (item.location) {
+                ip_location = '<span class="lang-sm ip-location-flag" lang="' + item.location + '"></span>';
+              } else {
+                ip_location = '';
+              }
+              if (item.service == "smtp") {
+                service = '<div class="label label-default">' + item.service.toUpperCase() + '<i class="bi bi-chevron-compact-right"></i></div>';
+              } else if (item.service == "imap") {
+                service = '<div class="label label-default"><i class="bi bi-chevron-compact-left"></i> ' + item.service.toUpperCase() + '</div>';
+              } else {
+                service = '<div class="label label-default">' + item.service.toUpperCase() + '</div>';
+              }
               if (item.real_rip.startsWith("Web")) {
                 real_rip = item.real_rip;
               } else {
-                real_rip = '<a href="https://bgp.he.net/ip/' + item.real_rip + '" target="_blank">' + item.real_rip + '</a>';
+                real_rip = '<a href="https://bgp.he.net/ip/' + item.real_rip + '" target="_blank">' + item.real_rip + '</a> ';
               }
+              real_rip = real_rip + ' ' + ip_location;
               $('.last-login').append('<li class="list-group-item">' + 
                 local_datetime + ' ' + service + ' ' + lang.from + ' ' +
                 real_rip +

+ 6 - 1
data/web/json_api.php

@@ -641,7 +641,12 @@ if (isset($_GET['query'])) {
 
           case "last-login":
             if ($object) {
-              $data = last_login('get', $object);
+              if (isset($extra) && intval($extra) >= 1) {
+                $data = last_login('get', $object, intval($extra));
+              }
+              else {
+                $data = last_login('get', $object);
+              }
               process_get_return($data);
             }
           break;

File diff suppressed because it is too large
+ 0 - 0
data/web/user.php


Some files were not shown because too many files changed in this diff