ServerAuthorization.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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="udpPort">The UDP port.</param>
  16. /// <param name="httpServerPort">The HTTP server port.</param>
  17. /// <param name="httpsServerPort">The HTTPS server port.</param>
  18. /// <param name="tempDirectory">The temp directory.</param>
  19. public static void AuthorizeServer(int udpPort, int httpServerPort, int httpsServerPort, 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}", udpPort, httpServerPort, httpsServerPort),
  36. CreateNoWindow = true,
  37. WindowStyle = ProcessWindowStyle.Hidden,
  38. Verb = "runas",
  39. ErrorDialog = false
  40. };
  41. using (var process = Process.Start(startInfo))
  42. {
  43. process.WaitForExit();
  44. }
  45. }
  46. }
  47. }