Skip to content

Instantly share code, notes, and snippets.

@t0rr3sp3dr0
Last active August 27, 2017 20:38
Show Gist options
  • Save t0rr3sp3dr0/fbf121329a8e6480bab63aff88c2279c to your computer and use it in GitHub Desktop.
Save t0rr3sp3dr0/fbf121329a8e6480bab63aff88c2279c to your computer and use it in GitHub Desktop.
#!/bin/bash
# UTILITIES BEGIN
function L {
cat << EOF
██╗
██║
██║ ██████╗ ██████╗██╗ ██╗██████╗ ██████╗ ██╗ ██╗███╗ ██╗
██║ ██╔═══██╗██╔════╝██║ ██╔╝██╔══██╗██╔═══██╗██║ ██║████╗ ██║
██║ ██║ ██║██║ █████╔╝ ██║ ██║██║ ██║██║ █╗ ██║██╔██╗ ██║
██║ ██║ ██║██║ ██╔═██╗ ██║ ██║██║ ██║██║███╗██║██║╚██╗██║
███████╗╚██████╔╝╚██████╗██║ ██╗██████╔╝╚██████╔╝╚███╔███╔╝██║ ╚████║
╚══════╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚══╝╚══╝ ╚═╝ ╚═══╝
Developed by Pedro Tôrres
EOF
}
function DFI {
gtk-update-icon-cache -f $HOME/.local/share/icons/hicolor >/dev/null 2>&1
desktop-file-install --dir=$HOME/.local/share/applications "$HOME/.local/share/applications/$1"
}
function LtL {
DFI $1
favorites=$(
python << EOF
from collections import OrderedDict
array = eval("$(gsettings get com.canonical.Unity.Launcher favorites)")
print(str(list(OrderedDict.fromkeys(array[:-3] + ["$1"] + array[-3:]))))
EOF
)
gsettings set com.canonical.Unity.Launcher favorites "$favorites"
}
function DF {
tput cuu1
tput el
ARGS=$(printf "$ARGS" | base64)
CMD="$(declare -f L);$(declare -f DFI);$(declare -f LtL);$(declare -f DF);$(declare -f Q);$(declare -f NULL);trap NULL INT;$(declare -f CCC);$(declare -f IO);$(declare -f EL);$(declare -f WMB);$(declare -f WGET);$(declare -f TAR);$(declare -f CP);$(declare -f RM);$(declare -f UNZIP);$(declare -f DPKG);$(declare -f APTGET);sleep 4;$(declare -f $1);$1"
xterm -fa "Monospace Regular" -fs 11 ${@:2} -e "bash -ic 'ARGS=\$(printf \"$ARGS\" | base64 -d);${CMD//\'/\'\"\'\"\'}'" < /dev/null > /dev/null 2>&1&
ARGS=$(printf "$ARGS" | base64 -d)
}
function Q {
tput clear
tput reset
exit
}
function NULL {
:
}
# UTILITIES END
# LEGACY TUI BEGIN
function CCC {
tput clear
tput reset
tput bold
printf "\n$(L)\n\n\e[1;49;32m$1\e[0m"
}
function IO {
tput bold
tput sc
for i in `seq 1 $(tput lines)`; do
tput el
printf '\033[B'
done
printf "\e[7;49;31m"
printf "Invalid Option"
for i in `seq 1 $(expr $(tput cols) - 14)`; do
printf ' '
done
printf "\e[0m"
tput rc
tput cuu1
tput el
tput sgr 0
}
function EL {
if [ $? -eq 0 ]
then
CCC "$1! Press any key to continue...\n\n"
else
CCC "Something went wrong! Please try again...\n\n"
fi
}
# LEGACY TUI END
# WHIPTAIL TUI BEGIN
function WMB {
whiptail --msgbox "$(L)" 0 0 0 --fb --title "$*"
}
function WGET {
{
while read line
do
remaining=$(echo "$line" | grep '%' | rev | cut -d ' ' -f 1 | rev)
echo XXX
echo "$line" | grep '%' | cut -d '%' -f 1 | rev | cut -d ' ' -f 1 | rev
echo "\nDownloading $1...\n$remaining remaining"
echo XXX
done < <(wget "$2" -O "$3" --header "$4" 2>&1)
} | whiptail --title "Lockdown" --gauge "\nDownloading $1...\n " 0 0 0
}
function TAR {
( pv -n "$2" | tar -xzf - -C "$3" ) 2>&1 | whiptail --title "Lockdown" --gauge "\nExtracting $1..." 0 0 0
}
function CP {
( cp -fRv ${@:2} | pv -elnps $(find ${@:2:(( $# - 2 ))} | wc -l) ) 2>&1 | whiptail --title "Lockdown" --gauge "\nCopying $1..." 0 0 0
}
function RM {
( rm -fRv ${@:2} | pv -elnps $(find ${@:2} | wc -l) ) 2>&1 | whiptail --title "Lockdown" --gauge "\nRemoving $1..." 0 0 0
}
function UNZIP {
( unzip "$2" -d "$3" | pv -elnps $(unzip -l "$2" | wc -l) ) 2>&1 | whiptail --title "Lockdown" --gauge "\nExtracting $1..." 0 0 0
}
function DPKG {
( dpkg --vextract "$2" "$3" | pv -elnps $(dpkg -c "$2" | wc -l) ) 2>&1 | whiptail --title "Lockdown" --gauge "\nExtracting $1..." 0 0 0
}
function APTGET {
( apt-get download -q ${@:2} | pv -elnps $# ) 2>&1 | whiptail --title "Lockdown" --gauge "\nDownloading $1..." 0 0 0
}
# WHIPTAIL TUI END
function S {
function F {
function M {
NAME=$(getent passwd $USER | cut -d ':' -f 5)
if (whiptail --yesno "$(L)" 0 0 --fb --title "What would you like to do?" --yes-button "Submit Answer" --no-button "Log Out")
then
while (( 1 ))
do
{
sleep 2
echo "EHLO cin.ufpe.br"
sleep 2
echo "MAIL FROM: $USER"
sleep 2
echo "RCPT TO: <gcf@cin.ufpe.br>"
echo "RCPT TO: <msc3@cin.ufpe.br>"
echo "RCPT TO: <phts@cin.ufpe.br>"
sleep 2
echo "DATA"
echo "MIME-Version: 1.0"
echo "Subject: [Lockdown-$ARGS] $USER"
echo "From: \"$NAME\" <$USER@cin.ufpe.br>"
echo "Reply-To: \"$NAME\" <$USER@cin.ufpe.br>"
echo "Content-Type: text/x-c; name=\"$(echo $NAME | sed -e 's/ /_/g').cpp\""
echo "Content-Disposition: attachment; filename=\"$(echo $NAME | sed -e 's/ /_/g').cpp\""
echo "Content-Transfer-Encoding: base64"
echo
cat $HOME/.lockdown/data/$USER-$ARGS.cpp | openssl base64
echo "."
sleep 4
echo ""
echo "QUIT"
} | telnet caruaru.cin.ufpe.br 25
if [ $? -eq 0 ]
then
break
fi
done
fi
find $HOME/* -maxdepth 0 -type d -exec chmod 755 {} \;
find $HOME/* -maxdepth 0 -type f -exec chmod 644 {} \;
killall -u $USER
}
function C {
{
while (( 1 ))
do
read min sec <<< $(date +'%M %S')
min=${min#0}
sec=${sec#0}
echo XXX
echo $(( -1 * (($min * 60 + $sec) - 3600) * 100 / 3600 ))
echo "\nTempo Restante: $(( 60 - $min - 1 ))min $(( 60 - $sec - 1 ))s "
echo XXX
sleep 0
done
} | whiptail --title "Lockdown" --gauge "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" 0 0 0
}
function PL {
while (( 1 ))
do
printf "$ARGS" | less
done
}
function TE {
touch $HOME/.lockdown/data/$USER-$ARGS.cpp $HOME/.lockdown/data/$USER-$ARGS.in $HOME/.lockdown/data/$USER-$ARGS.out $HOME/.lockdown/data/$USER-$ARGS.diff
while (( 1 ))
do
case $(whiptail --menu "$(L)" 0 0 0 "Atom" "" "gedit" "" "GVIm" "" "Sublime Text" "" --fb --title "Which text editor would you like to use?" 3>&1 1>&2 2>&3) in
"Atom")
atom $HOME/.lockdown/data/$USER-$ARGS.cpp $HOME/.lockdown/data/$USER-$ARGS.in $HOME/.lockdown/data/$USER-$ARGS.out $HOME/.lockdown/data/$USER-$ARGS.diff < /dev/null > /dev/null 2>&1&
;;
"gedit")
gedit $HOME/.lockdown/data/$USER-$ARGS.cpp $HOME/.lockdown/data/$USER-$ARGS.in $HOME/.lockdown/data/$USER-$ARGS.out $HOME/.lockdown/data/$USER-$ARGS.diff < /dev/null > /dev/null 2>&1&
;;
"GNU nano")
xterm -fa "Monospace Regular" -fs 11 -e "nano $HOME/.lockdown/data/$USER-$ARGS.cpp $HOME/.lockdown/data/$USER-$ARGS.in $HOME/.lockdown/data/$USER-$ARGS.out $HOME/.lockdown/data/$USER-$ARGS.diff" < /dev/null > /dev/null 2>&1&
;;
"GVIm")
gvim $HOME/.lockdown/data/$USER-$ARGS.cpp $HOME/.lockdown/data/$USER-$ARGS.in $HOME/.lockdown/data/$USER-$ARGS.out $HOME/.lockdown/data/$USER-$ARGS.diff < /dev/null > /dev/null 2>&1&
;;
"Sublime Text")
subl $HOME/.lockdown/data/$USER-$ARGS.cpp $HOME/.lockdown/data/$USER-$ARGS.in $HOME/.lockdown/data/$USER-$ARGS.out $HOME/.lockdown/data/$USER-$ARGS.diff < /dev/null > /dev/null 2>&1&
;;
"VIm")
xterm -fa "Monospace Regular" -fs 11 -e "vim $HOME/.lockdown/data/$USER-$ARGS.cpp $HOME/.lockdown/data/$USER-$ARGS.in $HOME/.lockdown/data/$USER-$ARGS.out $HOME/.lockdown/data/$USER-$ARGS.diff" < /dev/null > /dev/null 2>&1&
;;
esac
done
}
function ET {
function R {
g++ -std=c++11 $HOME/.lockdown/data/$USER-$ARGS.cpp -o $HOME/.lockdown/data/a.out
if [ $? -eq 0 ]
then
$HOME/.lockdown/data/a.out < $HOME/.lockdown/data/$USER-$ARGS.in > $HOME/.lockdown/data/$USER-$ARGS.out
else
sleep 3600
fi
}
function RI {
g++ -std=c++11 $HOME/.lockdown/data/$USER-$ARGS.cpp -o $HOME/.lockdown/data/a.out
if [ $? -eq 0 ]
then
echo
echo '--------BEGIN--------'
echo
$HOME/.lockdown/data/a.out
echo
echo '---------END---------'
echo
fi
sleep 3600
}
function RD {
g++ -std=c++11 $HOME/.lockdown/data/$USER-$ARGS.cpp -o $HOME/.lockdown/data/a.out
if [ $? -eq 0 ]
then
echo
echo '--------BEGIN--------'
echo
$HOME/.lockdown/data/a.out < $HOME/.lockdown/data/$USER-$ARGS.in > $HOME/.lockdown/data/$USER-$ARGS.out
if [ $? -eq 0 ]
then
diff -s $HOME/.lockdown/data/$USER-$ARGS.diff $HOME/.lockdown/data/$USER-$ARGS.out
fi
echo
echo '---------END---------'
echo
fi
sleep 3600
}
function CET {
g++ -std=c++11 $HOME/.lockdown/data/$USER-$ARGS.cpp -o $HOME/.lockdown/data/a.out
if [ $? -eq 0 ]
then
echo
echo '--------BEGIN--------'
echo
time $HOME/.lockdown/data/a.out < $HOME/.lockdown/data/$USER-$ARGS.in > $HOME/.lockdown/data/$USER-$ARGS.out
echo
echo '---------END---------'
echo
fi
sleep 3600
}
while (( 1 ))
do
case $(whiptail --menu "$(L)" 0 0 0 "Run" "" "Run Interactively" "" "Run then Diff" "" "Compute Execution Time" "" --fb --title "What would you like to do?" 3>&1 1>&2 2>&3) in
"Run")
DF R -title Run
;;
"Run Interactively")
DF RI -title Run-Interactively
;;
"Run then Diff")
DF RD -title Run-then-Diff
;;
"Compute Execution Time")
DF CET -title Compute-Execution-Time
;;
esac
done
}
while (( 1 ))
do
ID=$(whiptail --inputbox "$(L)" 0 0 "" --fb --title "Enter Problem ID" 3>&1 1>&2 2>&3)
if [ -n "$ID" ]
then
RAW=$(curl -s https://pastebin.com/raw/$ID)
if [ -n "$RAW" ]
then
ARGS=$ID
DF M -title Lockdown
ARGS=
DF C -title Countdown
ARGS=$RAW
DF PL -title Problem -bg white -fg black -fullscreen
ARGS=$ID
DF TE -title Text-Editor
ARGS=$ID
DF ET -title Execution-Tools
sleep 2
xclock -digital -update 1 < /dev/null > /dev/null 2>&1&
xclock -digital -update 1 < /dev/null > /dev/null 2>&1&
xclock -digital -update 1 < /dev/null > /dev/null 2>&1&
sleep 2
wmctrl -i -r $(wmctrl -l | awk '/xclock/{i++}i==2' | cut -f 1 -d " ") -t 2
wmctrl -i -r $(wmctrl -l | awk '/xclock/{i++}i==3' | cut -f 1 -d " ") -t 3
wmctrl -i -r $(wmctrl -l | grep Problem | cut -f 1 -d " ") -t 1
wmctrl -i -r $(wmctrl -l | grep Text-Editor | cut -f 1 -d " ") -t 2
wmctrl -i -r $(wmctrl -l | grep Execution-Tools | cut -f 1 -d " ") -t 3
break
fi
else
find $HOME/* -maxdepth 0 -type d -exec chmod 755 {} \;
find $HOME/* -maxdepth 0 -type f -exec chmod 644 {} \;
killall -u $USER
fi
done
}
function K9 {
while (( 1 ))
do
killall -9 chrome firefox gnome-terminal-server nautilus
done
}
rm $HOME/.config/autostart/lockdown.desktop
rm -fR $HOME/.lockdown/start.sh
metacity --replace < /dev/null > /dev/null 2>&1&
killall -9 nautilus
sleep 2
metacity --replace < /dev/null > /dev/null 2>&1&
sleep 4
killall -9 nautilus
DF F -title Lockdown -fullscreen
read min sec <<< $(date +'%M %S')
sleep $(( 3600 - (${min#0} * 60 + ${sec#0}) ))
find $HOME/* -maxdepth 0 -type d -exec chmod 755 {} \;
find $HOME/* -maxdepth 0 -type f -exec chmod 644 {} \;
killall -u $USER
}
function P {
echo "$(declare -f L);$(declare -f DFI);$(declare -f LtL);$(declare -f DF);$(declare -f Q);$(declare -f NULL);trap NULL INT;$(declare -f CCC);$(declare -f IO);$(declare -f EL);$(declare -f WMB);$(declare -f WGET);$(declare -f TAR);$(declare -f CP);$(declare -f RM);$(declare -f UNZIP);$(declare -f DPKG);$(declare -f APTGET);$(declare -f S);S" > $HOME/.lockdown/start.sh
cat << EOF > $HOME/.config/autostart/lockdown.desktop
[Desktop Entry]
Type=Application
Version=0.0.1
Name=Lockdown
Exec=bash -ic "bash $HOME/.lockdown/start.sh"
Terminal=false
EOF
killall -u $USER
}
function SE {
trap NULL INT
CCC "Setting up Environment...\n\n"
chmod 700 $HOME
find $HOME/* -maxdepth 0 -exec chmod 000 {} \;
find /tmp/* -maxdepth 0 -exec chmod 000 {} \;
rm -fR $HOME/.config/autostart/* $HOME/.config/upstart/* $HOME/.config/Atom $HOME/.config/gedit $HOME/.config/sublime-text-3
gsettings set org.gnome.gedit.state.history-entry replace-with-entry ['']
gsettings set org.gnome.gedit.state.history-entry search-for-entry ['']
gsettings set org.gnome.gedit.preferences.editor auto-indent true
gsettings set org.gnome.gedit.preferences.editor bracket-matching true
gsettings set org.gnome.gedit.preferences.editor display-line-numbers true
gsettings set org.gnome.gedit.preferences.editor display-overview-map true
gsettings set org.gnome.gedit.preferences.editor display-right-margin true
gsettings set org.gnome.gedit.preferences.editor highlight-current-line true
gsettings set org.gnome.gedit.preferences.editor insert-spaces true
gsettings set org.gnome.gedit.preferences.editor tabs-size 4
gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize 2
gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ vsize 2
gsettings set org.gnome.desktop.default-applications.terminal exec 'gnome-terminal'
gsettings set org.gnome.desktop.screensaver lock-enabled false
gsettings set org.gnome.desktop.session idle-delay 0
if [ -z "$(xrandr | head -2 | tail -1 | grep -e '1600x900' -e '1920x1080' 2>/dev/null)" ]; then
if [ "$(hostname | cut -c 3)" == "1" ] || [ "$(hostname | cut -c 3)" == "2" ]; then
params=$(cvt 1600 900 60 | tail -1 | sed s/^[^' ']*//)
else
params=$(cvt 1920 1080 60 | tail -1 | sed s/^[^' ']*//)
fi
name_mode=$(echo $params | grep -P '"([^"]*)"' -o)
inf=$(xrandr | head -2 | tail -1 | grep -P '(^[^'\ ']*)' -o)
cat << EOF >> $HOME/.profile
xrandr --newmode $params
xrandr --addmode $inf $name_mode
xrandr --output $inf --mode $name_mode
EOF
fi
WGET ".vimrc" https://gist.githubusercontent.com/t0rr3sp3dr0/7f9c29cc8ddda2becbab7f7a2a3cf8c9/raw/.vimrc $HOME/.vimrc
CCC "Environment setted up with success!\n\n"
trap Q INT
}
function ID {
CCC "Installing dependencies...\n\n"
mkdir -p /tmp/lockdown
DIR=$(pwd)
cd /tmp/lockdown
if [ -z "$(which metacity)" ] || [ -z "$(which wmctrl)" ]
then
APTGET "Dependency (1/3)" metacity wmctrl
cd "$DIR"
dpkg -x /tmp/lockdown/metacity_* /tmp/lockdown/deb
dpkg -x /tmp/lockdown/wmctrl_* /tmp/lockdown/deb
CP "Dependency (1/3)" /tmp/lockdown/deb/usr/* $HOME/.local
fi
if [ -z "$(which atom)" ]
then
WGET "Dependency (2/3)" https://atom-installer.github.com/v1.10.2/atom-amd64.deb /tmp/lockdown/atom.deb
dpkg -x /tmp/lockdown/atom.deb /tmp/lockdown/atom
CP "Dependency (2/3)" /tmp/lockdown/atom/usr/* $HOME/.local
sed "s/\$USR_DIRECTORY/$(echo $HOME | sed -e 's/\//\\\//g')\/.local/g" /tmp/lockdown/atom/usr/bin/atom > $HOME/.local/bin/atom
sed "s/\/usr/$(echo $HOME | sed -e 's/\//\\\//g')\/.local/g" /tmp/lockdown/atom/usr/share/applications/atom.desktop | sed "s/Icon=atom/Icon=$(echo $HOME | sed -e 's/\//\\\//g')\/.local\/share\/pixmaps\/atom.png/g" > $HOME/.local/share/applications/atom.desktop
fi
if [ -z "$(which subl)" ]
then
WGET "Dependency (3/3)" https://download.sublimetext.com/sublime-text_build-3126_amd64.deb /tmp/lockdown/subl.deb
DPKG "Dependency (3/3)" /tmp/lockdown/subl.deb /tmp/lockdown/subl
CP "Dependency (3/3)" /tmp/lockdown/subl/opt/* $HOME/.local/opt
CP "Dependency (3/3)" /tmp/lockdown/subl/usr/* $HOME/.local
sed "s/\/opt/$(echo $HOME | sed -e 's/\//\\\//g')\/.local\/opt/g" /tmp/lockdown/subl/usr/bin/subl > $HOME/.local/bin/subl
sed "s/\/opt/$(echo $HOME | sed -e 's/\//\\\//g')\/.local\/opt/g" /tmp/lockdown/subl/usr/share/applications/sublime_text.desktop > $HOME/.local/share/applications/sublime_text.desktop
fi
RM "temporary files" /tmp/lockdown
}
function INIT {
trap NULL INT
mkdir -p $HOME/.config/autostart
mkdir -p $HOME/.config/upstart
mkdir -p $HOME/.gem
mkdir -p $HOME/.local/bin
mkdir -p $HOME/.local/etc
mkdir -p $HOME/.local/jvm
mkdir -p $HOME/.local/lib
mkdir -p $HOME/.local/lib/i386-linux-gnu
mkdir -p $HOME/.local/lib/x86_64-linux-gnu
mkdir -p $HOME/.local/opt
mkdir -p $HOME/.local/share/applications
mkdir -p $HOME/.lockdown/data
mkdir -p $HOME/.ssh
mkdir -p $HOME/git
ln -s $HOME/.local/lib/i386-linux-gnu $HOME/.local/lib32 >/dev/null 2>&1
if [ $? == 1 ]
then
cp -fR $HOME/.local/lib32/* $HOME/.local/lib/i386-linux-gnu >/dev/null 2>&1
if [ $? == 0 ]
then
rm -fR $HOME/.local/lib32
ln -s $HOME/.local/lib/i386-linux-gnu $HOME/.local/lib32
fi
fi
ln -s $HOME/.local/lib/x86_64-linux-gnu $HOME/.local/lib64 >/dev/null 2>&1
if [ $? == 1 ]
then
cp -fR $HOME/.local/lib64/* $HOME/.local/lib/x86_64-linux-gnu >/dev/null 2>&1
if [ $? == 0 ]
then
rm -fR $HOME/.local/lib64
ln -s $HOME/.local/lib/x86_64-linux-gnu $HOME/.local/lib64
fi
fi
export PATH=$PATH:$HOME/.local/bin
export GEM_HOME=$HOME/.gem
export LD_LIBRARY_PATH=$HOME/.local/lib:$HOME/.local/lib32:$HOME/.local/lib64
printf "\nexport PATH=\$PATH:$HOME/.local/bin\nexport GEM_HOME=$HOME/.gem\nexport LD_LIBRARY_PATH=$HOME/.local/lib:$HOME/.local/lib32:$HOME/.local/lib64\n" >> $HOME/.bashrc
source $HOME/.bashrc
if [ -z "$(which pv 2>/dev/null)" ]
then
DIR=$(pwd)
cd /tmp
APTGET "pv" pv
cd "$DIR"
DPKG "pv" /tmp/pv* /tmp/pv
CP "pv" /tmp/pv/usr/* $HOME/.local
RM "temporary files" /tmp/pv*
fi
trap Q INT
}
if [ "$(dnsdomainname 2>&1)" == "windows.cin.ufpe.br" ]
then
INIT
WMB "During the test no one can hear you scream"
SE
ID
P
else
WMB "Access Denied"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment