Skip to content

Commit 3becbb7

Browse files
authored
Merge pull request #9 from JABirchall/GlobalBan
Global ban System Tested and functions as intended
2 parents 244cd17 + 966172c commit 3becbb7

File tree

13 files changed

+311
-14
lines changed

13 files changed

+311
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ PHP version 5.6+, php_PDO extentions, Teamspeak 3 Server
2626
* Asynchronous execution
2727
* Plugin Autoloading and Auotinstalling
2828
* Eloquent database support for Progresql, Mysql, MSSQL, SQLite
29+
* Global Ban system built in
2930

3031
## Coding style
3132

app/TeamSpeak3Bot.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class TeamSpeak3Bot
119119

120120
private $lastEvent;
121121

122-
private $carbon;
122+
public $carbon;
123123

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

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

149-
if($username === "serveradmin") {
149+
if($username === "serveradmin" && !Self::$config['ignoreWarnings']) {
150150
$this->printOutput("[WARNING] Running Nimda logged in as serveradmin is bad!");
151151
$this->printOutput("Start anyway? Y/N:", false);
152152
$response = rtrim(fgets(STDIN));
@@ -209,7 +209,7 @@ protected function register()
209209
{
210210
$this->node->notifyRegister("textserver");
211211
$this->node->notifyRegister("textchannel");
212-
$this->node->notifyRegister("textprivate");
212+
//$this->node->notifyRegister("textprivate");
213213
$this->node->notifyRegister("server");
214214
$this->node->notifyRegister("channel");
215215
}
@@ -341,6 +341,7 @@ private function loadPlugin($configFile)
341341
return false;
342342
}
343343

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

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

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

364366
Plugin::create([
@@ -446,7 +448,7 @@ public function onMessage(Event $event)
446448
public function onEvent(Event $event)
447449
{
448450
$data = $event->getData();
449-
if (@$data['invokername'] == $this->name || @$data['invokeruid'] == 'serveradmin' || @$data["client_unique_identifier"] == "ServerQuery") {
451+
if (@$data['client_type'] == 1) {
450452
return;
451453
} elseif ($this->lastEvent && $event->getMessage()->contains($this->lastEvent)) {
452454
return;

config/TeamSpeak.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
* Date: 10/08/2016
66
* Time: 22:44
77
*/
8-
98
namespace Config;
109

1110
class TeamSpeak
1211
{
1312

1413
public static $TS3config = [
1514
'username' => 'serveradmin',
16-
'password' => 'password',
17-
'host' => '127.0.0.1',
15+
'password' => 'Tyc00n..',
16+
'host' => 'ts3.mxgaming.com',
1817
'port' => 10011,
1918
'name' => 'Nimda',
2019
'serverPort' => 9987,
2120
'timeout' => 1,
2221
'misc' => [
2322
'debug' => true,
24-
'newLineNewMessage' => true,
23+
'newLineNewMessage' => false,
24+
'ignoreWarnings' => true
2525
]
2626
];
2727

config/plugins/AddWhitelist.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "AddWhitelist",
3+
"author": "JABirchall",
4+
"description": "Whitelist a user from the global ban system.",
5+
"version": "1.0",
6+
"triggers": [
7+
"!whitelist"
8+
],
9+
"table": "whitelists",
10+
"usage": "Usage: !whitelist [uid]"
11+
12+
}

config/plugins/GlobalBan.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "GlobalBan",
3+
"author": "JABirchall",
4+
"description": "Submit a Global Teamspeak ban",
5+
"version": "1.0",
6+
"triggers": [
7+
"!gban",
8+
"!globalban"
9+
],
10+
"usage": "Usage !gban [user] [reason]",
11+
"key": "5r607k124q5j43085o480a0m528m460i"
12+
}

config/plugins/GlobalBanList.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "GlobalBanList",
3+
"author": "JABirchall",
4+
"description": "Check for Global Teamspeak Ban",
5+
"version": "1.0",
6+
"triggers": [
7+
"event"
8+
],
9+
"event": "cliententerview",
10+
"usage": false,
11+
"enabled": true,
12+
"ban": false,
13+
"alert": true,
14+
"alert_groups": [
15+
12,
16+
13,
17+
14
18+
]
19+
}

plugins/AddWhitelist.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Jake
5+
* Date: 16/09/2016
6+
* Time: 08:22
7+
*/
8+
9+
namespace Plugin;
10+
11+
use Illuminate\Database\Capsule\Manager;
12+
use Illuminate\Database\Schema\Blueprint;
13+
use Plugin\Models\Whitelist;
14+
use App\Plugin;
15+
16+
class AddWhitelist extends Plugin implements AdvancedPluginContract
17+
{
18+
private $server;
19+
20+
public function isTriggered()
21+
{
22+
if (!isset($this->info['text'])) {
23+
$this->sendOutput($this->CONFIG['usage']);
24+
25+
return;
26+
}
27+
$this->server = $this->teamSpeak3Bot->node;
28+
29+
Whitelist::create([
30+
'uid' => $this->info['text'],
31+
'added_by' => $this->info['invokername'],
32+
'added_by_uid' => $this->info['invokeruid']
33+
]);
34+
35+
$this->sendOutput('Unique ID: %s has been successfully whitelisted.', $this->info['text']);
36+
}
37+
38+
public function install()
39+
{
40+
Manager::schema()->create($this->CONFIG['table'], function(Blueprint $table) {
41+
$table->increments('id');
42+
$table->text('uid');
43+
$table->text('added_by');
44+
$table->text('added_by_uid');
45+
46+
$table->timestamps();
47+
});
48+
}
49+
50+
public function update($version)
51+
{
52+
// TODO: Implement update() method.
53+
}
54+
}

plugins/GlobalBan.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Jake
5+
* Date: 16/09/2016
6+
* Time: 06:18
7+
*/
8+
9+
namespace Plugin;
10+
11+
use App\Plugin;
12+
use TeamSpeak3\Ts3Exception;
13+
14+
class GlobalBan extends Plugin implements PluginContract
15+
{
16+
17+
public $server;
18+
19+
public function isTriggered()
20+
{
21+
if (!isset($this->info['text'])) {
22+
$this->sendOutput($this->CONFIG['usage']);
23+
24+
return;
25+
}
26+
27+
$this->server = $this->teamSpeak3Bot->node;
28+
29+
list($name, $reason) = $this->info['text']->split(' ');
30+
31+
try {
32+
$client = current($this->server->clientFind($name));
33+
$client = $this->server->clientGetById($client['clid']);
34+
}catch(Ts3Exception $e){
35+
$message = $e->getMessage();
36+
if ($message === "invalid clientID") {
37+
$this->sendOutput("[COLOR=red][b] There are no users online by that name");
38+
39+
return;
40+
}
41+
}
42+
43+
$curl = curl_init();
44+
45+
$fields = [
46+
'key' => $this->CONFIG['key'],
47+
'uid' => $client['client_unique_identifier']->toString(),
48+
'ip' => $client['connection_client_ip'],
49+
'banned_by' => $this->info['invokername'],
50+
'banned_by_uid' => $this->info['invokeruid'],
51+
'reason' => $reason,
52+
'server_name' => $this->server->toString(),
53+
'server_uid' => $this->server['virtualserver_unique_identifier'],
54+
'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),
55+
56+
];
57+
58+
curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1/bans/submit');
59+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
60+
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
61+
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
62+
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
63+
64+
$response = json_decode(curl_exec($curl));
65+
if(curl_errno($curl) != 0) {
66+
$this->sendOutput(sprintf("There was a error with the request: %s", curl_error($curl)));
67+
return;
68+
}
69+
70+
curl_close($curl);
71+
72+
if($response->success === true) {
73+
try {
74+
$id = hash_pbkdf2("sha1", $client['client_unique_identifier']->toString(), '', 1, 8);
75+
$client->poke("[b][color=red]You are global banned by Nimda ID: #{$id}");
76+
$client->poke("[b][color=red]Visit [url=http://support.mxgaming.com/]Global Ban Support[/url].");
77+
$client->ban(1, "Global Ban ID #{$id} ({$reason})");
78+
}catch(Ts3Exception $e){
79+
return;
80+
}
81+
82+
$this->sendOutput(sprintf("[b][color=green] Client %s successfully global banned ID #%s", $client, $id));
83+
} elseif ($response->success === false && $response->err === 0x02) {
84+
$this->sendOutput("[COLOR=red][b]This server is not authorized to global ban, email support@mxgaming.com");
85+
}
86+
}
87+
88+
}

plugins/GlobalBanList.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Jake
5+
* Date: 16/09/2016
6+
* Time: 06:18
7+
*/
8+
9+
namespace Plugin;
10+
11+
12+
use App\Plugin;
13+
use Plugin\Models\Whitelist;
14+
use TeamSpeak3\Ts3Exception;
15+
16+
17+
class GlobalBanList extends Plugin implements PluginContract
18+
{
19+
20+
private $server;
21+
22+
public function isTriggered()
23+
{
24+
if($this->CONFIG['enabled'] === false) {
25+
return;
26+
}
27+
28+
$this->server = $this->teamSpeak3Bot->node;
29+
30+
$whitelisted = Whitelist::where('uid', $this->info['client_unique_identifier']->toString())->count();
31+
if($whitelisted >= 1) {
32+
return;
33+
}
34+
35+
$curl = curl_init();
36+
37+
$fields = [
38+
'uid' => $this->info['client_unique_identifier']->toString()
39+
];
40+
41+
curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1/bans/check');
42+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
43+
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
44+
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
45+
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
46+
47+
$response = json_decode(curl_exec($curl));
48+
49+
if(curl_errno($curl) != 0) {
50+
curl_close($curl);
51+
return;
52+
}
53+
54+
curl_close($curl);
55+
56+
if($response->success === false || $response->banned === false) {
57+
return;
58+
}
59+
60+
try {
61+
$client = $this->server->clientGetByUid($this->info['client_unique_identifier']);
62+
$id = hash_pbkdf2("sha1", $this->info['client_unique_identifier']->toString(), '', 1, 8);
63+
}catch(Ts3Exception $e){
64+
return;
65+
}
66+
67+
if($this->CONFIG['ban'] === true && $response->uid === $this->info['client_unique_identifier']->toString()) {
68+
try {
69+
$client->poke("[b][color=red]You are globally banned by Nimda ID: #{$id}");
70+
$client->poke("[b][color=red]Visit [url=http://support.mxgaming.com/]Global Ban Support[/url].");
71+
$client->ban(1, "Global Ban ID #{$id} ({$response->reason})");
72+
}catch(Ts3Exception $e){
73+
return;
74+
}
75+
}
76+
77+
if($this->CONFIG['alert'] === true) {
78+
$message = sprintf("[ALERT] Client %s is global banned ID #%s reason: %s\n", $client, $id, $response->reason);
79+
array_walk(array_map([$this->server, 'serverGroupGetById'], $this->CONFIG['alert_groups']), function($admin) use ($message) {
80+
$admin->message($message);
81+
});
82+
}
83+
84+
printf("[%s]: %s", $this->teamSpeak3Bot->carbon->now()->toTimeString(), $message);
85+
}
86+
87+
}

plugins/Models/Whitelist.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Jake
5+
* Date: 16/09/2016
6+
* Time: 06:29
7+
*/
8+
9+
namespace Plugin\Models;
10+
11+
12+
use Illuminate\Database\Eloquent\Model;
13+
14+
class Whitelist extends Model
15+
{
16+
protected $fillable = ['uid', 'added_by', 'added_by_uid',];
17+
}

0 commit comments

Comments
 (0)