mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 04:34:56 +00:00
MC: Catch https redirect and log error; first attempts at https auth
This commit is contained in:
@@ -10,6 +10,7 @@ using Crestron.SimplSharpPro.CrestronThread;
|
|||||||
using Crestron.SimplSharp.CrestronWebSocketClient;
|
using Crestron.SimplSharp.CrestronWebSocketClient;
|
||||||
using Crestron.SimplSharpPro;
|
using Crestron.SimplSharpPro;
|
||||||
using Crestron.SimplSharp.Net.Http;
|
using Crestron.SimplSharp.Net.Http;
|
||||||
|
using Crestron.SimplSharp.Net.Https;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
@@ -36,6 +37,8 @@ namespace PepperDash.Essentials
|
|||||||
|
|
||||||
public MobileControlConfig Config { get; private set; }
|
public MobileControlConfig Config { get; private set; }
|
||||||
|
|
||||||
|
public string Host { get; private set; }
|
||||||
|
|
||||||
Dictionary<string, Object> ActionDictionary = new Dictionary<string, Object>(StringComparer.InvariantCultureIgnoreCase);
|
Dictionary<string, Object> ActionDictionary = new Dictionary<string, Object>(StringComparer.InvariantCultureIgnoreCase);
|
||||||
|
|
||||||
Dictionary<string, CTimer> PushedActions = new Dictionary<string, CTimer>();
|
Dictionary<string, CTimer> PushedActions = new Dictionary<string, CTimer>();
|
||||||
@@ -73,6 +76,12 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
Config = config;
|
Config = config;
|
||||||
|
|
||||||
|
Host = config.ServerUrl;
|
||||||
|
if (!Host.StartsWith("http"))
|
||||||
|
{
|
||||||
|
Host = "https://" + Host;
|
||||||
|
}
|
||||||
|
|
||||||
SystemUuid = ConfigReader.ConfigObject.SystemUuid;
|
SystemUuid = ConfigReader.ConfigObject.SystemUuid;
|
||||||
|
|
||||||
Debug.Console(0, this, "Mobile UI controller initializing for server:{0}", config.ServerUrl);
|
Debug.Console(0, this, "Mobile UI controller initializing for server:{0}", config.ServerUrl);
|
||||||
@@ -241,67 +250,148 @@ namespace PepperDash.Essentials
|
|||||||
/// <param name="command"></param>
|
/// <param name="command"></param>
|
||||||
void AuthorizeSystem(string code)
|
void AuthorizeSystem(string code)
|
||||||
{
|
{
|
||||||
//SystemUuid = ConfigReader.ConfigObject.SystemUuid;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(SystemUuid))
|
if (string.IsNullOrEmpty(SystemUuid))
|
||||||
{
|
{
|
||||||
CrestronConsole.ConsoleCommandResponse("System does not have a UUID. Please ensure proper portal-format configuration is loaded and restart.");
|
CrestronConsole.ConsoleCommandResponse("System does not have a UUID. Please ensure proper configuration is loaded and restart.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(code))
|
if (string.IsNullOrEmpty(code))
|
||||||
{
|
{
|
||||||
CrestronConsole.ConsoleCommandResponse("Please enter a user code to authorize a system");
|
CrestronConsole.ConsoleCommandResponse("Please enter a user code to authorize a system");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var req = new HttpClientRequest();
|
|
||||||
string url = string.Format("http://{0}/api/system/grantcode/{1}/{2}", Config.ServerUrl, code, SystemUuid);
|
|
||||||
Debug.Console(0, this, "Authorizing to: {0}", url);
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(Config.ServerUrl))
|
if (string.IsNullOrEmpty(Config.ServerUrl))
|
||||||
{
|
{
|
||||||
CrestronConsole.ConsoleCommandResponse("Config URL address is not set. Check portal configuration");
|
CrestronConsole.ConsoleCommandResponse("Mobile control API address is not set. Check portal configuration");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
req.Url.Parse(url);
|
string path = string.Format("/api/system/grantcode/{0}/{1}", code, SystemUuid);
|
||||||
new HttpClient().DispatchAsync(req, (r, e) =>
|
string url = string.Format("{0}{1}", Host, path);
|
||||||
|
Debug.Console(0, this, "Authorizing to: {0}", url);
|
||||||
|
|
||||||
|
if (Host.StartsWith("https:"))
|
||||||
{
|
{
|
||||||
CheckHttpDebug(r, e);
|
var req = new HttpsClientRequest();
|
||||||
if (e == HTTP_CALLBACK_ERROR.COMPLETED)
|
req.Url.Parse(url);
|
||||||
|
var c = new HttpsClient();
|
||||||
|
Debug.Console(0, " host and peer verification disabled");
|
||||||
|
c.HostVerification = false;
|
||||||
|
c.PeerVerification = false;
|
||||||
|
c.DispatchAsync(req, (r, e) =>
|
||||||
{
|
{
|
||||||
if (r.Code == 200)
|
if (e == HTTPS_CALLBACK_ERROR.COMPLETED)
|
||||||
{
|
{
|
||||||
Debug.Console(0, "System authorized, sending config.");
|
if (r.Code == 200)
|
||||||
#warning This registration may need to wait for config ready. Maybe.
|
|
||||||
RegisterSystemToServer();
|
|
||||||
}
|
|
||||||
else if (r.Code == 404)
|
|
||||||
{
|
|
||||||
if (r.ContentString.Contains("codeNotFound"))
|
|
||||||
{
|
{
|
||||||
Debug.Console(0, "Authorization failed, code not found for system UUID {0}", SystemUuid);
|
Debug.Console(0, "System authorized, sending config.");
|
||||||
|
RegisterSystemToServer();
|
||||||
}
|
}
|
||||||
else if (r.ContentString.Contains("uuidNotFound"))
|
else if (r.Code == 404)
|
||||||
{
|
{
|
||||||
Debug.Console(0, "Authorization failed, uuid {0} not found. Check Essentials configuration is correct",
|
if (r.ContentString.Contains("codeNotFound"))
|
||||||
SystemUuid);
|
{
|
||||||
|
Debug.Console(0, "Authorization failed, code not found for system UUID {0}", SystemUuid);
|
||||||
|
}
|
||||||
|
else if (r.ContentString.Contains("uuidNotFound"))
|
||||||
|
{
|
||||||
|
Debug.Console(0, "Authorization failed, uuid {0} not found. Check Essentials configuration is correct",
|
||||||
|
SystemUuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (r.Code == 301)
|
||||||
|
{
|
||||||
|
var newUrl = r.Header.GetHeaderValue("Location");
|
||||||
|
var newHostValue = newUrl.Substring(0, newUrl.IndexOf(path));
|
||||||
|
Debug.Console(0, this, "ERROR: Mobile control API has moved. Please adjust configuration to \"{0}\"", newHostValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Console(0, "http authorization failed, code {0}: {1}", r.Code, r.ContentString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(0, "Authorization failed, code {0}: {1}", r.Code, r.ContentString);
|
if (r != null)
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, "Error in http authorization (A) {0}: {1}", r.Code, e);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, "Error in http authorization (B) {0}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
else
|
}
|
||||||
Debug.Console(0, this, "Error {0} in authorizing system", e);
|
|
||||||
});
|
else
|
||||||
|
{
|
||||||
|
var req = new HttpClientRequest();
|
||||||
|
req.Url.Parse(url);
|
||||||
|
|
||||||
|
var c = new HttpClient();
|
||||||
|
c.AllowAutoRedirect = false;
|
||||||
|
c.DispatchAsync(req, (r, e) =>
|
||||||
|
{
|
||||||
|
CheckHttpDebug(r, e);
|
||||||
|
if (e == HTTP_CALLBACK_ERROR.COMPLETED)
|
||||||
|
{
|
||||||
|
if (r.Code == 200)
|
||||||
|
{
|
||||||
|
Debug.Console(0, "System authorized, sending config.");
|
||||||
|
RegisterSystemToServer();
|
||||||
|
}
|
||||||
|
else if (r.Code == 404)
|
||||||
|
{
|
||||||
|
if (r.ContentString.Contains("codeNotFound"))
|
||||||
|
{
|
||||||
|
Debug.Console(0, "Authorization failed, code not found for system UUID {0}", SystemUuid);
|
||||||
|
}
|
||||||
|
else if (r.ContentString.Contains("uuidNotFound"))
|
||||||
|
{
|
||||||
|
Debug.Console(0, "Authorization failed, uuid {0} not found. Check Essentials configuration is correct",
|
||||||
|
SystemUuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (r.Code == 301)
|
||||||
|
{
|
||||||
|
var newUrl = r.Header.GetHeaderValue("Location");
|
||||||
|
var newHostValue = newUrl.Substring(0, newUrl.IndexOf(path));
|
||||||
|
Debug.Console(0, this, "ERROR: Mobile control API has moved. Please adjust configuration to \"{0}\"", newHostValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Console(0, "http authorization failed, code {0}: {1}", r.Code, r.ContentString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (r != null)
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, "Error in http authorization (A) {0}: {1}", r.Code, e);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, "Error in http authorization (B) {0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.Console(0, this, "Error in authorizing: {0}", e);
|
Debug.Console(0, this, "Error in authorizing (C): {0}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -415,7 +415,8 @@ namespace PepperDash.Essentials
|
|||||||
// Deal with any .cplz files
|
// Deal with any .cplz files
|
||||||
UnzipAndMoveCplzArchives();
|
UnzipAndMoveCplzArchives();
|
||||||
|
|
||||||
if(Directory.Exists(_loadedPluginsDirectoryPath) {
|
if(Directory.Exists(_loadedPluginsDirectoryPath))
|
||||||
|
{
|
||||||
// Load the assemblies from the loadedPlugins folder into the AppDomain
|
// Load the assemblies from the loadedPlugins folder into the AppDomain
|
||||||
LoadPluginAssemblies();
|
LoadPluginAssemblies();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user