Skip to content
View in the app

A better way to browse. Learn more.

Learn more

block-hltv-connections/addons/amxmodx/scripting/olds_hltvblock.sma

olds_hltvblock.sma
245 lines · 6.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Block HLTV Connections (olds.ro) - refuses HLTV proxies whose IP is not on
 * the allow list, so nobody can attach a hidden proxy to watch private rounds.
 *
 * Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details. You should have received a copy of the GNU General Public
 * License along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Block HLTV Connections"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_RULES 64
#define TASK_DROP 8330

new g_net[MAX_RULES]            // network address (host order) and mask of each allowed entry
new g_mask[MAX_RULES]
new g_rules
new g_cvarAll, g_cvarMsg

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    g_cvarAll = create_cvar("olds_hltvblock_all", "0", FCVAR_NONE, "1 = refuse every HLTV proxy, even allowed ones", true, 0.0, true, 1.0)
    g_cvarMsg = create_cvar("olds_hltvblock_msg", "HLTV not allowed", FCVAR_NONE, "Reason the refused proxy is given")
    AutoExecConfig(true, "olds_hltvblock")

    register_concmd("amx_hltvblock_reload", "cmd_reload", ADMIN_CFG, "- read hltv_allow.ini again")
    register_concmd("amx_hltvblock_check", "cmd_check", ADMIN_CFG, "<ip> - would an HLTV proxy from this IP be let in?")
}

public plugin_cfg()
{
    load_list()
}

/* ---------- allow list ---------- */

// "a.b.c.d" to a number; false when the text is not an IPv4 address.
bool:parse_ip(const text[], &value)
{
    new part = 0, octet = -1, count
    value = 0
    for (new i = 0; ; i++)
    {
        new c = text[i]
        if ('0' <= c <= '9')
        {
            octet = (octet < 0 ? 0 : octet) * 10 + (c - '0')
            if (octet > 255)
                return false
            continue
        }
        if (c == '.' || c == EOS)
        {
            if (octet < 0 || count >= 4)
                return false
            part = (part << 8) | octet
            count++
            octet = -1
            if (c == EOS)
                break
            continue
        }
        return false
    }
    if (count != 4)
        return false
    value = part
    return true
}

load_list()
{
    g_rules = 0
    new path[192]
    get_configsdir(path, charsmax(path))
    add(path, charsmax(path), "/hltv_allow.ini")
    new file = fopen(path, "rt")
    if (!file)
    {
        server_print("[HLTVBlock] %s not found: every HLTV proxy is refused.", path)
        return
    }
    new line[64], lineNo
    while (!feof(file) && fgets(file, line, charsmax(line)))
    {
        lineNo++
        trim(line)
        new cut = contain(line, ";")
        if (cut >= 0)
            line[cut] = EOS
        trim(line)
        if (!line[0] || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
            continue
        new bits = 32, slash = contain(line, "/")
        if (slash >= 0)
        {
            bits = is_str_num(line[slash + 1]) ? str_to_num(line[slash + 1]) : -1
            line[slash] = EOS
        }
        new address
        if (bits < 0 || bits > 32 || !parse_ip(line, address))
        {
            log_amx("[HLTVBlock] %s line %d skipped: expected an IPv4 address or address/bits", path, lineNo)
            continue
        }
        if (g_rules >= MAX_RULES)
        {
            log_amx("[HLTVBlock] %s: more than %d entries, the rest are skipped", path, MAX_RULES)
            break
        }
        new mask = bits == 0 ? 0 : (0xFFFFFFFF << (32 - bits))
        g_mask[g_rules] = mask
        g_net[g_rules] = address & mask
        g_rules++
    }
    fclose(file)
    server_print("[HLTVBlock] %d allowed HLTV address(es) loaded.", g_rules)
}

bool:ip_allowed(const ip[])
{
    new address
    if (!parse_ip(ip, address))
        return false
    for (new r = 0; r < g_rules; r++)
    {
        if ((address & g_mask[r]) == g_net[r])
            return true
    }
    return false
}

// Empty string = let in; otherwise why it is refused.
verdict(const ip[], reason[], len)
{
    if (get_pcvar_num(g_cvarAll))
        copy(reason, len, "olds_hltvblock_all is 1")
    else if (!ip_allowed(ip))
        copy(reason, len, "IP not in hltv_allow.ini")
    else
        reason[0] = EOS
}

/* ---------- connections ---------- */

bool:is_proxy(id)
{
    if (is_user_hltv(id))
        return true
    new value[4]
    get_user_info(id, "*hltv", value, charsmax(value))
    return value[0] != EOS && value[0] != '0'
}

check_client(id)
{
    if (!is_proxy(id) || task_exists(TASK_DROP + id))
        return
    new ip[24], reason[48]
    get_user_ip(id, ip, charsmax(ip), 1)
    verdict(ip, reason, charsmax(reason))
    if (!reason[0])
    {
        log_amx("[HLTVBlock] HLTV proxy from %s allowed (on the list).", ip)
        return
    }
    new name[32]
    get_user_name(id, name, charsmax(name))
    log_amx("[HLTVBlock] HLTV proxy ^"%s^" from %s refused: %s.", name, ip, reason)
    // Dropping inside the connect callback is unsafe: do it a moment later.
    set_task(0.1, "task_drop", TASK_DROP + id)
}

public client_connect(id)
{
    check_client(id)
}

public client_putinserver(id)
{
    check_client(id)
}

public client_disconnected(id)
{
    remove_task(TASK_DROP + id)
}

public task_drop(task)
{
    new id = task - TASK_DROP
    if (!is_user_connected(id) && !is_user_connecting(id))
        return
    new message[96]
    get_pcvar_string(g_cvarMsg, message, charsmax(message))
    if (!message[0])
        copy(message, charsmax(message), "HLTV not allowed")
    rh_drop_client(id, message)
}

/* ---------- commands ---------- */

public cmd_reload(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    load_list()
    console_print(id, "[HLTVBlock] %d allowed address(es).", g_rules)
    return PLUGIN_HANDLED
}

public cmd_check(id, level, cid)
{
    if (!cmd_access(id, level, cid, 2))
        return PLUGIN_HANDLED
    new ip[24], reason[48], address
    read_argv(1, ip, charsmax(ip))
    if (!parse_ip(ip, address))
    {
        console_print(id, "[HLTVBlock] ^"%s^" is not an IPv4 address.", ip)
        return PLUGIN_HANDLED
    }
    verdict(ip, reason, charsmax(reason))
    if (reason[0])
        console_print(id, "[HLTVBlock] An HLTV proxy from %s would be refused: %s.", ip, reason)
    else
        console_print(id, "[HLTVBlock] An HLTV proxy from %s would be let in.", ip)
    return PLUGIN_HANDLED
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.