wasSharpNET – Rev 11

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////

using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;

namespace wasSharpNET.Platform.Windows.Commands.NetSH
{
    public class URLACL
    {
        public string domain;
        public string URL;

        private readonly Regex URLReservationRegex;
        public string username;

        public URLACL(string URL, string username, string domain)
        {
            this.URL = URL;
            this.username = username;
            this.domain = domain;

            URLReservationRegex =
                new Regex(
                    string.Format(@"{0}.*{1}", Regex.Escape(URL),
                        Regex.Escape(string.Format(@"{0}\{1}", domain, username))),
                    RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase |
                    RegexOptions.Compiled);
        }

        public bool isReserved
        {
            get
            {
                var netSHOutput = new StringBuilder();
                var checkProcess = new System.Diagnostics.Process();
                checkProcess.StartInfo = new ProcessStartInfo("netsh", @"http show urlacl")
                {
                    RedirectStandardOutput = true,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = false
                };

                checkProcess.OutputDataReceived += (sender, output) =>
                {
                    if (output?.Data != null)
                    {
                        netSHOutput.Append(output.Data);
                    }
                };

                checkProcess.Start();
                checkProcess.BeginOutputReadLine();
                checkProcess.WaitForExit();

                return URLReservationRegex.IsMatch(netSHOutput.ToString());
            }
        }

        public void Reserve()
        {
            System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
                string.Format(@"http add urlacl url={0} user={1}\{2}", URL, domain, username))
            {
                Verb = @"runas",
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = true
            }).WaitForExit();
        }

        public void Release()
        {
            System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
                string.Format(@"http del urlacl url={0}", URL))
            {
                Verb = @"runas",
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = true
            }).WaitForExit();
        }
    }
}