From 3c5574d08f32042728c59c9a9f277142bdac42ed Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Fri, 27 Jan 2017 12:26:24 +0100 Subject: [PATCH 01/37] Base --- .idea/.name | 1 + .idea/WPS-pin-generator.iml | 2 ++ .idea/codeStyleSettings.xml | 35 +++++++++++++++++++++++++++++++++++ .idea/dictionaries/pily.xml | 3 +++ .idea/misc.xml | 4 ++++ .idea/modules.xml | 8 ++++++++ CMakeLists.txt | 7 +++++++ Generator.cpp | 14 ++++++++++++++ Generator.h | 21 +++++++++++++++++++++ GeneratorInterface.h | 21 +++++++++++++++++++++ Pin.cpp | 23 +++++++++++++++++++++++ Pin.h | 25 +++++++++++++++++++++++++ main.cpp | 16 ++++++++++++++++ 13 files changed, 180 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/WPS-pin-generator.iml create mode 100644 .idea/codeStyleSettings.xml create mode 100644 .idea/dictionaries/pily.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 CMakeLists.txt create mode 100644 Generator.cpp create mode 100644 Generator.h create mode 100644 GeneratorInterface.h create mode 100644 Pin.cpp create mode 100644 Pin.h create mode 100644 main.cpp diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..43ce7b7 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +WPS_pin_generator \ No newline at end of file diff --git a/.idea/WPS-pin-generator.iml b/.idea/WPS-pin-generator.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/WPS-pin-generator.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 0000000..5e6dd34 --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,35 @@ + + + + + + \ No newline at end of file diff --git a/.idea/dictionaries/pily.xml b/.idea/dictionaries/pily.xml new file mode 100644 index 0000000..0b54ec9 --- /dev/null +++ b/.idea/dictionaries/pily.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..227993b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8b191b0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.7) +project(WPS_pin_generator) + +set(CMAKE_CXX_STANDARD 11) + +set(SOURCE_FILES main.cpp GeneratorInterface.h Generator.h Generator.cpp Pin.cpp Pin.h) +add_executable(WPS_pin_generator ${SOURCE_FILES} GeneratorInterface.h Generator.h Generator.cpp) \ No newline at end of file diff --git a/Generator.cpp b/Generator.cpp new file mode 100644 index 0000000..87f559d --- /dev/null +++ b/Generator.cpp @@ -0,0 +1,14 @@ +// +// Created by pily on 27/01/17. +// + +#include +#include "Generator.h" + +Pin Generator::generatePin(const std::string &s) const { + return generatePinImp(s); +} + +Pin Generator::generatePinImp(const std::string &s) const { + throw std::logic_error("NOT IMPLEMENTED"); +} diff --git a/Generator.h b/Generator.h new file mode 100644 index 0000000..15fa160 --- /dev/null +++ b/Generator.h @@ -0,0 +1,21 @@ +// +// Created by pily on 27/01/17. +// + +#ifndef WPS_PIN_GENERATOR_GENERATOR_H +#define WPS_PIN_GENERATOR_GENERATOR_H + +#include "GeneratorInterface.h" + +#include + +class Generator : public GeneratorInterface { +public: + Pin generatePin(const std::string &s) const override; + +private: + Pin generatePinImp(const std::string &s) const; +}; + + +#endif //WPS_PIN_GENERATOR_GENERATOR_H diff --git a/GeneratorInterface.h b/GeneratorInterface.h new file mode 100644 index 0000000..3d00943 --- /dev/null +++ b/GeneratorInterface.h @@ -0,0 +1,21 @@ +// +// Created by pily on 27/01/17. +// + +/*** GeneratorInterface + * GeneratorInterface is an base class interface used to implement + * the design pattern strategy + */ + +#ifndef WPS_PIN_GENERATOR_GENERATORINTERFACE_H +#define WPS_PIN_GENERATOR_GENERATORINTERFACE_H + +#include "Pin.h" +#include + +class GeneratorInterface { +public: + virtual Pin generatePin(const std::string &s) const =0; +}; + +#endif //WPS_PIN_GENERATOR_GENERATORINTERFACE_H diff --git a/Pin.cpp b/Pin.cpp new file mode 100644 index 0000000..029689f --- /dev/null +++ b/Pin.cpp @@ -0,0 +1,23 @@ +// +// Created by pily on 27/01/17. +// + +#include +#include "Pin.h" + +/*** + * Checks if the pin passed is a valid pin + * @param pin: object of class Pin + * @return true if pin is valid, false otherwise + */ +bool Pin::valid() const { + throw std::logic_error("NOT IMPLEMENTED"); +} + +Pin::Pin(const std::string &pin) { + this->pin = pin; +} + +std::string Pin::toString() const { + return pin; +} diff --git a/Pin.h b/Pin.h new file mode 100644 index 0000000..6c67702 --- /dev/null +++ b/Pin.h @@ -0,0 +1,25 @@ +// +// Created by pily on 27/01/17. +// + +#ifndef WPS_PIN_GENERATOR_PIN_H +#define WPS_PIN_GENERATOR_PIN_H + +#include + +class Pin { + +private: + std::string pin; // content of the pin (string with 8 digits) + +public: + Pin(const std::string &pin); + + bool valid() const; + + std::string toString() const; + +}; + + +#endif //WPS_PIN_GENERATOR_PIN_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..20eb1e7 --- /dev/null +++ b/main.cpp @@ -0,0 +1,16 @@ +#include "Generator.h" +#include "Pin.h" + +#include + +int main() { + std::cout << "WPS Pin Generator - by Bertof" << std::endl; + + GeneratorInterface *g = new Generator(); + + Pin p = g->generatePin("TEST"); + + std::cout<<"PIN:\t"< Date: Fri, 27 Jan 2017 14:29:29 +0100 Subject: [PATCH 02/37] Movements --- CMakeLists.txt | 4 +- Generator.cpp | 14 ----- Generator/Generator.cpp | 32 +++++++++++ Generator.h => Generator/Generator.h | 9 +++- .../GeneratorInterface.h | 10 +++- Pin.h | 25 --------- Pin.cpp => Pin/Pin.cpp | 23 +++++++- Pin/Pin.h | 54 +++++++++++++++++++ main.cpp | 9 ++-- 9 files changed, 130 insertions(+), 50 deletions(-) delete mode 100644 Generator.cpp create mode 100644 Generator/Generator.cpp rename Generator.h => Generator/Generator.h (56%) rename GeneratorInterface.h => Generator/GeneratorInterface.h (59%) delete mode 100644 Pin.h rename Pin.cpp => Pin/Pin.cpp (54%) create mode 100644 Pin/Pin.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b191b0..7c31ebc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(WPS_pin_generator) set(CMAKE_CXX_STANDARD 11) -set(SOURCE_FILES main.cpp GeneratorInterface.h Generator.h Generator.cpp Pin.cpp Pin.h) -add_executable(WPS_pin_generator ${SOURCE_FILES} GeneratorInterface.h Generator.h Generator.cpp) \ No newline at end of file +set(SOURCE_FILES main.cpp Generator/GeneratorInterface.h Generator/Generator.h Generator/Generator.cpp Pin/Pin.cpp Pin/Pin.h) +add_executable(WPS_pin_generator ${SOURCE_FILES} Generator/GeneratorInterface.h Generator/Generator.h Generator/Generator.cpp) \ No newline at end of file diff --git a/Generator.cpp b/Generator.cpp deleted file mode 100644 index 87f559d..0000000 --- a/Generator.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// -// Created by pily on 27/01/17. -// - -#include -#include "Generator.h" - -Pin Generator::generatePin(const std::string &s) const { - return generatePinImp(s); -} - -Pin Generator::generatePinImp(const std::string &s) const { - throw std::logic_error("NOT IMPLEMENTED"); -} diff --git a/Generator/Generator.cpp b/Generator/Generator.cpp new file mode 100644 index 0000000..daa7df7 --- /dev/null +++ b/Generator/Generator.cpp @@ -0,0 +1,32 @@ +// +// Created by pily on 27/01/17. +// + +#include +#include +#include "Generator.h" + +/** + * Implementation of generatePin + * Returns result of generatePinImp based on the same data passed + * @param s string of data + * @return Pin valid pin generated + */ +std::vector Generator::generatePin(const std::string &s) const { return generatePinImp(s); } + +/** + * Generates a valid Pin object using an algorithm based on zhaochunsheng's ComputePIN-C83A35 and linkp2p's WPS-PIN + * @param s: data passed + * @return valid Pin object + */ +std::vector Generator::generatePinImp(const std::string &s) const { + + std::vector result = std::vector(); + + long calc; + + std::stringstream ss; + ss << std::hex << s; + ss >> calc; + +} diff --git a/Generator.h b/Generator/Generator.h similarity index 56% rename from Generator.h rename to Generator/Generator.h index 15fa160..6bc90a7 100644 --- a/Generator.h +++ b/Generator/Generator.h @@ -8,13 +8,18 @@ #include "GeneratorInterface.h" #include +#include +/** + * Base class Generator + * Used to generate valid Pins + */ class Generator : public GeneratorInterface { public: - Pin generatePin(const std::string &s) const override; + std::vector generatePin(const std::string &s) const override; private: - Pin generatePinImp(const std::string &s) const; + std::vector generatePinImp(const std::string &s) const; }; diff --git a/GeneratorInterface.h b/Generator/GeneratorInterface.h similarity index 59% rename from GeneratorInterface.h rename to Generator/GeneratorInterface.h index 3d00943..c20f94b 100644 --- a/GeneratorInterface.h +++ b/Generator/GeneratorInterface.h @@ -10,12 +10,18 @@ #ifndef WPS_PIN_GENERATOR_GENERATORINTERFACE_H #define WPS_PIN_GENERATOR_GENERATORINTERFACE_H -#include "Pin.h" +#include "../Pin/Pin.h" #include +#include class GeneratorInterface { public: - virtual Pin generatePin(const std::string &s) const =0; + /** + * Generates a valid pin based on the data passed + * @param s: string of data + * @return std::vector: valid Pin object vector + */ + virtual std::vector generatePin(const std::string &s) const =0; }; #endif //WPS_PIN_GENERATOR_GENERATORINTERFACE_H diff --git a/Pin.h b/Pin.h deleted file mode 100644 index 6c67702..0000000 --- a/Pin.h +++ /dev/null @@ -1,25 +0,0 @@ -// -// Created by pily on 27/01/17. -// - -#ifndef WPS_PIN_GENERATOR_PIN_H -#define WPS_PIN_GENERATOR_PIN_H - -#include - -class Pin { - -private: - std::string pin; // content of the pin (string with 8 digits) - -public: - Pin(const std::string &pin); - - bool valid() const; - - std::string toString() const; - -}; - - -#endif //WPS_PIN_GENERATOR_PIN_H diff --git a/Pin.cpp b/Pin/Pin.cpp similarity index 54% rename from Pin.cpp rename to Pin/Pin.cpp index 029689f..4da760f 100644 --- a/Pin.cpp +++ b/Pin/Pin.cpp @@ -11,13 +11,34 @@ * @return true if pin is valid, false otherwise */ bool Pin::valid() const { - throw std::logic_error("NOT IMPLEMENTED"); + bool valid = true; + + //TODO tests + + switch (valid) { + case true: + return valid; + break; + case false: + throw std::logic_error("Pin generated is not valid"); + break; + } + } Pin::Pin(const std::string &pin) { this->pin = pin; + this->valid(); } std::string Pin::toString() const { + std::to_string(pin); +} + +long Pin::toInteger() const { return pin; } + +Pin::Pin(const long &pin) { + return ; +} diff --git a/Pin/Pin.h b/Pin/Pin.h new file mode 100644 index 0000000..655f20f --- /dev/null +++ b/Pin/Pin.h @@ -0,0 +1,54 @@ +// +// Created by pily on 27/01/17. +// + +#ifndef WPS_PIN_GENERATOR_PIN_H +#define WPS_PIN_GENERATOR_PIN_H + +#include + +/** + * Class Pin + * This type identifies a WPS pin solution + * @var pin: contains all the info about the pin + */ +class Pin { + +private: + long pin; // content of the pin + +public: + /** + * Constructor using string + * @param pin + */ + Pin(const std::string &pin); + + /** + * Constructor using long integer + * @param pin + */ + Pin(const long &pin); + + /** + * Checks if the pin is a valid one + * @return true if valid, false otherwise + */ + bool valid() const; + + /** + * Returns pin using a string + * @return string of pin + */ + std::string toString() const; + + /** + * Returns pin using a long integer + * @return pin (integer) + * @attention This integer does not take in account initial 0 values + */ + long toInteger() const; +}; + + +#endif //WPS_PIN_GENERATOR_PIN_H diff --git a/main.cpp b/main.cpp index 20eb1e7..67fd37e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ -#include "Generator.h" -#include "Pin.h" +#include "Pin/Pin.h" +#include "Generator/Generator.h" +#include #include int main() { @@ -8,9 +9,9 @@ int main() { GeneratorInterface *g = new Generator(); - Pin p = g->generatePin("TEST"); + std::vector p = g->generatePin("C8:3A:35:2D:9C:C0"); - std::cout<<"PIN:\t"< Date: Sat, 11 Feb 2017 09:23:06 +0100 Subject: [PATCH 03/37] Updated readme.md --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f6f7713..7a9bd7d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ -Pixie - WPS Pin Generator +WPS Pin Generator ======== +## What is this? +It is a Wireless Protected Setup (WPS) pin generator, which uses a an algorithm commonly found on commercial routers to generate their default pin. +The pin found can be used with programs like Reaver to attack vulnerable WiFi routers. + +## Why? +Since i moved from Ubuntu to ArchLinux the old good WPSPIN.sh from linkp2p's WPS-PIN no longer works (it cannot find the network interfaces), so I'm creating this simple program in C++ to generate the same results but in a less integrated and more platform independent way. + +## Legal +This program is shared as it is and in an open license so everyone can use it. + +I am not the original creator of the algorithm. +The original discoverer of the algorithm is zhaochunsheng, who has written a C implementation in his script computepinC83A35. +All names and history of that file can be found in linkp2p's repository. + +The use of this program is intended to be in a authorized environment. Don't use it against networks you do not own or you do not have legal permission to attack. +Every illicit use of this program has nothing to do with the author. \ No newline at end of file From 8fa4e542af4e384720bb4d74b74a52cb385413be Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 09:33:56 +0100 Subject: [PATCH 04/37] Created NotImplementedException class --- Exceptions/NotImplementedException.cpp | 12 ++++++++++++ Exceptions/NotImplementedException.h | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 Exceptions/NotImplementedException.cpp create mode 100644 Exceptions/NotImplementedException.h diff --git a/Exceptions/NotImplementedException.cpp b/Exceptions/NotImplementedException.cpp new file mode 100644 index 0000000..aa17657 --- /dev/null +++ b/Exceptions/NotImplementedException.cpp @@ -0,0 +1,12 @@ +// +// Created by pily on 11/02/17. +// + +#include "NotImplementedException.h" + +NotImplementedException::NotImplementedException(const char *error) : errorMessage(error) { +} + +const char *NotImplementedException::what() { + return errorMessage.c_str(); +} diff --git a/Exceptions/NotImplementedException.h b/Exceptions/NotImplementedException.h new file mode 100644 index 0000000..d15424a --- /dev/null +++ b/Exceptions/NotImplementedException.h @@ -0,0 +1,22 @@ +// +// Created by pily on 11/02/17. +// + +#ifndef WPS_PIN_GENERATOR_NOTIMPLEMENTEDEXCEPTION_H +#define WPS_PIN_GENERATOR_NOTIMPLEMENTEDEXCEPTION_H + +#include +#include + +class NotImplementedException : public std::exception { + +private: + std::string errorMessage; + +public: + NotImplementedException(const char *error = "Functionality not implemented"); + const char * what(); +}; + + +#endif //WPS_PIN_GENERATOR_NOTIMPLEMENTEDEXCEPTION_H From a34b75bed68ca781f59912df8e8a3d46d86d49a5 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 10:10:31 +0100 Subject: [PATCH 05/37] Working on the debug class --- Debugger.cpp | 7 +++++++ Debugger.h | 32 ++++++++++++++++++++++++++++++++ main.cpp | 4 ++++ 3 files changed, 43 insertions(+) create mode 100644 Debugger.cpp create mode 100644 Debugger.h diff --git a/Debugger.cpp b/Debugger.cpp new file mode 100644 index 0000000..ae3b625 --- /dev/null +++ b/Debugger.cpp @@ -0,0 +1,7 @@ +// +// Created by pily on 11/02/17. +// + +#include "Debugger.h" + + diff --git a/Debugger.h b/Debugger.h new file mode 100644 index 0000000..1f4a41d --- /dev/null +++ b/Debugger.h @@ -0,0 +1,32 @@ +// +// Created by pily on 11/02/17. +// + +#ifndef WPS_PIN_GENERATOR_DEBUGGER_H +#define WPS_PIN_GENERATOR_DEBUGGER_H + +#include +#include +#include "Exceptions/NotImplementedException.h" + +#include "lib/rang/include/rang.hpp" + +class Debugger { + +public: + static void log(const std::string &string) { + throw NotImplementedException(); + } + + static void logError(const std::string &string) { + throw NotImplementedException(); + } + + static void testColors() { + std::cout << rang::bg::blue << rang::fg::red << "Test" << rang::style::reset << std::endl; + + } +}; + + +#endif //WPS_PIN_GENERATOR_DEBUGGER_H diff --git a/main.cpp b/main.cpp index 67fd37e..ab6a1dd 100644 --- a/main.cpp +++ b/main.cpp @@ -4,9 +4,13 @@ #include #include +#include "Debugger.h" + int main() { std::cout << "WPS Pin Generator - by Bertof" << std::endl; + Debugger::testColors(); + GeneratorInterface *g = new Generator(); std::vector p = g->generatePin("C8:3A:35:2D:9C:C0"); From 03f0d97c56ba779e0f5718dfc3398f08301df16f Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 10:58:32 +0100 Subject: [PATCH 06/37] Better debugger Added color support, needs "rang" --- .gitmodules | 3 +++ Debugger.h => Debugger/Debugger.h | 14 +++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) create mode 100644 .gitmodules rename Debugger.h => Debugger/Debugger.h (51%) diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..eb7e8c8 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/rang"] + path = lib/rang + url = https://github.com/agauniyal/rang.git diff --git a/Debugger.h b/Debugger/Debugger.h similarity index 51% rename from Debugger.h rename to Debugger/Debugger.h index 1f4a41d..77ab76f 100644 --- a/Debugger.h +++ b/Debugger/Debugger.h @@ -7,24 +7,20 @@ #include #include -#include "Exceptions/NotImplementedException.h" +#include "../Exceptions/NotImplementedException.h" -#include "lib/rang/include/rang.hpp" +#include "../lib/rang/include/rang.hpp" class Debugger { public: static void log(const std::string &string) { - throw NotImplementedException(); + std::cout << rang::style::reset << string << std::endl; } static void logError(const std::string &string) { - throw NotImplementedException(); - } - - static void testColors() { - std::cout << rang::bg::blue << rang::fg::red << "Test" << rang::style::reset << std::endl; - + std::cout << rang::fg::red << rang::style::bold << "ERROR: " << rang::style::reset << rang::fg::red << string + << rang::style::reset << std::endl; } }; From 890cd814d199f856a26a493f077caa14294141ff Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 11:09:55 +0100 Subject: [PATCH 07/37] Better generator management and pin management --- CMakeLists.txt | 4 ++-- Debugger.cpp | 7 ------ Exceptions.h | 11 +++++++++ Generator/Generator.cpp | 32 ------------------------- Generator/Generator.h | 21 ++++++++++++++-- Generator/GeneratorInterface.h | 3 ++- Pin/Pin.cpp | 44 ---------------------------------- Pin/Pin.h | 17 ++++++++----- lib/rang | 1 + main.cpp | 16 ++++--------- 10 files changed, 50 insertions(+), 106 deletions(-) delete mode 100644 Debugger.cpp create mode 100644 Exceptions.h delete mode 100644 Generator/Generator.cpp delete mode 100644 Pin/Pin.cpp create mode 160000 lib/rang diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c31ebc..7448f49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(WPS_pin_generator) set(CMAKE_CXX_STANDARD 11) -set(SOURCE_FILES main.cpp Generator/GeneratorInterface.h Generator/Generator.h Generator/Generator.cpp Pin/Pin.cpp Pin/Pin.h) -add_executable(WPS_pin_generator ${SOURCE_FILES} Generator/GeneratorInterface.h Generator/Generator.h Generator/Generator.cpp) \ No newline at end of file +set(SOURCE_FILES main.cpp Generator/GeneratorInterface.h Pin/Pin.h Exceptions.h Generator/Generator.h) +add_executable(WPS_pin_generator ${SOURCE_FILES} Generator/GeneratorInterface.h) \ No newline at end of file diff --git a/Debugger.cpp b/Debugger.cpp deleted file mode 100644 index ae3b625..0000000 --- a/Debugger.cpp +++ /dev/null @@ -1,7 +0,0 @@ -// -// Created by pily on 11/02/17. -// - -#include "Debugger.h" - - diff --git a/Exceptions.h b/Exceptions.h new file mode 100644 index 0000000..2cd6b27 --- /dev/null +++ b/Exceptions.h @@ -0,0 +1,11 @@ +// +// Created by pily on 11/02/17. +// + +#ifndef WPS_PIN_GENERATOR_EXCEPTIONS_H +#define WPS_PIN_GENERATOR_EXCEPTIONS_H + +#include "Exceptions/NotImplementedException.h" +#include "Exceptions/NotImplementedException.cpp" + +#endif //WPS_PIN_GENERATOR_EXCEPTIONS_H diff --git a/Generator/Generator.cpp b/Generator/Generator.cpp deleted file mode 100644 index daa7df7..0000000 --- a/Generator/Generator.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// -// Created by pily on 27/01/17. -// - -#include -#include -#include "Generator.h" - -/** - * Implementation of generatePin - * Returns result of generatePinImp based on the same data passed - * @param s string of data - * @return Pin valid pin generated - */ -std::vector Generator::generatePin(const std::string &s) const { return generatePinImp(s); } - -/** - * Generates a valid Pin object using an algorithm based on zhaochunsheng's ComputePIN-C83A35 and linkp2p's WPS-PIN - * @param s: data passed - * @return valid Pin object - */ -std::vector Generator::generatePinImp(const std::string &s) const { - - std::vector result = std::vector(); - - long calc; - - std::stringstream ss; - ss << std::hex << s; - ss >> calc; - -} diff --git a/Generator/Generator.h b/Generator/Generator.h index 6bc90a7..d233177 100644 --- a/Generator/Generator.h +++ b/Generator/Generator.h @@ -6,6 +6,7 @@ #define WPS_PIN_GENERATOR_GENERATOR_H #include "GeneratorInterface.h" +#include "../Exceptions.h" #include #include @@ -16,10 +17,26 @@ */ class Generator : public GeneratorInterface { public: - std::vector generatePin(const std::string &s) const override; + /*** + * Implementation of generatePin + * Returns result of generatePinImp based on the same data passed + * @param s string of data + * @return Pin valid pin generated + */ + std::vector generatePin(const std::string &s) const override { + throw NotImplementedException(); + } private: - std::vector generatePinImp(const std::string &s) const; + + /*** + * Generates a valid Pin object using an algorithm based on zhaochunsheng's ComputePIN-C83A35 and linkp2p's WPS-PIN + * @param s string of data + * @return Pin valid pin generated + */ + std::vector generatePinImp(const std::string &s) const { + throw NotImplementedException(); + } }; diff --git a/Generator/GeneratorInterface.h b/Generator/GeneratorInterface.h index c20f94b..813bce8 100644 --- a/Generator/GeneratorInterface.h +++ b/Generator/GeneratorInterface.h @@ -4,7 +4,8 @@ /*** GeneratorInterface * GeneratorInterface is an base class interface used to implement - * the design pattern strategy + * the design pattern strategy, this way it's easy to add new generation + * algorithms. */ #ifndef WPS_PIN_GENERATOR_GENERATORINTERFACE_H diff --git a/Pin/Pin.cpp b/Pin/Pin.cpp deleted file mode 100644 index 4da760f..0000000 --- a/Pin/Pin.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// -// Created by pily on 27/01/17. -// - -#include -#include "Pin.h" - -/*** - * Checks if the pin passed is a valid pin - * @param pin: object of class Pin - * @return true if pin is valid, false otherwise - */ -bool Pin::valid() const { - bool valid = true; - - //TODO tests - - switch (valid) { - case true: - return valid; - break; - case false: - throw std::logic_error("Pin generated is not valid"); - break; - } - -} - -Pin::Pin(const std::string &pin) { - this->pin = pin; - this->valid(); -} - -std::string Pin::toString() const { - std::to_string(pin); -} - -long Pin::toInteger() const { - return pin; -} - -Pin::Pin(const long &pin) { - return ; -} diff --git a/Pin/Pin.h b/Pin/Pin.h index 655f20f..c44d73e 100644 --- a/Pin/Pin.h +++ b/Pin/Pin.h @@ -15,39 +15,44 @@ class Pin { private: - long pin; // content of the pin + long pinValue; // content of the pinValue public: /** * Constructor using string * @param pin */ - Pin(const std::string &pin); + Pin(const std::string &pin) { + this->pinValue = atoi(pin.c_str()); + this->valid(); + } /** * Constructor using long integer * @param pin */ - Pin(const long &pin); + Pin(const long &pin) : pinValue(pin) { + this->valid(); + } /** * Checks if the pin is a valid one * @return true if valid, false otherwise */ - bool valid() const; + bool valid() const { return false; } /** * Returns pin using a string * @return string of pin */ - std::string toString() const; + std::string toString() const { return std::to_string(pinValue); } /** * Returns pin using a long integer * @return pin (integer) * @attention This integer does not take in account initial 0 values */ - long toInteger() const; + long toInteger() const { return pinValue; } }; diff --git a/lib/rang b/lib/rang new file mode 160000 index 0000000..dc29826 --- /dev/null +++ b/lib/rang @@ -0,0 +1 @@ +Subproject commit dc298268ac85f781b14fb1ff5ac1e44e4af4dfa1 diff --git a/main.cpp b/main.cpp index ab6a1dd..ffecf81 100644 --- a/main.cpp +++ b/main.cpp @@ -1,21 +1,13 @@ -#include "Pin/Pin.h" -#include "Generator/Generator.h" - -#include #include - -#include "Debugger.h" +#include "Generator/Generator.h" int main() { std::cout << "WPS Pin Generator - by Bertof" << std::endl; - Debugger::testColors(); - - GeneratorInterface *g = new Generator(); - - std::vector p = g->generatePin("C8:3A:35:2D:9C:C0"); + Generator g = Generator(); + g.generatePin("test"); - std::cout << "PIN:\t" << p[0].toString() << "\tVALID:\t" << p[0].valid() << std::endl; + std::cout << "END MAIN" << std::endl; return 0; } \ No newline at end of file From 228e48f0e916ce5ee4fb863ab3925d84bb0b9c41 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 11:35:27 +0100 Subject: [PATCH 08/37] Spit debugger in declaration and definition --- Debugger/Debugger.cpp | 17 +++++++++++++++++ Debugger/Debugger.h | 13 ++----------- 2 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 Debugger/Debugger.cpp diff --git a/Debugger/Debugger.cpp b/Debugger/Debugger.cpp new file mode 100644 index 0000000..94bc517 --- /dev/null +++ b/Debugger/Debugger.cpp @@ -0,0 +1,17 @@ +// +// Created by pily on 11/02/17. +// + +#include "Debugger.h" +#include +#include +#include "../lib/rang/include/rang.hpp" + +void Debugger::log(const std::string &string) { + std::cout << rang::style::reset << string << std::endl; +} + +void Debugger::logError(const std::string &string) { + std::cout << rang::fg::red << rang::style::bold << "ERROR: " << rang::style::reset << rang::fg::red << string + << rang::style::reset << std::endl; +} diff --git a/Debugger/Debugger.h b/Debugger/Debugger.h index 77ab76f..df586e1 100644 --- a/Debugger/Debugger.h +++ b/Debugger/Debugger.h @@ -6,22 +6,13 @@ #define WPS_PIN_GENERATOR_DEBUGGER_H #include -#include -#include "../Exceptions/NotImplementedException.h" - -#include "../lib/rang/include/rang.hpp" class Debugger { public: - static void log(const std::string &string) { - std::cout << rang::style::reset << string << std::endl; - } + static void log(const std::string &string); - static void logError(const std::string &string) { - std::cout << rang::fg::red << rang::style::bold << "ERROR: " << rang::style::reset << rang::fg::red << string - << rang::style::reset << std::endl; - } + static void logError(const std::string &string); }; From 1c2b487f9aeaa85b9f72dc7564d4ee3921462ee4 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 12:11:40 +0100 Subject: [PATCH 09/37] Better exceptions and generator definitions --- .idea/vcs.xml | 7 +++++ CMakeLists.txt | 4 +-- Exceptions.h | 11 -------- Exceptions/NotImplementedException.cpp | 12 -------- Exceptions/NotImplementedException.h | 38 ++++++++++++++++++++++---- Generator/Generator.cpp | 5 ++++ Generator/Generator.h | 7 +++-- main.cpp | 2 +- 8 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 .idea/vcs.xml delete mode 100644 Exceptions.h delete mode 100644 Exceptions/NotImplementedException.cpp create mode 100644 Generator/Generator.cpp diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..7a92d06 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 7448f49..32f005e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(WPS_pin_generator) set(CMAKE_CXX_STANDARD 11) -set(SOURCE_FILES main.cpp Generator/GeneratorInterface.h Pin/Pin.h Exceptions.h Generator/Generator.h) -add_executable(WPS_pin_generator ${SOURCE_FILES} Generator/GeneratorInterface.h) \ No newline at end of file +set(SOURCE_FILES main.cpp Generator/GeneratorInterface.h Pin/Pin.h Generator/Generator.h Exceptions/NotImplementedException.h Generator/Generator.cpp) +add_executable(WPS_pin_generator ${SOURCE_FILES}) \ No newline at end of file diff --git a/Exceptions.h b/Exceptions.h deleted file mode 100644 index 2cd6b27..0000000 --- a/Exceptions.h +++ /dev/null @@ -1,11 +0,0 @@ -// -// Created by pily on 11/02/17. -// - -#ifndef WPS_PIN_GENERATOR_EXCEPTIONS_H -#define WPS_PIN_GENERATOR_EXCEPTIONS_H - -#include "Exceptions/NotImplementedException.h" -#include "Exceptions/NotImplementedException.cpp" - -#endif //WPS_PIN_GENERATOR_EXCEPTIONS_H diff --git a/Exceptions/NotImplementedException.cpp b/Exceptions/NotImplementedException.cpp deleted file mode 100644 index aa17657..0000000 --- a/Exceptions/NotImplementedException.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// -// Created by pily on 11/02/17. -// - -#include "NotImplementedException.h" - -NotImplementedException::NotImplementedException(const char *error) : errorMessage(error) { -} - -const char *NotImplementedException::what() { - return errorMessage.c_str(); -} diff --git a/Exceptions/NotImplementedException.h b/Exceptions/NotImplementedException.h index d15424a..d090b6a 100644 --- a/Exceptions/NotImplementedException.h +++ b/Exceptions/NotImplementedException.h @@ -9,13 +9,41 @@ #include class NotImplementedException : public std::exception { +public: + /** Constructor (C strings). + * @param message C-style string error message. + * The string contents are copied upon construction. + * Hence, responsibility for deleting the char* lies + * with the caller. + */ + explicit NotImplementedException(const char *message) : + msg_(message) { + } -private: - std::string errorMessage; + /** Constructor (C++ STL strings). + * @param message The error message. + */ + explicit NotImplementedException(const std::string &message = "Function not implemented") : + msg_(message) {} -public: - NotImplementedException(const char *error = "Functionality not implemented"); - const char * what(); + /** Destructor. + * Virtual to allow for subclassing. + */ + virtual ~NotImplementedException() throw() {} + + /** Returns a pointer to the (constant) error description. + * @return A pointer to a const char*. The underlying memory + * is in posession of the Exception object. Callers must + * not attempt to free the memory. + */ + virtual const char *what() const throw() { + return msg_.c_str(); + } + +protected: + /** Error message. + */ + std::string msg_; }; diff --git a/Generator/Generator.cpp b/Generator/Generator.cpp new file mode 100644 index 0000000..d30274d --- /dev/null +++ b/Generator/Generator.cpp @@ -0,0 +1,5 @@ +// +// Created by pily on 11/02/17. +// + +#include "Generator.h" diff --git a/Generator/Generator.h b/Generator/Generator.h index d233177..87f88ed 100644 --- a/Generator/Generator.h +++ b/Generator/Generator.h @@ -6,7 +6,7 @@ #define WPS_PIN_GENERATOR_GENERATOR_H #include "GeneratorInterface.h" -#include "../Exceptions.h" +#include "../Exceptions/NotImplementedException.h" #include #include @@ -23,8 +23,8 @@ class Generator : public GeneratorInterface { * @param s string of data * @return Pin valid pin generated */ - std::vector generatePin(const std::string &s) const override { - throw NotImplementedException(); + virtual std::vector generatePin(const std::string &s) const override { + return generatePinImp(s); } private: @@ -36,6 +36,7 @@ class Generator : public GeneratorInterface { */ std::vector generatePinImp(const std::string &s) const { throw NotImplementedException(); + return std::vector(); } }; diff --git a/main.cpp b/main.cpp index ffecf81..39ce75c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ #include -#include "Generator/Generator.h" +#include "Generator/Generator.cpp" int main() { std::cout << "WPS Pin Generator - by Bertof" << std::endl; From 8e29c424e66bab5348f57a6cb7b7d76f1941a385 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 14:44:48 +0100 Subject: [PATCH 10/37] Unified exceptions, made generator working again, added toggle to debug --- CMakeLists.txt | 2 +- Debugger/Debugger.cpp | 31 +++++++++++++++--- Debugger/Debugger.h | 10 ++++++ Exceptions/InvalidInputException.h | 47 ++++++++++++++++++++++++++++ Exceptions/NotImplementedException.h | 10 +++--- Generator/Generator.cpp | 38 ++++++++++++++++++++++ Generator/Generator.h | 25 +++++++++------ Generator/GeneratorInerface.h | 29 +++++++++++++++++ Generator/GeneratorInterface.h | 28 ----------------- main.cpp | 8 +++-- 10 files changed, 179 insertions(+), 49 deletions(-) create mode 100644 Exceptions/InvalidInputException.h create mode 100644 Generator/GeneratorInerface.h delete mode 100644 Generator/GeneratorInterface.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 32f005e..69f3762 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(WPS_pin_generator) set(CMAKE_CXX_STANDARD 11) -set(SOURCE_FILES main.cpp Generator/GeneratorInterface.h Pin/Pin.h Generator/Generator.h Exceptions/NotImplementedException.h Generator/Generator.cpp) +set(SOURCE_FILES main.cpp Pin/Pin.h Generator/Generator.h Exceptions/NotImplementedException.h Generator/Generator.cpp Debugger/Debugger.h Debugger/Debugger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) \ No newline at end of file diff --git a/Debugger/Debugger.cpp b/Debugger/Debugger.cpp index 94bc517..2b1fd81 100644 --- a/Debugger/Debugger.cpp +++ b/Debugger/Debugger.cpp @@ -3,15 +3,38 @@ // #include "Debugger.h" -#include +#include #include #include "../lib/rang/include/rang.hpp" void Debugger::log(const std::string &string) { - std::cout << rang::style::reset << string << std::endl; + if (isDebugActive()) { + std::cout << rang::style::reset << string << std::endl; + } } void Debugger::logError(const std::string &string) { - std::cout << rang::fg::red << rang::style::bold << "ERROR: " << rang::style::reset << rang::fg::red << string - << rang::style::reset << std::endl; + if (isDebugActive()) { + std::cout << rang::fg::red << "ERROR" << rang::style::reset << ": " << rang::fg::red + << string + << rang::style::reset << std::endl; + } } + +void Debugger::logDebug(const std::string &string) { + if (isDebugActive()) { + std::cout << rang::fg::cyan << "DEBUG" << rang::style::reset << ": " << string + << std::endl; + } +} + +bool Debugger::isDebugActive() { + return Debugger::debugActivityStatus; +} + +bool Debugger::toggleDebugActivityStatus(const bool &status) { + Debugger::debugActivityStatus = status; + return Debugger::debugActivityStatus; +} + +bool Debugger::debugActivityStatus = false; \ No newline at end of file diff --git a/Debugger/Debugger.h b/Debugger/Debugger.h index df586e1..4a38801 100644 --- a/Debugger/Debugger.h +++ b/Debugger/Debugger.h @@ -6,6 +6,7 @@ #define WPS_PIN_GENERATOR_DEBUGGER_H #include +#include class Debugger { @@ -13,6 +14,15 @@ class Debugger { static void log(const std::string &string); static void logError(const std::string &string); + + static void logDebug(const std::string &string); + + static bool toggleDebugActivityStatus(const bool &status = true); + + static bool isDebugActive(); + +private: + static bool debugActivityStatus; }; diff --git a/Exceptions/InvalidInputException.h b/Exceptions/InvalidInputException.h new file mode 100644 index 0000000..1b66a9c --- /dev/null +++ b/Exceptions/InvalidInputException.h @@ -0,0 +1,47 @@ +// +// Created by pily on 11/02/17. +// + +#ifndef WPS_PIN_GENERATOR_INVALIDINPUTEXCEPTION_H +#define WPS_PIN_GENERATOR_INVALIDINPUTEXCEPTION_H + +#include +#include + +class InvalidInputException : public std::exception { +public: + + + /** Constructor (C string) + * @param message C-style string error message. + * The string contents are copied upon construction. + * Hence, responsibility for deleting the char* lies + * with the caller. + */ + explicit InvalidInputException(const char *message) : errorMessage_(message) {} + + /** Constructor (C++ STL string) + * @param message The error message. + */ + explicit InvalidInputException(const std::string &message = "Input passed is not valid") : errorMessage_(message) {} + + /** Destructor + * Virtual to allow subclassing + */ + virtual ~InvalidInputException() throw() {} + + /** Returns a pointer to the (constant) error description. + * + * @return + */ + const char *what() const throw() { + return errorMessage_.c_str(); + } + +private: + /** Saved error message + */ + std::string errorMessage_; +}; + +#endif //WPS_PIN_GENERATOR_INVALIDINPUTEXCEPTION_H diff --git a/Exceptions/NotImplementedException.h b/Exceptions/NotImplementedException.h index d090b6a..edd7fd2 100644 --- a/Exceptions/NotImplementedException.h +++ b/Exceptions/NotImplementedException.h @@ -17,14 +17,14 @@ class NotImplementedException : public std::exception { * with the caller. */ explicit NotImplementedException(const char *message) : - msg_(message) { + errorMessage_(message) { } /** Constructor (C++ STL strings). * @param message The error message. */ explicit NotImplementedException(const std::string &message = "Function not implemented") : - msg_(message) {} + errorMessage_(message) {} /** Destructor. * Virtual to allow for subclassing. @@ -32,18 +32,18 @@ class NotImplementedException : public std::exception { virtual ~NotImplementedException() throw() {} /** Returns a pointer to the (constant) error description. - * @return A pointer to a const char*. The underlying memory + * @return A pointer to a const char*. The underlying memory * is in posession of the Exception object. Callers must * not attempt to free the memory. */ virtual const char *what() const throw() { - return msg_.c_str(); + return errorMessage_.c_str(); } protected: /** Error message. */ - std::string msg_; + std::string errorMessage_; }; diff --git a/Generator/Generator.cpp b/Generator/Generator.cpp index d30274d..f81cc51 100644 --- a/Generator/Generator.cpp +++ b/Generator/Generator.cpp @@ -3,3 +3,41 @@ // #include "Generator.h" +#include "../Exceptions/NotImplementedException.h" +#include "../Exceptions/InvalidInputException.h" + +#include +#include + +#include "../Debugger/Debugger.h" + +std::vector Generator::generatePinImp(const std::string &s) const { +// throw NotImplementedException(); + + std::string stringPassed = std::string(s); + + Debugger::logDebug("String passed:\t" + stringPassed); + + // Cleaning input string, only hexadecimal characters allowed + std::string cleanedString = std::regex_replace(stringPassed, std::regex("[^0-9A-F]:"), ""); + + Debugger::logDebug("Cleaned string:\t"+cleanedString); + + // Check if length is valid + if (cleanedString.length() != 12 + 5) { + Debugger::logError("The string passed is not a valid MAC address"); + throw InvalidInputException("The string passed is not a valid MAC address"); + } + + + + return std::vector(); +} + +std::string Generator::author() const { + return "bertof"; +} + +std::string Generator::version() const { + return "0.0.1"; +} diff --git a/Generator/Generator.h b/Generator/Generator.h index 87f88ed..3067d33 100644 --- a/Generator/Generator.h +++ b/Generator/Generator.h @@ -5,17 +5,17 @@ #ifndef WPS_PIN_GENERATOR_GENERATOR_H #define WPS_PIN_GENERATOR_GENERATOR_H -#include "GeneratorInterface.h" -#include "../Exceptions/NotImplementedException.h" - #include #include +#include "../Pin/Pin.h" + +#include "GeneratorInerface.h" /** * Base class Generator * Used to generate valid Pins */ -class Generator : public GeneratorInterface { +class Generator : public GeneratorInterface{ public: /*** * Implementation of generatePin @@ -23,21 +23,28 @@ class Generator : public GeneratorInterface { * @param s string of data * @return Pin valid pin generated */ - virtual std::vector generatePin(const std::string &s) const override { + virtual std::vector generatePin(const std::string &s) const { return generatePinImp(s); } private: + /** Method called to return the author of the generator + * @return + */ + std::string author() const override; + + /** Method called to return the version of the generator + * @return + */ + std::string version() const override; /*** * Generates a valid Pin object using an algorithm based on zhaochunsheng's ComputePIN-C83A35 and linkp2p's WPS-PIN * @param s string of data * @return Pin valid pin generated */ - std::vector generatePinImp(const std::string &s) const { - throw NotImplementedException(); - return std::vector(); - } + std::vector generatePinImp(const std::string &s) const; + }; diff --git a/Generator/GeneratorInerface.h b/Generator/GeneratorInerface.h new file mode 100644 index 0000000..554f857 --- /dev/null +++ b/Generator/GeneratorInerface.h @@ -0,0 +1,29 @@ +// +// Created by pily on 11/02/17. +// + +#ifndef WPS_PIN_GENERATOR_GENERATORINERFACE_H +#define WPS_PIN_GENERATOR_GENERATORINERFACE_H + +#include +#include "../Pin/Pin.h" + +class GeneratorInterface { + /** Method called to generate a pin + * @param s string of the mac address + * @return non empty vector of valid Pins + */ + virtual std::vector generatePin(const std::string &s) const =0; + + /** Method called to return the author of the generator + * @return + */ + virtual std::string author() const =0; + + /** Method called to return the version of the generator + * @return + */ + virtual std::string version() const =0; +}; + +#endif //WPS_PIN_GENERATOR_GENERATORINERFACE_H diff --git a/Generator/GeneratorInterface.h b/Generator/GeneratorInterface.h deleted file mode 100644 index 813bce8..0000000 --- a/Generator/GeneratorInterface.h +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by pily on 27/01/17. -// - -/*** GeneratorInterface - * GeneratorInterface is an base class interface used to implement - * the design pattern strategy, this way it's easy to add new generation - * algorithms. - */ - -#ifndef WPS_PIN_GENERATOR_GENERATORINTERFACE_H -#define WPS_PIN_GENERATOR_GENERATORINTERFACE_H - -#include "../Pin/Pin.h" -#include -#include - -class GeneratorInterface { -public: - /** - * Generates a valid pin based on the data passed - * @param s: string of data - * @return std::vector: valid Pin object vector - */ - virtual std::vector generatePin(const std::string &s) const =0; -}; - -#endif //WPS_PIN_GENERATOR_GENERATORINTERFACE_H diff --git a/main.cpp b/main.cpp index 39ce75c..4053f53 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,15 @@ #include -#include "Generator/Generator.cpp" +#include "Generator/Generator.h" +#include "Debugger/Debugger.h" int main() { std::cout << "WPS Pin Generator - by Bertof" << std::endl; + Debugger::toggleDebugActivityStatus(); + Generator g = Generator(); - g.generatePin("test"); + + g.generatePin("7C:5C:F8:F6:D0:B5"); std::cout << "END MAIN" << std::endl; From 5019412a6f95a72514de53c91bd5c44f030c16c8 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 18:24:46 +0100 Subject: [PATCH 11/37] Unified loggers, added boost and working on the generator --- CMakeLists.txt | 17 +++++++- Debugger/Debugger.cpp | 40 ------------------- Debugger/Debugger.h | 29 -------------- Generator/Generator.cpp | 12 +++--- Logger/Logger.cpp | 15 +++++++ Logger/Logger.h | 32 +++++++++++++++ Logger/ScreenLogger/ScreenLogger.cpp | 26 ++++++++++++ Logger/ScreenLogger/ScreenLogger.h | 27 +++++++++++++ Logger/TextLogger/TextLogger.cpp | 60 ++++++++++++++++++++++++++++ Logger/TextLogger/TextLogger.h | 29 ++++++++++++++ main.cpp | 5 ++- 11 files changed, 214 insertions(+), 78 deletions(-) delete mode 100644 Debugger/Debugger.cpp delete mode 100644 Debugger/Debugger.h create mode 100644 Logger/Logger.cpp create mode 100644 Logger/Logger.h create mode 100644 Logger/ScreenLogger/ScreenLogger.cpp create mode 100644 Logger/ScreenLogger/ScreenLogger.h create mode 100644 Logger/TextLogger/TextLogger.cpp create mode 100644 Logger/TextLogger/TextLogger.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 69f3762..1d912c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,18 @@ project(WPS_pin_generator) set(CMAKE_CXX_STANDARD 11) -set(SOURCE_FILES main.cpp Pin/Pin.h Generator/Generator.h Exceptions/NotImplementedException.h Generator/Generator.cpp Debugger/Debugger.h Debugger/Debugger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h) -add_executable(WPS_pin_generator ${SOURCE_FILES}) \ No newline at end of file +set(Boost_USE_STATIC_LIBS OFF) +set(Boost_USE_MULTITHREADED ON) +set(Boost_USE_STATIC_RUNTIME OFF) +find_package(Boost 1.63.0 COMPONENTS filesystem) + +if (Boost_FOUND) + include_directories(${Boost_INCLUDE_DIRS}) + + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/Generator.h Exceptions/NotImplementedException.h Generator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h) + add_executable(WPS_pin_generator ${SOURCE_FILES}) + + + target_link_libraries(WPS_pin_generator ${Boost_LIBRARIES}) +endif () + diff --git a/Debugger/Debugger.cpp b/Debugger/Debugger.cpp deleted file mode 100644 index 2b1fd81..0000000 --- a/Debugger/Debugger.cpp +++ /dev/null @@ -1,40 +0,0 @@ -// -// Created by pily on 11/02/17. -// - -#include "Debugger.h" -#include -#include -#include "../lib/rang/include/rang.hpp" - -void Debugger::log(const std::string &string) { - if (isDebugActive()) { - std::cout << rang::style::reset << string << std::endl; - } -} - -void Debugger::logError(const std::string &string) { - if (isDebugActive()) { - std::cout << rang::fg::red << "ERROR" << rang::style::reset << ": " << rang::fg::red - << string - << rang::style::reset << std::endl; - } -} - -void Debugger::logDebug(const std::string &string) { - if (isDebugActive()) { - std::cout << rang::fg::cyan << "DEBUG" << rang::style::reset << ": " << string - << std::endl; - } -} - -bool Debugger::isDebugActive() { - return Debugger::debugActivityStatus; -} - -bool Debugger::toggleDebugActivityStatus(const bool &status) { - Debugger::debugActivityStatus = status; - return Debugger::debugActivityStatus; -} - -bool Debugger::debugActivityStatus = false; \ No newline at end of file diff --git a/Debugger/Debugger.h b/Debugger/Debugger.h deleted file mode 100644 index 4a38801..0000000 --- a/Debugger/Debugger.h +++ /dev/null @@ -1,29 +0,0 @@ -// -// Created by pily on 11/02/17. -// - -#ifndef WPS_PIN_GENERATOR_DEBUGGER_H -#define WPS_PIN_GENERATOR_DEBUGGER_H - -#include -#include - -class Debugger { - -public: - static void log(const std::string &string); - - static void logError(const std::string &string); - - static void logDebug(const std::string &string); - - static bool toggleDebugActivityStatus(const bool &status = true); - - static bool isDebugActive(); - -private: - static bool debugActivityStatus; -}; - - -#endif //WPS_PIN_GENERATOR_DEBUGGER_H diff --git a/Generator/Generator.cpp b/Generator/Generator.cpp index f81cc51..cd8d20c 100644 --- a/Generator/Generator.cpp +++ b/Generator/Generator.cpp @@ -8,29 +8,31 @@ #include #include +#include -#include "../Debugger/Debugger.h" +#include "../Logger/ScreenLogger/ScreenLogger.h" std::vector Generator::generatePinImp(const std::string &s) const { // throw NotImplementedException(); + Logger *log = new ScreenLogger(); + std::string stringPassed = std::string(s); - Debugger::logDebug("String passed:\t" + stringPassed); + log->logDebug("String passed:\t" + stringPassed); // Cleaning input string, only hexadecimal characters allowed std::string cleanedString = std::regex_replace(stringPassed, std::regex("[^0-9A-F]:"), ""); - Debugger::logDebug("Cleaned string:\t"+cleanedString); + log->logDebug("Cleaned string:\t" + cleanedString); // Check if length is valid if (cleanedString.length() != 12 + 5) { - Debugger::logError("The string passed is not a valid MAC address"); + log->logError("The string passed is not a valid MAC address"); throw InvalidInputException("The string passed is not a valid MAC address"); } - return std::vector(); } diff --git a/Logger/Logger.cpp b/Logger/Logger.cpp new file mode 100644 index 0000000..27373fc --- /dev/null +++ b/Logger/Logger.cpp @@ -0,0 +1,15 @@ +// +// Created by pily on 11/02/17. +// + +#include "Logger.h" + +bool Logger::debugLogActive = false; + +bool Logger::isDebugLogActive() { + return debugLogActive; +} + +void Logger::setDebugLogActive(bool debugLogActive) { + Logger::debugLogActive = debugLogActive; +} diff --git a/Logger/Logger.h b/Logger/Logger.h new file mode 100644 index 0000000..9900453 --- /dev/null +++ b/Logger/Logger.h @@ -0,0 +1,32 @@ +// +// Created by pily on 11/02/17. +// + +#ifndef WPS_PIN_GENERATOR_LOGGER_H +#define WPS_PIN_GENERATOR_LOGGER_H + +#include + +/** Plain text logger + */ +class Logger { + +public: + virtual void log(const std::string &message) = 0; + + virtual void logError(const std::string &messageError) = 0; + + virtual void logDebug(const std::string &messageDebug) = 0; + + virtual ~Logger() {} + + static bool isDebugLogActive(); + + static void setDebugLogActive(bool debugLogActive); + +private: + static bool debugLogActive; +}; + + +#endif //WPS_PIN_GENERATOR_LOGGER_H diff --git a/Logger/ScreenLogger/ScreenLogger.cpp b/Logger/ScreenLogger/ScreenLogger.cpp new file mode 100644 index 0000000..c60611c --- /dev/null +++ b/Logger/ScreenLogger/ScreenLogger.cpp @@ -0,0 +1,26 @@ +// +// Created by pily on 11/02/17. +// + +#include "ScreenLogger.h" +#include "../../lib/rang/include/rang.hpp" +#include +#include + +void ScreenLogger::log(const std::string &string) { + std::cout << rang::style::reset << string << std::endl; +} + +void ScreenLogger::logError(const std::string &string) { + std::cout << rang::fg::red << "ERROR" << rang::style::reset << ": " << rang::fg::red + << string + << rang::style::reset << std::endl; + +} + +void ScreenLogger::logDebug(const std::string &string) { + if (Logger::isDebugLogActive()) { + std::cout << rang::fg::cyan << "DEBUG" << rang::style::reset << ": " << string + << std::endl; + } +} \ No newline at end of file diff --git a/Logger/ScreenLogger/ScreenLogger.h b/Logger/ScreenLogger/ScreenLogger.h new file mode 100644 index 0000000..71acccd --- /dev/null +++ b/Logger/ScreenLogger/ScreenLogger.h @@ -0,0 +1,27 @@ +// +// Created by pily on 11/02/17. +// + +#ifndef WPS_PIN_GENERATOR_DEBUGGER_H +#define WPS_PIN_GENERATOR_DEBUGGER_H + +#include "../Logger.h" + +#include +#include + +class ScreenLogger : public Logger { + +public: + virtual ~ScreenLogger() {} + + virtual void log(const std::string &string); + + virtual void logError(const std::string &string); + + virtual void logDebug(const std::string &string); + +}; + + +#endif //WPS_PIN_GENERATOR_DEBUGGER_H diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp new file mode 100644 index 0000000..f9dd542 --- /dev/null +++ b/Logger/TextLogger/TextLogger.cpp @@ -0,0 +1,60 @@ +// +// Created by pily on 11/02/17. +// + +#include "TextLogger.h" +#include "../ScreenLogger/ScreenLogger.h" +#include +#include + +/** Logs a message in the log file + * Logs the message passed in the log file + * @param message + */ +void TextLogger::log(const std::string &message) { + logText(message); +} + +/** Logs an error message + * Logs in the log file an error message + * @param messageError + */ +void TextLogger::logError(const std::string &messageError) { + logText("Error:\t" + messageError); +} + +/** Log a debug message + * If debugging is enabled loggs the message passed in the log file + * @param messageDebug + */ +void TextLogger::logDebug(const std::string &messageDebug) { + if (Logger::isDebugLogActive()) { + logText("DEBUG:\t" + messageDebug); + } +} + +/** Constructor + * Checks if the file path chosen is safely portable or not + * @param filePath + */ +TextLogger::TextLogger(const std::string &filePath) : filePath(filePath) { + if (!boost::filesystem::portable_name(filePath)) { + ScreenLogger l; + l.logError("File path not portable. It may create problems."); + } +} + +/** Opens file and appends the message passed + * @param message text to write in the log file + */ +void TextLogger::logText(const std::string &message) { + boost::filesystem::fstream fileOutput; + fileOutput.open(filePath, std::ofstream::app); + if (!fileOutput.is_open()) { + ScreenLogger l; + l.logError("Can't open the log file (" + filePath + ")"); + } else { + fileOutput << message << std::endl; + fileOutput.close(); + } +} diff --git a/Logger/TextLogger/TextLogger.h b/Logger/TextLogger/TextLogger.h new file mode 100644 index 0000000..fa270f3 --- /dev/null +++ b/Logger/TextLogger/TextLogger.h @@ -0,0 +1,29 @@ +// +// Created by pily on 11/02/17. +// + +#ifndef WPS_PIN_GENERATOR_TEXTLOGGER_H +#define WPS_PIN_GENERATOR_TEXTLOGGER_H + +#include "../Logger.h" + +class TextLogger : public Logger { +public: + TextLogger(const std::string &filePath = "log.txt"); + + virtual ~TextLogger() {} + + virtual void log(const std::string &message) override; + + virtual void logError(const std::string &messageError) override; + + virtual void logDebug(const std::string &messageDebug) override; + +protected: + virtual void logText(const std::string &message); + + const std::string filePath; +}; + + +#endif //WPS_PIN_GENERATOR_TEXTLOGGER_H diff --git a/main.cpp b/main.cpp index 4053f53..bbddc1f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,12 @@ #include #include "Generator/Generator.h" -#include "Debugger/Debugger.h" +#include "Logger/ScreenLogger/ScreenLogger.h" int main() { std::cout << "WPS Pin Generator - by Bertof" << std::endl; - Debugger::toggleDebugActivityStatus(); + //TODO test enabled debug + Logger::setDebugLogActive(true); Generator g = Generator(); From 440ac0f7ffa3a00bb479bc064c62977f2e56ac5c Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 18:54:51 +0100 Subject: [PATCH 12/37] Added date/time to TextLogger output --- CMakeLists.txt | 2 +- Generator/Generator.cpp | 13 +++++++++---- Logger/TextLogger/TextLogger.cpp | 6 ++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d912c7..38f4eb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 11) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) -find_package(Boost 1.63.0 COMPONENTS filesystem) +find_package(Boost 1.63.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) diff --git a/Generator/Generator.cpp b/Generator/Generator.cpp index cd8d20c..4f54fde 100644 --- a/Generator/Generator.cpp +++ b/Generator/Generator.cpp @@ -11,24 +11,29 @@ #include #include "../Logger/ScreenLogger/ScreenLogger.h" +#include "../Logger/TextLogger/TextLogger.h" std::vector Generator::generatePinImp(const std::string &s) const { // throw NotImplementedException(); - Logger *log = new ScreenLogger(); + Logger *screenLog = new ScreenLogger(); + Logger *fileLog = new TextLogger(); std::string stringPassed = std::string(s); - log->logDebug("String passed:\t" + stringPassed); + screenLog->logDebug("String passed:\t" + stringPassed); + fileLog->logDebug("String passed:\t" + stringPassed); // Cleaning input string, only hexadecimal characters allowed std::string cleanedString = std::regex_replace(stringPassed, std::regex("[^0-9A-F]:"), ""); - log->logDebug("Cleaned string:\t" + cleanedString); + screenLog->logDebug("Cleaned string:\t" + cleanedString); + fileLog->logDebug("Cleaned string:\t" + cleanedString); // Check if length is valid if (cleanedString.length() != 12 + 5) { - log->logError("The string passed is not a valid MAC address"); + screenLog->logError("The string passed is not a valid MAC address"); + fileLog->logError("The string passed is not a valid MAC address"); throw InvalidInputException("The string passed is not a valid MAC address"); } diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp index f9dd542..d56bb98 100644 --- a/Logger/TextLogger/TextLogger.cpp +++ b/Logger/TextLogger/TextLogger.cpp @@ -5,7 +5,7 @@ #include "TextLogger.h" #include "../ScreenLogger/ScreenLogger.h" #include -#include +#include /** Logs a message in the log file * Logs the message passed in the log file @@ -54,7 +54,9 @@ void TextLogger::logText(const std::string &message) { ScreenLogger l; l.logError("Can't open the log file (" + filePath + ")"); } else { - fileOutput << message << std::endl; + std::string nowString = boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); + + fileOutput << nowString << "\t" << message << std::endl; fileOutput.close(); } } From 190df8ab0206e30fd43115925c1c690c5af97813 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 11 Feb 2017 19:05:42 +0100 Subject: [PATCH 13/37] Updated Readme --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a9bd7d..2ad1a3d 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,14 @@ The original discoverer of the algorithm is zhaochunsheng, who has written a C i All names and history of that file can be found in linkp2p's repository. The use of this program is intended to be in a authorized environment. Don't use it against networks you do not own or you do not have legal permission to attack. -Every illicit use of this program has nothing to do with the author. \ No newline at end of file +Every illicit use of this program has nothing to do with the author. + +## Dependencies +- Boost + - Check file path for log + - Get local date time for log +- Ranq (git submodule) + - Cross platform color terminal output + +## Build environment +I'm building this on ArchLinux, but I'm trying to keep all the components as cross platform capable as possible. It should work on Linux, Windows and MacOS, but I can test it only on Linux. \ No newline at end of file From 6714efd4ba9217a009f7665e806fb6a398ee014c Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sun, 12 Feb 2017 15:35:20 +0100 Subject: [PATCH 14/37] Moved my generator into namespace bertof, fixed interface non public --- CMakeLists.txt | 2 +- Generator/Generator.h | 51 ------------------ Generator/GeneratorInerface.h | 1 + Generator/{ => bertofGenerator}/Generator.cpp | 14 ++--- Generator/bertofGenerator/Generator.h | 53 +++++++++++++++++++ main.cpp | 6 +-- 6 files changed, 65 insertions(+), 62 deletions(-) delete mode 100644 Generator/Generator.h rename Generator/{ => bertofGenerator}/Generator.cpp (72%) create mode 100644 Generator/bertofGenerator/Generator.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 38f4eb3..0cc62dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Boost 1.63.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) - set(SOURCE_FILES main.cpp Pin/Pin.h Generator/Generator.h Exceptions/NotImplementedException.h Generator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h) + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) diff --git a/Generator/Generator.h b/Generator/Generator.h deleted file mode 100644 index 3067d33..0000000 --- a/Generator/Generator.h +++ /dev/null @@ -1,51 +0,0 @@ -// -// Created by pily on 27/01/17. -// - -#ifndef WPS_PIN_GENERATOR_GENERATOR_H -#define WPS_PIN_GENERATOR_GENERATOR_H - -#include -#include -#include "../Pin/Pin.h" - -#include "GeneratorInerface.h" - -/** - * Base class Generator - * Used to generate valid Pins - */ -class Generator : public GeneratorInterface{ -public: - /*** - * Implementation of generatePin - * Returns result of generatePinImp based on the same data passed - * @param s string of data - * @return Pin valid pin generated - */ - virtual std::vector generatePin(const std::string &s) const { - return generatePinImp(s); - } - -private: - /** Method called to return the author of the generator - * @return - */ - std::string author() const override; - - /** Method called to return the version of the generator - * @return - */ - std::string version() const override; - - /*** - * Generates a valid Pin object using an algorithm based on zhaochunsheng's ComputePIN-C83A35 and linkp2p's WPS-PIN - * @param s string of data - * @return Pin valid pin generated - */ - std::vector generatePinImp(const std::string &s) const; - -}; - - -#endif //WPS_PIN_GENERATOR_GENERATOR_H diff --git a/Generator/GeneratorInerface.h b/Generator/GeneratorInerface.h index 554f857..93a50b6 100644 --- a/Generator/GeneratorInerface.h +++ b/Generator/GeneratorInerface.h @@ -9,6 +9,7 @@ #include "../Pin/Pin.h" class GeneratorInterface { +public: /** Method called to generate a pin * @param s string of the mac address * @return non empty vector of valid Pins diff --git a/Generator/Generator.cpp b/Generator/bertofGenerator/Generator.cpp similarity index 72% rename from Generator/Generator.cpp rename to Generator/bertofGenerator/Generator.cpp index 4f54fde..6b0018c 100644 --- a/Generator/Generator.cpp +++ b/Generator/bertofGenerator/Generator.cpp @@ -3,17 +3,17 @@ // #include "Generator.h" -#include "../Exceptions/NotImplementedException.h" -#include "../Exceptions/InvalidInputException.h" +#include "../../Exceptions/NotImplementedException.h" +#include "../../Exceptions/InvalidInputException.h" #include #include #include -#include "../Logger/ScreenLogger/ScreenLogger.h" -#include "../Logger/TextLogger/TextLogger.h" +#include "../../Logger/ScreenLogger/ScreenLogger.h" +#include "../../Logger/TextLogger/TextLogger.h" -std::vector Generator::generatePinImp(const std::string &s) const { +std::vector bertof::Generator::generatePinImp(const std::string &s) const { // throw NotImplementedException(); Logger *screenLog = new ScreenLogger(); @@ -41,10 +41,10 @@ std::vector Generator::generatePinImp(const std::string &s) const { return std::vector(); } -std::string Generator::author() const { +std::string bertof::Generator::author() const { return "bertof"; } -std::string Generator::version() const { +std::string bertof::Generator::version() const { return "0.0.1"; } diff --git a/Generator/bertofGenerator/Generator.h b/Generator/bertofGenerator/Generator.h new file mode 100644 index 0000000..3894e73 --- /dev/null +++ b/Generator/bertofGenerator/Generator.h @@ -0,0 +1,53 @@ +// +// Created by pily on 27/01/17. +// + +#ifndef WPS_PIN_GENERATOR_GENERATOR_H +#define WPS_PIN_GENERATOR_GENERATOR_H + +#include +#include +#include "../../Pin/Pin.h" + +#include "../GeneratorInerface.h" + +namespace bertof { + +/** + * Base class Generator + * Used to generate valid Pins + */ + class Generator : public GeneratorInterface { + public: + /*** + * Implementation of generatePin + * Returns result of generatePinImp based on the same data passed + * @param s string of data + * @return Pin valid pin generated + */ + virtual std::vector generatePin(const std::string &s) const { + return generatePinImp(s); + } + + /** Method called to return the author of the generator + * @return + */ + std::string author() const override; + + /** Method called to return the version of the generator + * @return + */ + std::string version() const override; + + private: + /*** + * Generates a valid Pin object using an algorithm based on zhaochunsheng's ComputePIN-C83A35 and linkp2p's WPS-PIN + * @param s string of data + * @return Pin valid pin generated + */ + std::vector generatePinImp(const std::string &s) const; + + }; +} + +#endif //WPS_PIN_GENERATOR_GENERATOR_H diff --git a/main.cpp b/main.cpp index bbddc1f..fd2646b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ #include -#include "Generator/Generator.h" +#include "Generator/bertofGenerator/Generator.h" #include "Logger/ScreenLogger/ScreenLogger.h" int main() { @@ -8,9 +8,9 @@ int main() { //TODO test enabled debug Logger::setDebugLogActive(true); - Generator g = Generator(); + GeneratorInterface *g = new bertof::Generator(); - g.generatePin("7C:5C:F8:F6:D0:B5"); + g->generatePin("7C:5C:F8:F6:D0:B5"); std::cout << "END MAIN" << std::endl; From bca5d50c0b3789633eba9c049209703d6adef1da Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sun, 12 Feb 2017 15:37:19 +0100 Subject: [PATCH 15/37] Switched from cout to clog --- Logger/ScreenLogger/ScreenLogger.cpp | 6 +++--- main.cpp | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Logger/ScreenLogger/ScreenLogger.cpp b/Logger/ScreenLogger/ScreenLogger.cpp index c60611c..a2d1bc8 100644 --- a/Logger/ScreenLogger/ScreenLogger.cpp +++ b/Logger/ScreenLogger/ScreenLogger.cpp @@ -8,11 +8,11 @@ #include void ScreenLogger::log(const std::string &string) { - std::cout << rang::style::reset << string << std::endl; + std::clog << rang::style::reset << string << std::endl; } void ScreenLogger::logError(const std::string &string) { - std::cout << rang::fg::red << "ERROR" << rang::style::reset << ": " << rang::fg::red + std::clog << rang::fg::red << "ERROR" << rang::style::reset << ": " << rang::fg::red << string << rang::style::reset << std::endl; @@ -20,7 +20,7 @@ void ScreenLogger::logError(const std::string &string) { void ScreenLogger::logDebug(const std::string &string) { if (Logger::isDebugLogActive()) { - std::cout << rang::fg::cyan << "DEBUG" << rang::style::reset << ": " << string + std::clog << rang::fg::cyan << "DEBUG" << rang::style::reset << ": " << string << std::endl; } } \ No newline at end of file diff --git a/main.cpp b/main.cpp index fd2646b..e8dfacd 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,9 @@ #include "Logger/ScreenLogger/ScreenLogger.h" int main() { + //TODO better spash screen + //TODO input handling + //TODO guide on how to use it (--help) std::cout << "WPS Pin Generator - by Bertof" << std::endl; //TODO test enabled debug From 44d36bc6e1beca3b5496601ceb07e8d5d232f0fc Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Mon, 13 Feb 2017 20:11:08 +0100 Subject: [PATCH 16/37] Fixed a problem of inclusion of stdlib.h A method in the class Pin uses the method std::atoi which is found in std::basic_string. This class is available including "stdlib.h". Using both g++ (6.3.1) and cmake with the c++11 standard it works ok, but @FightingForFun reported an error in Ubuntu 16.04 saying atoi is not defined. I've now included stdlib.h in that class so it should not give that error anymore. --- Pin/Pin.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Pin/Pin.h b/Pin/Pin.h index c44d73e..3d61fae 100644 --- a/Pin/Pin.h +++ b/Pin/Pin.h @@ -6,6 +6,7 @@ #define WPS_PIN_GENERATOR_PIN_H #include +#include /** * Class Pin @@ -23,7 +24,7 @@ class Pin { * @param pin */ Pin(const std::string &pin) { - this->pinValue = atoi(pin.c_str()); + this->pinValue = std::atoi(pin.c_str()); this->valid(); } @@ -45,7 +46,8 @@ class Pin { * Returns pin using a string * @return string of pin */ - std::string toString() const { return std::to_string(pinValue); } + std::string toString() const { + return std::to_string(pinValue); } /** * Returns pin using a long integer From d447a1309972199a1ee67625f61b3e8202c9c33b Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Mon, 13 Feb 2017 23:07:50 +0100 Subject: [PATCH 17/37] Used older Boost libs for wider compatibility (Ubuntu 16.04) --- CMakeLists.txt | 2 +- Pin/Pin.h | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cc62dd..2c2df32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 11) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) -find_package(Boost 1.63.0 COMPONENTS filesystem date_time) +find_package(Boost 1.58.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) diff --git a/Pin/Pin.h b/Pin/Pin.h index 3d61fae..cc5d23d 100644 --- a/Pin/Pin.h +++ b/Pin/Pin.h @@ -46,8 +46,7 @@ class Pin { * Returns pin using a string * @return string of pin */ - std::string toString() const { - return std::to_string(pinValue); } + std::string toString() const { return std::to_string(pinValue); } /** * Returns pin using a long integer From bdcaec7f10eb83506a31389a442d8097c3778b43 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Thu, 23 Feb 2017 11:56:15 +0100 Subject: [PATCH 18/37] Made logger const --- Logger/Logger.h | 6 +++--- Logger/ScreenLogger/ScreenLogger.cpp | 6 +++--- Logger/ScreenLogger/ScreenLogger.h | 6 +++--- Logger/TextLogger/TextLogger.cpp | 8 ++++---- Logger/TextLogger/TextLogger.h | 8 ++++---- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Logger/Logger.h b/Logger/Logger.h index 9900453..e2d1a4e 100644 --- a/Logger/Logger.h +++ b/Logger/Logger.h @@ -12,11 +12,11 @@ class Logger { public: - virtual void log(const std::string &message) = 0; + virtual void log(const std::string &messageError) const = 0; - virtual void logError(const std::string &messageError) = 0; + virtual void logDebug(const std::string &messageDebug) const = 0; - virtual void logDebug(const std::string &messageDebug) = 0; + virtual void logError(const std::string &messageDebug) const = 0; virtual ~Logger() {} diff --git a/Logger/ScreenLogger/ScreenLogger.cpp b/Logger/ScreenLogger/ScreenLogger.cpp index a2d1bc8..6986526 100644 --- a/Logger/ScreenLogger/ScreenLogger.cpp +++ b/Logger/ScreenLogger/ScreenLogger.cpp @@ -7,18 +7,18 @@ #include #include -void ScreenLogger::log(const std::string &string) { +void ScreenLogger::log(const std::string &string) const { std::clog << rang::style::reset << string << std::endl; } -void ScreenLogger::logError(const std::string &string) { +void ScreenLogger::logError(const std::string &string) const { std::clog << rang::fg::red << "ERROR" << rang::style::reset << ": " << rang::fg::red << string << rang::style::reset << std::endl; } -void ScreenLogger::logDebug(const std::string &string) { +void ScreenLogger::logDebug(const std::string &string) const { if (Logger::isDebugLogActive()) { std::clog << rang::fg::cyan << "DEBUG" << rang::style::reset << ": " << string << std::endl; diff --git a/Logger/ScreenLogger/ScreenLogger.h b/Logger/ScreenLogger/ScreenLogger.h index 71acccd..6b6fb06 100644 --- a/Logger/ScreenLogger/ScreenLogger.h +++ b/Logger/ScreenLogger/ScreenLogger.h @@ -15,11 +15,11 @@ class ScreenLogger : public Logger { public: virtual ~ScreenLogger() {} - virtual void log(const std::string &string); + virtual void log(const std::string &string) const override; - virtual void logError(const std::string &string); + virtual void logError(const std::string &string) const override; - virtual void logDebug(const std::string &string); + virtual void logDebug(const std::string &string) const override; }; diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp index d56bb98..515929c 100644 --- a/Logger/TextLogger/TextLogger.cpp +++ b/Logger/TextLogger/TextLogger.cpp @@ -11,7 +11,7 @@ * Logs the message passed in the log file * @param message */ -void TextLogger::log(const std::string &message) { +void TextLogger::log(const std::string &message) const { logText(message); } @@ -19,7 +19,7 @@ void TextLogger::log(const std::string &message) { * Logs in the log file an error message * @param messageError */ -void TextLogger::logError(const std::string &messageError) { +void TextLogger::logError(const std::string &messageError) const { logText("Error:\t" + messageError); } @@ -27,7 +27,7 @@ void TextLogger::logError(const std::string &messageError) { * If debugging is enabled loggs the message passed in the log file * @param messageDebug */ -void TextLogger::logDebug(const std::string &messageDebug) { +void TextLogger::logDebug(const std::string &messageDebug) const { if (Logger::isDebugLogActive()) { logText("DEBUG:\t" + messageDebug); } @@ -47,7 +47,7 @@ TextLogger::TextLogger(const std::string &filePath) : filePath(filePath) { /** Opens file and appends the message passed * @param message text to write in the log file */ -void TextLogger::logText(const std::string &message) { +void TextLogger::logText(const std::string &message) const { boost::filesystem::fstream fileOutput; fileOutput.open(filePath, std::ofstream::app); if (!fileOutput.is_open()) { diff --git a/Logger/TextLogger/TextLogger.h b/Logger/TextLogger/TextLogger.h index fa270f3..f8ec6a1 100644 --- a/Logger/TextLogger/TextLogger.h +++ b/Logger/TextLogger/TextLogger.h @@ -13,14 +13,14 @@ class TextLogger : public Logger { virtual ~TextLogger() {} - virtual void log(const std::string &message) override; + virtual void log(const std::string &message) const override; - virtual void logError(const std::string &messageError) override; + virtual void logError(const std::string &messageError) const override; - virtual void logDebug(const std::string &messageDebug) override; + virtual void logDebug(const std::string &messageDebug) const override; protected: - virtual void logText(const std::string &message); + virtual void logText(const std::string &message) const; const std::string filePath; }; From cb5effa75a76ac908f2544aeda5fbca3f1f0c329 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Thu, 23 Feb 2017 11:59:49 +0100 Subject: [PATCH 19/37] Conversion to integer --- Generator/bertofGenerator/Generator.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Generator/bertofGenerator/Generator.cpp b/Generator/bertofGenerator/Generator.cpp index 6b0018c..f727848 100644 --- a/Generator/bertofGenerator/Generator.cpp +++ b/Generator/bertofGenerator/Generator.cpp @@ -37,6 +37,15 @@ std::vector bertof::Generator::generatePinImp(const std::string &s) const { throw InvalidInputException("The string passed is not a valid MAC address"); } + std::string hexValue = std::regex_replace(cleanedString, std::regex(":+"), ""); + + screenLog->logDebug("Hex value:\t" + hexValue); + + unsigned long intValue = std::stoul(hexValue, nullptr, 16); + + screenLog->logDebug("Int value:\t" + std::to_string(intValue)); + + return std::vector(); } From 6bc293f1a90b281f3ea8cd7cc92615eb1f0138ef Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Mon, 27 Feb 2017 09:30:56 +0100 Subject: [PATCH 20/37] First attempt to a working generator --- Generator/bertofGenerator/Generator.cpp | 36 ++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Generator/bertofGenerator/Generator.cpp b/Generator/bertofGenerator/Generator.cpp index f727848..3fad31e 100644 --- a/Generator/bertofGenerator/Generator.cpp +++ b/Generator/bertofGenerator/Generator.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "../../Logger/ScreenLogger/ScreenLogger.h" #include "../../Logger/TextLogger/TextLogger.h" @@ -16,6 +17,8 @@ std::vector bertof::Generator::generatePinImp(const std::string &s) const { // throw NotImplementedException(); + std::vector result = std::vector(); + Logger *screenLog = new ScreenLogger(); Logger *fileLog = new TextLogger(); @@ -45,9 +48,40 @@ std::vector bertof::Generator::generatePinImp(const std::string &s) const { screenLog->logDebug("Int value:\t" + std::to_string(intValue)); + screenLog->logDebug(std::to_string(std::ceil(std::log10(intValue)) - 7)); + + unsigned long first7Numbers = intValue / std::pow(10, std::ceil(std::log10(intValue)) - 7); + + screenLog->logDebug("First 7 numbers of the pin:\t" + std::to_string(first7Numbers)); + + unsigned long pin = 10 * first7Numbers; + unsigned long accum = 0; + unsigned long digit; + unsigned long checksum; + + accum = accum + 3 * ((pin / 10000000) % 10); + accum = accum + 1 * ((pin / 1000000) % 10); + accum = accum + 3 * ((pin / 100000) % 10); + accum = accum + 1 * ((pin / 10000) % 10); + accum = accum + 3 * ((pin / 1000) % 10); + accum = accum + 1 * ((pin / 100) % 10); + accum = accum + 3 * ((pin / 10) % 10); + + digit = accum % 10; + + checksum = (10 - digit) % 10; + + screenLog->logDebug("Accum:\t" + std::to_string(accum)); + screenLog->logDebug("Digit:\t" + std::to_string(digit)); + screenLog->logDebug("Checksum:\t" + std::to_string(checksum)); + + Pin firstResult = Pin(pin + checksum); + + screenLog->logDebug("First result:\t" + firstResult.toString()); + result.push_back(firstResult); - return std::vector(); + return result; } std::string bertof::Generator::author() const { From 147c2709f144921d90428492832309d58af8f2d2 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Tue, 28 Feb 2017 09:12:07 +0100 Subject: [PATCH 21/37] Fixed adding ZaoMode --- Generator/bertofGenerator/Generator.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Generator/bertofGenerator/Generator.cpp b/Generator/bertofGenerator/Generator.cpp index 3fad31e..da42b37 100644 --- a/Generator/bertofGenerator/Generator.cpp +++ b/Generator/bertofGenerator/Generator.cpp @@ -44,13 +44,19 @@ std::vector bertof::Generator::generatePinImp(const std::string &s) const { screenLog->logDebug("Hex value:\t" + hexValue); - unsigned long intValue = std::stoul(hexValue, nullptr, 16); + std::string lastHalfHexValue = hexValue.substr(6, 6); + + screenLog->logDebug("Last half hex value:\t" + lastHalfHexValue); + + unsigned long intValue = std::stoul(lastHalfHexValue, nullptr, 16); screenLog->logDebug("Int value:\t" + std::to_string(intValue)); - screenLog->logDebug(std::to_string(std::ceil(std::log10(intValue)) - 7)); + unsigned long zaoModeIntValue = intValue % 10000000; + + screenLog->logDebug("Zao Mode integer value=\t" + std::to_string(zaoModeIntValue)); - unsigned long first7Numbers = intValue / std::pow(10, std::ceil(std::log10(intValue)) - 7); + unsigned long first7Numbers = zaoModeIntValue / std::pow(10, std::ceil(std::log10(zaoModeIntValue)) - 7); screenLog->logDebug("First 7 numbers of the pin:\t" + std::to_string(first7Numbers)); From e7c7242c90030de1f3fe508e3275fcd0ed124fd7 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Tue, 28 Feb 2017 13:33:37 +0100 Subject: [PATCH 22/37] Definition of new log type verbouse --- Logger/Logger.cpp | 10 ++++++++++ Logger/Logger.h | 13 ++++++++++--- Logger/ScreenLogger/ScreenLogger.cpp | 9 ++++++++- Logger/ScreenLogger/ScreenLogger.h | 1 + Logger/TextLogger/TextLogger.cpp | 8 ++++++++ Logger/TextLogger/TextLogger.h | 2 ++ 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Logger/Logger.cpp b/Logger/Logger.cpp index 27373fc..7b0606b 100644 --- a/Logger/Logger.cpp +++ b/Logger/Logger.cpp @@ -6,6 +6,8 @@ bool Logger::debugLogActive = false; +bool Logger::verbouseLogActive = false; + bool Logger::isDebugLogActive() { return debugLogActive; } @@ -13,3 +15,11 @@ bool Logger::isDebugLogActive() { void Logger::setDebugLogActive(bool debugLogActive) { Logger::debugLogActive = debugLogActive; } + +bool Logger::isVerbouseLogActive() { + return verbouseLogActive; +} + +void Logger::setVerbouseLogActive(bool verbouseLogActive) { + Logger::verbouseLogActive = verbouseLogActive; +} diff --git a/Logger/Logger.h b/Logger/Logger.h index e2d1a4e..bae0064 100644 --- a/Logger/Logger.h +++ b/Logger/Logger.h @@ -12,20 +12,27 @@ class Logger { public: - virtual void log(const std::string &messageError) const = 0; + virtual void log(const std::string &messageLog) const = 0; + + virtual void logVerbouse(const std::string &messageLogVerbouse) const = 0; virtual void logDebug(const std::string &messageDebug) const = 0; - virtual void logError(const std::string &messageDebug) const = 0; + virtual void logError(const std::string &messageError) const = 0; virtual ~Logger() {} static bool isDebugLogActive(); static void setDebugLogActive(bool debugLogActive); - private: static bool debugLogActive; + + static bool verbouseLogActive; +public: + static bool isVerbouseLogActive(); + + static void setVerbouseLogActive(bool verbouseLogActive); }; diff --git a/Logger/ScreenLogger/ScreenLogger.cpp b/Logger/ScreenLogger/ScreenLogger.cpp index 6986526..f71ecd4 100644 --- a/Logger/ScreenLogger/ScreenLogger.cpp +++ b/Logger/ScreenLogger/ScreenLogger.cpp @@ -23,4 +23,11 @@ void ScreenLogger::logDebug(const std::string &string) const { std::clog << rang::fg::cyan << "DEBUG" << rang::style::reset << ": " << string << std::endl; } -} \ No newline at end of file +} + +void ScreenLogger::logVerbouse(const std::string &string) const { + if (Logger::isVerbouseLogActive()) { + std::clog << rang::fg::cyan << "VERBOUSE" << rang::style::reset << ": " << string + << std::endl; + } +} diff --git a/Logger/ScreenLogger/ScreenLogger.h b/Logger/ScreenLogger/ScreenLogger.h index 6b6fb06..b92cd2b 100644 --- a/Logger/ScreenLogger/ScreenLogger.h +++ b/Logger/ScreenLogger/ScreenLogger.h @@ -21,6 +21,7 @@ class ScreenLogger : public Logger { virtual void logDebug(const std::string &string) const override; + virtual void logVerbouse(const std::string &messageLogVerbouse) const override; }; diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp index 515929c..aa74757 100644 --- a/Logger/TextLogger/TextLogger.cpp +++ b/Logger/TextLogger/TextLogger.cpp @@ -60,3 +60,11 @@ void TextLogger::logText(const std::string &message) const { fileOutput.close(); } } + +void TextLogger::logVerbouse(const std::string &messageLogVerbouse) const { + if (Logger::isVerbouseLogActive()) { + logText("VERBOUSE:\t" + messageDebug); + } +} + + diff --git a/Logger/TextLogger/TextLogger.h b/Logger/TextLogger/TextLogger.h index f8ec6a1..f23ca9b 100644 --- a/Logger/TextLogger/TextLogger.h +++ b/Logger/TextLogger/TextLogger.h @@ -19,6 +19,8 @@ class TextLogger : public Logger { virtual void logDebug(const std::string &messageDebug) const override; + virtual void logVerbouse(const std::string &messageLogVerbouse) const override; + protected: virtual void logText(const std::string &message) const; From 77ee3d8f06d12f1939e8b8ce64064e11e0b274d4 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Wed, 1 Mar 2017 08:55:15 +0100 Subject: [PATCH 23/37] Created a DoubleLogger class DoubleLogger allows to log both on screen and on a log file --- CMakeLists.txt | 2 +- Logger/DoubleLogger/DoubleLogger.cpp | 27 +++++++++++++++++++++++++ Logger/DoubleLogger/DoubleLogger.h | 30 ++++++++++++++++++++++++++++ Logger/ScreenLogger/ScreenLogger.cpp | 6 +++--- Logger/TextLogger/TextLogger.cpp | 8 ++++---- main.cpp | 3 +++ 6 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 Logger/DoubleLogger/DoubleLogger.cpp create mode 100644 Logger/DoubleLogger/DoubleLogger.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c2df32..59362f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Boost 1.58.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) - set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h) + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) diff --git a/Logger/DoubleLogger/DoubleLogger.cpp b/Logger/DoubleLogger/DoubleLogger.cpp new file mode 100644 index 0000000..93ff0b1 --- /dev/null +++ b/Logger/DoubleLogger/DoubleLogger.cpp @@ -0,0 +1,27 @@ +// +// Created by pily on 28/02/17. +// + +#include "DoubleLogger.h" + +DoubleLogger::DoubleLogger(const std::string &filePath) : TextLogger(filePath) {} + +void DoubleLogger::log(const std::string &string) const { + ScreenLogger::log(string); + TextLogger::log(string); +} + +void DoubleLogger::logError(const std::string &string) const { + ScreenLogger::logError(string); + TextLogger::logError(string); +} + +void DoubleLogger::logDebug(const std::string &string) const { + ScreenLogger::logDebug(string); + TextLogger::logDebug(string); +} + +void DoubleLogger::logVerbouse(const std::string &string) const { + ScreenLogger::logVerbouse(string); + TextLogger::logVerbouse(string); +} diff --git a/Logger/DoubleLogger/DoubleLogger.h b/Logger/DoubleLogger/DoubleLogger.h new file mode 100644 index 0000000..1e66bb7 --- /dev/null +++ b/Logger/DoubleLogger/DoubleLogger.h @@ -0,0 +1,30 @@ +// +// Created by pily on 28/02/17. +// + +#ifndef WPS_PIN_GENERATOR_DOUBLELOGGER_H +#define WPS_PIN_GENERATOR_DOUBLELOGGER_H + +#include "../Logger.h" +#include "../ScreenLogger/ScreenLogger.h" +#include "../TextLogger/TextLogger.h" + +class DoubleLogger : public ScreenLogger, public TextLogger { +public: + + DoubleLogger(const std::string &filePath = "log.txt"); + + ~DoubleLogger() override {}; + + void log(const std::string &string) const override; + + void logError(const std::string &string) const override; + + void logDebug(const std::string &string) const override; + + void logVerbouse(const std::string &messageLogVerbouse) const override; + +}; + + +#endif //WPS_PIN_GENERATOR_DOUBLELOGGER_H diff --git a/Logger/ScreenLogger/ScreenLogger.cpp b/Logger/ScreenLogger/ScreenLogger.cpp index f71ecd4..e2ad838 100644 --- a/Logger/ScreenLogger/ScreenLogger.cpp +++ b/Logger/ScreenLogger/ScreenLogger.cpp @@ -12,7 +12,7 @@ void ScreenLogger::log(const std::string &string) const { } void ScreenLogger::logError(const std::string &string) const { - std::clog << rang::fg::red << "ERROR" << rang::style::reset << ": " << rang::fg::red + std::clog << rang::fg::red << "ERROR" << rang::style::reset << ":\t" << rang::fg::red << string << rang::style::reset << std::endl; @@ -20,14 +20,14 @@ void ScreenLogger::logError(const std::string &string) const { void ScreenLogger::logDebug(const std::string &string) const { if (Logger::isDebugLogActive()) { - std::clog << rang::fg::cyan << "DEBUG" << rang::style::reset << ": " << string + std::clog << rang::fg::cyan << "DBG" << rang::style::reset << ":\t" << string << std::endl; } } void ScreenLogger::logVerbouse(const std::string &string) const { if (Logger::isVerbouseLogActive()) { - std::clog << rang::fg::cyan << "VERBOUSE" << rang::style::reset << ": " << string + std::clog << rang::fg::cyan << "VERB" << rang::style::reset << ":\t" << string << std::endl; } } diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp index aa74757..607d626 100644 --- a/Logger/TextLogger/TextLogger.cpp +++ b/Logger/TextLogger/TextLogger.cpp @@ -20,7 +20,7 @@ void TextLogger::log(const std::string &message) const { * @param messageError */ void TextLogger::logError(const std::string &messageError) const { - logText("Error:\t" + messageError); + logText("ERROR:\t" + messageError); } /** Log a debug message @@ -29,7 +29,7 @@ void TextLogger::logError(const std::string &messageError) const { */ void TextLogger::logDebug(const std::string &messageDebug) const { if (Logger::isDebugLogActive()) { - logText("DEBUG:\t" + messageDebug); + logText("DBG:\t" + messageDebug); } } @@ -61,9 +61,9 @@ void TextLogger::logText(const std::string &message) const { } } -void TextLogger::logVerbouse(const std::string &messageLogVerbouse) const { +void TextLogger::logVerbouse(const std::string &string) const { if (Logger::isVerbouseLogActive()) { - logText("VERBOUSE:\t" + messageDebug); + logText("VERB:\t" + string); } } diff --git a/main.cpp b/main.cpp index e8dfacd..3af5c06 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include #include "Generator/bertofGenerator/Generator.h" #include "Logger/ScreenLogger/ScreenLogger.h" +#include "Logger/DoubleLogger/DoubleLogger.h" int main() { //TODO better spash screen @@ -10,6 +11,8 @@ int main() { //TODO test enabled debug Logger::setDebugLogActive(true); + Logger::setVerbouseLogActive(true); + GeneratorInterface *g = new bertof::Generator(); From 712e1a57d3f4d9b510812e2f20e03b7e3c99683b Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Wed, 1 Mar 2017 09:18:38 +0100 Subject: [PATCH 24/37] Base splash screen and TextLog toggle --- CMakeLists.txt | 2 +- Graphics/SplashScreen/SplashScreen.cpp | 17 ++++++++++++++ Graphics/SplashScreen/SplashScreen.h | 18 +++++++++++++++ Logger/DoubleLogger/DoubleLogger.h | 1 - Logger/TextLogger/TextLogger.cpp | 31 +++++++++++++++++--------- Logger/TextLogger/TextLogger.h | 15 +++++++++++++ main.cpp | 9 ++++++-- 7 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 Graphics/SplashScreen/SplashScreen.cpp create mode 100644 Graphics/SplashScreen/SplashScreen.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 59362f9..25aa8ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Boost 1.58.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) - set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h) + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) diff --git a/Graphics/SplashScreen/SplashScreen.cpp b/Graphics/SplashScreen/SplashScreen.cpp new file mode 100644 index 0000000..b9a555c --- /dev/null +++ b/Graphics/SplashScreen/SplashScreen.cpp @@ -0,0 +1,17 @@ +// +// Created by pily on 01/03/17. +// + +#include "SplashScreen.h" +#include +#include "../../Logger/DoubleLogger/DoubleLogger.h" + +void SplashScreen::printSplashScreen() { + DoubleLogger logger; + logger.logDebug("Splash screen"); + std::cout << SplashScreen::getSplashScreen() << std::endl; +} + +std::string SplashScreen::getSplashScreen() { + return "TEST"; +} diff --git a/Graphics/SplashScreen/SplashScreen.h b/Graphics/SplashScreen/SplashScreen.h new file mode 100644 index 0000000..1da4723 --- /dev/null +++ b/Graphics/SplashScreen/SplashScreen.h @@ -0,0 +1,18 @@ +// +// Created by pily on 01/03/17. +// + +#ifndef WPS_PIN_GENERATOR_SPASHSCREEN_H +#define WPS_PIN_GENERATOR_SPASHSCREEN_H + +#include + +class SplashScreen { +public: + static void printSplashScreen(); +private: + static std::string getSplashScreen(); +}; + + +#endif //WPS_PIN_GENERATOR_SPASHSCREEN_H diff --git a/Logger/DoubleLogger/DoubleLogger.h b/Logger/DoubleLogger/DoubleLogger.h index 1e66bb7..1a6a796 100644 --- a/Logger/DoubleLogger/DoubleLogger.h +++ b/Logger/DoubleLogger/DoubleLogger.h @@ -23,7 +23,6 @@ class DoubleLogger : public ScreenLogger, public TextLogger { void logDebug(const std::string &string) const override; void logVerbouse(const std::string &messageLogVerbouse) const override; - }; diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp index 607d626..4439d20 100644 --- a/Logger/TextLogger/TextLogger.cpp +++ b/Logger/TextLogger/TextLogger.cpp @@ -7,6 +7,8 @@ #include #include +bool TextLogger::writeOnLogFileActive = false; + /** Logs a message in the log file * Logs the message passed in the log file * @param message @@ -48,16 +50,17 @@ TextLogger::TextLogger(const std::string &filePath) : filePath(filePath) { * @param message text to write in the log file */ void TextLogger::logText(const std::string &message) const { - boost::filesystem::fstream fileOutput; - fileOutput.open(filePath, std::ofstream::app); - if (!fileOutput.is_open()) { - ScreenLogger l; - l.logError("Can't open the log file (" + filePath + ")"); - } else { - std::string nowString = boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); - - fileOutput << nowString << "\t" << message << std::endl; - fileOutput.close(); + if (TextLogger::isWriteOnLogFileActive()) { + boost::filesystem::fstream fileOutput; + fileOutput.open(filePath, std::ofstream::app); + if (!fileOutput.is_open()) { + ScreenLogger l; + l.logError("Can't open the log file (" + filePath + ")"); + } else { + std::string nowString = boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); + fileOutput << nowString << "\t" << message << std::endl; + fileOutput.close(); + } } } @@ -67,4 +70,12 @@ void TextLogger::logVerbouse(const std::string &string) const { } } +bool TextLogger::isWriteOnLogFileActive() { + return writeOnLogFileActive; +} + +void TextLogger::setWriteOnLogFileActive(bool writeOnLogFileActive) { + TextLogger::writeOnLogFileActive = writeOnLogFileActive; +} + diff --git a/Logger/TextLogger/TextLogger.h b/Logger/TextLogger/TextLogger.h index f23ca9b..32b13d0 100644 --- a/Logger/TextLogger/TextLogger.h +++ b/Logger/TextLogger/TextLogger.h @@ -25,6 +25,21 @@ class TextLogger : public Logger { virtual void logText(const std::string &message) const; const std::string filePath; + +private: + /*** Controls if any TextLogger should write on a file + */ + static bool writeOnLogFileActive; +public: + /*** Getter for writeOnLogFileActive + * @return writeOnLogFileActive status + */ + static bool isWriteOnLogFileActive(); + + /*** Setter for writeOnLogFileActive + * @param writeOnLogFileActive set to true to enable logging on a text file + */ + static void setWriteOnLogFileActive(bool writeOnLogFileActive); }; diff --git a/main.cpp b/main.cpp index 3af5c06..f0d742f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,7 @@ #include + +#include "Graphics/SplashScreen/SplashScreen.h" + #include "Generator/bertofGenerator/Generator.h" #include "Logger/ScreenLogger/ScreenLogger.h" #include "Logger/DoubleLogger/DoubleLogger.h" @@ -10,9 +13,11 @@ int main() { std::cout << "WPS Pin Generator - by Bertof" << std::endl; //TODO test enabled debug - Logger::setDebugLogActive(true); - Logger::setVerbouseLogActive(true); + DoubleLogger::setVerbouseLogActive(true); + DoubleLogger::setDebugLogActive(true); + DoubleLogger::setWriteOnLogFileActive(true); + SplashScreen::printSplashScreen(); GeneratorInterface *g = new bertof::Generator(); From ff26ab73225e2215afbbb0d8c7a179e920693f94 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Wed, 1 Mar 2017 18:19:30 +0100 Subject: [PATCH 25/37] Managed splash screen. Needs an ascii art --- Graphics/SplashScreen/SplashScreen.cpp | 2 +- main.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Graphics/SplashScreen/SplashScreen.cpp b/Graphics/SplashScreen/SplashScreen.cpp index b9a555c..063de27 100644 --- a/Graphics/SplashScreen/SplashScreen.cpp +++ b/Graphics/SplashScreen/SplashScreen.cpp @@ -13,5 +13,5 @@ void SplashScreen::printSplashScreen() { } std::string SplashScreen::getSplashScreen() { - return "TEST"; + return "WPS Pin Generator - by Bertof"; } diff --git a/main.cpp b/main.cpp index f0d742f..aae234a 100644 --- a/main.cpp +++ b/main.cpp @@ -10,7 +10,6 @@ int main() { //TODO better spash screen //TODO input handling //TODO guide on how to use it (--help) - std::cout << "WPS Pin Generator - by Bertof" << std::endl; //TODO test enabled debug DoubleLogger::setVerbouseLogActive(true); From 88c1edf1bb286e5d06e6ca3cda58df2cac6db5b9 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Fri, 3 Mar 2017 18:15:18 +0100 Subject: [PATCH 26/37] Unified use of debugger and initial input args management --- CMakeLists.txt | 2 +- Generator/bertofGenerator/Generator.cpp | 37 ++++++++--------- InputHandling/InputHandler.cpp | 53 +++++++++++++++++++++++++ InputHandling/InputHandler.h | 17 ++++++++ Logger/DoubleLogger/DoubleLogger.cpp | 2 +- Logger/DoubleLogger/DoubleLogger.h | 4 +- Logger/ScreenLogger/ScreenLogger.h | 2 +- Logger/TextLogger/TextLogger.cpp | 11 ++++- Logger/TextLogger/TextLogger.h | 15 +++++-- main.cpp | 10 ++--- 10 files changed, 117 insertions(+), 36 deletions(-) create mode 100644 InputHandling/InputHandler.cpp create mode 100644 InputHandling/InputHandler.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 25aa8ef..b393197 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Boost 1.58.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) - set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h) + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) diff --git a/Generator/bertofGenerator/Generator.cpp b/Generator/bertofGenerator/Generator.cpp index da42b37..8b2ace3 100644 --- a/Generator/bertofGenerator/Generator.cpp +++ b/Generator/bertofGenerator/Generator.cpp @@ -11,54 +11,50 @@ #include #include -#include "../../Logger/ScreenLogger/ScreenLogger.h" -#include "../../Logger/TextLogger/TextLogger.h" +#include "../../Logger/DoubleLogger/DoubleLogger.h" std::vector bertof::Generator::generatePinImp(const std::string &s) const { -// throw NotImplementedException(); std::vector result = std::vector(); - Logger *screenLog = new ScreenLogger(); - Logger *fileLog = new TextLogger(); + Logger *logger = new DoubleLogger(); + + logger->logVerbouse("Starting bertofGenerator"); std::string stringPassed = std::string(s); - screenLog->logDebug("String passed:\t" + stringPassed); - fileLog->logDebug("String passed:\t" + stringPassed); + logger->log("String passed:\t" + stringPassed); // Cleaning input string, only hexadecimal characters allowed std::string cleanedString = std::regex_replace(stringPassed, std::regex("[^0-9A-F]:"), ""); - screenLog->logDebug("Cleaned string:\t" + cleanedString); - fileLog->logDebug("Cleaned string:\t" + cleanedString); + logger->logDebug("Cleaned string:\t" + cleanedString); // Check if length is valid if (cleanedString.length() != 12 + 5) { - screenLog->logError("The string passed is not a valid MAC address"); - fileLog->logError("The string passed is not a valid MAC address"); + logger->logError("The string passed is not a valid MAC address"); throw InvalidInputException("The string passed is not a valid MAC address"); } std::string hexValue = std::regex_replace(cleanedString, std::regex(":+"), ""); - screenLog->logDebug("Hex value:\t" + hexValue); + logger->logDebug("Hex value:\t" + hexValue); std::string lastHalfHexValue = hexValue.substr(6, 6); - screenLog->logDebug("Last half hex value:\t" + lastHalfHexValue); + logger->logDebug("Last half hex value:\t" + lastHalfHexValue); unsigned long intValue = std::stoul(lastHalfHexValue, nullptr, 16); - screenLog->logDebug("Int value:\t" + std::to_string(intValue)); + logger->logDebug("Int value:\t" + std::to_string(intValue)); unsigned long zaoModeIntValue = intValue % 10000000; - screenLog->logDebug("Zao Mode integer value=\t" + std::to_string(zaoModeIntValue)); + logger->logDebug("Zao Mode integer value=\t" + std::to_string(zaoModeIntValue)); unsigned long first7Numbers = zaoModeIntValue / std::pow(10, std::ceil(std::log10(zaoModeIntValue)) - 7); - screenLog->logDebug("First 7 numbers of the pin:\t" + std::to_string(first7Numbers)); + logger->logDebug("First 7 numbers of the pin:\t" + std::to_string(first7Numbers)); unsigned long pin = 10 * first7Numbers; unsigned long accum = 0; @@ -77,13 +73,14 @@ std::vector bertof::Generator::generatePinImp(const std::string &s) const { checksum = (10 - digit) % 10; - screenLog->logDebug("Accum:\t" + std::to_string(accum)); - screenLog->logDebug("Digit:\t" + std::to_string(digit)); - screenLog->logDebug("Checksum:\t" + std::to_string(checksum)); + logger->logDebug("Accum:\t" + std::to_string(accum)); + logger->logDebug("Digit:\t" + std::to_string(digit)); + logger->logDebug("Checksum:\t" + std::to_string(checksum)); Pin firstResult = Pin(pin + checksum); - screenLog->logDebug("First result:\t" + firstResult.toString()); + logger->logVerbouse("End of bertofGenerator"); + logger->logVerbouse("First result:\t" + firstResult.toString()); result.push_back(firstResult); diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp new file mode 100644 index 0000000..df30048 --- /dev/null +++ b/InputHandling/InputHandler.cpp @@ -0,0 +1,53 @@ +// +// Created by pily on 01/03/17. +// + +#include "InputHandler.h" +#include "../Logger/Logger.h" +#include "../Logger/TextLogger/TextLogger.h" +#include "../Logger/DoubleLogger/DoubleLogger.h" +#include "../Exceptions/InvalidInputException.h" + +#include + +void InputHandler::handle(int argc, char **argv) { + + Logger *logger = new DoubleLogger(); + + // Get arguments in c++ style string array + auto args = std::vector(); + for (int i = 0; i < argc; ++i) { + args.push_back(argv[i]); + } + + // Loop starts from 1 as args[0] contains the executable path + for (int j = 1; j < args.size(); ++j) { + + // Activate verbouse mode + if (args[j] == "--verbouse" || args[j] == "-v") { + Logger::setVerbouseLogActive(true); + } + + // Activate debug mode + else if (args[j] == "--debug") { + Logger::setDebugLogActive(true); + } + + // Activate write on log file + else if (args[j] == "--logfile") { + TextLogger::setWriteOnLogFileActive(true); + + // If a path is passed nex uses it as the log file path + if (j + 1 < args.size() && !std::regex_match(args[j + 1], std::regex("^/-"))) { + TextLogger::setFilePath(args[j + 1]); + ++j; + } + } + + // Case the argument is invalid + else { + logger->logError("Argument passed is invalid:\t" + args[j]); + throw InvalidInputException("Argument passed is invalid:\t\"" + args[j] + "\""); + } + } +} \ No newline at end of file diff --git a/InputHandling/InputHandler.h b/InputHandling/InputHandler.h new file mode 100644 index 0000000..1cdbf14 --- /dev/null +++ b/InputHandling/InputHandler.h @@ -0,0 +1,17 @@ +// +// Created by pily on 01/03/17. +// + +#ifndef WPS_PIN_GENERATOR_INPUTHANDLER_H +#define WPS_PIN_GENERATOR_INPUTHANDLER_H + +#include +#include + +class InputHandler { +public: + static void handle(int argc, char *argv[]); +}; + + +#endif //WPS_PIN_GENERATOR_INPUTHANDLER_H diff --git a/Logger/DoubleLogger/DoubleLogger.cpp b/Logger/DoubleLogger/DoubleLogger.cpp index 93ff0b1..7610c78 100644 --- a/Logger/DoubleLogger/DoubleLogger.cpp +++ b/Logger/DoubleLogger/DoubleLogger.cpp @@ -4,7 +4,7 @@ #include "DoubleLogger.h" -DoubleLogger::DoubleLogger(const std::string &filePath) : TextLogger(filePath) {} +DoubleLogger::DoubleLogger() {} void DoubleLogger::log(const std::string &string) const { ScreenLogger::log(string); diff --git a/Logger/DoubleLogger/DoubleLogger.h b/Logger/DoubleLogger/DoubleLogger.h index 1a6a796..5b5d69a 100644 --- a/Logger/DoubleLogger/DoubleLogger.h +++ b/Logger/DoubleLogger/DoubleLogger.h @@ -9,10 +9,10 @@ #include "../ScreenLogger/ScreenLogger.h" #include "../TextLogger/TextLogger.h" -class DoubleLogger : public ScreenLogger, public TextLogger { +class DoubleLogger : virtual ScreenLogger, public TextLogger { public: - DoubleLogger(const std::string &filePath = "log.txt"); + DoubleLogger(); ~DoubleLogger() override {}; diff --git a/Logger/ScreenLogger/ScreenLogger.h b/Logger/ScreenLogger/ScreenLogger.h index b92cd2b..3148582 100644 --- a/Logger/ScreenLogger/ScreenLogger.h +++ b/Logger/ScreenLogger/ScreenLogger.h @@ -10,7 +10,7 @@ #include #include -class ScreenLogger : public Logger { +class ScreenLogger : virtual public Logger { public: virtual ~ScreenLogger() {} diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp index 4439d20..56b6a15 100644 --- a/Logger/TextLogger/TextLogger.cpp +++ b/Logger/TextLogger/TextLogger.cpp @@ -8,6 +8,7 @@ #include bool TextLogger::writeOnLogFileActive = false; +std::string TextLogger::filePath = "Log.txt"; /** Logs a message in the log file * Logs the message passed in the log file @@ -39,7 +40,7 @@ void TextLogger::logDebug(const std::string &messageDebug) const { * Checks if the file path chosen is safely portable or not * @param filePath */ -TextLogger::TextLogger(const std::string &filePath) : filePath(filePath) { +TextLogger::TextLogger() { if (!boost::filesystem::portable_name(filePath)) { ScreenLogger l; l.logError("File path not portable. It may create problems."); @@ -78,4 +79,12 @@ void TextLogger::setWriteOnLogFileActive(bool writeOnLogFileActive) { TextLogger::writeOnLogFileActive = writeOnLogFileActive; } +const std::string &TextLogger::getFilePath() { + return filePath; +} + +void TextLogger::setFilePath(const std::string &filePath) { + TextLogger::filePath = filePath; +} + diff --git a/Logger/TextLogger/TextLogger.h b/Logger/TextLogger/TextLogger.h index 32b13d0..26ee5d2 100644 --- a/Logger/TextLogger/TextLogger.h +++ b/Logger/TextLogger/TextLogger.h @@ -7,9 +7,9 @@ #include "../Logger.h" -class TextLogger : public Logger { +class TextLogger : virtual public Logger { public: - TextLogger(const std::string &filePath = "log.txt"); + TextLogger(); virtual ~TextLogger() {} @@ -24,12 +24,19 @@ class TextLogger : public Logger { protected: virtual void logText(const std::string &message) const; - const std::string filePath; - private: /*** Controls if any TextLogger should write on a file */ static bool writeOnLogFileActive; + + /*** File path of the log file + */ + static std::string filePath; +public: + static const std::string &getFilePath(); + + static void setFilePath(const std::string &filePath); + public: /*** Getter for writeOnLogFileActive * @return writeOnLogFileActive status diff --git a/main.cpp b/main.cpp index aae234a..63c27e8 100644 --- a/main.cpp +++ b/main.cpp @@ -1,20 +1,18 @@ #include #include "Graphics/SplashScreen/SplashScreen.h" +#include "InputHandling/InputHandler.h" #include "Generator/bertofGenerator/Generator.h" #include "Logger/ScreenLogger/ScreenLogger.h" #include "Logger/DoubleLogger/DoubleLogger.h" -int main() { +int main(int argc, char *argv[]) { //TODO better spash screen - //TODO input handling + //TODO better input handling //TODO guide on how to use it (--help) - //TODO test enabled debug - DoubleLogger::setVerbouseLogActive(true); - DoubleLogger::setDebugLogActive(true); - DoubleLogger::setWriteOnLogFileActive(true); + InputHandler::handle(argc, argv); SplashScreen::printSplashScreen(); From 367083c5c0bfb7a632b2d2f569b4d543df708fb0 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 4 Mar 2017 08:43:12 +0100 Subject: [PATCH 27/37] Help screen --- CMakeLists.txt | 2 +- Graphics/HelpScreen/HelpScreen.cpp | 10 ++++++++++ Graphics/HelpScreen/HelpScreen.h | 16 ++++++++++++++++ Graphics/SplashScreen/SplashScreen.cpp | 7 ------- Graphics/SplashScreen/SplashScreen.h | 1 - InputHandling/InputHandler.cpp | 8 ++++++++ main.cpp | 3 +-- 7 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 Graphics/HelpScreen/HelpScreen.cpp create mode 100644 Graphics/HelpScreen/HelpScreen.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b393197..088ec60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Boost 1.58.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) - set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h) + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h Graphics/HelpScreen/HelpScreen.cpp Graphics/HelpScreen/HelpScreen.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) diff --git a/Graphics/HelpScreen/HelpScreen.cpp b/Graphics/HelpScreen/HelpScreen.cpp new file mode 100644 index 0000000..43b13d6 --- /dev/null +++ b/Graphics/HelpScreen/HelpScreen.cpp @@ -0,0 +1,10 @@ +// +// Created by pily on 03/03/17. +// + +#include "HelpScreen.h" +#include "../SplashScreen/SplashScreen.h" + +std::string HelpScreen::getHelpScreen() { + return ""; +} diff --git a/Graphics/HelpScreen/HelpScreen.h b/Graphics/HelpScreen/HelpScreen.h new file mode 100644 index 0000000..6af248e --- /dev/null +++ b/Graphics/HelpScreen/HelpScreen.h @@ -0,0 +1,16 @@ +// +// Created by pily on 03/03/17. +// + +#ifndef WPS_PIN_GENERATOR_HELPSCREEN_H +#define WPS_PIN_GENERATOR_HELPSCREEN_H + +#include + +class HelpScreen { +public: + static std::string getHelpScreen(); +}; + + +#endif //WPS_PIN_GENERATOR_HELPSCREEN_H diff --git a/Graphics/SplashScreen/SplashScreen.cpp b/Graphics/SplashScreen/SplashScreen.cpp index 063de27..29a8e5b 100644 --- a/Graphics/SplashScreen/SplashScreen.cpp +++ b/Graphics/SplashScreen/SplashScreen.cpp @@ -4,13 +4,6 @@ #include "SplashScreen.h" #include -#include "../../Logger/DoubleLogger/DoubleLogger.h" - -void SplashScreen::printSplashScreen() { - DoubleLogger logger; - logger.logDebug("Splash screen"); - std::cout << SplashScreen::getSplashScreen() << std::endl; -} std::string SplashScreen::getSplashScreen() { return "WPS Pin Generator - by Bertof"; diff --git a/Graphics/SplashScreen/SplashScreen.h b/Graphics/SplashScreen/SplashScreen.h index 1da4723..610a296 100644 --- a/Graphics/SplashScreen/SplashScreen.h +++ b/Graphics/SplashScreen/SplashScreen.h @@ -10,7 +10,6 @@ class SplashScreen { public: static void printSplashScreen(); -private: static std::string getSplashScreen(); }; diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index df30048..0fa4f08 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -7,8 +7,10 @@ #include "../Logger/TextLogger/TextLogger.h" #include "../Logger/DoubleLogger/DoubleLogger.h" #include "../Exceptions/InvalidInputException.h" +#include "../Graphics/HelpScreen/HelpScreen.h" #include +#include void InputHandler::handle(int argc, char **argv) { @@ -44,10 +46,16 @@ void InputHandler::handle(int argc, char **argv) { } } + // Help dialog + else if (args[j] == "--help" || args[j] == "-h") { + std::cout << HelpScreen::getHelpScreen() << std::endl; + } + // Case the argument is invalid else { logger->logError("Argument passed is invalid:\t" + args[j]); throw InvalidInputException("Argument passed is invalid:\t\"" + args[j] + "\""); } } + } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 63c27e8..077a6ef 100644 --- a/main.cpp +++ b/main.cpp @@ -11,10 +11,9 @@ int main(int argc, char *argv[]) { //TODO better spash screen //TODO better input handling //TODO guide on how to use it (--help) - InputHandler::handle(argc, argv); - SplashScreen::printSplashScreen(); + std::cout << SplashScreen::getSplashScreen() << std::endl; GeneratorInterface *g = new bertof::Generator(); From cf9b5a49670d9f6ea18d323803f29076f50a66ed Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Sat, 4 Mar 2017 08:59:51 +0100 Subject: [PATCH 28/37] Fixed order and added prompts --- main.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 077a6ef..02c8220 100644 --- a/main.cpp +++ b/main.cpp @@ -11,14 +11,24 @@ int main(int argc, char *argv[]) { //TODO better spash screen //TODO better input handling //TODO guide on how to use it (--help) - InputHandler::handle(argc, argv); + // Spash screen std::cout << SplashScreen::getSplashScreen() << std::endl; + // Handle input + InputHandler::handle(argc, argv); + + // Choose the generator GeneratorInterface *g = new bertof::Generator(); + // Generate pins g->generatePin("7C:5C:F8:F6:D0:B5"); + // TODO show the pins + std::cout << "Solution:\t" << std::endl; + + + // END std::cout << "END MAIN" << std::endl; return 0; From d8a10216badd51b6ddaa6ad2b122d6f2a3ed0881 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Mon, 6 Mar 2017 08:23:57 +0100 Subject: [PATCH 29/37] Better solution output --- main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 02c8220..251c50b 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include "InputHandling/InputHandler.h" #include "Generator/bertofGenerator/Generator.h" + #include "Logger/ScreenLogger/ScreenLogger.h" #include "Logger/DoubleLogger/DoubleLogger.h" @@ -22,10 +23,13 @@ int main(int argc, char *argv[]) { GeneratorInterface *g = new bertof::Generator(); // Generate pins - g->generatePin("7C:5C:F8:F6:D0:B5"); + std::vector solution = g->generatePin("7C:5C:F8:F6:D0:B5"); // TODO show the pins std::cout << "Solution:\t" << std::endl; + for (int i = 0; i < solution.size(); ++i) { + std::cout << i + 1 << ":\t" << solution[i].toString() << std::endl; + } // END From 73c3ae4861c39f8ad09c8da0560ab9e972015de0 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Thu, 9 Mar 2017 17:58:25 +0100 Subject: [PATCH 30/37] WIP: moved most logic in inputHandler --- CMakeLists.txt | 2 +- Graphics/HelpScreen/HelpScreen.cpp | 11 +++- Graphics/Info/Info.cpp | 17 ++++++ Graphics/Info/Info.h | 18 ++++++ InputHandling/InputHandler.cpp | 98 ++++++++++++++++++++---------- InputHandling/InputHandler.h | 7 +++ LICENSE.md | 4 +- main.cpp | 14 +---- 8 files changed, 122 insertions(+), 49 deletions(-) create mode 100644 Graphics/Info/Info.cpp create mode 100644 Graphics/Info/Info.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 088ec60..3cd5917 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Boost 1.58.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) - set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h Graphics/HelpScreen/HelpScreen.cpp Graphics/HelpScreen/HelpScreen.h) + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h Graphics/HelpScreen/HelpScreen.cpp Graphics/HelpScreen/HelpScreen.h Graphics/Info/Info.cpp Graphics/Info/Info.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) diff --git a/Graphics/HelpScreen/HelpScreen.cpp b/Graphics/HelpScreen/HelpScreen.cpp index 43b13d6..251d24e 100644 --- a/Graphics/HelpScreen/HelpScreen.cpp +++ b/Graphics/HelpScreen/HelpScreen.cpp @@ -5,6 +5,15 @@ #include "HelpScreen.h" #include "../SplashScreen/SplashScreen.h" +#include "../Info/Info.h" + std::string HelpScreen::getHelpScreen() { - return ""; + return Info::getProjectName() + " " + Info::getVersion() + "\n" + + "Usage: " + Info::getExecutableName() + " [options]\n" + + "OPTIONS:\n" + + "-b, --bssid \t\tInput target BSSID\n" + + "-h, --help\t\t\t\tPrint this help page\n" + + "--debug\t\t\t\t\tPrint all th debug info" + + "-v, --verbouse\tLog is more verbouse\n" + + "--logfile [log.txt]\t\tEnables log to file (default log.txt)\n"; } diff --git a/Graphics/Info/Info.cpp b/Graphics/Info/Info.cpp new file mode 100644 index 0000000..7cec957 --- /dev/null +++ b/Graphics/Info/Info.cpp @@ -0,0 +1,17 @@ +// +// Created by pily on 06/03/17. +// + +#include "Info.h" + +std::string Info::getVersion() { + return "0.0.1"; +} + +std::string Info::getProjectName() { + return "WPS-pin-generator"; +} + +std::string Info::getExecutableName() { + return "WPS_pin_generator"; +} diff --git a/Graphics/Info/Info.h b/Graphics/Info/Info.h new file mode 100644 index 0000000..7f917d5 --- /dev/null +++ b/Graphics/Info/Info.h @@ -0,0 +1,18 @@ +// +// Created by pily on 06/03/17. +// + +#ifndef WPS_PIN_GENERATOR_VERSION_H +#define WPS_PIN_GENERATOR_VERSION_H + +#include + +class Info { +public: + static std::string getVersion(); + static std::string getProjectName(); + static std::string getExecutableName(); +}; + + +#endif //WPS_PIN_GENERATOR_VERSION_H diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index 0fa4f08..c358a5f 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -12,50 +12,84 @@ #include #include +std::string InputHandler::bssidInput = ""; + void InputHandler::handle(int argc, char **argv) { - Logger *logger = new DoubleLogger(); + try { - // Get arguments in c++ style string array - auto args = std::vector(); - for (int i = 0; i < argc; ++i) { - args.push_back(argv[i]); - } + std::unique_ptr logger(new DoubleLogger()); - // Loop starts from 1 as args[0] contains the executable path - for (int j = 1; j < args.size(); ++j) { +// Logger *logger = new DoubleLogger(); - // Activate verbouse mode - if (args[j] == "--verbouse" || args[j] == "-v") { - Logger::setVerbouseLogActive(true); + // Get arguments in c++ style string array + auto args = std::vector(); + for (int i = 0; i < argc; ++i) { + args.push_back(argv[i]); } - // Activate debug mode - else if (args[j] == "--debug") { - Logger::setDebugLogActive(true); - } + // Loop starts from 1 as args[0] contains the executable path + for (int j = 1; j < args.size(); ++j) { - // Activate write on log file - else if (args[j] == "--logfile") { - TextLogger::setWriteOnLogFileActive(true); + // SWITCHES - // If a path is passed nex uses it as the log file path - if (j + 1 < args.size() && !std::regex_match(args[j + 1], std::regex("^/-"))) { - TextLogger::setFilePath(args[j + 1]); - ++j; + // Activate verbouse mode + if (args[j] == "--verbouse" || args[j] == "-v") { + Logger::setVerbouseLogActive(true); } - } - // Help dialog - else if (args[j] == "--help" || args[j] == "-h") { - std::cout << HelpScreen::getHelpScreen() << std::endl; - } + // Activate debug mode + else if (args[j] == "--debug") { + Logger::setDebugLogActive(true); + } + + // Activate write on log file + else if (args[j] == "--logfile") { + TextLogger::setWriteOnLogFileActive(true); + + // If a path is passed nex uses it as the log file path + if (j + 1 < args.size() && !std::regex_match(args[j + 1], std::regex("^/-"))) { + TextLogger::setFilePath(args[j + 1]); + ++j; + } + } - // Case the argument is invalid - else { - logger->logError("Argument passed is invalid:\t" + args[j]); - throw InvalidInputException("Argument passed is invalid:\t\"" + args[j] + "\""); + // ARGUMENTS + + // BSSID input + else if (args[j] == "-b" || args[j] == "--bssid") { + // Listen for a BSSID as next argument + if (j + 1 < args.size() && + std::regex_match(args[j + 1], std::regex("^([0-9A-F][0-9A-F]:){5}[0-9A-F][0-9A-F]$"))) { + InputHandler::setBssidInput(args[j + 1]); + ++j; + } else throw InvalidInputException(args[j + 1]); + } + + // Help dialog + else if (args[j] == "--help" || args[j] == "-h") { + std::cout << HelpScreen::getHelpScreen() << std::endl; + break; + } + + // Case the argument is invalid + else { + throw InvalidInputException(args[j]); + } } + + } catch (InvalidInputException e) { + Logger *logger = new DoubleLogger(); + logger->logError("Argument passed is invalid:\t" + std::string(e.what())); + throw InvalidInputException("Argument passed is invalid:\t\"" + std::string(e.what()) + "\""); } -} \ No newline at end of file +} + +const std::string &InputHandler::getBssidInput() { + return bssidInput; +} + +void InputHandler::setBssidInput(const std::string &bssidInput) { + InputHandler::bssidInput = bssidInput; +} diff --git a/InputHandling/InputHandler.h b/InputHandling/InputHandler.h index 1cdbf14..f8e4695 100644 --- a/InputHandling/InputHandler.h +++ b/InputHandling/InputHandler.h @@ -11,6 +11,13 @@ class InputHandler { public: static void handle(int argc, char *argv[]); + +private: + static std::string bssidInput; +public: + static const std::string &getBssidInput(); + + static void setBssidInput(const std::string &bssidInput); }; diff --git a/LICENSE.md b/LICENSE.md index 8dada3e..c7884c2 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,5 +1,5 @@ Apache License - Version 2.0, January 2004 + Info 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION @@ -188,7 +188,7 @@ Copyright {yyyy} {name of copyright owner} - Licensed under the Apache License, Version 2.0 (the "License"); + Licensed under the Apache License, Info 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at diff --git a/main.cpp b/main.cpp index 251c50b..48beb58 100644 --- a/main.cpp +++ b/main.cpp @@ -12,6 +12,7 @@ int main(int argc, char *argv[]) { //TODO better spash screen //TODO better input handling //TODO guide on how to use it (--help) + //TODO switch to smart pointers // Spash screen std::cout << SplashScreen::getSplashScreen() << std::endl; @@ -19,19 +20,6 @@ int main(int argc, char *argv[]) { // Handle input InputHandler::handle(argc, argv); - // Choose the generator - GeneratorInterface *g = new bertof::Generator(); - - // Generate pins - std::vector solution = g->generatePin("7C:5C:F8:F6:D0:B5"); - - // TODO show the pins - std::cout << "Solution:\t" << std::endl; - for (int i = 0; i < solution.size(); ++i) { - std::cout << i + 1 << ":\t" << solution[i].toString() << std::endl; - } - - // END std::cout << "END MAIN" << std::endl; From 850676667d1d40a3872860e08a2c3cf7a259e0d2 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Fri, 10 Mar 2017 13:41:30 +0100 Subject: [PATCH 31/37] Better input handling and rework of loggers using smart pointers --- InputHandling/InputHandler.cpp | 127 ++++++++++++++++----------- InputHandling/InputHandler.h | 14 ++- Logger/DoubleLogger/DoubleLogger.cpp | 31 +++---- Logger/DoubleLogger/DoubleLogger.h | 21 +++-- Logger/Logger.cpp | 2 +- Logger/Logger.h | 30 ++++++- Logger/ScreenLogger/ScreenLogger.cpp | 40 ++++++--- Logger/ScreenLogger/ScreenLogger.h | 34 ++++++- Logger/TextLogger/TextLogger.cpp | 84 ++++++++++-------- Logger/TextLogger/TextLogger.h | 114 ++++++++++++++++-------- main.cpp | 23 ++--- 11 files changed, 323 insertions(+), 197 deletions(-) diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index c358a5f..14a0fc7 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -9,6 +9,9 @@ #include "../Exceptions/InvalidInputException.h" #include "../Graphics/HelpScreen/HelpScreen.h" +#include "../Generator/GeneratorInerface.h" +#include "../Generator/bertofGenerator/Generator.h" + #include #include @@ -16,80 +19,100 @@ std::string InputHandler::bssidInput = ""; void InputHandler::handle(int argc, char **argv) { - try { + try { - std::unique_ptr logger(new DoubleLogger()); + std::shared_ptr logger(new DoubleLogger); // Logger *logger = new DoubleLogger(); - // Get arguments in c++ style string array - auto args = std::vector(); - for (int i = 0; i < argc; ++i) { - args.push_back(argv[i]); - } - - // Loop starts from 1 as args[0] contains the executable path - for (int j = 1; j < args.size(); ++j) { + // Get arguments in c++ style string array + std::vector args; - // SWITCHES + for (int i = 0; i < argc; ++i) { + args.push_back(std::string(argv[i])); + } - // Activate verbouse mode - if (args[j] == "--verbouse" || args[j] == "-v") { - Logger::setVerbouseLogActive(true); - } + // Loop starts from 1 as args[0] contains the executable path + for (int j = 1; j < args.size(); ++j) { - // Activate debug mode - else if (args[j] == "--debug") { - Logger::setDebugLogActive(true); - } + // SWITCHES - // Activate write on log file - else if (args[j] == "--logfile") { - TextLogger::setWriteOnLogFileActive(true); + // Activate verbouse mode + if (args[j] == "--verbouse" || args[j] == "-v") { + Logger::setVerbouseLogActive(true); + } - // If a path is passed nex uses it as the log file path - if (j + 1 < args.size() && !std::regex_match(args[j + 1], std::regex("^/-"))) { - TextLogger::setFilePath(args[j + 1]); - ++j; - } - } + // Activate debug mode + else if (args[j] == "--debug") { + Logger::setDebugLogActive(true); + } - // ARGUMENTS + // Activate write on log file + else if (args[j] == "--logfile") { + TextLogger::setWriteOnFileActive(true); - // BSSID input - else if (args[j] == "-b" || args[j] == "--bssid") { - // Listen for a BSSID as next argument - if (j + 1 < args.size() && - std::regex_match(args[j + 1], std::regex("^([0-9A-F][0-9A-F]:){5}[0-9A-F][0-9A-F]$"))) { - InputHandler::setBssidInput(args[j + 1]); - ++j; - } else throw InvalidInputException(args[j + 1]); + // If a path is passed nex uses it as the log file path + if (j + 1 < args.size() && !std::regex_match(args[j + 1], std::regex("^/-"))) { + TextLogger::getTextLogger(args[j + 1]); + ++j; + } + } + + // ARGUMENTS + + // Help dialog + else if (args[j] == "--help" || args[j] == "-h") { + std::cout << HelpScreen::getHelpScreen() << std::endl; + break; + } + + // Pin generator + else if (args[j] == "-b" || args[j] == "--bssid") { + // Listen for a BSSID as next argument + ++j; + while (j < args.size() && + std::regex_match(args[j], std::regex("^([0-9A-F][0-9A-F]:){5}[0-9A-F][0-9A-F]$"))) { + + for (int k = j + 2; k < args.size(); ++k) { + + // Pin using generator + if (args[k] == "-g" || args[k] == "--generator") { + std::cout << "Results from generator:" << std::endl; + std::shared_ptr generator(new bertof::Generator); + std::vector results = generator->generatePin(args[j]); + for (int l = 0; l < results.size(); ++l) { + std::cout << l << ":\t" << results[l].toString() << std::endl; + } + std::cout << std::endl; } - // Help dialog - else if (args[j] == "--help" || args[j] == "-h") { - std::cout << HelpScreen::getHelpScreen() << std::endl; - break; - } + //TODO Pin from DB using BSSID - // Case the argument is invalid - else { - throw InvalidInputException(args[j]); - } + //TODO Pin from DB using ASSID + } + ++j; } + } + - } catch (InvalidInputException e) { - Logger *logger = new DoubleLogger(); - logger->logError("Argument passed is invalid:\t" + std::string(e.what())); - throw InvalidInputException("Argument passed is invalid:\t\"" + std::string(e.what()) + "\""); + // Case the argument is invalid + else { + throw InvalidInputException(args[j]); + } } + } catch (InvalidInputException e) { + Logger *logger = new DoubleLogger(); + logger->logError("Argument passed is invalid:\t" + std::string(e.what())); + throw InvalidInputException("Argument passed is invalid:\t\"" + std::string(e.what()) + "\""); + } + } const std::string &InputHandler::getBssidInput() { - return bssidInput; + return bssidInput; } void InputHandler::setBssidInput(const std::string &bssidInput) { - InputHandler::bssidInput = bssidInput; + InputHandler::bssidInput = bssidInput; } diff --git a/InputHandling/InputHandler.h b/InputHandling/InputHandler.h index f8e4695..0762c00 100644 --- a/InputHandling/InputHandler.h +++ b/InputHandling/InputHandler.h @@ -9,16 +9,14 @@ #include class InputHandler { -public: - static void handle(int argc, char *argv[]); + private: + static std::string bssidInput; + public: + static void handle(int argc, char *argv[]); -private: - static std::string bssidInput; -public: - static const std::string &getBssidInput(); + static const std::string &getBssidInput(); - static void setBssidInput(const std::string &bssidInput); + static void setBssidInput(const std::string &bssidInput); }; - #endif //WPS_PIN_GENERATOR_INPUTHANDLER_H diff --git a/Logger/DoubleLogger/DoubleLogger.cpp b/Logger/DoubleLogger/DoubleLogger.cpp index 7610c78..b041f7f 100644 --- a/Logger/DoubleLogger/DoubleLogger.cpp +++ b/Logger/DoubleLogger/DoubleLogger.cpp @@ -4,24 +4,21 @@ #include "DoubleLogger.h" -DoubleLogger::DoubleLogger() {} - -void DoubleLogger::log(const std::string &string) const { - ScreenLogger::log(string); - TextLogger::log(string); +DoubleLogger::DoubleLogger(const std::string &filePath) + : sLogger(ScreenLogger::getScreenLogger()), tLogger(TextLogger::getTextLogger(filePath)) {} +void DoubleLogger::log(const std::string &message) const { + sLogger->log(message); + tLogger->log(message); } - -void DoubleLogger::logError(const std::string &string) const { - ScreenLogger::logError(string); - TextLogger::logError(string); +void DoubleLogger::logError(const std::string &errorMessage) const { + sLogger->logError(errorMessage); + tLogger->logError(errorMessage); } - -void DoubleLogger::logDebug(const std::string &string) const { - ScreenLogger::logDebug(string); - TextLogger::logDebug(string); +void DoubleLogger::logDebug(const std::string &debugMessage) const { + sLogger->logDebug(debugMessage); + tLogger->logDebug(debugMessage); } - -void DoubleLogger::logVerbouse(const std::string &string) const { - ScreenLogger::logVerbouse(string); - TextLogger::logVerbouse(string); +void DoubleLogger::logVerbouse(const std::string &verbouseMessage) const { + sLogger->logVerbouse(verbouseMessage); + tLogger->logVerbouse(verbouseMessage); } diff --git a/Logger/DoubleLogger/DoubleLogger.h b/Logger/DoubleLogger/DoubleLogger.h index 5b5d69a..6fd9198 100644 --- a/Logger/DoubleLogger/DoubleLogger.h +++ b/Logger/DoubleLogger/DoubleLogger.h @@ -9,21 +9,24 @@ #include "../ScreenLogger/ScreenLogger.h" #include "../TextLogger/TextLogger.h" -class DoubleLogger : virtual ScreenLogger, public TextLogger { -public: +class DoubleLogger : public Logger { + protected: + const std::shared_ptr sLogger; + const std::shared_ptr tLogger; - DoubleLogger(); + public: - ~DoubleLogger() override {}; + DoubleLogger(const std::string &filePath = TextLogger::DEFAULT_FILE_PATH); - void log(const std::string &string) const override; + ~DoubleLogger() override {}; - void logError(const std::string &string) const override; + void log(const std::string &message) const override; - void logDebug(const std::string &string) const override; + void logError(const std::string &errorMessage) const override; - void logVerbouse(const std::string &messageLogVerbouse) const override; -}; + void logDebug(const std::string &debugMessage) const override; + void logVerbouse(const std::string &verbouseMessage) const override; +}; #endif //WPS_PIN_GENERATOR_DOUBLELOGGER_H diff --git a/Logger/Logger.cpp b/Logger/Logger.cpp index 7b0606b..ae15e90 100644 --- a/Logger/Logger.cpp +++ b/Logger/Logger.cpp @@ -22,4 +22,4 @@ bool Logger::isVerbouseLogActive() { void Logger::setVerbouseLogActive(bool verbouseLogActive) { Logger::verbouseLogActive = verbouseLogActive; -} +} \ No newline at end of file diff --git a/Logger/Logger.h b/Logger/Logger.h index bae0064..1287a1a 100644 --- a/Logger/Logger.h +++ b/Logger/Logger.h @@ -6,30 +6,52 @@ #define WPS_PIN_GENERATOR_LOGGER_H #include +#include /** Plain text logger */ class Logger { +private: + /*** Debug log status switch + */ + static bool debugLogActive; + + /*** Verbouse log status switch + */ + static bool verbouseLogActive; public: + + // Log commands + + /*** Log message + * @param messageLog + */ virtual void log(const std::string &messageLog) const = 0; + /*** Log only in verbouse mode + * @param messageLogVerbouse + */ virtual void logVerbouse(const std::string &messageLogVerbouse) const = 0; + /*** Log only in debug mode + * @param messageDebug + */ virtual void logDebug(const std::string &messageDebug) const = 0; + /*** Log error message + */ virtual void logError(const std::string &messageError) const = 0; + // Virtual destructor virtual ~Logger() {} + // Debug mode getter and setter static bool isDebugLogActive(); static void setDebugLogActive(bool debugLogActive); -private: - static bool debugLogActive; - static bool verbouseLogActive; -public: + // Verbouse mode getter ad setter static bool isVerbouseLogActive(); static void setVerbouseLogActive(bool verbouseLogActive); diff --git a/Logger/ScreenLogger/ScreenLogger.cpp b/Logger/ScreenLogger/ScreenLogger.cpp index e2ad838..918fe4d 100644 --- a/Logger/ScreenLogger/ScreenLogger.cpp +++ b/Logger/ScreenLogger/ScreenLogger.cpp @@ -7,27 +7,39 @@ #include #include -void ScreenLogger::log(const std::string &string) const { - std::clog << rang::style::reset << string << std::endl; +std::shared_ptr ScreenLogger::mainScreenLoggerPtr = nullptr; + +void ScreenLogger::log(const std::string &message) const { + std::clog << rang::style::reset << message << std::endl; } -void ScreenLogger::logError(const std::string &string) const { - std::clog << rang::fg::red << "ERROR" << rang::style::reset << ":\t" << rang::fg::red - << string - << rang::style::reset << std::endl; +void ScreenLogger::logError(const std::string &errorMessage) const { + std::clog << rang::fg::red << "ERROR" << rang::style::reset << ":\t" << rang::fg::red + << errorMessage + << rang::style::reset << std::endl; } void ScreenLogger::logDebug(const std::string &string) const { - if (Logger::isDebugLogActive()) { - std::clog << rang::fg::cyan << "DBG" << rang::style::reset << ":\t" << string - << std::endl; - } + if (Logger::isDebugLogActive()) { + std::clog << rang::fg::cyan << "DBG" << rang::style::reset << ":\t" << string + << std::endl; + } } void ScreenLogger::logVerbouse(const std::string &string) const { - if (Logger::isVerbouseLogActive()) { - std::clog << rang::fg::cyan << "VERB" << rang::style::reset << ":\t" << string - << std::endl; - } + if (Logger::isVerbouseLogActive()) { + std::clog << rang::fg::cyan << "VERB" << rang::style::reset << ":\t" << string + << std::endl; + } } + +ScreenLogger::ScreenLogger() {} + +std::shared_ptr ScreenLogger::getScreenLogger() { + if (mainScreenLoggerPtr == nullptr) { + mainScreenLoggerPtr.reset(new ScreenLogger); + } + return mainScreenLoggerPtr; +} + diff --git a/Logger/ScreenLogger/ScreenLogger.h b/Logger/ScreenLogger/ScreenLogger.h index 3148582..26ef26e 100644 --- a/Logger/ScreenLogger/ScreenLogger.h +++ b/Logger/ScreenLogger/ScreenLogger.h @@ -9,19 +9,47 @@ #include #include +#include class ScreenLogger : virtual public Logger { +private: + /*** Default constructor + */ + ScreenLogger(); + + /*** Smart pointer to the main ScreenLogger object + * Points a real object only when the factory creates the first + */ + static std::shared_ptr mainScreenLoggerPtr; + public: virtual ~ScreenLogger() {} - virtual void log(const std::string &string) const override; + /*** Log message + * @param message + */ + virtual void log(const std::string &message) const override; - virtual void logError(const std::string &string) const override; + /*** Log error message + * @param errorMessage + */ + virtual void logError(const std::string &errorMessage) const override; - virtual void logDebug(const std::string &string) const override; + /*** Log debug only message + * @param debugMessage + */ + virtual void logDebug(const std::string &debugMessage) const override; + /*** Log verbouse only message + * @param messageLogVerbouse + */ virtual void logVerbouse(const std::string &messageLogVerbouse) const override; + + /*** ScreenLogger singleton factory + * @return ScreenLogger singletor pointer + */ + static std::shared_ptr getScreenLogger(); }; diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp index 56b6a15..522e785 100644 --- a/Logger/TextLogger/TextLogger.cpp +++ b/Logger/TextLogger/TextLogger.cpp @@ -7,84 +7,90 @@ #include #include -bool TextLogger::writeOnLogFileActive = false; -std::string TextLogger::filePath = "Log.txt"; +std::shared_ptr TextLogger::mainTextLoggerPtr = nullptr; + +const std::string TextLogger::DEFAULT_FILE_PATH = "log.txt"; + +bool TextLogger::writeOnFileActive = false; /** Logs a message in the log file * Logs the message passed in the log file * @param message */ void TextLogger::log(const std::string &message) const { - logText(message); + logText(message); } /** Logs an error message * Logs in the log file an error message - * @param messageError + * @param errorMessage */ -void TextLogger::logError(const std::string &messageError) const { - logText("ERROR:\t" + messageError); +void TextLogger::logError(const std::string &errorMessage) const { + logText("ERROR:\t" + errorMessage); } /** Log a debug message * If debugging is enabled loggs the message passed in the log file - * @param messageDebug + * @param debugMessage */ -void TextLogger::logDebug(const std::string &messageDebug) const { - if (Logger::isDebugLogActive()) { - logText("DBG:\t" + messageDebug); - } +void TextLogger::logDebug(const std::string &debugMessage) const { + if (Logger::isDebugLogActive()) { + logText("DBG:\t" + debugMessage); + } } /** Constructor * Checks if the file path chosen is safely portable or not * @param filePath */ -TextLogger::TextLogger() { - if (!boost::filesystem::portable_name(filePath)) { - ScreenLogger l; - l.logError("File path not portable. It may create problems."); - } +TextLogger::TextLogger(const std::string &filePath) : filePath(filePath) { + if (!boost::filesystem::portable_name(filePath)) { + std::shared_ptr l = ScreenLogger::getScreenLogger(); + l->logError("File path not portable. It may create problems."); + } } /** Opens file and appends the message passed * @param message text to write in the log file */ void TextLogger::logText(const std::string &message) const { - if (TextLogger::isWriteOnLogFileActive()) { - boost::filesystem::fstream fileOutput; - fileOutput.open(filePath, std::ofstream::app); - if (!fileOutput.is_open()) { - ScreenLogger l; - l.logError("Can't open the log file (" + filePath + ")"); - } else { - std::string nowString = boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); - fileOutput << nowString << "\t" << message << std::endl; - fileOutput.close(); - } + if (TextLogger::isWriteOnFileActive()) { + boost::filesystem::fstream fileOutput; + fileOutput.open(filePath, std::ofstream::app); + if (!fileOutput.is_open()) { + std::shared_ptr l = ScreenLogger::getScreenLogger(); + l->logError("Can't open the log file (" + filePath + ")"); + } else { + std::string nowString = boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); + fileOutput << nowString << "\t" << message << std::endl; + fileOutput.close(); } + } } -void TextLogger::logVerbouse(const std::string &string) const { - if (Logger::isVerbouseLogActive()) { - logText("VERB:\t" + string); - } +void TextLogger::logVerbouse(const std::string &verbouseMessage) const { + if (Logger::isVerbouseLogActive()) { + logText("VERB:\t" + verbouseMessage); + } } -bool TextLogger::isWriteOnLogFileActive() { - return writeOnLogFileActive; +bool TextLogger::isWriteOnFileActive() { + return writeOnFileActive; } -void TextLogger::setWriteOnLogFileActive(bool writeOnLogFileActive) { - TextLogger::writeOnLogFileActive = writeOnLogFileActive; +void TextLogger::setWriteOnFileActive(bool status) { + TextLogger::writeOnFileActive = status; } -const std::string &TextLogger::getFilePath() { - return filePath; +std::shared_ptr TextLogger::getTextLogger(const std::string &filePath) { + if (mainTextLoggerPtr == nullptr) { + mainTextLoggerPtr.reset(new TextLogger(filePath)); + } + return mainTextLoggerPtr; } -void TextLogger::setFilePath(const std::string &filePath) { - TextLogger::filePath = filePath; +const std::string &TextLogger::getFilePath() const { + return filePath; } diff --git a/Logger/TextLogger/TextLogger.h b/Logger/TextLogger/TextLogger.h index 26ee5d2..fc4ff76 100644 --- a/Logger/TextLogger/TextLogger.h +++ b/Logger/TextLogger/TextLogger.h @@ -8,46 +8,82 @@ #include "../Logger.h" class TextLogger : virtual public Logger { -public: - TextLogger(); - virtual ~TextLogger() {} - - virtual void log(const std::string &message) const override; - - virtual void logError(const std::string &messageError) const override; - - virtual void logDebug(const std::string &messageDebug) const override; - - virtual void logVerbouse(const std::string &messageLogVerbouse) const override; - -protected: - virtual void logText(const std::string &message) const; - -private: - /*** Controls if any TextLogger should write on a file - */ - static bool writeOnLogFileActive; - - /*** File path of the log file - */ - static std::string filePath; -public: - static const std::string &getFilePath(); - - static void setFilePath(const std::string &filePath); - -public: - /*** Getter for writeOnLogFileActive - * @return writeOnLogFileActive status - */ - static bool isWriteOnLogFileActive(); - - /*** Setter for writeOnLogFileActive - * @param writeOnLogFileActive set to true to enable logging on a text file - */ - static void setWriteOnLogFileActive(bool writeOnLogFileActive); + private: + + /*** Generic method to log on file + * @param message + */ + virtual void logText(const std::string &message) const; + + protected: + + /*** Main shared pointer to a TextLogger singleton + */ + static std::shared_ptr mainTextLoggerPtr; + + /*** Controls if any TextLogger should write on a file + */ + static bool writeOnFileActive; + + /*** File path of the log file + */ + const std::string filePath; + + /*** Default constructor + */ + TextLogger(const std::string &logFilePath = DEFAULT_FILE_PATH); + + public: + + /*** Default for filePath + */ + static const std::string DEFAULT_FILE_PATH; + + /*** Virtual default destructor + */ + virtual ~TextLogger() {} + + /*** FilePath getter + * @return + */ + const std::string &getFilePath() const; + + /*** Log standard message + * @param message + */ + virtual void log(const std::string &message) const override; + + /*** Log error message + * @param errorMessage + */ + virtual void logError(const std::string &errorMessage) const override; + + /*** Log debug only message + * @param debugMessage + */ + virtual void logDebug(const std::string &debugMessage) const override; + + /*** Log verbouse only message + * @param verbouseMessage + */ + virtual void logVerbouse(const std::string &verbouseMessage) const override; + + /*** Getter for writeOnLogFileActive + * @return writeOnFileActive status + */ + static bool isWriteOnFileActive(); + + /*** Setter for writeOnLogFileActive + * @param status set to true to enable logging on a text file + */ + static void setWriteOnFileActive(bool status); + + /*** TextLogger singleton pointer factory + * @param filePath + * @return + */ + static std::shared_ptr getTextLogger(const std::string &filePath); }; - #endif //WPS_PIN_GENERATOR_TEXTLOGGER_H diff --git a/main.cpp b/main.cpp index 48beb58..0629109 100644 --- a/main.cpp +++ b/main.cpp @@ -7,21 +7,22 @@ #include "Logger/ScreenLogger/ScreenLogger.h" #include "Logger/DoubleLogger/DoubleLogger.h" +#include "lib/rang/include/rang.hpp" int main(int argc, char *argv[]) { - //TODO better spash screen - //TODO better input handling - //TODO guide on how to use it (--help) - //TODO switch to smart pointers + //TODO better spash screen + //TODO better input handling + //TODO guide on how to use it (--help) + //TODO switch to smart pointers - // Spash screen - std::cout << SplashScreen::getSplashScreen() << std::endl; + // Spash screen + std::cout << SplashScreen::getSplashScreen() << std::endl; - // Handle input - InputHandler::handle(argc, argv); + // Handle input + InputHandler::handle(argc, argv); - // END - std::cout << "END MAIN" << std::endl; + // END + std::cout << "END MAIN" << std::endl; - return 0; + return 0; } \ No newline at end of file From 0a888a0e27e0e56732f40a8c634406e4cfc9cdd7 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Mon, 13 Mar 2017 17:11:50 +0100 Subject: [PATCH 32/37] Finally complete switch to smart pointers for logging --- .idea/codeStyleSettings.xml | 38 ++++- Exceptions/InvalidInputException.h | 1 - Exceptions/NotImplementedException.h | 5 +- Generator/bertofGenerator/Generator.cpp | 4 +- Generator/bertofGenerator/Generator.h | 62 +++---- Graphics/HelpScreen/HelpScreen.cpp | 16 +- Graphics/HelpScreen/HelpScreen.h | 1 - Graphics/Info/Info.cpp | 2 +- Graphics/Info/Info.h | 1 - Graphics/SplashScreen/SplashScreen.h | 1 - InputHandling/InputHandler.cpp | 205 +++++++++++++++--------- InputHandling/InputHandler.h | 12 +- Logger/DoubleLogger/DoubleLogger.cpp | 25 ++- Logger/DoubleLogger/DoubleLogger.h | 25 +-- Logger/Logger.h | 1 - Logger/ScreenLogger/ScreenLogger.cpp | 32 ++-- Logger/ScreenLogger/ScreenLogger.h | 1 - Logger/TextLogger/TextLogger.cpp | 60 +++---- Logger/TextLogger/TextLogger.h | 150 ++++++++--------- Pin/Pin.h | 1 - main.cpp | 26 +-- 21 files changed, 388 insertions(+), 281 deletions(-) diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml index 5e6dd34..1aecc16 100644 --- a/.idea/codeStyleSettings.xml +++ b/.idea/codeStyleSettings.xml @@ -3,6 +3,22 @@ - \ No newline at end of file diff --git a/Exceptions/InvalidInputException.h b/Exceptions/InvalidInputException.h index 1b66a9c..8999cbd 100644 --- a/Exceptions/InvalidInputException.h +++ b/Exceptions/InvalidInputException.h @@ -11,7 +11,6 @@ class InvalidInputException : public std::exception { public: - /** Constructor (C string) * @param message C-style string error message. * The string contents are copied upon construction. diff --git a/Exceptions/NotImplementedException.h b/Exceptions/NotImplementedException.h index edd7fd2..d21e36d 100644 --- a/Exceptions/NotImplementedException.h +++ b/Exceptions/NotImplementedException.h @@ -17,14 +17,14 @@ class NotImplementedException : public std::exception { * with the caller. */ explicit NotImplementedException(const char *message) : - errorMessage_(message) { + errorMessage_(message) { } /** Constructor (C++ STL strings). * @param message The error message. */ explicit NotImplementedException(const std::string &message = "Function not implemented") : - errorMessage_(message) {} + errorMessage_(message) {} /** Destructor. * Virtual to allow for subclassing. @@ -46,5 +46,4 @@ class NotImplementedException : public std::exception { std::string errorMessage_; }; - #endif //WPS_PIN_GENERATOR_NOTIMPLEMENTEDEXCEPTION_H diff --git a/Generator/bertofGenerator/Generator.cpp b/Generator/bertofGenerator/Generator.cpp index 8b2ace3..dddc8dc 100644 --- a/Generator/bertofGenerator/Generator.cpp +++ b/Generator/bertofGenerator/Generator.cpp @@ -17,13 +17,13 @@ std::vector bertof::Generator::generatePinImp(const std::string &s) const { std::vector result = std::vector(); - Logger *logger = new DoubleLogger(); + std::shared_ptr logger(DoubleLogger::getDoubleLogger()); logger->logVerbouse("Starting bertofGenerator"); std::string stringPassed = std::string(s); - logger->log("String passed:\t" + stringPassed); + logger->logDebug("String passed:\t" + stringPassed); // Cleaning input string, only hexadecimal characters allowed std::string cleanedString = std::regex_replace(stringPassed, std::regex("[^0-9A-F]:"), ""); diff --git a/Generator/bertofGenerator/Generator.h b/Generator/bertofGenerator/Generator.h index 3894e73..635beee 100644 --- a/Generator/bertofGenerator/Generator.h +++ b/Generator/bertofGenerator/Generator.h @@ -17,37 +17,37 @@ namespace bertof { * Base class Generator * Used to generate valid Pins */ - class Generator : public GeneratorInterface { - public: - /*** - * Implementation of generatePin - * Returns result of generatePinImp based on the same data passed - * @param s string of data - * @return Pin valid pin generated - */ - virtual std::vector generatePin(const std::string &s) const { - return generatePinImp(s); - } - - /** Method called to return the author of the generator - * @return - */ - std::string author() const override; - - /** Method called to return the version of the generator - * @return - */ - std::string version() const override; - - private: - /*** - * Generates a valid Pin object using an algorithm based on zhaochunsheng's ComputePIN-C83A35 and linkp2p's WPS-PIN - * @param s string of data - * @return Pin valid pin generated - */ - std::vector generatePinImp(const std::string &s) const; - - }; +class Generator : public GeneratorInterface { +public: + /*** + * Implementation of generatePin + * Returns result of generatePinImp based on the same data passed + * @param s string of data + * @return Pin valid pin generated + */ + virtual std::vector generatePin(const std::string &s) const { + return generatePinImp(s); + } + + /** Method called to return the author of the generator + * @return + */ + std::string author() const override; + + /** Method called to return the version of the generator + * @return + */ + std::string version() const override; + +private: + /*** + * Generates a valid Pin object using an algorithm based on zhaochunsheng's ComputePIN-C83A35 and linkp2p's WPS-PIN + * @param s string of data + * @return Pin valid pin generated + */ + std::vector generatePinImp(const std::string &s) const; + +}; } #endif //WPS_PIN_GENERATOR_GENERATOR_H diff --git a/Graphics/HelpScreen/HelpScreen.cpp b/Graphics/HelpScreen/HelpScreen.cpp index 251d24e..8b786e2 100644 --- a/Graphics/HelpScreen/HelpScreen.cpp +++ b/Graphics/HelpScreen/HelpScreen.cpp @@ -8,12 +8,12 @@ #include "../Info/Info.h" std::string HelpScreen::getHelpScreen() { - return Info::getProjectName() + " " + Info::getVersion() + "\n" + - "Usage: " + Info::getExecutableName() + " [options]\n" + - "OPTIONS:\n" + - "-b, --bssid \t\tInput target BSSID\n" + - "-h, --help\t\t\t\tPrint this help page\n" + - "--debug\t\t\t\t\tPrint all th debug info" + - "-v, --verbouse\tLog is more verbouse\n" + - "--logfile [log.txt]\t\tEnables log to file (default log.txt)\n"; + return "Version: " + Info::getVersion() + "\n" + + "Usage: " + Info::getExecutableName() + " [options]\n" + + "OPTIONS:\n" + + "-b, --bssid \t\tInput target BSSID\n" + + "-h, --help\t\t\t\tPrint this help page\n" + + "--debug\t\t\t\t\tPrint all th debug info" + + "-v, --verbouse\tLog is more verbouse\n" + + "--logfile [log.txt]\t\tEnables log to file (default log.txt)\n"; } diff --git a/Graphics/HelpScreen/HelpScreen.h b/Graphics/HelpScreen/HelpScreen.h index 6af248e..11b98e4 100644 --- a/Graphics/HelpScreen/HelpScreen.h +++ b/Graphics/HelpScreen/HelpScreen.h @@ -12,5 +12,4 @@ class HelpScreen { static std::string getHelpScreen(); }; - #endif //WPS_PIN_GENERATOR_HELPSCREEN_H diff --git a/Graphics/Info/Info.cpp b/Graphics/Info/Info.cpp index 7cec957..f186653 100644 --- a/Graphics/Info/Info.cpp +++ b/Graphics/Info/Info.cpp @@ -5,7 +5,7 @@ #include "Info.h" std::string Info::getVersion() { - return "0.0.1"; + return "0.0.2"; } std::string Info::getProjectName() { diff --git a/Graphics/Info/Info.h b/Graphics/Info/Info.h index 7f917d5..677ec06 100644 --- a/Graphics/Info/Info.h +++ b/Graphics/Info/Info.h @@ -14,5 +14,4 @@ class Info { static std::string getExecutableName(); }; - #endif //WPS_PIN_GENERATOR_VERSION_H diff --git a/Graphics/SplashScreen/SplashScreen.h b/Graphics/SplashScreen/SplashScreen.h index 610a296..9c30bef 100644 --- a/Graphics/SplashScreen/SplashScreen.h +++ b/Graphics/SplashScreen/SplashScreen.h @@ -13,5 +13,4 @@ class SplashScreen { static std::string getSplashScreen(); }; - #endif //WPS_PIN_GENERATOR_SPASHSCREEN_H diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index 14a0fc7..8069079 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -19,100 +19,161 @@ std::string InputHandler::bssidInput = ""; void InputHandler::handle(int argc, char **argv) { - try { + try { + /*** Default logger */ + std::shared_ptr logger(DoubleLogger::getDoubleLogger()); + /*** bssid input from arguments */ + std::vector bssid_arguments; + /*** assid input from arguments */ + std::vector assid_arguments; + /*** switch to ask for a result based on the generator */ + bool useGenerator = false; + /*** switch to ask for a result from the database */ + bool useDatabase = false; + + // Get arguments in c++ style string array + /*** c++ string container of arguments */ + std::vector args; + for (int i = 0; i < argc; ++i) { + args.push_back(std::string(argv[i])); + } - std::shared_ptr logger(new DoubleLogger); + // Default behaviour: show help screen + if (args.size() == 1) { + std::cout << HelpScreen::getHelpScreen() << std::endl; + } -// Logger *logger = new DoubleLogger(); + // Loop starts from 1 as args[0] allways contains the executable path + for (int j = 1; j < args.size(); ++j) { + // SWITCHES - // Get arguments in c++ style string array - std::vector args; + // Activate verbouse mode + if (args[j] == "--verbouse" || args[j] == "-v") { Logger::setVerbouseLogActive(true); } - for (int i = 0; i < argc; ++i) { - args.push_back(std::string(argv[i])); - } + // Activate debug mode + else if (args[j] == "--debug") { Logger::setDebugLogActive(true); } - // Loop starts from 1 as args[0] contains the executable path - for (int j = 1; j < args.size(); ++j) { + // Activate write on log file + else if (args[j] == "--logfile") { + TextLogger::setWriteOnFileActive(true); - // SWITCHES + // If a path is passed nex uses it as the log file path + if (j + 1 < args.size() && !std::regex_match(args[j + 1], std::regex("^/-"))) { + TextLogger::getTextLogger(args[j + 1]); + ++j; + } + } - // Activate verbouse mode - if (args[j] == "--verbouse" || args[j] == "-v") { - Logger::setVerbouseLogActive(true); - } + // Help dialog + else if (args[j] == "--help" || args[j] == "-h") { + std::cout << HelpScreen::getHelpScreen() << std::endl; + break; + } - // Activate debug mode - else if (args[j] == "--debug") { - Logger::setDebugLogActive(true); - } + // BSSID input + else if (args[j] == "-b" || args[j] == "--bssid") { - // Activate write on log file - else if (args[j] == "--logfile") { - TextLogger::setWriteOnFileActive(true); + // Listen for a BSSID as next argument + // moves j to the first BSSID + ++j; - // If a path is passed nex uses it as the log file path - if (j + 1 < args.size() && !std::regex_match(args[j + 1], std::regex("^/-"))) { - TextLogger::getTextLogger(args[j + 1]); - ++j; - } - } - - // ARGUMENTS - - // Help dialog - else if (args[j] == "--help" || args[j] == "-h") { - std::cout << HelpScreen::getHelpScreen() << std::endl; - break; - } - - // Pin generator - else if (args[j] == "-b" || args[j] == "--bssid") { - // Listen for a BSSID as next argument - ++j; - while (j < args.size() && - std::regex_match(args[j], std::regex("^([0-9A-F][0-9A-F]:){5}[0-9A-F][0-9A-F]$"))) { - - for (int k = j + 2; k < args.size(); ++k) { - - // Pin using generator - if (args[k] == "-g" || args[k] == "--generator") { - std::cout << "Results from generator:" << std::endl; - std::shared_ptr generator(new bertof::Generator); - std::vector results = generator->generatePin(args[j]); - for (int l = 0; l < results.size(); ++l) { - std::cout << l << ":\t" << results[l].toString() << std::endl; - } - std::cout << std::endl; + // While i find arguments that match a BSSID format + while (j < args.size() && + std::regex_match(args[j], std::regex("^(([0-9A-Fa-f]){2}:){5}([0-9a-fA-F]){2}$"))) { + bssid_arguments.push_back(args[j]); + ++j; + } + } + + // ASSID input + else if (args[j] == "-a" || args[j] == "--assid") { + + // Listen for ASSID as next argument + // moves j to the first ASSID + ++j; + + // While i find arguments that match a ASSID format + while (j < args.size() && + !std::regex_match(args[j], std::regex("^/-"))) { + assid_arguments.push_back(args[j]); + ++j; + } + } + + // Switch to enable generator results + else if (args[j] == "-g" || args[j] == "--generator") { + useGenerator = true; + } + + // Switch to enable database results + else if (args[j] == "-d" || args[j] == "--database") { + useDatabase = true; } - //TODO Pin from DB using BSSID + // Case the argument is invalid + else { + throw InvalidInputException(args[j]); + } + + } // End of for cycle - //TODO Pin from DB using ASSID - } - ++j; + // Output generation + //TODO output generation + + if (useGenerator || useDatabase) { + logger->log("Results:"); + } + if (useGenerator) { + GeneratorInterface *generator = new bertof::Generator(); + logger->logVerbouse("Results from generator by " + generator->author() + " version " + generator->version()); + for (int k = 0; k < bssid_arguments.size(); ++k) { + std::vector results = generator->generatePin(bssid_arguments[k]); + logger->log(bssid_arguments[k] + " results:"); + for (int i = 0; i < results.size(); ++i) { + logger->log(results[i].toString()); + } + } } - } + if (useDatabase) { + /* - // Case the argument is invalid - else { - throw InvalidInputException(args[j]); - } - } + DatabaseInterface *database = new bertof::Database(); + logger->logVerbouse("Results from database by " + database->author() + " version " + database->version()); + + for (int k = 0; k < bssid_arguments.size(); ++k) { + std::vector results = generator.generatePin(bssid_arguments[k]); + logger->log(bssid_arguments[k] + " results:"); + for (int i = 0; i < results.size(); ++i) { + logger->log(results[i].toString()); + } + } + + for (int k = 0; k < assid_arguments.size(); ++k) { + std::vector results = generator.generatePin(assid_arguments[k]); + logger->log(bssid_arguments[k] + " results:"); + for (int i = 0; i < results.size(); ++i) { + logger->log(results[i].toString()); + } + } + + */ - } catch (InvalidInputException e) { - Logger *logger = new DoubleLogger(); - logger->logError("Argument passed is invalid:\t" + std::string(e.what())); - throw InvalidInputException("Argument passed is invalid:\t\"" + std::string(e.what()) + "\""); - } + logger->logError("Database is not implemented yet"); + } + + } catch (InvalidInputException e) { + std::shared_ptr logger(DoubleLogger::getDoubleLogger()); + logger->logError("Argument passed is invalid:\t" + std::string(e.what())); + throw InvalidInputException("Argument passed is invalid:\t\"" + std::string(e.what()) + "\""); + } } const std::string &InputHandler::getBssidInput() { - return bssidInput; + return bssidInput; } void InputHandler::setBssidInput(const std::string &bssidInput) { - InputHandler::bssidInput = bssidInput; + InputHandler::bssidInput = bssidInput; } diff --git a/InputHandling/InputHandler.h b/InputHandling/InputHandler.h index 0762c00..ebff2d6 100644 --- a/InputHandling/InputHandler.h +++ b/InputHandling/InputHandler.h @@ -9,14 +9,14 @@ #include class InputHandler { - private: - static std::string bssidInput; - public: - static void handle(int argc, char *argv[]); +private: + static std::string bssidInput; +public: + static void handle(int argc, char *argv[]); - static const std::string &getBssidInput(); + static const std::string &getBssidInput(); - static void setBssidInput(const std::string &bssidInput); + static void setBssidInput(const std::string &bssidInput); }; #endif //WPS_PIN_GENERATOR_INPUTHANDLER_H diff --git a/Logger/DoubleLogger/DoubleLogger.cpp b/Logger/DoubleLogger/DoubleLogger.cpp index b041f7f..33c8f96 100644 --- a/Logger/DoubleLogger/DoubleLogger.cpp +++ b/Logger/DoubleLogger/DoubleLogger.cpp @@ -4,21 +4,30 @@ #include "DoubleLogger.h" +std::shared_ptr DoubleLogger::mainDoubleLoggerPointer = nullptr; + DoubleLogger::DoubleLogger(const std::string &filePath) : sLogger(ScreenLogger::getScreenLogger()), tLogger(TextLogger::getTextLogger(filePath)) {} void DoubleLogger::log(const std::string &message) const { - sLogger->log(message); - tLogger->log(message); + sLogger->log(message); + tLogger->log(message); } void DoubleLogger::logError(const std::string &errorMessage) const { - sLogger->logError(errorMessage); - tLogger->logError(errorMessage); + sLogger->logError(errorMessage); + tLogger->logError(errorMessage); } void DoubleLogger::logDebug(const std::string &debugMessage) const { - sLogger->logDebug(debugMessage); - tLogger->logDebug(debugMessage); + sLogger->logDebug(debugMessage); + tLogger->logDebug(debugMessage); } void DoubleLogger::logVerbouse(const std::string &verbouseMessage) const { - sLogger->logVerbouse(verbouseMessage); - tLogger->logVerbouse(verbouseMessage); + sLogger->logVerbouse(verbouseMessage); + tLogger->logVerbouse(verbouseMessage); +} +std::shared_ptr DoubleLogger::getDoubleLogger(const std::string &filePath) { + if (mainDoubleLoggerPointer == nullptr) { + mainDoubleLoggerPointer.reset(new DoubleLogger(filePath)); + } + return mainDoubleLoggerPointer; } + diff --git a/Logger/DoubleLogger/DoubleLogger.h b/Logger/DoubleLogger/DoubleLogger.h index 6fd9198..4ccea8c 100644 --- a/Logger/DoubleLogger/DoubleLogger.h +++ b/Logger/DoubleLogger/DoubleLogger.h @@ -10,23 +10,28 @@ #include "../TextLogger/TextLogger.h" class DoubleLogger : public Logger { - protected: - const std::shared_ptr sLogger; - const std::shared_ptr tLogger; +private: + static std::shared_ptr mainDoubleLoggerPointer; - public: +protected: + const std::shared_ptr sLogger; + const std::shared_ptr tLogger; - DoubleLogger(const std::string &filePath = TextLogger::DEFAULT_FILE_PATH); + DoubleLogger(const std::string &filePath = TextLogger::DEFAULT_FILE_PATH); - ~DoubleLogger() override {}; +public: - void log(const std::string &message) const override; + static std::shared_ptr getDoubleLogger(const std::string &filePath = TextLogger::DEFAULT_FILE_PATH); - void logError(const std::string &errorMessage) const override; + ~DoubleLogger() override {}; - void logDebug(const std::string &debugMessage) const override; + void log(const std::string &message) const override; - void logVerbouse(const std::string &verbouseMessage) const override; + void logError(const std::string &errorMessage) const override; + + void logDebug(const std::string &debugMessage) const override; + + void logVerbouse(const std::string &verbouseMessage) const override; }; #endif //WPS_PIN_GENERATOR_DOUBLELOGGER_H diff --git a/Logger/Logger.h b/Logger/Logger.h index 1287a1a..b28e914 100644 --- a/Logger/Logger.h +++ b/Logger/Logger.h @@ -57,5 +57,4 @@ class Logger { static void setVerbouseLogActive(bool verbouseLogActive); }; - #endif //WPS_PIN_GENERATOR_LOGGER_H diff --git a/Logger/ScreenLogger/ScreenLogger.cpp b/Logger/ScreenLogger/ScreenLogger.cpp index 918fe4d..c943c9a 100644 --- a/Logger/ScreenLogger/ScreenLogger.cpp +++ b/Logger/ScreenLogger/ScreenLogger.cpp @@ -10,36 +10,36 @@ std::shared_ptr ScreenLogger::mainScreenLoggerPtr = nullptr; void ScreenLogger::log(const std::string &message) const { - std::clog << rang::style::reset << message << std::endl; + std::clog << rang::style::reset << message << std::endl; } void ScreenLogger::logError(const std::string &errorMessage) const { - std::clog << rang::fg::red << "ERROR" << rang::style::reset << ":\t" << rang::fg::red - << errorMessage - << rang::style::reset << std::endl; + std::clog << rang::fg::red << "ERROR" << rang::style::reset << ":\t" << rang::fg::red + << errorMessage + << rang::style::reset << std::endl; } void ScreenLogger::logDebug(const std::string &string) const { - if (Logger::isDebugLogActive()) { - std::clog << rang::fg::cyan << "DBG" << rang::style::reset << ":\t" << string - << std::endl; - } + if (Logger::isDebugLogActive()) { + std::clog << rang::fg::cyan << "DBG" << rang::style::reset << ":\t" << string + << std::endl; + } } void ScreenLogger::logVerbouse(const std::string &string) const { - if (Logger::isVerbouseLogActive()) { - std::clog << rang::fg::cyan << "VERB" << rang::style::reset << ":\t" << string - << std::endl; - } + if (Logger::isVerbouseLogActive()) { + std::clog << rang::fg::cyan << "VERB" << rang::style::reset << ":\t" << string + << std::endl; + } } ScreenLogger::ScreenLogger() {} std::shared_ptr ScreenLogger::getScreenLogger() { - if (mainScreenLoggerPtr == nullptr) { - mainScreenLoggerPtr.reset(new ScreenLogger); - } - return mainScreenLoggerPtr; + if (mainScreenLoggerPtr == nullptr) { + mainScreenLoggerPtr.reset(new ScreenLogger); + } + return mainScreenLoggerPtr; } diff --git a/Logger/ScreenLogger/ScreenLogger.h b/Logger/ScreenLogger/ScreenLogger.h index 26ef26e..f01084f 100644 --- a/Logger/ScreenLogger/ScreenLogger.h +++ b/Logger/ScreenLogger/ScreenLogger.h @@ -52,5 +52,4 @@ class ScreenLogger : virtual public Logger { static std::shared_ptr getScreenLogger(); }; - #endif //WPS_PIN_GENERATOR_DEBUGGER_H diff --git a/Logger/TextLogger/TextLogger.cpp b/Logger/TextLogger/TextLogger.cpp index 522e785..23832bc 100644 --- a/Logger/TextLogger/TextLogger.cpp +++ b/Logger/TextLogger/TextLogger.cpp @@ -18,7 +18,7 @@ bool TextLogger::writeOnFileActive = false; * @param message */ void TextLogger::log(const std::string &message) const { - logText(message); + logText(message); } /** Logs an error message @@ -26,7 +26,7 @@ void TextLogger::log(const std::string &message) const { * @param errorMessage */ void TextLogger::logError(const std::string &errorMessage) const { - logText("ERROR:\t" + errorMessage); + logText("ERROR:\t" + errorMessage); } /** Log a debug message @@ -34,9 +34,9 @@ void TextLogger::logError(const std::string &errorMessage) const { * @param debugMessage */ void TextLogger::logDebug(const std::string &debugMessage) const { - if (Logger::isDebugLogActive()) { - logText("DBG:\t" + debugMessage); - } + if (Logger::isDebugLogActive()) { + logText("DBG:\t" + debugMessage); + } } /** Constructor @@ -44,53 +44,53 @@ void TextLogger::logDebug(const std::string &debugMessage) const { * @param filePath */ TextLogger::TextLogger(const std::string &filePath) : filePath(filePath) { - if (!boost::filesystem::portable_name(filePath)) { - std::shared_ptr l = ScreenLogger::getScreenLogger(); - l->logError("File path not portable. It may create problems."); - } + if (!boost::filesystem::portable_name(filePath)) { + std::shared_ptr l = ScreenLogger::getScreenLogger(); + l->logError("File path not portable. It may create problems."); + } } /** Opens file and appends the message passed * @param message text to write in the log file */ void TextLogger::logText(const std::string &message) const { - if (TextLogger::isWriteOnFileActive()) { - boost::filesystem::fstream fileOutput; - fileOutput.open(filePath, std::ofstream::app); - if (!fileOutput.is_open()) { - std::shared_ptr l = ScreenLogger::getScreenLogger(); - l->logError("Can't open the log file (" + filePath + ")"); - } else { - std::string nowString = boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); - fileOutput << nowString << "\t" << message << std::endl; - fileOutput.close(); + if (TextLogger::isWriteOnFileActive()) { + boost::filesystem::fstream fileOutput; + fileOutput.open(filePath, std::ofstream::app); + if (!fileOutput.is_open()) { + std::shared_ptr l = ScreenLogger::getScreenLogger(); + l->logError("Can't open the log file (" + filePath + ")"); + } else { + std::string nowString = boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); + fileOutput << nowString << "\t" << message << std::endl; + fileOutput.close(); + } } - } } void TextLogger::logVerbouse(const std::string &verbouseMessage) const { - if (Logger::isVerbouseLogActive()) { - logText("VERB:\t" + verbouseMessage); - } + if (Logger::isVerbouseLogActive()) { + logText("VERB:\t" + verbouseMessage); + } } bool TextLogger::isWriteOnFileActive() { - return writeOnFileActive; + return writeOnFileActive; } void TextLogger::setWriteOnFileActive(bool status) { - TextLogger::writeOnFileActive = status; + TextLogger::writeOnFileActive = status; } std::shared_ptr TextLogger::getTextLogger(const std::string &filePath) { - if (mainTextLoggerPtr == nullptr) { - mainTextLoggerPtr.reset(new TextLogger(filePath)); - } - return mainTextLoggerPtr; + if (mainTextLoggerPtr == nullptr) { + mainTextLoggerPtr.reset(new TextLogger(filePath)); + } + return mainTextLoggerPtr; } const std::string &TextLogger::getFilePath() const { - return filePath; + return filePath; } diff --git a/Logger/TextLogger/TextLogger.h b/Logger/TextLogger/TextLogger.h index fc4ff76..70dccab 100644 --- a/Logger/TextLogger/TextLogger.h +++ b/Logger/TextLogger/TextLogger.h @@ -9,81 +9,81 @@ class TextLogger : virtual public Logger { - private: - - /*** Generic method to log on file - * @param message - */ - virtual void logText(const std::string &message) const; - - protected: - - /*** Main shared pointer to a TextLogger singleton - */ - static std::shared_ptr mainTextLoggerPtr; - - /*** Controls if any TextLogger should write on a file - */ - static bool writeOnFileActive; - - /*** File path of the log file - */ - const std::string filePath; - - /*** Default constructor - */ - TextLogger(const std::string &logFilePath = DEFAULT_FILE_PATH); - - public: - - /*** Default for filePath - */ - static const std::string DEFAULT_FILE_PATH; - - /*** Virtual default destructor - */ - virtual ~TextLogger() {} - - /*** FilePath getter - * @return - */ - const std::string &getFilePath() const; - - /*** Log standard message - * @param message - */ - virtual void log(const std::string &message) const override; - - /*** Log error message - * @param errorMessage - */ - virtual void logError(const std::string &errorMessage) const override; - - /*** Log debug only message - * @param debugMessage - */ - virtual void logDebug(const std::string &debugMessage) const override; - - /*** Log verbouse only message - * @param verbouseMessage - */ - virtual void logVerbouse(const std::string &verbouseMessage) const override; - - /*** Getter for writeOnLogFileActive - * @return writeOnFileActive status - */ - static bool isWriteOnFileActive(); - - /*** Setter for writeOnLogFileActive - * @param status set to true to enable logging on a text file - */ - static void setWriteOnFileActive(bool status); - - /*** TextLogger singleton pointer factory - * @param filePath - * @return - */ - static std::shared_ptr getTextLogger(const std::string &filePath); +private: + + /*** Generic method to log on file + * @param message + */ + virtual void logText(const std::string &message) const; + +protected: + + /*** Main shared pointer to a TextLogger singleton + */ + static std::shared_ptr mainTextLoggerPtr; + + /*** Controls if any TextLogger should write on a file + */ + static bool writeOnFileActive; + + /*** File path of the log file + */ + const std::string filePath; + + /*** Default constructor + */ + TextLogger(const std::string &logFilePath = DEFAULT_FILE_PATH); + +public: + + /*** Default for filePath + */ + static const std::string DEFAULT_FILE_PATH; + + /*** Virtual default destructor + */ + virtual ~TextLogger() {} + + /*** FilePath getter + * @return + */ + const std::string &getFilePath() const; + + /*** Log standard message + * @param message + */ + virtual void log(const std::string &message) const override; + + /*** Log error message + * @param errorMessage + */ + virtual void logError(const std::string &errorMessage) const override; + + /*** Log debug only message + * @param debugMessage + */ + virtual void logDebug(const std::string &debugMessage) const override; + + /*** Log verbouse only message + * @param verbouseMessage + */ + virtual void logVerbouse(const std::string &verbouseMessage) const override; + + /*** Getter for writeOnLogFileActive + * @return writeOnFileActive status + */ + static bool isWriteOnFileActive(); + + /*** Setter for writeOnLogFileActive + * @param status set to true to enable logging on a text file + */ + static void setWriteOnFileActive(bool status); + + /*** TextLogger singleton pointer factory + * @param filePath + * @return + */ + static std::shared_ptr getTextLogger(const std::string &filePath); }; #endif //WPS_PIN_GENERATOR_TEXTLOGGER_H diff --git a/Pin/Pin.h b/Pin/Pin.h index cc5d23d..0778add 100644 --- a/Pin/Pin.h +++ b/Pin/Pin.h @@ -56,5 +56,4 @@ class Pin { long toInteger() const { return pinValue; } }; - #endif //WPS_PIN_GENERATOR_PIN_H diff --git a/main.cpp b/main.cpp index 0629109..ec95932 100644 --- a/main.cpp +++ b/main.cpp @@ -10,19 +10,23 @@ #include "lib/rang/include/rang.hpp" int main(int argc, char *argv[]) { - //TODO better spash screen - //TODO better input handling - //TODO guide on how to use it (--help) - //TODO switch to smart pointers - // Spash screen - std::cout << SplashScreen::getSplashScreen() << std::endl; + std::shared_ptr logger; - // Handle input - InputHandler::handle(argc, argv); + DoubleLogger::getDoubleLogger(); + //TODO better spash screen + //TODO better input handling + //TODO guide on how to use it (--help) + //TODO switch to smart pointers - // END - std::cout << "END MAIN" << std::endl; + // Spash screen + std::cout << SplashScreen::getSplashScreen() << std::endl; - return 0; + // Handle input + InputHandler::handle(argc, argv); + + // END + logger->logDebug("END MAIN"); + + return 0; } \ No newline at end of file From b002e79f3d2833c978256caadd35b6f99179af06 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Tue, 14 Mar 2017 08:44:10 +0100 Subject: [PATCH 33/37] Better input hanfling --- InputHandling/InputHandler.cpp | 217 +++++++++++++++++++-------------- main.cpp | 6 +- 2 files changed, 128 insertions(+), 95 deletions(-) diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index 8069079..1a343cb 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -11,6 +11,7 @@ #include "../Generator/GeneratorInerface.h" #include "../Generator/bertofGenerator/Generator.h" +#include "../Exceptions/NotImplementedException.h" #include #include @@ -22,10 +23,12 @@ void InputHandler::handle(int argc, char **argv) { try { /*** Default logger */ std::shared_ptr logger(DoubleLogger::getDoubleLogger()); + /*** bssid input from arguments */ std::vector bssid_arguments; /*** assid input from arguments */ std::vector assid_arguments; + /*** switch to ask for a result based on the generator */ bool useGenerator = false; /*** switch to ask for a result from the database */ @@ -38,128 +41,160 @@ void InputHandler::handle(int argc, char **argv) { args.push_back(std::string(argv[i])); } - // Default behaviour: show help screen + // Default behaviour when not using any aguments: show help screen if (args.size() == 1) { std::cout << HelpScreen::getHelpScreen() << std::endl; - } + } else { - // Loop starts from 1 as args[0] allways contains the executable path - for (int j = 1; j < args.size(); ++j) { - // SWITCHES + // Loop starts from 1 as args[0] allways contains the executable path + int i = 1; + while (i < args.size()) { - // Activate verbouse mode - if (args[j] == "--verbouse" || args[j] == "-v") { Logger::setVerbouseLogActive(true); } + // SWITCHES - // Activate debug mode - else if (args[j] == "--debug") { Logger::setDebugLogActive(true); } - - // Activate write on log file - else if (args[j] == "--logfile") { - TextLogger::setWriteOnFileActive(true); + // Activate verbouse mode + if (args[i] == "--verbouse" || args[i] == "-v") { + Logger::setVerbouseLogActive(true); + ++i; + } - // If a path is passed nex uses it as the log file path - if (j + 1 < args.size() && !std::regex_match(args[j + 1], std::regex("^/-"))) { - TextLogger::getTextLogger(args[j + 1]); - ++j; + // Activate debug mode + else if (args[i] == "--debug") { + Logger::setDebugLogActive(true); + ++i; } - } - // Help dialog - else if (args[j] == "--help" || args[j] == "-h") { - std::cout << HelpScreen::getHelpScreen() << std::endl; - break; - } + // Activate write on log file + else if (args[i] == "--logfile") { + TextLogger::setWriteOnFileActive(true); - // BSSID input - else if (args[j] == "-b" || args[j] == "--bssid") { + // If a path is passed nex uses it as the log file path + if (i + 1 < args.size() && std::regex_match(args[i + 1], std::regex("^(^-)"))) { + TextLogger::getTextLogger(args[i + 1]); + ++i; + } - // Listen for a BSSID as next argument - // moves j to the first BSSID - ++j; + ++i; + } - // While i find arguments that match a BSSID format - while (j < args.size() && - std::regex_match(args[j], std::regex("^(([0-9A-Fa-f]){2}:){5}([0-9a-fA-F]){2}$"))) { - bssid_arguments.push_back(args[j]); - ++j; + // Help dialog + else if (args[i] == "--help" || args[i] == "-h") { + std::cout << HelpScreen::getHelpScreen() << std::endl; + break; } - } - // ASSID input - else if (args[j] == "-a" || args[j] == "--assid") { + // BSSID input + else if (args[i] == "-b" || args[i] == "--bssid") { - // Listen for ASSID as next argument - // moves j to the first ASSID - ++j; + // Listen for a BSSID as next argument - // While i find arguments that match a ASSID format - while (j < args.size() && - !std::regex_match(args[j], std::regex("^/-"))) { - assid_arguments.push_back(args[j]); - ++j; + // While i find arguments that match a BSSID format + while (i + 1 < args.size() + && std::regex_match(args[i + 1], std::regex("^(([0-9A-Fa-f]){2}:){5}([0-9a-fA-F]){2}$"))) { + + bssid_arguments.push_back(args[i + 1]); + ++i; + } + + // Check if any BSSID is been given + if (bssid_arguments.size() == 0) { + logger->logError("No BSSID found"); + throw InvalidInputException("No BSSID found"); + } + + ++i; } - } - // Switch to enable generator results - else if (args[j] == "-g" || args[j] == "--generator") { - useGenerator = true; - } + // ASSID input + else if (args[i] == "-a" || args[i] == "--assid") { - // Switch to enable database results - else if (args[j] == "-d" || args[j] == "--database") { - useDatabase = true; - } + // Listen for a ASSID as next argument - // Case the argument is invalid - else { - throw InvalidInputException(args[j]); - } + // While i find arguments that match a BSSID format + while (i + 1 < args.size() && + std::regex_match(args[i + 1], std::regex("^(^-)"))) { + bssid_arguments.push_back(args[i + 1]); + ++i; + } - } // End of for cycle + // Check if any ASSID is been given + if (assid_arguments.size() == 0) { + logger->logError("No ASSID found"); + throw InvalidInputException("No ASSID found"); + } - // Output generation - //TODO output generation + ++i; + } - if (useGenerator || useDatabase) { - logger->log("Results:"); - } - if (useGenerator) { - GeneratorInterface *generator = new bertof::Generator(); - logger->logVerbouse("Results from generator by " + generator->author() + " version " + generator->version()); - for (int k = 0; k < bssid_arguments.size(); ++k) { - std::vector results = generator->generatePin(bssid_arguments[k]); - logger->log(bssid_arguments[k] + " results:"); - for (int i = 0; i < results.size(); ++i) { - logger->log(results[i].toString()); + // Switch to enable generator results + else if (args[i] == "-g" || args[i] == "--generator") { + useGenerator = true; + ++i; } - } - } - if (useDatabase) { - /* + // Switch to enable database results + else if (args[i] == "-d" || args[i] == "--database") { + useDatabase = true; + ++i; + } - DatabaseInterface *database = new bertof::Database(); - logger->logVerbouse("Results from database by " + database->author() + " version " + database->version()); + // Case the argument is invalid + else { + throw InvalidInputException(args[i]); + } - for (int k = 0; k < bssid_arguments.size(); ++k) { - std::vector results = generator.generatePin(bssid_arguments[k]); - logger->log(bssid_arguments[k] + " results:"); - for (int i = 0; i < results.size(); ++i) { - logger->log(results[i].toString()); - } + } // End of for cycle + + // Output generation + //TODO output generation + + if (useGenerator || useDatabase) { + logger->log("Results:"); + } else { + logger->logError("-g or -d options not set."); } - for (int k = 0; k < assid_arguments.size(); ++k) { - std::vector results = generator.generatePin(assid_arguments[k]); - logger->log(bssid_arguments[k] + " results:"); - for (int i = 0; i < results.size(); ++i) { - logger->log(results[i].toString()); - } + if (useGenerator) { + std::shared_ptr generator(new bertof::Generator()); + logger->logVerbouse( + "Results from generator by " + generator->author() + " version " + generator->version()); + for (int k = 0; k < bssid_arguments.size(); ++k) { + std::vector results = generator->generatePin(bssid_arguments[k]); + logger->log("BSSID:\t" + bssid_arguments[k]); + logger->logVerbouse("Pins generated:"); + for (int i = 0; i < results.size(); ++i) { + logger->log(results[i].toString()); + } + } } - */ + if (useDatabase) { + /* + + DatabaseInterface *database = new bertof::Database(); + logger->logVerbouse("Results from database by " + database->author() + " version " + database->version()); - logger->logError("Database is not implemented yet"); + for (int k = 0; k < bssid_arguments.size(); ++k) { + std::vector results = generator.generatePin(bssid_arguments[k]); + logger->log(bssid_arguments[k] + " results:"); + for (int i = 0; i < results.size(); ++i) { + logger->log(results[i].toString()); + } + } + + for (int k = 0; k < assid_arguments.size(); ++k) { + std::vector results = generator.generatePin(assid_arguments[k]); + logger->log(bssid_arguments[k] + " results:"); + for (int i = 0; i < results.size(); ++i) { + logger->log(results[i].toString()); + } + } + + */ + + logger->logError("Database is not implemented yet"); + throw NotImplementedException("Database is not implemented yet"); + } } } catch (InvalidInputException e) { diff --git a/main.cpp b/main.cpp index ec95932..b563f8c 100644 --- a/main.cpp +++ b/main.cpp @@ -11,16 +11,14 @@ int main(int argc, char *argv[]) { - std::shared_ptr logger; - - DoubleLogger::getDoubleLogger(); + std::shared_ptr logger(DoubleLogger::getDoubleLogger()); //TODO better spash screen //TODO better input handling //TODO guide on how to use it (--help) //TODO switch to smart pointers // Spash screen - std::cout << SplashScreen::getSplashScreen() << std::endl; + logger->log(SplashScreen::getSplashScreen()); // Handle input InputHandler::handle(argc, argv); From 6e2a24c12e9ab5a4373e2fc57549307cb607b03e Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Wed, 15 Mar 2017 08:11:03 +0100 Subject: [PATCH 34/37] Better help info and argument handling --- CMakeLists.txt | 2 +- Exceptions/HelpScreenException.h | 45 ++++++ Graphics/HelpScreen/HelpScreen.cpp | 28 ++-- InputHandling/InputHandler.cpp | 227 +++++++++++++++-------------- 4 files changed, 179 insertions(+), 123 deletions(-) create mode 100644 Exceptions/HelpScreenException.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cd5917..6161e7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Boost 1.58.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) - set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h Graphics/HelpScreen/HelpScreen.cpp Graphics/HelpScreen/HelpScreen.h Graphics/Info/Info.cpp Graphics/Info/Info.h) + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h Graphics/HelpScreen/HelpScreen.cpp Graphics/HelpScreen/HelpScreen.h Graphics/Info/Info.cpp Graphics/Info/Info.h Exceptions/HelpScreenException.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) diff --git a/Exceptions/HelpScreenException.h b/Exceptions/HelpScreenException.h new file mode 100644 index 0000000..5b04efa --- /dev/null +++ b/Exceptions/HelpScreenException.h @@ -0,0 +1,45 @@ +// +// Created by pily on 14/03/17. +// + +#ifndef WPS_PIN_GENERATOR_HELPSCREENEXCEPTION_H +#define WPS_PIN_GENERATOR_HELPSCREENEXCEPTION_H + +#include +#include + +class HelpScreenException : public std::exception { +public: + /** Constructor (C string) + * @param message C-style string error message. + * The string contents are copied upon construction. + * Hence, responsibility for deleting the char* lies + * with the caller. + */ + explicit HelpScreenException(const char *message) : errorMessage_(message) {} + + /** Constructor (C++ STL string) + * @param message The error message. + */ + explicit HelpScreenException(const std::string &message = "Help screen showed") : errorMessage_(message) {} + + /** Destructor + * Virtual to allow subclassing + */ + virtual ~HelpScreenException() throw() {} + + /** Returns a pointer to the (constant) error description. + * + * @return + */ + const char *what() const throw() { + return errorMessage_.c_str(); + } + +private: + /** Saved error message + */ + std::string errorMessage_; +}; + +#endif //WPS_PIN_GENERATOR_HELPSCREENEXCEPTION_H diff --git a/Graphics/HelpScreen/HelpScreen.cpp b/Graphics/HelpScreen/HelpScreen.cpp index 8b786e2..c4550ff 100644 --- a/Graphics/HelpScreen/HelpScreen.cpp +++ b/Graphics/HelpScreen/HelpScreen.cpp @@ -4,16 +4,26 @@ #include "HelpScreen.h" #include "../SplashScreen/SplashScreen.h" - #include "../Info/Info.h" +#include + std::string HelpScreen::getHelpScreen() { - return "Version: " + Info::getVersion() + "\n" + - "Usage: " + Info::getExecutableName() + " [options]\n" + - "OPTIONS:\n" + - "-b, --bssid \t\tInput target BSSID\n" + - "-h, --help\t\t\t\tPrint this help page\n" + - "--debug\t\t\t\t\tPrint all th debug info" + - "-v, --verbouse\tLog is more verbouse\n" + - "--logfile [log.txt]\t\tEnables log to file (default log.txt)\n"; + return + "Version: " + Info::getVersion() + "\n" + + "USAGE:\n" + + Info::getExecutableName() + " [options]\n" + + "\n" + + "OPTIONS:\n" + + "-b, --bssid \t\tInput target BSSID\n" + + "-g\t\t\t\t\t\tUse the generator algorithm\n" + + "-d\t\t\t\t\t\tUse the database\n" + + "-h, --help\t\t\t\tPrint this help page\n" + + "--debug\t\t\t\t\tPrint all th debug info" + + "-v, --verbouse\tLog is more verbouse\n" + + "--logfile [log.txt]\t\tEnables log to file (default log.txt)\n" + + "\n" + + "EXAMPLES:\n" + + Info::getExecutableName() + " -h\n" + + Info::getExecutableName() + " -b 00:11:22:33:44:55 -g -d --logfile"; } diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index 1a343cb..8bce74e 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -12,6 +12,7 @@ #include "../Generator/GeneratorInerface.h" #include "../Generator/bertofGenerator/Generator.h" #include "../Exceptions/NotImplementedException.h" +#include "../Exceptions/HelpScreenException.h" #include #include @@ -43,166 +44,166 @@ void InputHandler::handle(int argc, char **argv) { // Default behaviour when not using any aguments: show help screen if (args.size() == 1) { - std::cout << HelpScreen::getHelpScreen() << std::endl; - } else { - - // Loop starts from 1 as args[0] allways contains the executable path - int i = 1; - while (i < args.size()) { + throw HelpScreenException(); + } - // SWITCHES + // Loop starts from 1 as args[0] allways contains the executable path + int i = 1; + while (i < args.size()) { - // Activate verbouse mode - if (args[i] == "--verbouse" || args[i] == "-v") { - Logger::setVerbouseLogActive(true); - ++i; - } + // SWITCHES - // Activate debug mode - else if (args[i] == "--debug") { - Logger::setDebugLogActive(true); - ++i; - } + // Activate verbouse mode + if (args[i] == "--verbouse" || args[i] == "-v") { + Logger::setVerbouseLogActive(true); + ++i; + } - // Activate write on log file - else if (args[i] == "--logfile") { - TextLogger::setWriteOnFileActive(true); + // Activate debug mode + else if (args[i] == "--debug") { + Logger::setDebugLogActive(true); + ++i; + } - // If a path is passed nex uses it as the log file path - if (i + 1 < args.size() && std::regex_match(args[i + 1], std::regex("^(^-)"))) { - TextLogger::getTextLogger(args[i + 1]); - ++i; - } + // Activate write on log file + else if (args[i] == "--logfile") { + TextLogger::setWriteOnFileActive(true); + // If a path is passed nex uses it as the log file path + if (i + 1 < args.size() && std::regex_match(args[i + 1], std::regex("^(^-)"))) { + TextLogger::getTextLogger(args[i + 1]); ++i; } - // Help dialog - else if (args[i] == "--help" || args[i] == "-h") { - std::cout << HelpScreen::getHelpScreen() << std::endl; - break; - } - - // BSSID input - else if (args[i] == "-b" || args[i] == "--bssid") { + ++i; + } - // Listen for a BSSID as next argument + // Help dialog + else if (args[i] == "--help" || args[i] == "-h") { + throw HelpScreenException(); + } - // While i find arguments that match a BSSID format - while (i + 1 < args.size() - && std::regex_match(args[i + 1], std::regex("^(([0-9A-Fa-f]){2}:){5}([0-9a-fA-F]){2}$"))) { + // BSSID input + else if (args[i] == "-b" || args[i] == "--bssid") { - bssid_arguments.push_back(args[i + 1]); - ++i; - } + // Listen for a BSSID as next argument - // Check if any BSSID is been given - if (bssid_arguments.size() == 0) { - logger->logError("No BSSID found"); - throw InvalidInputException("No BSSID found"); - } + // While i find arguments that match a BSSID format + while (i + 1 < args.size() + && std::regex_match(args[i + 1], std::regex("^(([0-9A-Fa-f]){2}:){5}([0-9a-fA-F]){2}$"))) { + bssid_arguments.push_back(args[i + 1]); ++i; } - // ASSID input - else if (args[i] == "-a" || args[i] == "--assid") { - - // Listen for a ASSID as next argument - - // While i find arguments that match a BSSID format - while (i + 1 < args.size() && - std::regex_match(args[i + 1], std::regex("^(^-)"))) { - bssid_arguments.push_back(args[i + 1]); - ++i; - } + // Check if any BSSID is been given + if (bssid_arguments.size() == 0) { + logger->logError("No BSSID found"); + throw InvalidInputException("No BSSID found"); + } - // Check if any ASSID is been given - if (assid_arguments.size() == 0) { - logger->logError("No ASSID found"); - throw InvalidInputException("No ASSID found"); - } + ++i; + } - ++i; - } + // ASSID input + else if (args[i] == "-a" || args[i] == "--assid") { - // Switch to enable generator results - else if (args[i] == "-g" || args[i] == "--generator") { - useGenerator = true; - ++i; - } + // Listen for a ASSID as next argument - // Switch to enable database results - else if (args[i] == "-d" || args[i] == "--database") { - useDatabase = true; + // While i find arguments that match a BSSID format + while (i + 1 < args.size() && + std::regex_match(args[i + 1], std::regex("^(^-)"))) { + bssid_arguments.push_back(args[i + 1]); ++i; } - // Case the argument is invalid - else { - throw InvalidInputException(args[i]); + // Check if any ASSID is been given + if (assid_arguments.size() == 0) { + logger->logError("No ASSID found"); + throw InvalidInputException("No ASSID found"); } - } // End of for cycle + ++i; + } - // Output generation - //TODO output generation + // Switch to enable generator results + else if (args[i] == "-g" || args[i] == "--generator") { + useGenerator = true; + ++i; + } - if (useGenerator || useDatabase) { - logger->log("Results:"); - } else { - logger->logError("-g or -d options not set."); + // Switch to enable database results + else if (args[i] == "-d" || args[i] == "--database") { + useDatabase = true; + ++i; } - if (useGenerator) { - std::shared_ptr generator(new bertof::Generator()); - logger->logVerbouse( - "Results from generator by " + generator->author() + " version " + generator->version()); - for (int k = 0; k < bssid_arguments.size(); ++k) { - std::vector results = generator->generatePin(bssid_arguments[k]); - logger->log("BSSID:\t" + bssid_arguments[k]); - logger->logVerbouse("Pins generated:"); - for (int i = 0; i < results.size(); ++i) { - logger->log(results[i].toString()); - } - } + // Case the argument is invalid + else { + throw InvalidInputException(args[i]); } - if (useDatabase) { - /* + } // End of for cycle - DatabaseInterface *database = new bertof::Database(); - logger->logVerbouse("Results from database by " + database->author() + " version " + database->version()); + // Output generation + //TODO output generation - for (int k = 0; k < bssid_arguments.size(); ++k) { - std::vector results = generator.generatePin(bssid_arguments[k]); - logger->log(bssid_arguments[k] + " results:"); - for (int i = 0; i < results.size(); ++i) { - logger->log(results[i].toString()); - } - } + if (useGenerator || useDatabase) { + logger->log("Results:"); + } else { + logger->logError("-g or -d options not set."); + throw InvalidInputException(); + } - for (int k = 0; k < assid_arguments.size(); ++k) { - std::vector results = generator.generatePin(assid_arguments[k]); - logger->log(bssid_arguments[k] + " results:"); - for (int i = 0; i < results.size(); ++i) { + if (useGenerator) { + std::shared_ptr generator(new bertof::Generator()); + logger->logVerbouse( + "Results from generator by " + generator->author() + " version " + generator->version()); + for (int k = 0; k < bssid_arguments.size(); ++k) { + std::vector results = generator->generatePin(bssid_arguments[k]); + logger->log("BSSID:\t" + bssid_arguments[k]); + logger->logVerbouse("Pins generated:"); + for (int i = 0; i < results.size(); ++i) { logger->log(results[i].toString()); - } } + } + } - */ + if (useDatabase) { + /* - logger->logError("Database is not implemented yet"); - throw NotImplementedException("Database is not implemented yet"); + DatabaseInterface *database = new bertof::Database(); + logger->logVerbouse("Results from database by " + database->author() + " version " + database->version()); + + for (int k = 0; k < bssid_arguments.size(); ++k) { + std::vector results = generator.generatePin(bssid_arguments[k]); + logger->log(bssid_arguments[k] + " results:"); + for (int i = 0; i < results.size(); ++i) { + logger->log(results[i].toString()); + } } + + for (int k = 0; k < assid_arguments.size(); ++k) { + std::vector results = generator.generatePin(assid_arguments[k]); + logger->log(bssid_arguments[k] + " results:"); + for (int i = 0; i < results.size(); ++i) { + logger->log(results[i].toString()); + } + } + + */ + + logger->logError("Database is not implemented yet"); + throw NotImplementedException("Database is not implemented yet"); } } catch (InvalidInputException e) { std::shared_ptr logger(DoubleLogger::getDoubleLogger()); logger->logError("Argument passed is invalid:\t" + std::string(e.what())); throw InvalidInputException("Argument passed is invalid:\t\"" + std::string(e.what()) + "\""); + } catch (HelpScreenException e) { + std::cout << HelpScreen::getHelpScreen() << std::endl; } - } const std::string &InputHandler::getBssidInput() { From 46338ae9e006bc42fc36233babd454a83d923089 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Wed, 15 Mar 2017 08:21:09 +0100 Subject: [PATCH 35/37] Typo --- InputHandling/InputHandler.cpp | 38 ++++++++++++++++------------------ 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index 8bce74e..39735f5 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -25,10 +25,10 @@ void InputHandler::handle(int argc, char **argv) { /*** Default logger */ std::shared_ptr logger(DoubleLogger::getDoubleLogger()); - /*** bssid input from arguments */ + /*** BSSID input from arguments */ std::vector bssid_arguments; - /*** assid input from arguments */ - std::vector assid_arguments; + /*** ESSID input from arguments */ + std::vector essid_arguments; /*** switch to ask for a result based on the generator */ bool useGenerator = false; @@ -105,10 +105,10 @@ void InputHandler::handle(int argc, char **argv) { ++i; } - // ASSID input - else if (args[i] == "-a" || args[i] == "--assid") { + // ESSID input + else if (args[i] == "-e" || args[i] == "--essid") { - // Listen for a ASSID as next argument + // Listen for a ESSID as next argument // While i find arguments that match a BSSID format while (i + 1 < args.size() && @@ -117,10 +117,9 @@ void InputHandler::handle(int argc, char **argv) { ++i; } - // Check if any ASSID is been given - if (assid_arguments.size() == 0) { - logger->logError("No ASSID found"); - throw InvalidInputException("No ASSID found"); + // Check if any ESSID is been given + if (essid_arguments.size() == 0) { + throw InvalidInputException("No ESSID found"); } ++i; @@ -148,11 +147,12 @@ void InputHandler::handle(int argc, char **argv) { // Output generation //TODO output generation - if (useGenerator || useDatabase) { - logger->log("Results:"); - } else { - logger->logError("-g or -d options not set."); - throw InvalidInputException(); + if (!useGenerator && !useDatabase) { + throw InvalidInputException("-g or -d options not set."); + } + + if (bssid_arguments.size() + essid_arguments.size() == 0) { + throw InvalidInputException("No BSSID or ESSID argument given."); } if (useGenerator) { @@ -183,8 +183,8 @@ void InputHandler::handle(int argc, char **argv) { } } - for (int k = 0; k < assid_arguments.size(); ++k) { - std::vector results = generator.generatePin(assid_arguments[k]); + for (int k = 0; k < essid_arguments.size(); ++k) { + std::vector results = generator.generatePin(essid_arguments[k]); logger->log(bssid_arguments[k] + " results:"); for (int i = 0; i < results.size(); ++i) { logger->log(results[i].toString()); @@ -193,14 +193,12 @@ void InputHandler::handle(int argc, char **argv) { */ - logger->logError("Database is not implemented yet"); throw NotImplementedException("Database is not implemented yet"); } } catch (InvalidInputException e) { std::shared_ptr logger(DoubleLogger::getDoubleLogger()); - logger->logError("Argument passed is invalid:\t" + std::string(e.what())); - throw InvalidInputException("Argument passed is invalid:\t\"" + std::string(e.what()) + "\""); + logger->logError(std::string(e.what())); } catch (HelpScreenException e) { std::cout << HelpScreen::getHelpScreen() << std::endl; } From d193cf68c675c5b39ba01b295bf65c53d4121091 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Wed, 15 Mar 2017 08:22:53 +0100 Subject: [PATCH 36/37] Better exception handling --- InputHandling/InputHandler.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index 39735f5..9aa3e80 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -98,7 +98,6 @@ void InputHandler::handle(int argc, char **argv) { // Check if any BSSID is been given if (bssid_arguments.size() == 0) { - logger->logError("No BSSID found"); throw InvalidInputException("No BSSID found"); } @@ -201,6 +200,9 @@ void InputHandler::handle(int argc, char **argv) { logger->logError(std::string(e.what())); } catch (HelpScreenException e) { std::cout << HelpScreen::getHelpScreen() << std::endl; + } catch (NotImplementedException e) { + std::shared_ptr logger(DoubleLogger::getDoubleLogger()); + logger->logError(std::string(e.what())); } } From ddc75d2cc224fd724e0ae218686a474a44e07068 Mon Sep 17 00:00:00 2001 From: Filippo Berto Date: Wed, 15 Mar 2017 09:21:41 +0100 Subject: [PATCH 37/37] Authors and credits (partial info) --- CMakeLists.txt | 2 +- Exceptions/AuthorsScreenException.h | 45 ++++++++++++++++++++++++ Graphics/AuthorsScreen/AuthorsScreen.cpp | 15 ++++++++ Graphics/AuthorsScreen/AuthorsScreen.h | 13 +++++++ Graphics/HelpScreen/HelpScreen.cpp | 6 ++-- Graphics/Info/Info.cpp | 9 ++++- Graphics/Info/Info.h | 19 ++++++++++ Graphics/SplashScreen/SplashScreen.cpp | 7 ++-- Graphics/SplashScreen/SplashScreen.h | 1 - InputHandling/InputHandler.cpp | 8 +++++ main.cpp | 2 +- 11 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 Exceptions/AuthorsScreenException.h create mode 100644 Graphics/AuthorsScreen/AuthorsScreen.cpp create mode 100644 Graphics/AuthorsScreen/AuthorsScreen.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6161e7f..67fa918 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Boost 1.58.0 COMPONENTS filesystem date_time) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) - set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h Graphics/HelpScreen/HelpScreen.cpp Graphics/HelpScreen/HelpScreen.h Graphics/Info/Info.cpp Graphics/Info/Info.h Exceptions/HelpScreenException.h) + set(SOURCE_FILES main.cpp Pin/Pin.h Generator/bertofGenerator/Generator.h Exceptions/NotImplementedException.h Generator/bertofGenerator/Generator.cpp Logger/ScreenLogger/ScreenLogger.h Logger/ScreenLogger/ScreenLogger.cpp Exceptions/InvalidInputException.h Generator/GeneratorInerface.h Logger/Logger.cpp Logger/Logger.h Logger/TextLogger/TextLogger.cpp Logger/TextLogger/TextLogger.h Logger/DoubleLogger/DoubleLogger.cpp Logger/DoubleLogger/DoubleLogger.h Graphics/SplashScreen/SplashScreen.cpp Graphics/SplashScreen/SplashScreen.h InputHandling/InputHandler.cpp InputHandling/InputHandler.h Graphics/HelpScreen/HelpScreen.cpp Graphics/HelpScreen/HelpScreen.h Graphics/Info/Info.cpp Graphics/Info/Info.h Exceptions/HelpScreenException.h Exceptions/AuthorsScreenException.h Graphics/AuthorsScreen/AuthorsScreen.cpp Graphics/AuthorsScreen/AuthorsScreen.h) add_executable(WPS_pin_generator ${SOURCE_FILES}) diff --git a/Exceptions/AuthorsScreenException.h b/Exceptions/AuthorsScreenException.h new file mode 100644 index 0000000..d84bfd3 --- /dev/null +++ b/Exceptions/AuthorsScreenException.h @@ -0,0 +1,45 @@ +// +// Created by pily on 15/03/17. +// + +#ifndef WPS_PIN_GENERATOR_AUTHORSCREENEXCEPTION_H +#define WPS_PIN_GENERATOR_AUTHORSCREENEXCEPTION_H + +#include +#include + +class AuthorsScreenException : public std::exception { +public: + /** Constructor (C string) + * @param message C-style string error message. + * The string contents are copied upon construction. + * Hence, responsibility for deleting the char* lies + * with the caller. + */ + explicit AuthorsScreenException(const char *message) : errorMessage_(message) {} + + /** Constructor (C++ STL string) + * @param message The error message. + */ + explicit AuthorsScreenException(const std::string &message = "Authors screen showed") : errorMessage_(message) {} + + /** Destructor + * Virtual to allow subclassing + */ + virtual ~AuthorsScreenException() throw() {} + + /** Returns a pointer to the (constant) error description. + * + * @return + */ + const char *what() const throw() { + return errorMessage_.c_str(); + } + +private: + /** Saved error message + */ + std::string errorMessage_; +}; + +#endif //WPS_PIN_GENERATOR_AUTHORSCREENEXCEPTION_H diff --git a/Graphics/AuthorsScreen/AuthorsScreen.cpp b/Graphics/AuthorsScreen/AuthorsScreen.cpp new file mode 100644 index 0000000..27441b5 --- /dev/null +++ b/Graphics/AuthorsScreen/AuthorsScreen.cpp @@ -0,0 +1,15 @@ +// +// Created by pily on 15/03/17. +// + +#include "AuthorsScreen.h" +#include +#include "../Info/Info.h" +#include "../../lib/rang/include/rang.hpp" + +void AuthorsScreen::printAuthorsScreen() { + + std::cout << rang::style::reset << rang::fg::green << "AUTHORS" << rang::style::reset << std::endl + << Info::getAuthorInfos() + << std::endl; +} diff --git a/Graphics/AuthorsScreen/AuthorsScreen.h b/Graphics/AuthorsScreen/AuthorsScreen.h new file mode 100644 index 0000000..b8ca725 --- /dev/null +++ b/Graphics/AuthorsScreen/AuthorsScreen.h @@ -0,0 +1,13 @@ +// +// Created by pily on 15/03/17. +// + +#ifndef WPS_PIN_GENERATOR_AUTHORSSCREEN_H +#define WPS_PIN_GENERATOR_AUTHORSSCREEN_H + +class AuthorsScreen { +public: + static void printAuthorsScreen(); +}; + +#endif //WPS_PIN_GENERATOR_AUTHORSSCREEN_H diff --git a/Graphics/HelpScreen/HelpScreen.cpp b/Graphics/HelpScreen/HelpScreen.cpp index c4550ff..6cc7bc9 100644 --- a/Graphics/HelpScreen/HelpScreen.cpp +++ b/Graphics/HelpScreen/HelpScreen.cpp @@ -10,8 +10,9 @@ std::string HelpScreen::getHelpScreen() { return - "Version: " + Info::getVersion() + "\n" + - "USAGE:\n" + + "Authors:\t" + Info::getAuthorNames() + "\n" + + "Version:\t" + Info::getVersion() + "\n" + + "USAGE:\t\t" + Info::getExecutableName() + " [options]\n" + "\n" + "OPTIONS:\n" + @@ -19,6 +20,7 @@ std::string HelpScreen::getHelpScreen() { "-g\t\t\t\t\t\tUse the generator algorithm\n" + "-d\t\t\t\t\t\tUse the database\n" + "-h, --help\t\t\t\tPrint this help page\n" + + "--authors\t\tPrint authors credits\n" + "--debug\t\t\t\t\tPrint all th debug info" + "-v, --verbouse\tLog is more verbouse\n" + "--logfile [log.txt]\t\tEnables log to file (default log.txt)\n" + diff --git a/Graphics/Info/Info.cpp b/Graphics/Info/Info.cpp index f186653..65f887d 100644 --- a/Graphics/Info/Info.cpp +++ b/Graphics/Info/Info.cpp @@ -9,9 +9,16 @@ std::string Info::getVersion() { } std::string Info::getProjectName() { - return "WPS-pin-generator"; + return "WPS-pin generator"; } std::string Info::getExecutableName() { return "WPS_pin_generator"; } +std::string Info::getAuthorNames() { + return "bertof"; +} +std::string Info::getAuthorInfos() { + //TODO complete those info + return "bertof\n\tEmail:\tbertof [at] protonmail.com\n\tGitHub:\thttps://github.com/bertof"; +} diff --git a/Graphics/Info/Info.h b/Graphics/Info/Info.h index 677ec06..c272d1b 100644 --- a/Graphics/Info/Info.h +++ b/Graphics/Info/Info.h @@ -7,11 +7,30 @@ #include +/*** Centralized information about this program + */ class Info { public: + /*** Get a string with the version of the program + * @return version string + */ static std::string getVersion(); + /*** Get the full name of the project + * @return project name string + */ static std::string getProjectName(); + /*** Get the executable name + * @return executable name + */ static std::string getExecutableName(); + /*** Get author name + * @return author string + */ + static std::string getAuthorNames(); + /*** Get some info about the author + * @return author info + */ + static std::string getAuthorInfos(); }; #endif //WPS_PIN_GENERATOR_VERSION_H diff --git a/Graphics/SplashScreen/SplashScreen.cpp b/Graphics/SplashScreen/SplashScreen.cpp index 29a8e5b..e62f40d 100644 --- a/Graphics/SplashScreen/SplashScreen.cpp +++ b/Graphics/SplashScreen/SplashScreen.cpp @@ -4,7 +4,10 @@ #include "SplashScreen.h" #include +#include "../../lib/rang/include/rang.hpp" +#include "../Info/Info.h" -std::string SplashScreen::getSplashScreen() { - return "WPS Pin Generator - by Bertof"; +void SplashScreen::printSplashScreen() { + std::cout << rang::style::reset << rang::style::bold << rang::fg::cyan + << Info::getProjectName() << rang::style::reset << std::endl; } diff --git a/Graphics/SplashScreen/SplashScreen.h b/Graphics/SplashScreen/SplashScreen.h index 9c30bef..64fa290 100644 --- a/Graphics/SplashScreen/SplashScreen.h +++ b/Graphics/SplashScreen/SplashScreen.h @@ -10,7 +10,6 @@ class SplashScreen { public: static void printSplashScreen(); - static std::string getSplashScreen(); }; #endif //WPS_PIN_GENERATOR_SPASHSCREEN_H diff --git a/InputHandling/InputHandler.cpp b/InputHandling/InputHandler.cpp index 9aa3e80..4db0ce3 100644 --- a/InputHandling/InputHandler.cpp +++ b/InputHandling/InputHandler.cpp @@ -7,7 +7,9 @@ #include "../Logger/TextLogger/TextLogger.h" #include "../Logger/DoubleLogger/DoubleLogger.h" #include "../Exceptions/InvalidInputException.h" +#include "../Exceptions/AuthorsScreenException.h" #include "../Graphics/HelpScreen/HelpScreen.h" +#include "../Graphics/AuthorsScreen/AuthorsScreen.h" #include "../Generator/GeneratorInerface.h" #include "../Generator/bertofGenerator/Generator.h" @@ -82,6 +84,10 @@ void InputHandler::handle(int argc, char **argv) { else if (args[i] == "--help" || args[i] == "-h") { throw HelpScreenException(); } + // Authors dialog + else if (args[i] == "--authors") { + throw AuthorsScreenException(); + } // BSSID input else if (args[i] == "-b" || args[i] == "--bssid") { @@ -203,6 +209,8 @@ void InputHandler::handle(int argc, char **argv) { } catch (NotImplementedException e) { std::shared_ptr logger(DoubleLogger::getDoubleLogger()); logger->logError(std::string(e.what())); + } catch (AuthorsScreenException e) { + AuthorsScreen::printAuthorsScreen(); } } diff --git a/main.cpp b/main.cpp index b563f8c..9120d85 100644 --- a/main.cpp +++ b/main.cpp @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) { //TODO switch to smart pointers // Spash screen - logger->log(SplashScreen::getSplashScreen()); + SplashScreen::printSplashScreen(); // Handle input InputHandler::handle(argc, argv);