-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eddy Daoud
authored
Jan 17, 2022
1 parent
5a30f2c
commit 567d0a3
Showing
1 changed file
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
#!/data/data/com.termux/files/usr/bin/bash | ||
|
||
export bin="$PREFIX/bin" | ||
export PATH=export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/usr/local/sbin:$bin:$PATH | ||
export ROOTFS="$HOME/.rootfs" | ||
export ROOTFS_URL="https://media.githubusercontent.com/media/daoudeddy/Astroid/main/rootfs/minimal-rootfs.tar.xz" | ||
|
||
unset LD_PRELOAD | ||
|
||
# This script need to be run as root | ||
if [[ "$EUID" -ne 0 ]]; then | ||
echo "[!] Please run as root" | ||
exit | ||
fi | ||
|
||
# Checking for busybox | ||
if [[ ! -x "$PREFIX/bin/busybox" ]]; then | ||
echo "[!] Busybox not found" | ||
exit | ||
fi | ||
|
||
# Checking root | ||
if [[ ! -x $PREFIX/bin/sudo ]]; then | ||
echo "[!] sudo not found!" | ||
exit | ||
fi | ||
|
||
# Usage help | ||
function script_usage() { | ||
cat <<EOF | ||
Usage: | ||
-h --help Displays this help | ||
-i --install Download and install the container | ||
-s --start Start the container | ||
-d --daemon Start the container in background | ||
-k --kill Kill and exit the container | ||
-d --delete Delete the container | ||
EOF | ||
} | ||
|
||
# Setup the chroot | ||
setup() { | ||
# Creating virtual Kernel file systems. | ||
mkdir -p "$ROOTFS/dev" | ||
mkdir -p "$ROOTFS/dev/pts" | ||
mkdir -p "$ROOTFS/dev/shm" | ||
mkdir -p "$ROOTFS/system" | ||
mkdir -p "$ROOTFS/tmp" | ||
} | ||
|
||
# Check if rootfs already mounted | ||
is_mounted() { | ||
if mount | grep "$ROOTFS$1" >/dev/null; then | ||
echo "[~] $1 already mounted" | ||
return 0 | ||
else | ||
return 1 | ||
fi | ||
} | ||
|
||
# Start the required services | ||
start_services() { | ||
printf "[+] Staring services\n" | ||
chroot $ROOTFS servicectl start sshd indiwebmanager virtualgps 2>/dev/null | ||
} | ||
|
||
# Stop the running services | ||
stop_services() { | ||
printf "[-] Stopping services\n" | ||
chroot $ROOTFS servicectl stop sshd indiwebmanager virtualgps 2>/dev/null | ||
|
||
local pids=$(lsof -t $ROOTFS 2>/dev/null | uniq) | ||
if [ -n "${pids}" ]; then | ||
kill -9 ${pids} 2>/dev/null | ||
fi | ||
} | ||
|
||
# mount the chroot env | ||
mount_env() { | ||
# Calling setup before chrooting | ||
setup | ||
|
||
mount -o remount,suid /data | ||
|
||
if ! is_mounted "/dev"; then | ||
mount -o bind /dev "$ROOTFS/dev" && echo "[+] mounting /dev" | ||
fi | ||
if ! is_mounted "/dev/pts"; then | ||
mount -t devpts devpts "$ROOTFS/dev/pts" && printf "[+] mounting /dev/pts\n" | ||
fi | ||
if ! is_mounted "/dev/shm"; then | ||
mount -t tmpfs tmpfs "$ROOTFS/dev/shm" && printf "[+] mounting /dev/shm\n" | ||
fi | ||
if ! is_mounted "/proc"; then | ||
mount -t proc proc "$ROOTFS/proc" && printf "[+] mounting /proc\n" | ||
fi | ||
if ! is_mounted "/sys"; then | ||
mount -t sysfs sysfs "$ROOTFS/sys" && printf "[+] mounting /sys\n" | ||
fi | ||
if ! is_mounted "/tmp"; then | ||
mount -t tmpfs tmpfs "$ROOTFS/tmp" && printf "[+] mounting /tmp\n" | ||
fi | ||
} | ||
|
||
# Start the chroot env in foreground | ||
start() { | ||
mount_env | ||
start_services | ||
|
||
printf "[~] Chrooting into Astroid \n" | ||
chroot $ROOTFS /usr/bin/env su -l - astroid | ||
} | ||
|
||
# Start the chroot env in background | ||
background() { | ||
mount_env | ||
start_services | ||
printf "[~] Astroid started in background \n" | ||
} | ||
|
||
# Stop the chroot env | ||
stop() { | ||
stop_services | ||
|
||
# Unmount virtual Kernel file systems on exit. | ||
umount "$ROOTFS/dev/pts" 2>/dev/null && printf "[-] unmounting /dev/pts\n" | ||
umount "$ROOTFS/dev/shm" 2>/dev/null && printf "[-] unmounting /dev/shm\n" | ||
umount "$ROOTFS/dev" 2>/dev/null && printf "[-] unmounting /dev\n" | ||
umount "$ROOTFS/proc" 2>/dev/null && printf "[-] unmounting /proc\n" | ||
umount "$ROOTFS/sys" 2>/dev/null && printf "[-] unmounting /sys\n" | ||
umount "$ROOTFS/tmp" 2>/dev/null && printf "[-] unmounting /tmp\n" | ||
|
||
printf "[~] Exiting chroot environment...\n" | ||
} | ||
|
||
# Download and install the rootfs | ||
install() { | ||
if [[ -d "$ROOTFS" ]]; then | ||
printf "[!] Astroid rootfs is already installed\n" | ||
else | ||
local tarball="$HOME/rootfs.tar.xz" | ||
printf "[!] Downloading Astroid rootfs\n" | ||
|
||
wget -c -q --show-progress --tries=5 -N "$ROOTFS_URL" -O $tarball | ||
|
||
printf "[!] Extracting Astroid rootfs\n" | ||
|
||
if [[ $? -eq 0 ]]; then | ||
# printf "[!] Cleaning up\n" | ||
# rm $tarball | ||
mkdir $ROOTFS | ||
tar xf $tarball -C $ROOTFS | ||
printf "[!] Run astroid --help for usage info\n" | ||
else | ||
printf "[!] Failed to download\n" | ||
fi | ||
fi | ||
} | ||
|
||
# Delete rootfs | ||
delete() { | ||
printf "[?] Are you sure [Y/n]\n" | ||
read -r response | ||
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then | ||
stop | ||
printf "[!] Deleting\n" | ||
rm -rf $ROOTFS | ||
else | ||
printf "[!] Canceling\n" | ||
fi | ||
} | ||
|
||
# Parameter parser | ||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
-h | --help) | ||
script_usage | ||
exit 0 | ||
;; | ||
-s | --start) | ||
start | ||
exit 0 | ||
;; | ||
-b | --background) | ||
background | ||
exit 0 | ||
;; | ||
-k | --kill) | ||
stop | ||
exit 0 | ||
;; | ||
-i | --install) | ||
install | ||
exit 0 | ||
;; | ||
-d | --delete) | ||
delete | ||
exit 0 | ||
;; | ||
*) | ||
printf "[!] Invalid option was provided: $1" | ||
exit 0 | ||
;; | ||
esac | ||
done | ||
|
||
script_usage |