Skip to content

Commit

Permalink
Merge pull request #9 from JABirchall/GlobalBan
Browse files Browse the repository at this point in the history
Global ban System Tested and functions as intended
  • Loading branch information
JABirchall authored Sep 20, 2016
2 parents 244cd17 + 966172c commit 3becbb7
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ PHP version 5.6+, php_PDO extentions, Teamspeak 3 Server
* Asynchronous execution
* Plugin Autoloading and Auotinstalling
* Eloquent database support for Progresql, Mysql, MSSQL, SQLite
* Global Ban system built in

## Coding style

Expand Down
12 changes: 7 additions & 5 deletions app/TeamSpeak3Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class TeamSpeak3Bot

private $lastEvent;

private $carbon;
public $carbon;

/**
* TeamSpeak3Bot constructor.
Expand All @@ -136,7 +136,7 @@ public function __construct($username, $password, $host = "127.0.0.1", $port = 1
{
$this->carbon = new Carbon;

if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && posix_getuid() === 0) {
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && posix_getuid() === 0 && !Self::$config['ignoreWarnings']) {
$this->printOutput("[WARNING] Running Nimda as root is bad!");
$this->printOutput("Start anyway? Y/N:", false);
$response = rtrim(fgets(STDIN));
Expand All @@ -146,7 +146,7 @@ public function __construct($username, $password, $host = "127.0.0.1", $port = 1
}
}

if($username === "serveradmin") {
if($username === "serveradmin" && !Self::$config['ignoreWarnings']) {
$this->printOutput("[WARNING] Running Nimda logged in as serveradmin is bad!");
$this->printOutput("Start anyway? Y/N:", false);
$response = rtrim(fgets(STDIN));
Expand Down Expand Up @@ -209,7 +209,7 @@ protected function register()
{
$this->node->notifyRegister("textserver");
$this->node->notifyRegister("textchannel");
$this->node->notifyRegister("textprivate");
//$this->node->notifyRegister("textprivate");
$this->node->notifyRegister("server");
$this->node->notifyRegister("channel");
}
Expand Down Expand Up @@ -341,6 +341,7 @@ private function loadPlugin($configFile)
return false;
}


$this->printOutput(sprintf("%- 80s %s", "Loading plugin [{$config['name']}] by {$config['author']} ", "::"), false, false);

$config['class'] = \Plugin::class . '\\' . $config['name'];
Expand All @@ -359,6 +360,7 @@ private function loadPlugin($configFile)

if ($this->plugins[$config['name']] instanceof \Plugin\AdvancedPluginContract) {
if (!Manager::schema()->hasTable($config['table'])) {
$this->printOutput("Install, ", false, false);
$this->plugins[$config['name']]->install();

Plugin::create([
Expand Down Expand Up @@ -446,7 +448,7 @@ public function onMessage(Event $event)
public function onEvent(Event $event)
{
$data = $event->getData();
if (@$data['invokername'] == $this->name || @$data['invokeruid'] == 'serveradmin' || @$data["client_unique_identifier"] == "ServerQuery") {
if (@$data['client_type'] == 1) {
return;
} elseif ($this->lastEvent && $event->getMessage()->contains($this->lastEvent)) {
return;
Expand Down
8 changes: 4 additions & 4 deletions config/TeamSpeak.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
* Date: 10/08/2016
* Time: 22:44
*/

namespace Config;

class TeamSpeak
{

public static $TS3config = [
'username' => 'serveradmin',
'password' => 'password',
'host' => '127.0.0.1',
'password' => 'Tyc00n..',
'host' => 'ts3.mxgaming.com',
'port' => 10011,
'name' => 'Nimda',
'serverPort' => 9987,
'timeout' => 1,
'misc' => [
'debug' => true,
'newLineNewMessage' => true,
'newLineNewMessage' => false,
'ignoreWarnings' => true
]
];

Expand Down
12 changes: 12 additions & 0 deletions config/plugins/AddWhitelist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "AddWhitelist",
"author": "JABirchall",
"description": "Whitelist a user from the global ban system.",
"version": "1.0",
"triggers": [
"!whitelist"
],
"table": "whitelists",
"usage": "Usage: !whitelist [uid]"

}
12 changes: 12 additions & 0 deletions config/plugins/GlobalBan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "GlobalBan",
"author": "JABirchall",
"description": "Submit a Global Teamspeak ban",
"version": "1.0",
"triggers": [
"!gban",
"!globalban"
],
"usage": "Usage !gban [user] [reason]",
"key": "5r607k124q5j43085o480a0m528m460i"
}
19 changes: 19 additions & 0 deletions config/plugins/GlobalBanList.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "GlobalBanList",
"author": "JABirchall",
"description": "Check for Global Teamspeak Ban",
"version": "1.0",
"triggers": [
"event"
],
"event": "cliententerview",
"usage": false,
"enabled": true,
"ban": false,
"alert": true,
"alert_groups": [
12,
13,
14
]
}
54 changes: 54 additions & 0 deletions plugins/AddWhitelist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Created by PhpStorm.
* User: Jake
* Date: 16/09/2016
* Time: 08:22
*/

namespace Plugin;

use Illuminate\Database\Capsule\Manager;
use Illuminate\Database\Schema\Blueprint;
use Plugin\Models\Whitelist;
use App\Plugin;

class AddWhitelist extends Plugin implements AdvancedPluginContract
{
private $server;

public function isTriggered()
{
if (!isset($this->info['text'])) {
$this->sendOutput($this->CONFIG['usage']);

return;
}
$this->server = $this->teamSpeak3Bot->node;

Whitelist::create([
'uid' => $this->info['text'],
'added_by' => $this->info['invokername'],
'added_by_uid' => $this->info['invokeruid']
]);

$this->sendOutput('Unique ID: %s has been successfully whitelisted.', $this->info['text']);
}

public function install()
{
Manager::schema()->create($this->CONFIG['table'], function(Blueprint $table) {
$table->increments('id');
$table->text('uid');
$table->text('added_by');
$table->text('added_by_uid');

$table->timestamps();
});
}

public function update($version)
{
// TODO: Implement update() method.
}
}
88 changes: 88 additions & 0 deletions plugins/GlobalBan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Created by PhpStorm.
* User: Jake
* Date: 16/09/2016
* Time: 06:18
*/

namespace Plugin;

use App\Plugin;
use TeamSpeak3\Ts3Exception;

class GlobalBan extends Plugin implements PluginContract
{

public $server;

public function isTriggered()
{
if (!isset($this->info['text'])) {
$this->sendOutput($this->CONFIG['usage']);

return;
}

$this->server = $this->teamSpeak3Bot->node;

list($name, $reason) = $this->info['text']->split(' ');

try {
$client = current($this->server->clientFind($name));
$client = $this->server->clientGetById($client['clid']);
}catch(Ts3Exception $e){
$message = $e->getMessage();
if ($message === "invalid clientID") {
$this->sendOutput("[COLOR=red][b] There are no users online by that name");

return;
}
}

$curl = curl_init();

$fields = [
'key' => $this->CONFIG['key'],
'uid' => $client['client_unique_identifier']->toString(),
'ip' => $client['connection_client_ip'],
'banned_by' => $this->info['invokername'],
'banned_by_uid' => $this->info['invokeruid'],
'reason' => $reason,
'server_name' => $this->server->toString(),
'server_uid' => $this->server['virtualserver_unique_identifier'],
'h' => hash_pbkdf2('sha1', sprintf("%s-%s-%s", $this->CONFIG['key'], $client['client_unique_identifier']->toString(),$this->info['invokeruid']),$this->server['virtualserver_unique_identifier'], 1, 8),

];

curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1/bans/submit');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);

$response = json_decode(curl_exec($curl));
if(curl_errno($curl) != 0) {
$this->sendOutput(sprintf("There was a error with the request: %s", curl_error($curl)));
return;
}

curl_close($curl);

if($response->success === true) {
try {
$id = hash_pbkdf2("sha1", $client['client_unique_identifier']->toString(), '', 1, 8);
$client->poke("[b][color=red]You are global banned by Nimda ID: #{$id}");
$client->poke("[b][color=red]Visit [url=http://support.mxgaming.com/]Global Ban Support[/url].");
$client->ban(1, "Global Ban ID #{$id} ({$reason})");
}catch(Ts3Exception $e){
return;
}

$this->sendOutput(sprintf("[b][color=green] Client %s successfully global banned ID #%s", $client, $id));
} elseif ($response->success === false && $response->err === 0x02) {
$this->sendOutput("[COLOR=red][b]This server is not authorized to global ban, email [email protected]");
}
}

}
87 changes: 87 additions & 0 deletions plugins/GlobalBanList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Created by PhpStorm.
* User: Jake
* Date: 16/09/2016
* Time: 06:18
*/

namespace Plugin;


use App\Plugin;
use Plugin\Models\Whitelist;
use TeamSpeak3\Ts3Exception;


class GlobalBanList extends Plugin implements PluginContract
{

private $server;

public function isTriggered()
{
if($this->CONFIG['enabled'] === false) {
return;
}

$this->server = $this->teamSpeak3Bot->node;

$whitelisted = Whitelist::where('uid', $this->info['client_unique_identifier']->toString())->count();
if($whitelisted >= 1) {
return;
}

$curl = curl_init();

$fields = [
'uid' => $this->info['client_unique_identifier']->toString()
];

curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1/bans/check');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);

$response = json_decode(curl_exec($curl));

if(curl_errno($curl) != 0) {
curl_close($curl);
return;
}

curl_close($curl);

if($response->success === false || $response->banned === false) {
return;
}

try {
$client = $this->server->clientGetByUid($this->info['client_unique_identifier']);
$id = hash_pbkdf2("sha1", $this->info['client_unique_identifier']->toString(), '', 1, 8);
}catch(Ts3Exception $e){
return;
}

if($this->CONFIG['ban'] === true && $response->uid === $this->info['client_unique_identifier']->toString()) {
try {
$client->poke("[b][color=red]You are globally banned by Nimda ID: #{$id}");
$client->poke("[b][color=red]Visit [url=http://support.mxgaming.com/]Global Ban Support[/url].");
$client->ban(1, "Global Ban ID #{$id} ({$response->reason})");
}catch(Ts3Exception $e){
return;
}
}

if($this->CONFIG['alert'] === true) {
$message = sprintf("[ALERT] Client %s is global banned ID #%s reason: %s\n", $client, $id, $response->reason);
array_walk(array_map([$this->server, 'serverGroupGetById'], $this->CONFIG['alert_groups']), function($admin) use ($message) {
$admin->message($message);
});
}

printf("[%s]: %s", $this->teamSpeak3Bot->carbon->now()->toTimeString(), $message);
}

}
17 changes: 17 additions & 0 deletions plugins/Models/Whitelist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: Jake
* Date: 16/09/2016
* Time: 06:29
*/

namespace Plugin\Models;


use Illuminate\Database\Eloquent\Model;

class Whitelist extends Model
{
protected $fillable = ['uid', 'added_by', 'added_by_uid',];
}
Loading

0 comments on commit 3becbb7

Please sign in to comment.