ServerAuthorization.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. namespace MediaBrowser.ServerApplication.Native
  6. {
  7. /// <summary>
  8. /// Class Authorization
  9. /// </summary>
  10. public static class ServerAuthorization
  11. {
  12. /// <summary>
  13. /// Authorizes the server.
  14. /// </summary>
  15. /// <param name="httpServerPort">The HTTP server port.</param>
  16. /// <param name="httpServerUrlPrefix">The HTTP server URL prefix.</param>
  17. /// <param name="webSocketPort">The web socket port.</param>
  18. /// <param name="udpPort">The UDP port.</param>
  19. /// <param name="tempDirectory">The temp directory.</param>
  20. public static void AuthorizeServer(int httpServerPort, string httpServerUrlPrefix, int webSocketPort, int udpPort, string tempDirectory)
  21. {
  22. Directory.CreateDirectory(tempDirectory);
  23. // Create a temp file path to extract the bat file to
  24. var tmpFile = Path.Combine(tempDirectory, Guid.NewGuid() + ".bat");
  25. // Extract the bat file
  26. using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(ServerAuthorization).Namespace + ".RegisterServer.bat"))
  27. {
  28. using (var fileStream = File.Create(tmpFile))
  29. {
  30. stream.CopyTo(fileStream);
  31. }
  32. }
  33. var startInfo = new ProcessStartInfo
  34. {
  35. FileName = tmpFile,
  36. Arguments = string.Format("{0} {1} {2} {3}", httpServerPort,
  37. httpServerUrlPrefix,
  38. udpPort,
  39. webSocketPort),
  40. CreateNoWindow = true,
  41. WindowStyle = ProcessWindowStyle.Hidden,
  42. Verb = "runas",
  43. ErrorDialog = false
  44. };
  45. using (var process = Process.Start(startInfo))
  46. {
  47. process.WaitForExit();
  48. }
  49. }
  50. }
  51. }