From 4aa766f885a10df8ec3caaae01def0fdf88e6cd6 Mon Sep 17 00:00:00 2001 From: Ari Madian Date: Sun, 9 Dec 2018 23:08:40 -0800 Subject: [PATCH] Big Update, See Notes - Added App.Config - Moved Config Values to Config.cs, example at Config.example.cs - Added MDBController file, nothing in it yet. - Updated AlexaCompSERVER to reflect config updates. - AlexaCompCore.cs - Added IsConnectedToInternet method to check internet connection. - Added GetInternalIP method to dynamically set ip to port map with. - Moved GetExternalIP and ReadConfig to AlexaCompCore.cs from AlexaComp.cs - AlexaComp.cs - Added check for internet connection. - Added section to set internal IP for port mapping and server. --- .gitignore | 14 +--- AlexaComp/AlexaComp.cs | 54 +++++++-------- AlexaComp/AlexaComp.csproj | 6 +- AlexaComp/AlexaCompCore.cs | 75 +++++++++++++++++++-- AlexaComp/AlexaCompSERVER.cs | 6 +- AlexaComp/App.config | 48 +++++++++++++ AlexaComp/Core/Config.example.cs | 17 +++++ AlexaComp/Core/Controllers/MDBController.cs | 10 +++ AlexaComp/Forms/LoadingScreenForm.cs | 2 +- 9 files changed, 178 insertions(+), 54 deletions(-) create mode 100644 AlexaComp/App.config create mode 100644 AlexaComp/Core/Config.example.cs create mode 100644 AlexaComp/Core/Controllers/MDBController.cs diff --git a/.gitignore b/.gitignore index de36f13..e457544 100644 --- a/.gitignore +++ b/.gitignore @@ -37,22 +37,13 @@ AlexaComp/bin AlexaComp/obj AlexaComp/pathDir.xml -AlexaComp/AlexaCompTESTS.cs -AlexaComp/SavedMethods.cs -Deprecated/ -AlexaComp/_SubProjects/CompatibilityTool/bin/Debug/Images -AlexaComp/_SubProjects/CompatibilityTool/bin/Debug/Layouts -AlexaComp/_SubProjects/CompatibilityTool/bin/Debug/x86 -AlexaComp/_SubProjects/CompatibilityTool/bin/Debug/x64 -AlexaComp/HardwareDevice.cs -AlexaComp/LightingController.cs AlexaComp/Setup *packages/ *node_modules/ -*config.json -Setup1/ +config.json setup.exe *package.zip +*Config.cs # Visual Studio 2015/2017 cache/options directory .vs/ @@ -60,7 +51,6 @@ setup.exe *.code-workspace # Config -AlexaComp/App.config *.pfx Lambda/Main_Lambda/Device_Linking_Email/config.py AlexaComp/testCommands.txt diff --git a/AlexaComp/AlexaComp.cs b/AlexaComp/AlexaComp.cs index 7739a46..1177f3e 100644 --- a/AlexaComp/AlexaComp.cs +++ b/AlexaComp/AlexaComp.cs @@ -1,9 +1,6 @@ using System; -using System.Configuration; -using System.Threading; -using System.Net; using System.Diagnostics; -using System.Text.RegularExpressions; +using System.Windows.Forms; using log4net.Config; @@ -12,8 +9,6 @@ using AlexaComp.Core.Requests; using AlexaComp.Core.Controllers; -// TODO : Documentation -// TODO : Add region tags to files where appropriate. /** Documentation format * Description * @param @@ -34,6 +29,7 @@ class AlexaComp : AlexaCompCore { Clog(exception.ToString(), "FATAL"); throw exception; }; + Clog("Exception Handler Registered"); _log.Info("Start Program"); @@ -60,34 +56,34 @@ class AlexaComp : AlexaCompCore { Clog("PathToDebug - " + pathToDebug); Clog("PathToProject - " + pathToProject); + if (!IsConnectedToInternet()) { + Clog("No Internet Connection Detected... Quitting...", "FATAL"); + MessageBox.Show("No Internet Connection Detected, Stopping AlexaComp...", + "AlexaComp", + MessageBoxButtons.OK, + MessageBoxIcon.Warning); + StopApplication(); + } + + // Set Internal IP for port mapping and server + try { + string host = GetInternalIP(); + Clog("Internal IP Found - " + host); + ServerConfig.HOST = host; + } catch (Exception) { + Clog("No Network Adapters With IPv4 Addresses Detected, Cannot Start Server... Quitting...", "FATAL"); + MessageBox.Show("No Network Adapters With IPv4 Addresses Detected, Cannot Start Server. Stopping AlexaComp...", + "AlexaComp", + MessageBoxButtons.OK, + MessageBoxIcon.Warning); + StopApplication(); + } + GetExternalIP(); Clog("Initializing Hardware Sensors"); loadingScreenThread.Start(timer); } - - - /* - * Reads all user configured values into the settingsDict. - */ - public static void ReadConfig(){ - foreach (string key in ConfigurationManager.AppSettings.AllKeys){ - settingsDict[key] = GetConfigValue(key); - } - } - - /* - * Gets the user's public IP address. - */ - public static void GetExternalIP() { - string data = new WebClient().DownloadString("http://checkip.dyndns.org/"); - Match match = Regex.Match(data, @"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b"); // Regex match for IP - if (match.Success) { - Clog("Public IP - " + match); - } else { - Clog("IP Regex Match Unsuccessful.", "ERROR"); - } - } } } diff --git a/AlexaComp/AlexaComp.csproj b/AlexaComp/AlexaComp.csproj index fe11470..9f49dbf 100644 --- a/AlexaComp/AlexaComp.csproj +++ b/AlexaComp/AlexaComp.csproj @@ -5,7 +5,7 @@ Debug AnyCPU {B8379353-325F-4858-9E4A-0E9FD4357872} - Exe + WinExe AlexaComp AlexaComp v4.6.1 @@ -32,7 +32,7 @@ Ari Madian true 0 - 1.0.2.0 + 1.0.3.0 false true true @@ -170,7 +170,9 @@ + + diff --git a/AlexaComp/AlexaCompCore.cs b/AlexaComp/AlexaCompCore.cs index de88ff7..9d3fe9a 100644 --- a/AlexaComp/AlexaCompCore.cs +++ b/AlexaComp/AlexaCompCore.cs @@ -2,17 +2,15 @@ using System.Collections.Generic; using System.Configuration; using AlexaComp.Controllers; -using System.Diagnostics; using System.Linq; -using System.Text; +using System.Text.RegularExpressions; +using System.Net; using System.Threading; -using System.IO; -using System.Security.Cryptography; using log4net; -using log4net.Config; using AlexaComp.Core; +using System.Net.Sockets; namespace AlexaComp { @@ -62,11 +60,40 @@ public class AlexaCompCore { public static void StopApplication() { Clog("CLOSING PROGRAM"); stopProgramFlag = true; - AlexaCompSERVER.StopServer(); - AlexaCompSERVER.DelPortMap(); + try { + AlexaCompSERVER.StopServer(); + } catch (NullReferenceException) { + Clog("NullReferenceException Caught When Stopping Server"); + } catch (Exception e) { + Clog("Exception Caught When Stopping Server \n" + e); + } + + try { + AlexaCompSERVER.DelPortMap(); + } catch (NullReferenceException) { + Clog("NullReferenceException Caught When Deleting Port Maps"); + } catch (Exception e) { + Clog("Exception Caught When Deleting Port Maps\n" + e); + } + Clog("Exiting..."); Environment.Exit(1); } + public static bool IsConnectedToInternet(bool log = true) { + Clog("Checking For Internet Connection..."); + try { + using (var client = new WebClient()) + using (client.OpenRead("http://clients3.google.com/generate_204")) { + if (log) { Clog("Internet Connection DOES exist."); } + return true; + } + } + catch { + if (log) { Clog("Internet Connection DOES NOT exist."); } + return false; + } + } + /// /// Encryption Methods From: https://odan.github.io/2017/08/10/aes-256-encryption-and-decryption-in-php-and-csharp.html /// @@ -80,6 +107,40 @@ public class AlexaCompCore { return ConfigurationManager.AppSettings[key]; } + + /* + * Gets the user's public IP address. + */ + public static void GetExternalIP() { + string data = new WebClient().DownloadString("http://checkip.dyndns.org/"); + Match match = Regex.Match(data, @"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b"); // Regex match for IP + if (match.Success) { + Clog("Public IP - " + match); + } + else { + Clog("IP Regex Match Unsuccessful.", "ERROR"); + } + } + + public static string GetInternalIP() { + var host = Dns.GetHostEntry(Dns.GetHostName()); + foreach (var ip in host.AddressList) { + if (ip.AddressFamily == AddressFamily.InterNetwork) { + return ip.ToString(); + } + } + throw new Exception(); + } + + /* + * Reads all user configured values into the settingsDict. + */ + public static void ReadConfig() { + foreach (string key in ConfigurationManager.AppSettings.AllKeys) { + settingsDict[key] = GetConfigValue(key); + } + } + #endregion } } diff --git a/AlexaComp/AlexaCompSERVER.cs b/AlexaComp/AlexaCompSERVER.cs index 190b47b..f5c4f20 100644 --- a/AlexaComp/AlexaCompSERVER.cs +++ b/AlexaComp/AlexaCompSERVER.cs @@ -22,9 +22,9 @@ class AlexaCompSERVER : AlexaCompCore { public static TcpClient client; private static NetworkStream nwStream; - public static int PORT = int.Parse(GetConfigValue("PORT")); - public static string AUTH = GetConfigValue("AUTH"); - public static string HOST = "10.0.0.59"; + public static int PORT = ServerConfig.PORT; + public static string AUTH = ServerConfig.LambdaAuth; + public static string HOST = ServerConfig.HOST; private static INatDevice device; #endregion diff --git a/AlexaComp/App.config b/AlexaComp/App.config new file mode 100644 index 0000000..556bf80 --- /dev/null +++ b/AlexaComp/App.config @@ -0,0 +1,48 @@ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexaComp/Core/Config.example.cs b/AlexaComp/Core/Config.example.cs new file mode 100644 index 0000000..ed93a19 --- /dev/null +++ b/AlexaComp/Core/Config.example.cs @@ -0,0 +1,17 @@ +namespace AlexaComp.Core { + + public static class MDBConfig{ + public const string APIURI = ""; + public const string APIAUTH = ""; + } + + public static class LambdaConfig{ + public const string LambdaAuth = ""; + } + + public static class ServerConfig { + public const int PORT; + public static string HOST; + public const string LambdaAuth = ""; + } +} diff --git a/AlexaComp/Core/Controllers/MDBController.cs b/AlexaComp/Core/Controllers/MDBController.cs new file mode 100644 index 0000000..a1f2c36 --- /dev/null +++ b/AlexaComp/Core/Controllers/MDBController.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AlexaComp.Core.Controllers { + class MDBController { + } +} diff --git a/AlexaComp/Forms/LoadingScreenForm.cs b/AlexaComp/Forms/LoadingScreenForm.cs index 6a1724d..9315726 100644 --- a/AlexaComp/Forms/LoadingScreenForm.cs +++ b/AlexaComp/Forms/LoadingScreenForm.cs @@ -53,7 +53,7 @@ public partial class LoadingScreenForm : Form { UpdateProgress("Starting Server Loop"); AlexaCompCore.ServerLoopThread.Start(); - UpdateProgress("Starting AleComp", 400); + UpdateProgress("Starting AlexaComp", 400); CloseSplashScreen(); AlexaComp.AppWindowThread.Start();