Skip to content
View in the app

A better way to browse. Learn more.

Learn more

hltv-inviter/addons/amxmodx/scripting/olds_hltvinv.sma

olds_hltvinv.sma
335 lines · 11.2 KB · Pawn (AMX Mod X)
Raw
/*
 * HLTV Inviter (olds.ro workshop card LIB-0836)
 * Copyright (C) 2026 daemon (olds.ro)
 *
 * 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: <https://www.gnu.org/licenses/>.
 *
 * amx_hltvinvite <ip:port> (ADMIN_RCON) asks an HLTV proxy to connect to this server. A game
 * server cannot pull a proxy in by itself, so the plugin talks to the proxy the way an
 * admin would: over UDP it asks the proxy for an rcon challenge, then sends
 * rcon <challenge> "<olds_hltvinv_pass>" connect <this server>, with the proxy's
 * adminpassword. The server allows proxies (sv_proxies at least 1) and keeps
 * olds_hltvinv_reserve slots free for HLTV: sv_visiblemaxplayers shows the smaller number,
 * and a player who would take a reserved slot is turned away while no proxy holds it.
 * amx_hltvkick disconnects every HLTV proxy, which frees its slot again.
 */
#include <amxmodx>
#include <amxmisc>
#include <sockets>

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

#define TASK_POLL   8360
#define POLL_STEP   0.1
#define POLL_TRIES  30

new g_reserve
new g_cvarPass, g_cvarServer
new g_oldVisible = -2

/* the invite in progress */
new g_sock, g_tries, g_admin, g_adminUserid
new g_target[32]
new g_invites, g_answered, g_refused

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    new pcvar = create_cvar("olds_hltvinv_reserve", "1", FCVAR_NONE, "Player slots kept free for HLTV proxies", true, 0.0, true, 8.0)
    bind_pcvar_num(pcvar, g_reserve)
    hook_cvar_change(pcvar, "on_reserve_change")
    g_cvarPass = create_cvar("olds_hltvinv_pass", "", FCVAR_PROTECTED, "The HLTV proxy's adminpassword (needed to invite it)")
    g_cvarServer = create_cvar("olds_hltvinv_server", "", FCVAR_NONE, "This server's public ip:port for the proxy (empty = the server's own address)")
    AutoExecConfig(true, "olds_hltvinv")
    register_concmd("amx_hltvinvite", "cmd_invite", ADMIN_RCON, "<ip:port> - asks that HLTV proxy to connect here")
    register_concmd("amx_hltvkick", "cmd_kick", ADMIN_RCON, "- disconnects every HLTV proxy")
    register_concmd("amx_hltvinv_status", "cmd_status", ADMIN_RCON, "- reserved slots, proxies and the last invite")
}

public plugin_cfg()
{
    apply_reserve()
}

public plugin_end()
{
    close_socket()
    if (g_oldVisible != -2)
        set_cvar_num("sv_visiblemaxplayers", g_oldVisible)
}

/* ------------------------------------------------------------------ the reserved slot */

public on_reserve_change(pcvar, const oldValue[], const newValue[])
{
    RequestFrame("apply_reserve")
}

public apply_reserve()
{
    if (g_oldVisible == -2)
        g_oldVisible = get_cvar_num("sv_visiblemaxplayers")
    new reserve = clamp(g_reserve, 0, MaxClients - 1)
    if (reserve > 0)
    {
        set_cvar_num("sv_visiblemaxplayers", MaxClients - reserve)
        if (get_cvar_num("sv_proxies") < 1)
        {
            set_cvar_num("sv_proxies", 1)
            log_amx("sv_proxies was 0: set to 1 so an HLTV proxy may connect")
        }
    }
    else
        set_cvar_num("sv_visiblemaxplayers", g_oldVisible)
}

count_clients(&hltv)
{
    new players
    hltv = 0
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_user_connected(id) && !is_user_connecting(id))
            continue
        if (is_user_hltv(id))
            hltv++
        else
            players++
    }
    return players
}

