Instantly share code, notes, and snippets.

Embed
What would you like to do?
migrate server in batch
#!/bin/bash
# (C) 2017 by Sebastian 'bueddl' Büttner <tm@bueddl.de>
#
# MP4 & NADEO rocks!
# ---------------------------------------------------------------------
# !!!! ATTENTION !!!!
# You are supposed to change at least the next 2 variables.
# Despite it's possible to leave username and passwort to $1 and $2, which
# makes them command line parameters, it is strongly discouraged, as the
# password entered into your shell might be logged by some machanism equal
# or comparable to the .bash_history file. So please don't ;-)
# Change this to your mp3/4 player login/username
username="$1"
# Change this to your account password
password="$2"
# Change this to match your server password
# or leave it empty to generate a new randomized password for each gameserver
default_serverpassword=''
# Change this to the zone for your mp4 servers
default_serverzone='World|Europe|Germany|Hessen|Frankfurt'
server_prefix='paragonleague'
minid=1
maxid=9
# ---------------------------------------------------------------------
playerpage_url_mp3='https://player.maniaplanet.com'
login_url_mp3='https://mp02.maniaplanet.com/player/html/'
dedicated_servers_mp3='https://player.maniaplanet.com/dedicated-servers'
playerpage_url_mp4='https://www.maniaplanet.com/login'
login_url_mp4='https://www.maniaplanet.com/login'
dedicated_servers_mp4='https://www.maniaplanet.com/account/dedicated-servers'
urlencode()
{
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
urldecode()
{
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
function mp3_login()
{
local login="$1"
local password="$2"
output=$(wget --load-cookies=cookies.txt \
--save-cookies=cookies.txt \
--keep-session-cookies \
--output-file=/dev/null \
--output-document=- \
"$playerpage_url_mp3")
redirect_uri=$(echo $output | egrep -oe 'redirect_uri%3D.*?&amp;' | head -c-6 | tail -c+16)
redirect_uri=$(urldecode $redirect_uri)
if [ -z "$redirect_uri" ]; then
echo "Error: Invalid redirect_uri fetched. Please try again."
exit 1
fi
local post_data="userlogin=ws.maniaplanet.com&"\
"redirection=https://ws.maniaplanet.com/oauth2/authorize/?client_id=maniaplanet&"\
"redirect_uri=$redirect_uri&"\
"money=0&"\
"submitLogin=Ok&"\
"iframe-hide-menu=&"\
"view=&"\
"login=$login&"\
"password=$password"
wget --load-cookies=cookies.txt \
--save-cookies=cookies.txt \
--keep-session-cookies \
--output-file=/dev/null \
--post-data=$post_data \
--output-document=/dev/null \
"$login_url_mp3"
}
function mp3_fetch_dedicated_servers()
{
output=$(wget --load-cookies=cookies.txt \
--save-cookies=cookies.txt \
--keep-session-cookies \
--output-file=/dev/null \
--output-document=- \
-S --header="accept-encoding: gzip" \
"$dedicated_servers_mp3" | gunzip -)
echo "$output" | egrep -oe 'selectedServer=(.*?)"' | sed -r 's/selectedServer=(.*?)"/\1/g'
}
function end()
{
rm cookies.txt
}
function mp4_login()
{
local login="$1"
local password="$2"
local post_data="_remember_me=off&"\
"_chat=off&"\
"_username=$login&"\
"_password=$password"
wget --load-cookies=cookies.txt \
--save-cookies=cookies.txt \
--keep-session-cookies \
--output-file=/dev/null \
--post-data=$post_data \
--output-document=/dev/null \
"$login_url_mp4"
}
function mp4_fetch_dedicated_servers()
{
output=$(wget --load-cookies=cookies.txt \
--save-cookies=cookies.txt \
--keep-session-cookies \
--output-file=/dev/null \
--output-document=- \
-S --header="accept-encoding: gzip" \
"$dedicated_servers_mp4" | gunzip -)
echo "$output" | egrep -oe 'dedicated-servers/(.*?)/edit' | sed -r 's/dedicated-servers\/(.*?)\/edit/\1/g'
}
function mp4_create_dedicated_server()
{
local server_login="$1"
local server_password="$2"
local server_zone="$3"
output=$(wget --load-cookies=cookies.txt \
--save-cookies=cookies.txt \
--keep-session-cookies \
--output-file=/dev/null \
--output-document=- \
-S --header="accept-encoding: gzip" \
"$dedicated_servers_mp4" | gunzip -)
token=$(echo "$output" | \
egrep -oe 'name="dedicated_server_create\[_token\]" value="(.*?)"' | \
sed -r 's/name="dedicated_server_create\[_token\]" value="(.*?)"/\1/')
local post_data="dedicated_server_create[login]=$server_login&"\
"dedicated_server_create[password]=$server_password&"\
"dedicated_server_create[zone]=$server_zone&"\
"dedicated_server_create[create]=&"\
"dedicated_server_create[_token]=$token"
wget --load-cookies=cookies.txt \
--save-cookies=cookies.txt \
--keep-session-cookies \
--output-file=/dev/null \
--post-data=$post_data \
--output-document=/dev/null \
"$dedicated_servers_mp4"
}
mp3_login $username $password
mp4_login $username $password
#mp4_fetch_dedicated_servers
for server in $(mp3_fetch_dedicated_servers); do
_id=$(echo $server | sed -r "s/$server_prefix([0-9]+)/\1/g")
if [[ "$_id" == "$server" ]]; then
continue
fi
if [[ $_id -ge $minid && $_id -le $maxid ]]; then
echo "Importing server: $server" >&2
if [ -z "$default_serverpassword" ]; then
server_password=$(pwgen -s 16 1)
else
server_password="$default_serverpassword"
fi
mp4_create_dedicated_server $server "$server_password" "$default_serverzone"
echo -e "$server_login\t$server_password"
fi
done
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment