PDA

View Full Version : Detection API



Shytoos
05-08-2016, 03:27 AM
Greetings,


I'm just wondering if you could create an API to get the timestamp of the last Ban Wave, then we could auto-restart our servers when a ban wave is done, because we actually need to reboot our servers manually, otherwise players can't connect to them anymore.

That would be great and not so hard to accomplish, a simple php page which echo the timestamp would be enough.


Thanks in advance,

Regards,
Shytoos.

Neuro Toxin
05-08-2016, 09:48 AM
Made this on my phone on my way to work lol.

https://csgo.tokenstash.com/api/lastban/v0001/?apikey=<your API key from TokenStash>

Shytoos
06-08-2016, 04:05 AM
Still doing the job though, thanks bud!

lydonuis
06-08-2016, 04:12 AM
i think we need to restart our servers after 10-15 minutes. because auto generator does not generate when the token gets banned. is it possible to do ?

Shytoos
06-08-2016, 05:48 AM
Yes btw, my Auto Generator did not generate the tokens today, even if it is written "Enabled" with a green background, and is set to 4.

What may be better than the Auto Generator would be to auto generate a token on server startup if the plugin detects that the actual one is banned.
That would prevent generating more tokens than needed, and, I think, make things more easier for everyone, TokenStash included.

Neuro Toxin
06-08-2016, 09:30 AM
The auto generator runs every 15 minutes due to restrictions on the web host.

If you generate tokens before the auto generator gets a chance you wont have addition tokens generated in error.

The updater plugin uses mysql to check your servers token. Mysql cant connect to the steamapi to generate a token on the spot.

I could add another api method that gives you your token count on the current tokenstash account. You would then detect the new ban time, then detect a token count > 0 so you know your servers are ready for a restart.

I would suggest if your running a cron to the bantime api. Run it at .05, .20, .35, .50 so it's 5 minutes after my cron.

Shytoos
06-08-2016, 10:24 AM
Thanks for the clarifications, the solution I found would be to create an API that allows us to generate tokens through a simple URL, so asap as our crontab detects a banwave through your API, we could generate by ourselves the needed tokens, applying the right configuration to our scripts.

I would personally be able to simply having an URL such as LINK/?api=KEY&tokens=AMOUNT

As far as I remember, there is a cURL extension existing for SourceMod, what you could do (or what I'm gonna do if you create the API asked just above), is:
- Token detected as banned, get the web page "LINK/?api=KEY&tokens=1" (so 1 token is generated), then reboot the server

That may be the simpliest way to achieve what we're looking for, and would make your entire system way much more easier.


EDIT: For the moment, I just placed a 13,28,43,58 * * * * cron, since my scripts display a 3 minutes countdown before restarting the servers, so it might be done after the auto-generation, but I hope we'll be able to figure out how to get this to be as fast as possible! :)

EDIT˛: Just noticed you talked about a "web host", why don't you run your entire system on a VPS or dedicated server, to get ride of any restriction?

Neuro Toxin
06-08-2016, 12:43 PM
I have a few different webhosts.

The host of this site doesn't restrict crons to 15 minutes.

I'll add add a cron on this hosting to call the TokenStash cron php script every minute which should speed things up.

I want to avoid forcing more prerequisites to the updater plugin.

I'm just at the coffee shop after replacing a power steering pump and belt tensioner in my car.

I plan to add this cron today and push an update to the plugin so checks are run every 5 minutes.

Neuro Toxin
06-08-2016, 02:22 PM
I've just implemented a 1 minute cron ;)

No more waiting 15 minutes...

Shytoos
06-08-2016, 10:08 PM
Awesome, since now my servers will now restart 3 minutes after each banwave, thanks a lot for this ! ;)

lydonuis
07-08-2016, 02:35 AM
i dont have so much linux experience so how can i make it like that. restart 3 minutes after banwaves ? Thanks !

Shytoos
07-08-2016, 11:03 AM
Personally, I made a PHP script that is looking at the API, translating the date to an UNIX timestamp, if the timestamp isn't the same as in the database, update the timestamp inside the database.
This PHP script is only executed by a UNIX script (assigned to a cron), every minute, if the PHP script detects a new banwave, it will output "true", if it does so, the UNIX script will call a sleep for 180 seconds, then restart all the servers.

You can leave it as it is, but if you want to go further as I did, write a SourceMod script that reads the database, if the last banwave is not older than 180 seconds, you can print to chat that the server will reboot, then your users are not surprised by the brutal shutdown of it.


EDIT: Since you're not well experienced with Linux, there's a short script achieving what you're looking for


NEW_VERSION="$(curl http://URL_TO_YOUR_PHP_SCRIPT)"

if [ "$NEW_VERSION" = "true" ]; then
echo "New ban wave!"
sleep 180
service csgo-servers restart
else
echo "Servers not banned yet"
fi;

exit 0

There's a scheme of the PHP script:


1. Get timestamp from API
2. Compare with timestamp from DB
3. If not same, echo "true" and set new timestamp in DB

And there's one for the Sourcemod one:


1. Read every x seconds the DB with the last ban wave timestamp
2. If LASTBAN+180-UNIX_TIMESTAMP > 0, new ban wave, so do whatever you need

lydonuis
07-08-2016, 09:48 PM
I tried but i sucked. Can you share or sell your php script ? If its ok to you.

Shytoos
07-08-2016, 09:55 PM
<?php
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

