ServerAuthorization.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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="tempDirectory">The temp directory.</param>
  18. public static void AuthorizeServer(int udpPort, int httpServerPort, string tempDirectory)
  19. {
  20. Directory.CreateDirectory(tempDirectory);
  21. // Create a temp file path to extract the bat file to
  22. var tmpFile = Path.Combine(tempDirectory, Guid.NewGuid() + ".bat");
  23. // Extract the bat file
  24. using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(ServerAuthorization).Namespace + ".RegisterServer.bat"))
  25. {
  26. using (var fileStream = File.Create(tmpFile))
  27. {
  28. stream.CopyTo(fileStream);
  29. }
  30. }
  31. var startInfo = new ProcessStartInfo
  32. {
  33. FileName = tmpFile,
  34. Arguments = string.Format("{0} {1}", udpPort, httpServerPort),
  35. CreateNoWindow = true,
  36. WindowStyle = ProcessWindowStyle.Hidden,
  37. Verb = "runas",
  38. ErrorDialog = false
  39. };
  40. using (var process = Process.Start(startInfo))
  41. {
  42. process.WaitForExit();
  43. }
  44. }
  45. }
  46. }