Skip to content
This repository has been archived by the owner on Jan 9, 2019. It is now read-only.

Commit

Permalink
Add ability to get logs via the API
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Sampson committed Dec 19, 2016
1 parent 44cfd17 commit 1533c72
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ var sensor in
break;

case HardwareType.GpuAti:
Console.WriteLine("ATI");
foreach (
var sensor in
hardwareItem.Sensors.Where(
Expand Down
90 changes: 72 additions & 18 deletions RemoteTaskServer/Api/Network/PacketHandlers/ServerPacketHandler.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#region

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using UlteriusServer.Api.Network.Messages;
using UlteriusServer.Utilities;
using UlteriusServer.Utilities.Security;
using UlteriusServer.Utilities.Settings;
using UlteriusServer.WebSocketAPI.Authentication;
using vtortola.WebSockets;
using Version = System.Version;

#endregion

Expand All @@ -22,16 +22,15 @@ namespace UlteriusServer.Api.Network.PacketHandlers
public class ServerPacketHandler : PacketHandler
{
private AuthClient _authClient;
private WebSocket _client;
private MessageBuilder _builder;
private WebSocket _client;
private Packet _packet;


public void AesHandshake()
{
try
{

var connectionId = CookieManager.GetConnectionId(_client);
AuthClient authClient;
UlteriusApiServer.AllClients.TryGetValue(connectionId, out authClient);
Expand Down Expand Up @@ -82,23 +81,22 @@ public void ListPorts()
var screenSharePort = config.ScreenShareService.ScreenSharePort;
var portData = new
{
webServerPort,
apiPort,
webcamPort,
terminalPort,
screenSharePort
webServerPort,
apiPort,
webcamPort,
terminalPort,
screenSharePort
};
_builder.WriteMessage(portData);
}

public void Login()
{

var authenticated = false;
var connectionId = CookieManager.GetConnectionId(_client);
var password = _packet.Args[0].ToString();
authenticated = !string.IsNullOrEmpty(password) && AuthUtils.Authenticate(password);

AuthClient authClient;
UlteriusApiServer.AllClients.TryGetValue(connectionId, out authClient);
if (authClient != null)
Expand All @@ -123,20 +121,66 @@ public void Login()
_builder.WriteMessage(authenticationData);
}



public void CheckForUpdate()
{
var versionInfo = new
{
productVersion = new Version(System.Windows.Forms.Application.ProductVersion)
productVersion = new Version(Application.ProductVersion)
};
_builder.WriteMessage(versionInfo);
}


public void GetLogs()
{
string logData;
var stringBuilder = new StringBuilder();
try
{
using (
var fs = new FileStream(Path.Combine(AppEnvironment.DataPath, "server.log"), FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(fs, Encoding.Default))
{
while (!sr.EndOfStream)
{
var line = sr.ReadLine();
if (line != null)
{
stringBuilder.AppendLine(line);
}
}
logData = stringBuilder.ToString();
}
}
catch (Exception)
{
logData = string.Empty;
}
var logsPath = Path.Combine(AppEnvironment.DataPath, "Logs");
var exceptionList = new List<ExceptionModel>();
if (Directory.Exists(logsPath))
{
exceptionList = (from filePath in Directory.GetFiles(logsPath)
let fileName = Path.GetFileName(filePath)?.Split(Convert.ToChar("_"))
select new ExceptionModel
{
Type = fileName[0],
Date = fileName[1].Replace(".json", ""),
Json = File.ReadAllText(filePath)
}).ToList();
}
var debugInfo = new
{
serverLog = logData,
exceptions = exceptionList
};
_builder.WriteMessage(debugInfo);
}

public void RestartServer()
{

if (UlteriusApiServer.RunningAsService)
{
var restartServiceScript = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
Expand All @@ -150,7 +194,7 @@ public void RestartServer()
else
{
var restartServerScript = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"restartulterius.bat");
"restartulterius.bat");
var startInfo = new ProcessStartInfo(restartServerScript)
{
WindowStyle = ProcessWindowStyle.Minimized
Expand All @@ -177,12 +221,22 @@ public override void HandlePacket(Packet packet)
RestartServer();
break;
case PacketManager.PacketTypes.ListPorts:
ListPorts();
ListPorts();
break;
case PacketManager.PacketTypes.CheckVersion:
CheckForUpdate();
break;
case PacketManager.PacketTypes.GetLogs:
GetLogs();
break;
}
}

public class ExceptionModel
{
public string Type { get; set; }
public string Json { get; set; }
public string Date { get; set; }
}
}
}
}
5 changes: 4 additions & 1 deletion RemoteTaskServer/Api/Network/PacketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public enum PacketTypes
RightUp,
ChangeDisplayResolution,
RotateDisplay,
SetPrimaryDisplay
SetPrimaryDisplay,
GetLogs
}

#endregion
Expand Down Expand Up @@ -269,6 +270,8 @@ public PacketInfo GetPacketInfo(string endpoint)
return new PacketInfo {Type = PacketTypes.AesHandshake, Handler = typeof(ServerPacketHandler)};
case "checkversion":
return new PacketInfo { Type = PacketTypes.CheckVersion, Handler = typeof(ServerPacketHandler) };
case "getlogs":
return new PacketInfo { Type = PacketTypes.GetLogs, Handler = typeof(ServerPacketHandler) };
case "requestfile":
return new PacketInfo {Type = PacketTypes.RequestFile, Handler = typeof(FilePacketHandler)};
case "approvefile":
Expand Down
3 changes: 0 additions & 3 deletions RemoteTaskServer/Api/Services/Network/NetworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,9 @@ public static string GetMacAddress()

foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine($"Found MAC Address: {nic.GetPhysicalAddress()} Type: {nic.NetworkInterfaceType}");

var tempMac = nic.GetPhysicalAddress().ToString();
if (nic.Speed <= maxSpeed || string.IsNullOrEmpty(tempMac) || tempMac.Length < minMacAddrLength)
continue;
Console.WriteLine($"New Max Speed = {nic.Speed}, MAC: {tempMac}");
maxSpeed = nic.Speed;
macAddress = tempMac;
}
Expand Down
Loading

0 comments on commit 1533c72

Please sign in to comment.