ServerAuthorization.cs 1.8 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="udpPort">The UDP port.</param>
  18. /// <param name="tempDirectory">The temp directory.</param>
  19. public static void AuthorizeServer(int httpServerPort, string httpServerUrlPrefix, int udpPort, string tempDirectory)
  20. {
  21. Directory.CreateDirectory(tempDirectory);
  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}", httpServerPort,
  36. httpServerUrlPrefix,
  37. udpPort),
  38. CreateNoWindow = true,
  39. WindowStyle = ProcessWindowStyle.Hidden,
  40. Verb = "runas",
  41. ErrorDialog = false
  42. };
  43. using (var process = Process.Start(startInfo))
  44. {
  45. process.WaitForExit();
  46. }
  47. }
  48. }
  49. }