ServerAuthorization.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // Create a temp file path to extract the bat file to
  23. var tmpFile = Path.Combine(tempDirectory, Guid.NewGuid() + ".bat");
  24. // Extract the bat file
  25. using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(ServerAuthorization).Namespace + ".RegisterServer.bat"))
  26. {
  27. using (var fileStream = File.Create(tmpFile))
  28. {
  29. stream.CopyTo(fileStream);
  30. }
  31. }
  32. var startInfo = new ProcessStartInfo
  33. {
  34. FileName = tmpFile,
  35. Arguments = string.Format("{0} {1} {2} {3}", httpServerPort,
  36. httpServerUrlPrefix,
  37. udpPort,
  38. webSocketPort),
  39. CreateNoWindow = true,
  40. WindowStyle = ProcessWindowStyle.Hidden,
  41. Verb = "runas",
  42. ErrorDialog = false
  43. };
  44. using (var process = Process.Start(startInfo))
  45. {
  46. process.WaitForExit();
  47. }
  48. }
  49. }
  50. }