Загрузка...

[PHP] Easy getting IP+UserAgent and sending it to TG, as well as IP blocking

Thread in Backend created by lastwek Sep 22, 2021. 509 views

  1. lastwek
    lastwek Topic starter Sep 22, 2021 140 Nov 10, 2017
    Не судите строго запросы к БД, буду рад к вашим поправкам
    index.php:
    PHP
    <?php

    $user = "";
    $pass = "";
    $host = "";
    $dbdb = "";

    $conn = new mysqli($host, $user, $pass, $dbdb);

    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else{
    $ip=$_SERVER['REMOTE_ADDR'];
    }

    $find_ip = mysqli_query($conn, "SELECT ip FROM banned_ip WHERE ip='$ip'");
    $ban = mysqli_fetch_array($find_ip);
    if($ip == $ban['ip']){
    header('HTTP/1.0 403 Forbidden');
    die("");
    }

    function sendMessage($chatID, $messaggio, $ipaddr, $token) {

    $keyboard = json_encode([
    "inline_keyboard" => [
    [
    [
    "text" => "Заблокировать IP",
    "callback_data" => "banip_$ipaddr"
    ]
    ]
    ]
    ]);

    $url = "https://api.telegram.org/bot$token/sendMessage?chat_id=$chatID&reply_markup=$keyboard&parse_mode=html";
    $url = $url . "&text=" . urlencode($messaggio);
    $ch = curl_init();
    $optArray = array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true
    );
    curl_setopt_array($ch, $optArray);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
    }

    $useragent = $_SERVER['HTTP_USER_AGENT'];

    $token = "";
    $chatid = "";
    sendMessage($chatid, "<b>Данные пользователя</b>\nIP: <i>$ip</i>\nUserAgent: <i>$useragent</i>", $ip, $token);
    ?>
    bot.php(сюда вебхук надо закинуть):
    PHP
    <?php

    $user = "";
    $pass = "";
    $host = "";
    $dbdb = "";
    $update = json_decode(file_get_contents('php://input'), TRUE);

    $botToken = "";
    $botAPI = "https://api.telegram.org/bot" . $botToken;

    if (explode("_", $update['callback_query']['data'])[0]=='banip'){
    $conn = new mysqli($host, $user, $pass, $dbdb);
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    $ipaddr = explode("_", $update['callback_query']['data'])[1];
    $sql = "INSERT INTO `banned_ip` (`ip`) VALUES ('$ipaddr');";
    mysqli_query($conn, $sql);
    $text = "$ipaddr был успешно заблокирован!"
    $data = http_build_query([
    'text' => $text,
    'chat_id' => $update['callback_query']['from']['id'],
    "message_id" => $update["message"]["message_id"]
    ]);
    $keyboard = json_encode([
    "inline_keyboard" => [
    [
    [
    "text" => "Разблокировать IP",
    "callback_data" => "unbanip_$ipaddr"
    ]
    ]
    ]
    ]);
    file_get_contents($botAPI . "/sendmessage?{$data}&reply_markup=$keyboard");
    mysqli_close($conn);
    }

    if (explode("_", $update['callback_query']['data'])[0]=='unbanip') {

    $conn = new mysqli($host, $user, $pass, $dbdb);
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    $ipaddr = explode("_", $update['callback_query']['data'])[1];
    $sql = "DELETE FROM `banned_ip` WHERE ip='$ipaddr';";
    mysqli_query($conn, $sql);
    $text = "$ipaddr был успешно разаблокирован!"
    $data = http_build_query([
    'text' => $text,
    'chat_id' => $update['callback_query']['from']['id'],
    "message_id" => $update["message"]["message_id"]
    ]);
    $keyboard = json_encode([
    "inline_keyboard" => [
    [
    [
    "text" => "Заблокировать IP",
    "callback_data" => "banip_$ipaddr"
    ]
    ]
    ]
    ]);
    file_get_contents($botAPI . "/sendmessage?{$data}&reply_markup=$keyboard");
    mysqli_close($conn);
    }
    ?>
    БД:
    Code
    CREATE TABLE `banned_ip` (
    `ip` text NOT NULL
    );
    UPD: Код не имеет смысла, потому что токен бота не скрывается)
     
  2. COLLYN
    COLLYN Aug 25, 2023 Me When Im @ COLLYN 468 Jul 11, 2018
     
    1. View previous comments (3)
    2. lastwek Topic starter
      COLLYN, и в index,php вставь токен
    3. COLLYN
    4. lastwek Topic starter
      COLLYN, Тогда не подскажу
Loading...
Top