From 39ca4722c5fdf47a4d64f2f915b6fe816c2cb008 Mon Sep 17 00:00:00 2001 From: Markus Date: Sat, 31 Aug 2019 19:57:42 +0200 Subject: [PATCH] add sets5ProxytoSettings Function to default --- parameterManager.cpp | 124 ++++++++++++++++++++++++++++--------------- parameterManager.h | 19 +++++++ 2 files changed, 99 insertions(+), 44 deletions(-) diff --git a/parameterManager.cpp b/parameterManager.cpp index 9ff65dc..0e0a3c3 100644 --- a/parameterManager.cpp +++ b/parameterManager.cpp @@ -137,31 +137,15 @@ int loadDefaulOptions(Settings &settings) std::string what = line.substr(0, line.find("=")), data = line.substr(line.find("=") + 1, line.length() - line.find("=") -1); if(what == "") { - } else if (what == "Socks5Proxy") { ///--- - if(data.find(":") == std::string::npos) { - std::cout << " => Error_Defaults: Ungültiger Socks5 Proxy: '" << data << "'" << std::endl; - return 31; - } - std::string ip = data.substr(0, data.find(":")); - std::string portStr = data.substr(data.find(":") + 1, data.length() - data.find(":")); - int port = atoi(portStr.c_str()); - if(std::to_string(port) != portStr || port <= 0) { - std::cout << " => Error_Defaults: Socks5 Proxy: Invalid Port: " << portStr << std::endl; - return 32; - } else if (ip == "") { - std::cout << " => Error_Defaults: Socks5 Proxy: Invalid Ip Addresse: " << ip << std::endl; - return 34; - } else if(settings.debugMode) { - std::cout << " > Defaults: Proxy Addresse: "<< ip << ":" << port << std::endl; - } - settings.proxy_ip = ip; - settings.proxy_port = port; + } else if (what == "Socks5Proxy") { + if(setS5ProxytoSettings(settings, data) != 0) + return 140; - } else if (what == "DefaultFileVersion") { ///--- + } else if (what == "DefaultFileVersion") { if(data != settings.defaultFileVersion) std::cout << " => Warnung: Veraltetes Defaults-File. Löschen sie die Datei, um die neuste Version zu bekommen." << std::endl; - } else if (what == "DebugModus") { ///--- + } else if (what == "DebugModus") { if(data == "true") settings.debugMode=true; else if (data == "false") @@ -311,29 +295,9 @@ int unterOption_default(Settings *settings, int argc, char ** argv) while( ( c = getopt_long (argc, argv, "p:g:l:o:e:E:s:S:C:m:D:t:hcd", long_opts, nullptr) ) != -1 ) { switch(c) { - case 'p': { - if(!optarg || std::string(optarg) == "") - break; - std::string optarg = ::optarg; - if(optarg.find(":") == std::string::npos) { - std::cout << "Invalid Socks5 Proxy: " << optarg << std::endl; - return 31; - } - std::string ip = optarg.substr(0, optarg.find(":")); - std::string portStr = optarg.substr(optarg.find(":") + 1, optarg.length() - optarg.find(":")); - int port = atoi(portStr.c_str()); - if(std::to_string(port) != portStr || port <= 0) { - std::cout << " => Error: [-p]: Invalid Port: " << portStr << std::endl; - return 32; - } else if (ip == "") { - std::cout << " => Error: [-p]: Invalid Ip Addresse: " << ip << std::endl; - return 34; - } else if(settings->debugMode) { - std::cout << "Proxy Addresse: "<< ip << ":" << port << std::endl; - } - settings->proxy_ip = ip; - settings->proxy_port = port; - } + case 'p': + if(optarg && setS5ProxytoSettings(*settings, optarg) != 0) + return 2; break; case 'g': if(optarg) @@ -1034,3 +998,75 @@ bool nothingExists(std::string path) struct stat buffer; return (stat(path.c_str(), &buffer) == 0) ? false : true; } + +bool isNumber(std::string number) +{ + for (size_t i = 0; i < number.length(); ++i) { + if( number[i] == '0') { + number.erase(i, 1); + i--; + } + } + return (std::to_string(atoll(number.c_str())) == number) ? true : false; +} + + +int setS5ProxytoSettings(Settings &settings, std::string Optarg) +{ + std::string optarg = Optarg; + //Ersetze localhost mit 127.0.0.1 + if(optarg.find("localhost") != std::string::npos) + optarg.insert(optarg.find("localhost"), "127.0.0.1").erase(optarg.find("localhost"), 9); + + //Wenn optarg keine vollständige Addresse ( ip:port ) ist: + if(optarg.find(":") == std::string::npos) { + //Wenn optarg eine Zahl ist, also Port: + if(isNumber(optarg)) { + settings.proxy_port = atoi(optarg.c_str()); + //Wenn es scheint eine Ip zu sein, also mit '.': + } else if (optarg.find(".") != std::string::npos) { + settings.proxy_ip = optarg; + // Sonst error: + } else { + std::cout << " => Error: Ungültige Socks5 Proxy Addresse: " << Optarg << std::endl; + return 1; + } + // Wenn vollständige Addr: + } else { + //setze schein Ip: + settings.proxy_ip = optarg.substr(0, optarg.find(":")); + //setze Port, falls dieser eine Zahl ist: + std::string tmpPort = optarg.substr(optarg.find(":") + 1, optarg.length() - optarg.find(":")); + if(isNumber(tmpPort)) + settings.proxy_port = atoi(optarg.substr(optarg.find(":") + 1, optarg.length() - optarg.find(":")).c_str()); + else { + std::cout << " => Error: Ungültiger Socks5 Proxy Port: " << tmpPort << std::endl; + return 2; + } + } + //Überprüfe ob Ip aus 4 * '.' besteht und dann eine Zahl ist: + std::string tmpIp = settings.proxy_ip; + for (int i = 0; i < 4; ++i) { + if(tmpIp.find(".") != std::string::npos) + tmpIp.erase(static_cast(i), 1); + else { + std::cout << " => Error: Ungültige Socks5 Proxy Ip Addresse: " << settings.proxy_ip << std::endl; + return 3; + } + } + if(!isNumber(tmpIp)) { + std::cout << " => Error: Ungültige Socks5 Proxy Ip Addresse: " << settings.proxy_ip << std::endl; + return 4; + // Überprüfe ob Port Positiv oder 0 und kliner als 2 Byte ist: + } else if (settings.proxy_port < 0 || settings.proxy_port > 65535) { + std::cout << " => Error: Ungültiger Socks5 Proxy Port: " << settings.proxy_port << std::endl; + return 5; + //Debug Nachricht: + } else if(settings.debugMode) + std::cout << " > Defaults: Proxy Addresse: "<< settings.proxy_ip << ":" << settings.proxy_port << std::endl; + + return 0; +} + + + diff --git a/parameterManager.h b/parameterManager.h index 3c0672d..3a6bcb0 100644 --- a/parameterManager.h +++ b/parameterManager.h @@ -63,6 +63,7 @@ struct Settings { int manageParameter(Settings &settings, int argc, char ** argv); int loadDefaulOptions(Settings & settings); std::vector compare(std::string All_Options_with_komma_between, std::string input); +bool isNumber(std::string number); std::string getProgramName(); std::string getexepath(); @@ -91,4 +92,22 @@ void unterOption_info_help(); void unterOption_clean(Settings * settings, int argc, char **argv); +int setS5ProxytoSettings(Settings &settings, std::string optarg); + + + + + + + + + + + + + + + + + #endif // PARAMETERMANAGER_H