public client_putinserver(id)
{
    if (is_user_hltv(id))
    {
        new ip[32]
        get_user_ip(id, ip, charsmax(ip))
        log_amx("HLTV proxy connected from %s", ip)
        return
    }
    new reserve = clamp(g_reserve, 0, MaxClients - 1)
    if (!reserve)
        return
    new hltv, players = count_clients(hltv)
    /* the slots still free for proxies: the reserve minus the proxies already here */
    new keep = reserve - hltv
    if (keep > 0 && players > MaxClients - reserve)
    {
        new name[MAX_NAME_LENGTH]
        get_user_name(id, name, charsmax(name))
        log_amx("%s turned away: the last %d slot(s) are kept for HLTV", name, reserve)
        server_cmd("kick #%d ^"Server is full (a slot is kept for HLTV)^"", get_user_userid(id))
    }
}

/* ------------------------------------------------------------------ the invite */

/* "a.b.c.d:port" with every part in range. */
bool:parse_address(const text[], ip[], iplen, &port)
{
    new host[24], portText[8]
    if (strlen(text) > 21 || contain(text, ":") < 0)
        return false
    strtok2(text, host, charsmax(host), portText, charsmax(portText), ':', TRIM_FULL)
    if (!is_str_num(portText))
        return false
    port = str_to_num(portText)
    if (port < 1 || port > 65535)
        return false
    new part[8], rest[24], left[24], dots
    copy(left, charsmax(left), host)
    while (left[0])
    {
        strtok2(left, part, charsmax(part), rest, charsmax(rest), '.', TRIM_FULL)
        copy(left, charsmax(left), rest)
        if (!is_str_num(part) || strlen(part) > 3 || str_to_num(part) > 255)
            return false
        dots++
    }
    if (dots != 4 || host[strlen(host) - 1] == '.')
        return false
    copy(ip, iplen, host)
    return true
}

reply(const text[])
{
    if (g_admin && is_user_connected(g_admin) && get_user_userid(g_admin) == g_adminUserid)
        console_print(g_admin, "%s", text)
    else
        server_print("%s", text)
}

public cmd_invite(id, level, cid)
{
    if (!cmd_access(id, level, cid, 2))
        return PLUGIN_HANDLED
    new arg[48], ip[16], port
    read_argv(1, arg, charsmax(arg))
    if (!parse_address(arg, ip, charsmax(ip), port))
    {
        console_print(id, "Usage: amx_hltvinvite <ip:port>, for example amx_hltvinvite 203.0.113.5:27020")
        return PLUGIN_HANDLED
    }
    new pass[64]
    get_pcvar_string(g_cvarPass, pass, charsmax(pass))
    if (!pass[0])
    {
        console_print(id, "[HLTV] Set olds_hltvinv_pass to the proxy's adminpassword first: nothing was sent.")
        return PLUGIN_HANDLED
    }
    if (g_sock > 0)
    {
        console_print(id, "[HLTV] An invite to %s is still waiting for an answer.", g_target)
        return PLUGIN_HANDLED
    }
    new error
    g_sock = socket_open(ip, port, SOCKET_UDP, error)
    if (g_sock <= 0)
    {
        g_sock = 0
        console_print(id, "[HLTV] Could not open a UDP socket to %s (error %d).", arg, error)
        return PLUGIN_HANDLED
    }
    copy(g_target, charsmax(g_target), arg)
    g_admin = id
    g_adminUserid = 0
    if (id)
        g_adminUserid = get_user_userid(id)
    g_tries = 0
    g_invites++
    send_packet("challenge rcon^n")
    set_task(POLL_STEP, "poll_answer", TASK_POLL, _, _, "a", POLL_TRIES + 1)
    new name[MAX_NAME_LENGTH]
    if (id)
        get_user_name(id, name, charsmax(name))
    else
        copy(name, charsmax(name), "server console")
    log_amx("%s invites the HLTV proxy at %s", name, arg)
    console_print(id, "[HLTV] Asking the proxy at %s for an rcon challenge...", arg)
    return PLUGIN_HANDLED
}

