#!/usr/bin/env bash
#Usted es libre de editar y/o distribuir este programa bajo los terminos de la licencia GPL v3 o posterior.

printf "\nEste script está diseñado para que te conectes a internet sin necesidad de un gestor gráfico de red
          haciendo uso de software como wpa_supplicant, dhcpcd/dhclient e iptools desde la línea de comandos.
                                Programador: Eduard Eliecer Tolosa Toloza 
                                    XMPP/Email: tolosaeduard@disroot.org
               Contacto y sala de chat: https://riot.im/app/#/room/#securityhacklabs:matrix.org
			   				Telegram: https://t.me/sechacklabs
                                  Security Hack Labs Team. @SecHackLabs
                              Blog: https://securityhacklabs.net\n
"


if [ "$UID" -ne 0 ]; then
	echo -e "\n Usted necesita privilegios root o sudo."
	exit
fi

echo -e "Elige el tipo de red que deseas usar:\n
a) Sin autenticación \nb) Con contraseña\nc) Red cableada (Ethernet)"
read -p "::" iskey

if [ -f /usr/bin/wpa_supplicant ] || [ -f /sbin/wpa_supplicant ] ; then
    echo -e "\nwpa_supplicant está instalado."
else
    if [ -f /etc/pacman.conf ] ; then
        pacman -S wpa_supplicant
    elif [ -f /etc/apt/sources.list ] ; then
        apt install wpasupplicant
		dpkg -i packages/*.deb
    fi
fi
if [ -f /usr/bin/dhcpcd ] || [ -f /sbin/dhcpcd ] ; then
    echo -e "dhcpcd está instalado.\n"
else
    if [ -f /etc/pacman.conf ] ; then
        pacman -S dhcpcd
    elif [ -f /etc/apt/sources.list ] ; then
        apt install dhcpcd5
    fi
fi
function connect(){
    iface=$(ls /sys/class/net/ | grep w)
	rfkill unblock all
    dhcpcd -k $iface
    killall dhcpcd
    killall dhclient
    killall wpa_supplicant
    ip link set dev $iface up
    wpa_supplicant -B -i $iface -c /etc/wpa.conf
    dhcpcd -4 --noarp $iface
}

if [ $iskey == "a" ] ; then
	if [ -f /etc/wpa.conf ] ; then
    	connect
    else
	    echo -e "\nNecesitas configurar tu archivo de conexión, por favor ingrese los datos cuando sean solicitados.\n"
    	read -p "Introduce tu nombre de red: " nombre
	    echo -e "network={\n\tssid=\"$nombre\"\n\tkey_mgmt=NONE\n\tpriority=100\n}" > /etc/wpa.conf
		connect
    	if [ $? -eq 0 ] ; then
			echo -e "\nConexión establecida."
		else
			echo -e "\nHa ocurrido un error."
		fi
	fi
fi

if [ $iskey == "b" ] ; then
	if [ -f /etc/wpa.conf ] ; then
		connect
	else
	    echo -e "\nNecesitas configurar tu archivo de conexión, por favor ingrese los datos cuando sean solicitados.\n"
		read -p "Introduce tu nombre de red: " nombre
	    read -p  "Introduce tu contraseña: " password
    	wpa_passphrase "$nombre" "$password" > /etc/wpa.conf
	    connect
		if [ $? -eq 0 ] ; then
			echo -e "\nConexión establecida."
		else
			echo -e "\nHa ocurrido un error."
		fi
	fi
fi

if [ $iskey == "c" ] ; then
	if [ -f ~/.conectarc ] ; then
		echo -e "\nUsted ya tiene habilitado DHCPCD, si no tiene internet, revisa tu moden o cable\n"
		exit
	elif [ -f ~/.nosystemd ] ; then
        iface=$(ls /sys/class/net/ | grep e)
        echo -e "\nEstableciendo una conexión.\n"
        rfkill unblock all
        dhcpcd -k $iface
        killall dhcpcd
        killall dhclient
        killall wpa_supplicant
        ip link set dev $iface up
        dhcpcd -4 --noarp $iface
		if [ $? -eq 0 ] ; then
			echo -e "\nConexión establecida."
			exit
		else
			echo -e "\nHa ocurrido un error.\n"
		fi
	else
		iface=$(ls /sys/class/net/ | grep e)
		echo -e "\nEstableciendo una conexión.\n"
		rfkill unblock all
    	dhcpcd -k $iface
	    killall dhcpcd
    	killall dhclient
	    killall wpa_supplicant
    	ip link set dev $iface up
		dhcpcd -4 --noarp $iface
		if [ "$?" -eq 0 ] ; then
			read -p "¿Está usted usando SystemD? (s/n)" systemd
			if [ "$systemd" == "s" ] ; then
				echo -e "\nHabilitando DHCPCD mediante systemctl.\n"
				systemctl enable dhcpcd
				if [ "$?" -eq 0 ] ; then
					echo "Archivo para comprobar que DHCPCD está habilitando en el inicio del systema, creado por el script 'conectar'" > ~/.conectarc
					echo -e "\nConexión establecida."
					exit
				fi
			else
				echo -e "\nUsted no usa SystemD, puede habilitar DHCPCD usando tu gestor init."
				echo "Archivo para comprobar que no usas Systemd." > ~/.nosystemd
				echo -e "\nConexión establecida."
			fi
		else
			echo -e "\nHa ocurrido un error.\n"
		fi
	fi
fi


