wasSharpNET – Blame information for rev 31

Subversion Repositories:
Rev:
Rev Author Line No. Line
9 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
31 office 7 using System;
20 office 8 using System.ComponentModel;
9 office 9 using System.Diagnostics;
10 using System.Text;
11 using System.Text.RegularExpressions;
12  
13 namespace wasSharpNET.Platform.Windows.Commands.NetSH
14 {
31 office 15 public class URLACL : IDisposable
9 office 16 {
31 office 17 private string domain { get; set; }
18 private int timeout { get; set; } = 60000;
19 private string URL { get; set; }
9 office 20  
31 office 21 private Regex URLReservationRegex { get; set; }
22 private string username { get; set; }
9 office 23  
31 office 24 public URLACL(string URL, string username, string domain)
9 office 25 {
26 this.URL = URL;
27 this.username = username;
28 this.domain = domain;
29  
30 URLReservationRegex =
31 new Regex(
22 office 32 $@"{Regex.Escape(URL)}.*{Regex.Escape($@"{domain}\{username}")}",
9 office 33 RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase |
34 RegexOptions.Compiled);
31 office 35 }
36  
37 public URLACL(string URL, string username, string domain, int timeout) : this(URL, username, domain)
38 {
39 this.timeout = timeout;
9 office 40 }
41  
31 office 42 public bool IsReserved
9 office 43 {
44 get
45 {
46 var netSHOutput = new StringBuilder();
22 office 47 var checkProcess = new System.Diagnostics.Process
9 office 48 {
22 office 49 StartInfo = new ProcessStartInfo("netsh", @"http show urlacl")
50 {
51 RedirectStandardOutput = true,
52 CreateNoWindow = true,
53 WindowStyle = ProcessWindowStyle.Hidden,
54 UseShellExecute = false
55 }
9 office 56 };
57  
58 checkProcess.OutputDataReceived += (sender, output) =>
59 {
60 if (output?.Data != null)
61 netSHOutput.Append(output.Data);
62 };
63  
64 checkProcess.Start();
65 checkProcess.BeginOutputReadLine();
31 office 66 checkProcess.WaitForExit(timeout);
9 office 67  
68 return URLReservationRegex.IsMatch(netSHOutput.ToString());
69 }
70 }
71  
20 office 72 public bool Reserve()
9 office 73 {
20 office 74 System.Diagnostics.Process process = null;
75 try
9 office 76 {
20 office 77 process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
78 $@"http add urlacl url={URL} user={domain}\{username}")
79 {
80 Verb = @"runas",
81 CreateNoWindow = true,
82 WindowStyle = ProcessWindowStyle.Hidden,
83 UseShellExecute = true
84 });
85 }
86 catch (Win32Exception ex)
87 {
88 // User cancelled the UAC elevation prompt.
89 if (ex.NativeErrorCode == 1223)
90 return false;
91 }
92  
93 if (process == null)
94 return false;
95  
96 return process.WaitForExit(timeout);
9 office 97 }
98  
20 office 99 public bool Release()
9 office 100 {
20 office 101 System.Diagnostics.Process process = null;
102 try
9 office 103 {
20 office 104 process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
105 $@"http del urlacl url={URL}")
106 {
107 Verb = @"runas",
108 CreateNoWindow = true,
109 WindowStyle = ProcessWindowStyle.Hidden,
110 UseShellExecute = true
111 });
112 }
113 catch (Win32Exception ex)
114 {
115 // User cancelled the UAC elevation prompt.
116 if (ex.NativeErrorCode == 1223)
117 return false;
118 }
119  
120 if (process == null)
121 return false;
122  
123 return process.WaitForExit(timeout);
9 office 124 }
31 office 125  
126 public void Dispose()
127 {
128 }
9 office 129 }
27 office 130 }