PyPlanet / PyPlanet

:rocket: PyPlanet is the next-gen Server Controller for ManiaPlanet (v4+) and Trackmania. Using Python3 and AsyncIO.
http://pypla.net/
GNU General Public License v3.0
108 stars 47 forks source link

[TM2020] Conversion of WebserviceID #1038

Open w1lla opened 3 years ago

w1lla commented 3 years ago

Hi,

as spoken here: https://forum.nadeo.com/viewtopic.php?f=94&t=2303 you can do a conversion of webserviceId to Login and back.

I did a basic php example which can be seen here:

<?php

function hex_to_base64($hex){
  $return = '';
  foreach(str_split($hex, 2) as $pair){
    $return .= chr(hexdec($pair));
  }
  return base64_encode($return);
}

function base64_to_hex($hex){
    $return = '';
  foreach(str_split($hex, 2) as $pair){
    $return .= chr(dechex($pair));
  }
  return base64_encode($return);
}
$login = 'q2-lckjXSxai11x2CgX5ew';
$webserviceID = 'ab6fa572-48d7-4b16-a2d7-5c760a05f97b';

$webserviceID_nohyphen = str_replace("-","",$webserviceID);
$conversionbase64 = hex_to_base64($webserviceID_nohyphen);
$replace1sttry = str_replace("/","_",$conversionbase64);
$replace2ndtry = str_replace("+","-",$replace1sttry);
$trim3rdtry = trim($replace2ndtry,"=");
$webserviceoutputlogin = $trim3rdtry;
echo $webserviceoutputlogin;
if ($login == $webserviceoutputlogin){
echo '<br>Login && Webservice ID are the same';
}
else
{
echo '<br>Login && Webservice ID are not the same';
}

$add1sttry = str_pad($login, 24, "=", STR_PAD_RIGHT);
$replace3rdtry = str_replace("-","+",$add1sttry);
$replace4thtry = str_replace("_","/",$replace3rdtry);
$decodedbase64 = base64_decode($replace4thtry);
$loginwebserviceoutput = vsprintf("%s%s-%s-%s-%s-%s%s%s", str_split(bin2hex($decodedbase64), 4));
echo '<br>';
echo $loginwebserviceoutput;
if ($webserviceID == $loginwebserviceoutput){
echo '<br>Login && Webservice ID are the same';
}
else
{
echo '<br>Login && Webservice ID are not the same';
}
?>

Even though they might add in future updates the requested WebserviceId this is a temporary fix for in the meantime.

It comes in handy for example Knockout mode where the callback Trackmania.Knockout.Elimination only gives back the webserviceId.

Or in this case:

https://github.com/PyPlanet/PyPlanet/blob/c491a9265c53a47c3bd2867537003bf59c9a0357/pyplanet/apps/core/trackmania/callbacks.py#L72

It gives the account_id which is the webserviceId. so in a case you could on player_connect let it decode the player_login so in future cases it can be needed.

w1lla commented 3 years ago

For pyplanet it can be done in manager.py as seen in the pull request no: #1039

w1lla commented 3 years ago

Only did the conversion not vice-versa. But its basicly the other way around.

tomvlk commented 1 year ago

Hi @w1lla can you make functions to do the opposite as well. We have to prepare for potential usage in the future of this specific ID!

w1lla commented 1 year ago

player_login = 'q2-lckjXSxai11x2CgX5ew';
webserviceID = 'ab6fa572-48d7-4b16-a2d7-5c760a05f97b';
player_addweb = player_login.ljust(24, "=")
player_replaceweb1 = player_addweb.replace("-", "+")
player_replaceweb2 = player_replaceweb1.replace("_", "/")
decodedbase64 = base64.b64decode(bytes(player_replaceweb2, 'ascii'))
player_webserviceId = uuid.UUID(bytes=decodedbase64)

print(player_webserviceId)

webservicereplace = webserviceID.replace('-','')
b64 = base64.b64encode(bytes.fromhex(webservicereplace)).decode()
replace1sttry = b64.replace('/','_')
replace2ndtry = replace1sttry.replace('+','-')
trim3rdtry = replace2ndtry.strip('=')
webserviceoutputlogin = trim3rdtry

print(webserviceoutputlogin)

outputs:

ab6fa572-48d7-4b16-a2d7-5c760a05f97b q2-lckjXSxai11x2CgX5ew