using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core; using Tango.Core.Components; using Tango.PPC.Common.Connection; namespace Tango.PPC.Common.RemoteAssistance { /// /// Represents the default remote assistance provider. /// /// /// public class DefaultRemoteAssistanceProvider : ExtendedObject, IRemoteAssistanceProvider { private IMachineProvider _machineProvider; private String _installer_path = "C:\\Installers\\TeamViewer_Host.msi"; private bool _isEnabled; /// /// Gets a value indicating whether the remote assistance service is enabled. /// public bool IsEnabled { get { return _isEnabled; } private set { _isEnabled = value; RaisePropertyChangedAuto(); } } /// /// Initializes a new instance of the class. /// /// The machine provider. public DefaultRemoteAssistanceProvider(IMachineProvider machineProvider) { _machineProvider = machineProvider; } /// /// Enables the remote assistance. /// /// public async Task EnableRemoteAssistance() { if (!IsEnabled) { try { CmdCommand command = new CmdCommand("sc.exe", "config TeamViewer start=auto"); command.Timeout = TimeSpan.FromSeconds(10); await command.Run(); command = new CmdCommand("net", "start TeamViewer"); command.Timeout = TimeSpan.FromSeconds(10); await command.Run(); IsEnabled = true; } catch (Exception ex) { LogManager.Log(ex, "Error enabling remote assistance."); } } } /// /// Disables the remote assistance. /// /// public async Task DisableRemoteAssistance() { if (IsEnabled) { try { CmdCommand command = new CmdCommand("sc.exe", "config TeamViewer start=disabled"); command.Timeout = TimeSpan.FromSeconds(10); await command.Run(); command = new CmdCommand("net", "stop TeamViewer"); command.Timeout = TimeSpan.FromSeconds(10); await command.Run(); IsEnabled = false; } catch (Exception ex) { LogManager.Log(ex, "Error disabling remote assistance."); } } } /// /// Installs the remote assistance. /// /// The machine serial number. /// The remote assistance group. /// The machine working environment. /// /// /// The remote assistance service was installed but could not be found. public async Task InstallRemoteAssistance(String machineSerialNumber, String group, String environment) { try { if (!File.Exists(_installer_path)) { throw new FileNotFoundException($"The remote assistance installer file could not be found at {_installer_path}."); } String q = "\""; //CmdCommand command = new CmdCommand("msiexec.exe", $"/i \"{_installer_path}\" /qn CUSTOMCONFIGID=ke43ann APITOKEN=4765529-gon1LwO1N1TTrlLI21ji ASSIGNMENTOPTIONS=\" --reassign --alias {"TANGO-" + machineSerialNumber}-{environment} --grant-easy-access\""); CmdCommand command = new CmdCommand("msiexec.exe", $"/i {q}{_installer_path}{q} /qn CUSTOMCONFIGID=ke43ann APITOKEN=4765529-gon1LwO1N1TTrlLI21ji ASSIGNMENTOPTIONS={q} --group {q}{q} {group} {q}{q} --reassign --alias TANGO-{machineSerialNumber}-{environment} --grant-easy-access{q}"); command.Timeout = TimeSpan.FromSeconds(30); await command.Run(); bool exist = await IsRemoteAssistanceInstalled(); if (exist) { await DisableRemoteAssistance(); } else { throw new ApplicationException("The remote assistance service was installed but could not be found."); } } catch (Exception ex) { LogManager.Log(ex, "Error installing remote assistance."); throw; } } /// /// Determines whether remote assistance is currently installed. /// /// private async Task IsRemoteAssistanceInstalled() { bool exist = false; for (int i = 0; i < 6; i++) { LogManager.Log("Checking remote assistance service..."); await Task.Delay(1000); CmdCommand command = new CmdCommand("sc.exe", "query TeamViewer"); try { await command.Run(); exist = true; break; } catch { } } if (exist) { LogManager.Log("Remote assistance service found."); } else { LogManager.Log("Remote assistance service was not found."); } return exist; } } }