From ae8737ccdb793b73792cc4db586d0a2af9c3df4a Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Sun, 28 Nov 2021 02:09:04 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20add=20some=20features?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit run only with privileges identify the system we are running on --- src/definitions.h | 28 ---------------------- src/main.cpp | 47 +++++++++++++++++++++++++----------- src/utils.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++--- src/utils.h | 21 ---------------- src/utils.hpp | 7 ++++-- 5 files changed, 96 insertions(+), 68 deletions(-) delete mode 100644 src/definitions.h delete mode 100644 src/utils.h diff --git a/src/definitions.h b/src/definitions.h deleted file mode 100644 index 705bfdc..0000000 --- a/src/definitions.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef DEFINITIONS_H -#define DEFINITIONS_H - -#define RESET "\033[0m" -#define BLACK "\033[30m" /* Black */ -#define RED "\033[31m" /* Red */ -#define GREEN "\033[32m" /* Green */ -#define YELLOW "\033[33m" /* Yellow */ -#define BLUE "\033[34m" /* Blue */ -#define MAGENTA "\033[35m" /* Magenta */ -#define CYAN "\033[36m" /* Cyan */ -#define WHITE "\033[37m" /* White */ -#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ -#define BOLDRED "\033[1m\033[31m" /* Bold Red */ -#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ -#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ -#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ -#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ -#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ -#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ - -#define output(value) cout << value << endl -#define error(errorString) cout << RED << errorString << RESET << endl -#define warning(warningString) cout << YELLOW << warningString << RESET << endl -#define info(infoString) cout << CYAN << infoString << RESET << endl -#define success(successString) cout << GREEN << successString << RESET << endl - -#endif diff --git a/src/main.cpp b/src/main.cpp index 6de5c5e..1978114 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,32 +1,51 @@ #include "definitions.hpp" #include "utils.hpp" +#include #include static constexpr int32_t SLEEP_TIMEOUT = 15; +void show_iwctl() { + info("\nInstructions to connect to wifi using iwclt:\n"); + info("1 - Find your wifi device name ex: wlan0\n"); + info("2 - type `station wlan0 scan`, and wait couple seconds\n"); + info("3 - type `station wlan0 get-networks` (find your wifi Network name ex. my_wifi)\n"); + info("4 - type `station wlan0 connect my_wifi` (don't forget to press TAB for auto completion!\n"); + info("5 - type `station wlan0 show` (status should be connected)\n"); + info("6 - type `exit`\n"); + while (utils::prompt_char("Press a key to continue...", CYAN)) { + utils::exec("iwctl"); + break; + } +} + int main() { + const auto& tty = utils::exec("tty"); + const std::regex tty_regex("/dev/tty[0-9]*"); + if (std::regex_search(tty, tty_regex)) { + utils::exec("setterm -blank 0 -powersave off"); + } + +#ifdef NDEBUG + if (!utils::check_root()) { + std::this_thread::sleep_for(std::chrono::seconds(3)); + utils::clear_screen(); + return 1; + } +#endif + + utils::id_system(); if (!utils::is_connected()) { - warning("It seems you are not connected, waiting for 15s before retrying...\n"); + warning("It seems you are not connected, waiting for 15s before trying again...\n"); std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIMEOUT)); if (!utils::is_connected()) { char type = '\0'; - while (utils::prompt_char("An internet connection could not be detected, do you want to connect to a wifi network? [y/n]", type, RED)) { + while (utils::prompt_char("An internet connection could not be detected, do you want to connect to a wifi network? [y/n]", RED, &type)) { if (type != 'n') { - - info("\nInstructions to connect to wifi using iwclt:\n"); - info("1 - Find your wifi device name ex: wlan0\n"); - info("2 - type `station wlan0 scan`, and wait couple seconds\n"); - info("3 - type `station wlan0 get-networks` (find your wifi Network name ex. my_wifi)\n"); - info("4 - type `station wlan0 connect my_wifi` (don't forget to press TAB for auto completion!\n"); - info("5 - type `station wlan0 show` (status should be connected)\n"); - info("6 - type `exit`\n"); - while (utils::prompt_char("Press a key to continue...", type, CYAN)) { - utils::exec("iwctl"); - break; - } + show_iwctl(); } break; diff --git a/src/utils.cpp b/src/utils.cpp index cdce4d7..8de92fb 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,15 +1,33 @@ #include "utils.hpp" +#include + +#include #include #include #include +namespace fs = std::filesystem; + +static std::string_view H_INIT{"openrc"}; +static std::string_view SYSTEM{"BIOS"}; +static std::string_view NW_CMD{}; + namespace utils { bool is_connected() noexcept { return gethostbyname("google.com"); } +bool check_root() noexcept { + return (utils::exec("whoami") == "root\n"); +} + +void clear_screen() noexcept { + static constexpr auto CLEAR_SCREEN_ANSI = "\033[1;1H\033[2J"; + write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 11); +} + std::string exec(const std::string_view& command) noexcept { char buffer[128]; std::string result; @@ -31,20 +49,57 @@ std::string exec(const std::string_view& command) noexcept { return result; } -bool prompt_char(const char* prompt, char& read, const char* color) noexcept { +bool prompt_char(const char* prompt, const char* color, char* read) noexcept { fmt::print("{}{}{}\n", color, prompt, RESET); std::string tmp; if (std::getline(std::cin, tmp)) { + char read_char{}; if (tmp.length() == 1) { - read = tmp[0]; + read_char = tmp[0]; } else { - read = '\0'; + read_char = '\0'; } + if (read != nullptr) + *read = read_char; + return true; } return false; } + +void id_system() noexcept { + // Apple System Detection + const auto& sys_vendor = utils::exec("cat /sys/class/dmi/id/sys_vendor"); + if ((sys_vendor == "Apple Inc.\n") || (sys_vendor == "Apple Computer, Inc.\n")) + utils::exec("modprobe -r -q efivars || true"); // if MAC + else + utils::exec("modprobe -q efivarfs"); // all others + + // BIOS or UEFI Detection + static constexpr auto efi_path = "/sys/firmware/efi/"; + if (fs::exists(efi_path) && fs::is_directory(efi_path)) { + // Mount efivarfs if it is not already mounted + const auto& mount_out = utils::exec("mount | grep /sys/firmware/efi/efivars"); + if (mount_out == "\n") { + if (mount("efivarfs", "/sys/firmware/efi/efivars", "efivarfs", 0, "") != 0) { + perror("utils::id_system"); + exit(1); + } + } + SYSTEM = "UEFI"; + } + + // init system + const auto& init_sys = utils::exec("cat /proc/1/comm"); + if (init_sys == "systemd\n") + H_INIT = "systemd"; + + // TODO: Test which nw-client is available, including if the service according to $H_INIT is running + if (H_INIT == "systemd" && utils::exec("systemctl is-active NetworkManager") == "active\n") + NW_CMD = "nmtui"; +} + } // namespace utils diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index 59df325..0000000 --- a/src/utils.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef UTILS_H -#define UTILS_H - -#include "definitions.h" -#include - -using namespace std; - - -class Utils -{ -public: - static void printBanner(); - static bool isConnected(); - static std::string exec(const string &command); - static bool promptForChar(const char *prompt, char &read, const char *colour = RESET); - -private: -}; - -#endif//UTILS_H diff --git a/src/utils.hpp b/src/utils.hpp index 7437801..3d5f15e 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -8,9 +8,12 @@ namespace utils { void print_banner() noexcept; bool is_connected() noexcept; -bool prompt_char(const char* prompt, char& read, const char* color = RESET) noexcept; +bool prompt_char(const char* prompt, const char* color = RESET, char* read = nullptr) noexcept; +void clear_screen() noexcept; -std::string exec(const std::string_view& command) noexcept; +auto exec(const std::string_view& command) noexcept -> std::string; +[[nodiscard]] bool check_root() noexcept; +void id_system() noexcept; } // namespace utils #endif // UTILS_HPP