Skip to content
This repository has been archived by the owner on Dec 1, 2020. It is now read-only.

Commit

Permalink
Big Update, See Notes
Browse files Browse the repository at this point in the history
 - 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.
  • Loading branch information
Ari Madian committed Dec 10, 2018
1 parent 6db9ac9 commit 4aa766f
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 54 deletions.
14 changes: 2 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,20 @@
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/
.vscode/
*.code-workspace

# Config
AlexaComp/App.config
*.pfx
Lambda/Main_Lambda/Device_Linking_Email/config.py
AlexaComp/testCommands.txt
Expand Down
54 changes: 25 additions & 29 deletions AlexaComp/AlexaComp.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 <paramname> <description>
Expand All @@ -34,6 +29,7 @@ class AlexaComp : AlexaCompCore {
Clog(exception.ToString(), "FATAL");
throw exception;
};

Clog("Exception Handler Registered");

_log.Info("Start Program");
Expand All @@ -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");
}
}
}
}
6 changes: 4 additions & 2 deletions AlexaComp/AlexaComp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B8379353-325F-4858-9E4A-0E9FD4357872}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<RootNamespace>AlexaComp</RootNamespace>
<AssemblyName>AlexaComp</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
Expand All @@ -32,7 +32,7 @@
<PublisherName>Ari Madian</PublisherName>
<AutorunEnabled>true</AutorunEnabled>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.2.0</ApplicationVersion>
<ApplicationVersion>1.0.3.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<CreateDesktopShortcut>true</CreateDesktopShortcut>
<PublishWizardCompleted>true</PublishWizardCompleted>
Expand Down Expand Up @@ -170,7 +170,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AlexaCompCore.cs" />
<Compile Include="Core\Config.cs" />
<Compile Include="Core\Controllers\HardwareController.cs" />
<Compile Include="Core\Controllers\MDBController.cs" />
<Compile Include="Core\Extensions.cs" />
<Compile Include="Core\Hardware.cs" />
<Compile Include="Core\Request.cs" />
Expand Down
75 changes: 68 additions & 7 deletions AlexaComp/AlexaCompCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}
}

/// <summary>
/// Encryption Methods From: https://odan.github.io/2017/08/10/aes-256-encryption-and-decryption-in-php-and-csharp.html
/// </summary>
Expand All @@ -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
}
}
6 changes: 3 additions & 3 deletions AlexaComp/AlexaCompSERVER.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions AlexaComp/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="Logs\AlexaComp-%date{yyyy-MM-dd;HH-mm,ss}.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%2thread] %-5level - %message%newline" />
</layout>
</appender>
<appender name="SensorListAppender" type="log4net.Appender.FileAppender">
<file value="SensorList.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root>
<logger additivity="false" name="SensorListAppender">
<level value="Debug" />
<appender-ref ref="SensorListAppender" />
</logger>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
17 changes: 17 additions & 0 deletions AlexaComp/Core/Config.example.cs
Original file line number Diff line number Diff line change
@@ -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 = "";
}
}
10 changes: 10 additions & 0 deletions AlexaComp/Core/Controllers/MDBController.cs
Original file line number Diff line number Diff line change
@@ -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 {
}
}
2 changes: 1 addition & 1 deletion AlexaComp/Forms/LoadingScreenForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4aa766f

Please sign in to comment.