function ConnectSQL() {
global $mysqli;
$mysqli = new mysqli("HOST", "USERNAME", "PASSWORD", "DATABASE");
if (mysqli_connect_errno()) {
die("Can't connect to MySQL server: " . mysqli_connect_error());
} else {
mysqli_set_charset($mysqli, "utf8");
}
}

function unix_timestamp($date) {
$date = str_replace(array(' ', ':'), '-', $date);
$c = explode('-', $date);
$c = array_pad($c, 6, 0);
array_walk($c, 'intval');

return mktime($c[3], $c[4], $c[5], $c[1], $c[2], $c[0]);
}

ConnectSQL();

$previousban = mysqli_fetch_array(mysqli_query($mysqli, "SELECT lastban FROM csgo_banwave LIMIT 0,1"), MYSQLI_ASSOC)['lastban'];
$lastban = unix_timestamp(curl("https://csgo.tokenstash.com/api/lastban/v0001/?apikey=API_KEY"));

if (!empty($lastban) && !empty($previousban) && $previousban != $lastban) {
mysqli_query($mysqli, "UPDATE csgo_banwave SET lastban = ".$lastban." AND timestamp = ".time());
echo "true";
}

mysqli_close($mysqli);
?>

csgo_banwave table should have:
- lastban (INT 11)
- timestamp (INT 11)


There you go, hope it helps.

lydonuis
07-08-2016, 10:39 PM
oh thanks man you are the best!

[TNN] Lubricant Jam II
09-08-2016, 04:37 AM
Shytoos, I thank you very much for sharing a script which you may have not wanted to, if it is okay with you, would you be able to share the sourcemod script or is that asking too much? Many thanks in advance!

Neuro Toxin
09-08-2016, 10:01 AM
Question: Do you guys have players in your servers wben the banwaves occur?

[TNN] Lubricant Jam II
09-08-2016, 10:20 AM
Question: Do you guys have players in your servers wben the banwaves occur?

Generally, my server are always full however no one bothers to inform me.

Shytoos
09-08-2016, 08:35 PM
Question: Do you guys have players in your servers wben the banwaves occur?

Of course, some of my servers actually have players on it whatever time it is.


Lubricant Jam II;2878']Shytoos, I thank you very much for sharing a script which you may have not wanted to, if it is okay with you, would you be able to share the sourcemod script or is that asking too much? Many thanks in advance!

There you go: http://hastebin.com/egucoyodac
Make sure to replace "YOUR_DATABASE" with the name of the database containing csgo_banwave table, and also to add it to your databases.cfg.

Also, there is the csgo_banwave.phrases.txt:


"Phrases"
{
"chat_advert"
{
"#format" "{1:i},{2:s}"
"en" "Server reboot in {green}{1} {default}second{2}."
"fr" "Redémarrage du serveur dans {green}{1} {default}seconde{2}."
}
}


@Neuro Toxin, do you want me to make a tutorial of "how to auto-reboot your servers on each banwave" ?

Neuro Toxin
09-08-2016, 08:59 PM
I'm just thinking about how to implement auto updater checks.

Starting off, I'll have the auto updater running every 5 minutes to query the TokenStash public database for a new GSLT token. I recommend that members don't increase the frequency and recompile their plugins. 5 minutes should be a perfectly suitable interval that doesn't smash the TokenStash public databases.

This requires implementing a timer and threaded database calls. Currently, the updater runs OnPluginStart as a non-threaded check, so servers don't restart before a new token is received. On going calls with a timer need to be threaded so your game servers don't freeze while the TokenStash plugin waits for it's database result sets.

There is one downside to all of this. If your server has no players, timers wont tick:= meaning your servers cant restart themselves. A solution is to disable hibernation, I just don't see this as a good solution.

This leads me to my second implementation. I could add a remote console (rcon) command that can be called from the TokenStash web servers. Only problem, TokenStash will need to store your rcon passwords. Using such an implementation would resolve the hibernation problem and make the API method I've implemented redundant.

Shytoos
09-08-2016, 09:23 PM
Why don't you simply use my method I shared here to detect when there is a ban, and so if there is one, instead of sending a message in chat, simply get a new token the same way it should be done at server startup and set it ?

lydonuis
09-08-2016, 11:35 PM
Since you shared your script you helped i couldnt make it work and you did so much so i didnt want to bother you. But when i type ./tokenupdate.sh i get this error :
./tokenupdate.sh: line 2: $'\r': command not found
./tokenupdate.sh: line 9: $'\r': command not found
./tokenupdate.sh: line 10: $'\r': command not found

This is my script which you shared : http://hastebin.com/emaleluzuw.hs

Neuro Toxin
10-08-2016, 12:03 AM
The new plugin is now available.

Download version 006 via the `Download / Installation / Settings` link @ TokenStash.

Version 0.0.6
- Check for bans every 5 minutes

If any servers are empty when the banwave occurs, they will need to still be manually restarted.

Shytoos
10-08-2016, 05:32 AM
Since you shared your script you helped i couldnt make it work and you did so much so i didnt want to bother you. But when i type ./tokenupdate.sh i get this error :
./tokenupdate.sh: line 2: $'\r': command not found
./tokenupdate.sh: line 9: $'\r': command not found
./tokenupdate.sh: line 10: $'\r': command not found

This is my script which you shared : http://hastebin.com/emaleluzuw.hs

You can now use the new ts-auto-updater plugin, since it does pretty much the same thing.

Otherwise if you still to use my scripts, you simply need to run this command, while being in the folder containing your script:


dos2unix ./tokenupdate.sh

If you can't run dos2unix, you must install tofrodos by running (for Ubuntu/Debian):


sudo apt-get install tofrodos