aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication/Native/ServerAuthorization.cs
blob: 91f0974eb97ae1d953f9e5cc7aebd53fc791a8de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace MediaBrowser.ServerApplication.Native
{
    /// <summary>
    /// Class Authorization
    /// </summary>
    public static class ServerAuthorization
    {
        /// <summary>
        /// Authorizes the server.
        /// </summary>
        /// <param name="httpServerPort">The HTTP server port.</param>
        /// <param name="httpServerUrlPrefix">The HTTP server URL prefix.</param>
        /// <param name="webSocketPort">The web socket port.</param>
        /// <param name="udpPort">The UDP port.</param>
        /// <param name="tempDirectory">The temp directory.</param>
        public static void AuthorizeServer(int httpServerPort, string httpServerUrlPrefix, int webSocketPort, int udpPort, string tempDirectory)
        {
            // Create a temp file path to extract the bat file to
            var tmpFile = Path.Combine(tempDirectory, Guid.NewGuid() + ".bat");

            // Extract the bat file
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(ServerAuthorization).Namespace + ".RegisterServer.bat"))
            {
                using (var fileStream = File.Create(tmpFile))
                {
                    stream.CopyTo(fileStream);
                }
            }

            var startInfo = new ProcessStartInfo
            {
                FileName = tmpFile,

                Arguments = string.Format("{0} {1} {2} {3}", httpServerPort,
                httpServerUrlPrefix,
                udpPort,
                webSocketPort),

                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                Verb = "runas",
                ErrorDialog = false
            };

            using (var process = Process.Start(startInfo))
            {
                process.WaitForExit();
            }
        }
    }
}