/* An out-of-band packet: four 0xFF bytes, then the text. */
send_packet(const text[])
{
    new packet[256], len = strlen(text)
    packet[0] = packet[1] = packet[2] = packet[3] = 0xFF
    for (new i = 0; i < len && i + 4 < charsmax(packet); i++)
        packet[i + 4] = text[i]
    socket_send2(g_sock, packet, min(len + 4, charsmax(packet)))
}

close_socket()
{
    remove_task(TASK_POLL)
    if (g_sock > 0)
        socket_close(g_sock)
    g_sock = 0
}

public poll_answer()
{
    if (g_sock <= 0)
    {
        remove_task(TASK_POLL)
        return
    }
    g_tries++
    if (socket_is_readable(g_sock, 0))
    {
        new data[256], text[256]
        new len = socket_recv(g_sock, data, charsmax(data))
        /* skip the 0xFF header, keep printable text */
        new n
        for (new i = 4; i < len && n < charsmax(text); i++)
            if (data[i] >= ' ' && data[i] < 127)
                text[n++] = data[i]
        text[n] = 0
        new word1[16], word2[16], challenge[24]
        parse(text, word1, charsmax(word1), word2, charsmax(word2), challenge, charsmax(challenge))
        if (equal(word1, "challenge") && equal(word2, "rcon") && is_str_num(challenge))
        {
            g_answered++
            send_connect(challenge)
        }
        else
        {
            g_refused++
            new line[160]
            formatex(line, charsmax(line), "[HLTV] Unexpected answer from %s: %s", g_target, text)
            reply(line)
            log_amx("Unexpected answer from the HLTV proxy at %s: %s", g_target, text)
        }
        close_socket()
        return
    }
    if (g_tries >= POLL_TRIES)
    {
        new line[160]
        formatex(line, charsmax(line), "[HLTV] No answer from %s within %.0f s: is the proxy running there?", g_target, POLL_STEP * float(POLL_TRIES))
        reply(line)
        log_amx("No answer from the HLTV proxy at %s", g_target)
        close_socket()
    }
}

send_connect(const challenge[])
{
    new pass[64], server[32], text[200]
    get_pcvar_string(g_cvarPass, pass, charsmax(pass))
    get_pcvar_string(g_cvarServer, server, charsmax(server))
    if (!server[0])
        get_user_ip(0, server, charsmax(server))
    /* The password may not hold a quote: it would end the quoted argument. */
    replace_all(pass, charsmax(pass), "^"", "")
    formatex(text, charsmax(text), "rcon %s ^"%s^" connect %s^n", challenge, pass, server)
    send_packet(text)
    new line[160]
    formatex(line, charsmax(line), "[HLTV] Sent: connect %s to the proxy at %s. It joins in a few seconds if the password is right.", server, g_target)
    reply(line)
    log_amx("Asked the HLTV proxy at %s to connect to %s", g_target, server)
}

/* ------------------------------------------------------------------ kick and status */

public cmd_kick(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new kicked
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p) || !is_user_hltv(p))
            continue
        new ip[32]
        get_user_ip(p, ip, charsmax(ip))
        server_cmd("kick #%d ^"HLTV disconnected by an admin^"", get_user_userid(p))
        log_amx("HLTV proxy at %s disconnected by an admin", ip)
        kicked++
    }
    if (kicked)
        console_print(id, "[HLTV] %d proxy(s) disconnected; the slot is free for the next invite.", kicked)
    else
        console_print(id, "[HLTV] No HLTV proxy is connected.")
    return PLUGIN_HANDLED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new hltv, players = count_clients(hltv)
    console_print(id, "[HLTV] maxplayers %d, reserve %d, sv_visiblemaxplayers %d, sv_proxies %d, players %d, proxies %d",
        MaxClients, g_reserve, get_cvar_num("sv_visiblemaxplayers"), get_cvar_num("sv_proxies"), players, hltv)
    console_print(id, "[HLTV] invites %d, answered %d, odd answers %d, waiting %d (%s)", g_invites, g_answered, g_refused, g_sock > 0, g_target)
    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.