wasSharpNET – Rev 31

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;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;

namespace wasSharpNET.Platform.Windows.Commands.NetSH
{
    public class URLACL : IDisposable
    {
        private string domain { get; set; }
        private int timeout { get; set; } = 60000;
        private string URL { get; set; }

        private Regex URLReservationRegex { get; set; }
        private string username { get; set; }

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

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

        public URLACL(string URL, string username, string domain, int timeout) : this(URL, username, domain)
        {
            this.timeout = timeout;
        }

        public bool IsReserved
        {
            get
            {
                var netSHOutput = new StringBuilder();
                var checkProcess = new System.Diagnostics.Process
                {
                    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(timeout);

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

        public bool Reserve()
        {
            System.Diagnostics.Process process = null;
            try
            {
                process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
                    $@"http add urlacl url={URL} user={domain}\{username}")
                {
                    Verb = @"runas",
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = true
                });
            }
            catch (Win32Exception ex)
            {
                // User cancelled the UAC elevation prompt.
                if (ex.NativeErrorCode == 1223)
                    return false;
            }

            if (process == null)
                return false;

            return process.WaitForExit(timeout);
        }

        public bool Release()
        {
            System.Diagnostics.Process process = null;
            try
            {
                process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
                    $@"http del urlacl url={URL}")
                {
                    Verb = @"runas",
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = true
                });
            }
            catch (Win32Exception ex)
            {
                // User cancelled the UAC elevation prompt.
                if (ex.NativeErrorCode == 1223)
                    return false;
            }

            if (process == null)
                return false;

            return process.WaitForExit(timeout);
        }

        public void Dispose()
        {
        }
    }
}

Generated by GNU Enscript 1.6.5.90.