/// <summary> /// Click handler for the rebootDevice button. /// </summary> /// <param name="sender">The caller of this method.</param> /// <param name="e">The arguments associated with this event.</param> private void RebootDevice_Click(object sender, RoutedEventArgs e) { bool reenableDeviceControls = false; this.ClearOutput(); this.EnableConnectionControls(false); this.EnableDeviceControls(false); StringBuilder sb = new StringBuilder(); Task rebootTask = new Task( async() => { sb.Append(this.MarshalGetCommandOutput()); sb.AppendLine("Rebooting the device"); this.MarshalUpdateCommandOutput(sb.ToString()); try { await portal.Reboot(); } catch (Exception ex) { sb.AppendLine("Failed to reboot the device."); sb.AppendLine(ex.GetType().ToString() + " - " + ex.Message); reenableDeviceControls = true; } }); Task continuationTask = rebootTask.ContinueWith( (t) => { this.MarshalUpdateCommandOutput(sb.ToString()); this.MarshalEnableDeviceControls(reenableDeviceControls); this.MarshalEnableConnectionControls(true); }); rebootTask.Start(); }
/// <summary> /// Main entry point for handling a Sandbox operation /// </summary> /// <param name="portal">DevicePortal reference for communicating with the device.</param> /// <param name="parameters">Parsed command line parameters.</param> public static void HandleOperation(DevicePortal portal, ParameterHelper parameters) { if (parameters.HasFlag(ParameterHelper.HelpFlag)) { Console.WriteLine(SandboxUsageMessage); return; } string desiredValue = parameters.GetParameterValue("value"); if (string.IsNullOrEmpty(desiredValue)) { Task <Sandbox> getSandboxTask = portal.GetXboxLiveSandbox(); getSandboxTask.Wait(); Console.WriteLine(getSandboxTask.Result); } else { Task <Sandbox> setSandboxTask = portal.SetXboxLiveSandbox(desiredValue); setSandboxTask.Wait(); Console.WriteLine("{0} -> {1}", setSandboxTask.Result, desiredValue); if (parameters.HasFlag("reboot")) { Task rebootTask = portal.Reboot(); rebootTask.Wait(); Console.WriteLine("Console rebooting..."); } else { Console.WriteLine("A reboot is required before this setting takes effect."); } } }
/// <summary> /// Main entry point /// </summary> /// <param name="args">command line args</param> public static void Main(string[] args) { ParameterHelper parameters = new ParameterHelper(); Program app = new Program(); string targetConsole = string.Empty; try { parameters.ParseCommandLine(args); OperationType operation = OperationType.None; if (parameters.HasParameter(ParameterHelper.Operation)) { operation = OperationStringToEnum(parameters.GetParameterValue("op")); } // Allow /ip: to still function, even though we've moved to /x: in the documentation. if (parameters.HasParameter(ParameterHelper.IpOrHostnameOld) && !parameters.HasParameter(ParameterHelper.IpOrHostname)) { targetConsole = parameters.GetParameterValue(ParameterHelper.IpOrHostnameOld); } else if (parameters.HasParameter(ParameterHelper.IpOrHostname)) { targetConsole = parameters.GetParameterValue(ParameterHelper.IpOrHostname); } if (string.IsNullOrEmpty(targetConsole)) { object regValue; regValue = Microsoft.Win32.Registry.GetValue(DefaultConsoleRegkey, null, null); if (regValue == null) { regValue = Microsoft.Win32.Registry.GetValue(DefaultXtfConsoleRegkey, null, null); } if (regValue is string) { targetConsole = regValue as string; } else { throw new Exception("No default console is currently set. Must provide an ip address or hostname to connect to: /x:<ip or hostname>."); } } string finalConnectionAddress = string.Format("https://{0}:11443", targetConsole); string userName = parameters.GetParameterValue(ParameterHelper.WdpUser); string password = parameters.GetParameterValue(ParameterHelper.WdpPassword); if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) { try { // No creds were provided on the command line. CredManager.RetrieveStoredCreds(targetConsole, ref userName, ref password); } catch (TypeLoadException) { // Windows 7 doesn't support credential storage so we'll get a TypeLoadException throw new Exception("Credential storage is not supported on your PC. It requires Windows 8+ to run. Please provide the user and password parameters."); } } else { try { // Creds were provided on the command line. CredManager.UpdateStoredCreds(targetConsole, userName, password); } catch (TypeLoadException) { // Do nothing. We can't store these on Win7 } } X509Certificate2 cert = null; IDevicePortalConnection connection = new DefaultDevicePortalConnection(finalConnectionAddress, userName, password); DevicePortal portal = new DevicePortal(connection); if (parameters.HasParameter(ParameterHelper.Cert)) { string certFile = parameters.GetParameterValue(ParameterHelper.Cert); try { cert = new X509Certificate2(certFile); } catch (Exception e) { throw new Exception(string.Format("Failed to read manual cert file {0}, {1}", certFile, e.Message), e); } } // Add additional handling for untrusted certs. portal.UnvalidatedCert += app.DoCertValidation; // If a thumbprint is provided, use it for this connection. Otherwise check the registry. if (parameters.HasParameter("thumbprint")) { app.AcceptedThumbprint = parameters.GetParameterValue("thumbprint"); } else { object regValue; regValue = Microsoft.Win32.Registry.GetValue(DefaultConsoleRegkey, targetConsole, null); if (regValue is string) { app.AcceptedThumbprint = regValue as string; } } Task connectTask = portal.Connect(updateConnection: false, manualCertificate: cert); connectTask.Wait(); if (portal.ConnectionHttpStatusCode != HttpStatusCode.OK) { if (portal.ConnectionHttpStatusCode == HttpStatusCode.Unauthorized) { if (connection.Credentials == null) { Console.WriteLine("The WDP connection was rejected due to missing credentials.\n\nPlease provide the /user:<username> and /pwd:<pwd> parameters on your first call to WDP."); } else { Console.WriteLine("The WDP connection was rejected due to bad credentials.\n\nPlease check the /user:<username> and /pwd:<pwd> parameters."); } } else if (!string.IsNullOrEmpty(portal.ConnectionFailedDescription)) { Console.WriteLine(string.Format("Failed to connect to WDP (HTTP {0}) : {1}", (int)portal.ConnectionHttpStatusCode, portal.ConnectionFailedDescription)); } else { Console.WriteLine("Failed to connect to WDP for unknown reason."); } } else { // If the operation is more than a couple lines, it should // live in its own file. These are arranged alphabetically // for ease of use. switch (operation) { case OperationType.AppOperation: AppOperation.HandleOperation(portal, parameters); break; case OperationType.ConfigOperation: ConfigOperation.HandleOperation(portal, parameters); break; case OperationType.ConnectOperation: // User provided a new ip or hostname to set as the default. if (parameters.HasParameter(ParameterHelper.IpOrHostname) || parameters.HasParameter(ParameterHelper.IpOrHostnameOld)) { Microsoft.Win32.Registry.SetValue(DefaultConsoleRegkey, null, targetConsole); Console.WriteLine("Default console set to {0}", targetConsole); } else { Console.WriteLine("Connected to Default console: {0}", targetConsole); } if (parameters.HasParameter("thumbprint")) { string thumbprint = parameters.GetParameterValue("thumbprint"); Microsoft.Win32.Registry.SetValue(DefaultConsoleRegkey, targetConsole, thumbprint); Console.WriteLine("Thumbprint {0} saved for console with address {1}.", thumbprint, targetConsole); } break; case OperationType.FiddlerOperation: FiddlerOperation.HandleOperation(portal, parameters); break; case OperationType.FileOperation: FileOperation.HandleOperation(portal, parameters); break; case OperationType.InfoOperation: Console.WriteLine("OS version: " + portal.OperatingSystemVersion); Console.WriteLine("Platform: " + portal.PlatformName + " (" + portal.Platform.ToString() + ")"); Task <string> getNameTask = portal.GetDeviceName(); getNameTask.Wait(); Console.WriteLine("Device name: " + getNameTask.Result); break; case OperationType.InstallOperation: // Ensure we have an IP since SMB might need it for path generation. parameters.AddParameter(ParameterHelper.IpOrHostname, targetConsole); InstallOperation.HandleOperation(portal, parameters); break; case OperationType.ListProcessesOperation: ListProcessesOperation.HandleOperation(portal, parameters); break; case OperationType.RebootOperation: Task rebootTask = portal.Reboot(); rebootTask.Wait(); Console.WriteLine("Rebooting device."); break; case OperationType.SandboxOperation: SandboxOperation.HandleOperation(portal, parameters); break; case OperationType.ScreenshotOperation: ScreenshotOperation.HandleOperation(portal, parameters); break; case OperationType.SystemPerfOperation: SystemPerfOperation.HandleOperation(portal, parameters); break; case OperationType.XblUserOperation: UserOperation.HandleOperation(portal, parameters); break; default: Console.WriteLine("Successfully connected to console but no operation was specified. \n" + "Use the '/op:<operation type>' parameter to run a specified operation."); Console.WriteLine(); Console.WriteLine(AvailableOperationsText); break; } } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(); Console.WriteLine(GeneralUsageMessage); } // If a debugger is attached, don't close but instead loop here until // closed. while (Debugger.IsAttached) { Thread.Sleep(0); } }
/// <summary> /// Main entry point for handling a Fiddler operation /// </summary> /// <param name="portal">DevicePortal reference for communicating with the device.</param> /// <param name="parameters">Parsed command line parameters.</param> public static void HandleOperation(DevicePortal portal, ParameterHelper parameters) { if (parameters.HasFlag(ParameterHelper.HelpFlag)) { Console.WriteLine(FiddlerUsageMessage); return; } string state = parameters.GetParameterValue("state"); if (string.IsNullOrEmpty(state)) { Console.WriteLine("/state parameter is required."); Console.WriteLine(); Console.WriteLine(FiddlerUsageMessage); return; } try { if (string.Equals(state, "on", StringComparison.OrdinalIgnoreCase)) { string proxyAddress = parameters.GetParameterValue("proxyaddress"); string proxyPort = parameters.GetParameterValue("proxyport"); if (string.IsNullOrEmpty(proxyAddress) || string.IsNullOrEmpty(proxyPort)) { Console.WriteLine("/proxyaddress and /proxyport are required for enabling Fiddler."); Console.WriteLine(); Console.WriteLine(FiddlerUsageMessage); return; } Task fiddlerEnableTask = portal.EnableFiddlerTracing(proxyAddress, proxyPort, parameters.GetParameterValue("certpath")); fiddlerEnableTask.Wait(); Console.WriteLine("Fiddler enabled."); } else if (string.Equals(state, "off", StringComparison.OrdinalIgnoreCase)) { Task fiddlerDisableTask = portal.DisableFiddlerTracing(); fiddlerDisableTask.Wait(); Console.WriteLine("Fiddler disabled."); } else { Console.WriteLine("Unknown state parameter: {0}. Must be 'on' or 'off'.", state); Console.WriteLine(); Console.WriteLine(FiddlerUsageMessage); return; } if (parameters.HasFlag("reboot")) { Task rebootTask = portal.Reboot(); rebootTask.Wait(); Console.WriteLine("Console rebooting..."); } else { Console.WriteLine("A reboot is required before this takes effect."); } } catch (AggregateException e) { if (e.InnerException is DevicePortalException) { DevicePortalException innerException = e.InnerException as DevicePortalException; Console.WriteLine(string.Format("Exception encountered: {0}, hr = 0x{1:X} : {2}", innerException.StatusCode, innerException.HResult, innerException.Reason)); } else { Console.WriteLine(string.Format("Unexpected exception encountered: {0}", e.Message)); } return; } }