Whitelist/Blacklist specific countries

Norvik

Retired Staff
Contributor
Jul 18, 2015
635
588
157
Whitelist/Blacklist specific countries
It's using the ts3admin.class so they have to be in the same folder.
PHP:
<?PHP
// Teamspeak config
$teamspeakInfo = array(
    'username' => 'serveradmin',
    'password' => 'your password',
    'host' => '127.0.0.1',
    'portQuery' => '10011',
    'portServer' => '9987',
    'displayname' => 'R4P3.NET'
);

$blacklist = array('DE'); // array('DE', 'HU');
$whitelist = array('DE'); // array('DE', 'HU');
$clientType = 2; // 1 = Everyone, 2 = Ignore Query, 3 = Ignore Clients
$listMode = 1; // 1 = blacklist, 2 = whitelist
$punishMode = 1; // 1 = kick, 2 = ban
$punishMessage = 'Your country is not allowed to join our server.'; // Message the user will see once he got kicked/banned

/*
|--------------------------------------------------------------------------------
|   Do not modify anything below this area unless you know what you're doing.
|--------------------------------------------------------------------------------
*/

echo "|--------------------------------------|\n|      Disallow specific countries     |\n|--------------------------------------|\n";
require_once("ts3admin.class.php");
$tsAdmin = new ts3admin($teamspeakInfo['host'], $teamspeakInfo['portQuery']);

if($tsAdmin->getElement('success', $tsAdmin->connect())) {
    echo "> Successfully connected to the teamspeak server\n";
    $tsAdmin->login($teamspeakInfo['username'], $teamspeakInfo['password']);
    echo "> Successfully logged in\n";
    $tsAdmin->selectServer($teamspeakInfo['portServer']);
    echo "> Successfully selected server ".$teamspeakInfo['portServer']."\n";
    $tsAdmin->setName($teamspeakInfo['displayname']);
    echo "> Successfully changed name to ".$teamspeakInfo['displayname']."\n";

    $connectionInfo = $tsAdmin->whoAmI()['data'];

    for(;;){
        $clients = $tsAdmin->clientList("-country -ip");

        foreach($clients['data'] as $client) {
            if ($listMode == 1) {
                $invalidCountry = false;
                foreach($blacklist as $blacklistCountry){
                    if ($client['client_country'] == $blacklistCountry || $client['client_country'] == "") {
                        switch ($clientType) {
                            case '1':
                                $invalidCountry = true;
                                break;

                            case '2':
                                if ($client['client_type'] == 0) {
                                    $invalidCountry = true;
                                }
                                break;

                            case '3':
                                if ($client['client_type'] == 1) {
                                    $invalidCountry = true;
                                }
                                break;
                        }
                    }
                }
            } else if ($listMode == 2) {
                $invalidCountry = true;
                foreach($whitelist as $whitelistCountry){
                    if ($client['client_country'] == $whitelistCountry) {
                        switch ($clientType) {
                            case '1':
                                $invalidCountry = false;
                                break;

                            case '2':
                                if ($client['client_type'] == 0) {
                                    $invalidCountry = false;
                                }
                                break;

                            case '3':
                                if ($client['client_type'] == 1) {
                                    $invalidCountry = false;
                                }
                                break;
                        }
                    }
                }
            }

            if ($invalidCountry && $connectionInfo['client_id'] != $client['clid']) {
                if ($punishMode == 1) {
                    $tsAdmin->clientKick($client['clid'], "server", $punishMessage);
                    echo "> Successfully kicked ".$client['client_nickname']." from ".$client['client_country']." -> ".$client['connection_client_ip']."\n";
                } else if ($punishMode == 2) {
                    $tsAdmin->banClient($client['clid'], 0, $punishMessage);
                    echo "> Successfully banned ".$client['client_nickname']." from ".$client['client_country']."\n";
                }
            }
        }
    }
} else {
    die('Connection could not be established.');
}
?>
 

senheiser

Member
May 4, 2016
41
6
58
Great script but It try to kick my query (ts rank System) all The time... And i use the Option "ignore query" can you help me?
 

h1web

VIP
Sep 2, 2015
206
134
131
Can we also use this if we only have a normal query account, no serverquery?
 

Kleberstoff

Knowledge Seeker
VIP
Dec 29, 2015
308
214
158
Can we also use this if we only have a normal query account, no serverquery?

Don't Quote me on this, But I'm almost certain that this will work with a Normal Query, if the Query has enough Permissions.
 

XRV-Webix

Member
May 2, 2016
111
55
64
So... let me guess... you run this every minute on the query?
Wouldn't be easyer to add iptables rules to ban the countries?
 

Norvik

Retired Staff
Contributor
Jul 18, 2015
635
588
157
So... let me guess... you run this every minute on the query?
Wouldn't be easyer to add iptables rules to ban the countries?
That script is useless anyways but somebody requested it.
 

PirateControl

Member
Apr 19, 2016
33
11
43
I fixed that Problem but now i got this :/

Code:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\sec\index.php on line 43
 

Najsr

Moderator
TeamSpeak Developer
Apr 23, 2016
483
249
167
If you are hosting on a server that you have root access to then you can easily make whitelist only for your country by using iptables. + This script is resource inefficient because it is permanently looping --> events would save you some cpu cycles.
 

XRV-Webix

Member
May 2, 2016
111
55
64
Like @Nicer said, it's better, easier, cleaner to use a simple firewall to handle this than using the teamspeak query.
- This script won't block a user from accessing your server, he just kick/ban it after he login.
- This script won't block attempts to your server query.
- This script, if you will run it every minute, consumes resources from your server (and if he hangs, it will eat up your CPU and crash your teamspeak server).
Read my post here: https://r4p3.net/posts/48674/
 
Top