Compare commits

...

2 Commits

Author SHA1 Message Date
hvolmer
85c961e3cc Many attempts to get websocket to talk secure 2020-03-27 19:00:10 -06:00
hvolmer
7f475c1716 MC: Catch https redirect and log error; first attempts at https auth 2020-03-27 16:14:07 -06:00
2 changed files with 129 additions and 35 deletions

View File

@@ -10,6 +10,7 @@ using Crestron.SimplSharpPro.CrestronThread;
using Crestron.SimplSharp.CrestronWebSocketClient;
using Crestron.SimplSharpPro;
using Crestron.SimplSharp.Net.Http;
using Crestron.SimplSharp.Net.Https;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -36,6 +37,8 @@ namespace PepperDash.Essentials
public MobileControlConfig Config { get; private set; }
public string Host { get; private set; }
Dictionary<string, Object> ActionDictionary = new Dictionary<string, Object>(StringComparer.InvariantCultureIgnoreCase);
Dictionary<string, CTimer> PushedActions = new Dictionary<string, CTimer>();
@@ -73,6 +76,12 @@ namespace PepperDash.Essentials
{
Config = config;
Host = config.ServerUrl;
if (!Host.StartsWith("http"))
{
Host = "https://" + Host;
}
SystemUuid = ConfigReader.ConfigObject.SystemUuid;
Debug.Console(0, this, "Mobile UI controller initializing for server:{0}", config.ServerUrl);
@@ -241,67 +250,143 @@ namespace PepperDash.Essentials
/// <param name="command"></param>
void AuthorizeSystem(string code)
{
//SystemUuid = ConfigReader.ConfigObject.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;
}
if (string.IsNullOrEmpty(code))
{
CrestronConsole.ConsoleCommandResponse("Please enter a user code to authorize a system");
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))
{
CrestronConsole.ConsoleCommandResponse("Config URL address is not set. Check portal configuration");
CrestronConsole.ConsoleCommandResponse("Mobile control API address is not set. Check portal configuration");
return;
}
try
{
req.Url.Parse(url);
new HttpClient().DispatchAsync(req, (r, e) =>
string path = string.Format("/api/system/grantcode/{0}/{1}", code, SystemUuid);
string url = string.Format("{0}{1}", Host, path);
Debug.Console(0, this, "Authorizing to: {0}", url);
if (Host.StartsWith("https:"))
{
CheckHttpDebug(r, e);
if (e == HTTP_CALLBACK_ERROR.COMPLETED)
var req = new HttpsClientRequest();
req.Url.Parse(url);
var c = new HttpsClient();
Debug.Console(0, " host and peer verification disabled");
c.HostVerification = false;
c.PeerVerification = false;
c.Verbose = true;
c.DispatchAsync(req, (r, e) =>
{
if (r.Code == 200)
if (e == HTTPS_CALLBACK_ERROR.COMPLETED)
{
Debug.Console(0, "System authorized, sending config.");
#warning This registration may need to wait for config ready. Maybe.
RegisterSystemToServer();
}
else if (r.Code == 404)
{
if (r.ContentString.Contains("codeNotFound"))
if (r.Code == 200)
{
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",
SystemUuid);
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
{
Debug.Console(0, "https authorization failed, code {0}: {1}", r.Code, r.ContentString);
}
}
else
{
Debug.Console(0, "Authorization failed, code {0}: {1}", r.Code, r.ContentString);
if (r != null)
{
Debug.Console(0, this, "Error in https authorization (A) {0}: {1}", r.Code, e);
}
else
{
Debug.Console(0, this, "Error in https 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)
{
Debug.Console(0, this, "Error in authorizing: {0}", e);
Debug.Console(0, this, "Error in authorizing (C): {0}", e);
}
}
@@ -355,7 +440,6 @@ namespace PepperDash.Essentials
void ConnectWebsocketClient()
{
Debug.Console(1, this, "Initializing Stream client to server.");
if (WSClient != null)
{
@@ -364,7 +448,15 @@ namespace PepperDash.Essentials
}
WSClient = new WebSocketClient();
WSClient.URL = string.Format("wss://{0}/system/join/{1}", Config.ServerUrl, this.SystemUuid);
var wsHost = Host.Replace("http", "ws");
WSClient.URL = string.Format("{0}/system/join/{1}", wsHost, this.SystemUuid);
Debug.Console(1, this, "Initializing mobile control client to {0}", WSClient.URL);
if(wsHost.StartsWith("wss"))
{
WSClient.SSL = true;
Debug.Console(0, this, "Using secure websocket, cert verification disabled");
WSClient.VerifyServerCertificate = false;
}
WSClient.ConnectionCallBack = Websocket_ConnectCallback;
WSClient.ConnectAsync();
}
@@ -391,6 +483,7 @@ namespace PepperDash.Essentials
}
else
{
Debug.Console(1, this, "Websocket protocol: {0}", WSClient.Protocol);
if (code == WebSocketClient.WEBSOCKET_RESULT_CODES.WEBSOCKET_CLIENT_HTTP_HANDSHAKE_TOKEN_ERROR)
{
// This is the case when app server is running behind a websever and app server is down

View File

@@ -415,7 +415,8 @@ namespace PepperDash.Essentials
// Deal with any .cplz files
UnzipAndMoveCplzArchives();
if(Directory.Exists(_loadedPluginsDirectoryPath) {
if(Directory.Exists(_loadedPluginsDirectoryPath))
{
// Load the assemblies from the loadedPlugins folder into the AppDomain
LoadPluginAssemblies();