wasSharpNET – Blame information for rev 27

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  
20 office 7 using System.ComponentModel;
9 office 8 using System.Diagnostics;
9 using System.Text;
10 using System.Text.RegularExpressions;
11  
12 namespace wasSharpNET.Platform.Windows.Commands.NetSH
13 {
14 public class URLACL
15 {
22 office 16 private readonly string domain;
27 office 17 private readonly int timeout;
22 office 18 private readonly string URL;
9 office 19  
20 private readonly Regex URLReservationRegex;
22 office 21 private readonly string username;
9 office 22  
16 office 23 public URLACL(string URL, string username, string domain, int timeout)
9 office 24 {
25 this.URL = URL;
26 this.username = username;
27 this.domain = domain;
16 office 28 this.timeout = timeout;
9 office 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);
35 }
36  
37 public bool isReserved
38 {
39 get
40 {
41 var netSHOutput = new StringBuilder();
22 office 42 var checkProcess = new System.Diagnostics.Process
9 office 43 {
22 office 44 StartInfo = new ProcessStartInfo("netsh", @"http show urlacl")
45 {
46 RedirectStandardOutput = true,
47 CreateNoWindow = true,
48 WindowStyle = ProcessWindowStyle.Hidden,
49 UseShellExecute = false
50 }
9 office 51 };
52  
53 checkProcess.OutputDataReceived += (sender, output) =>
54 {
55 if (output?.Data != null)
56 netSHOutput.Append(output.Data);
57 };
58  
59 checkProcess.Start();
60 checkProcess.BeginOutputReadLine();
16 office 61 checkProcess.WaitForExit(60000);
9 office 62  
63 return URLReservationRegex.IsMatch(netSHOutput.ToString());
64 }
65 }
66  
20 office 67 public bool Reserve()
9 office 68 {
20 office 69 System.Diagnostics.Process process = null;
70 try
9 office 71 {
20 office 72 process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
73 $@"http add urlacl url={URL} user={domain}\{username}")
74 {
75 Verb = @"runas",
76 CreateNoWindow = true,
77 WindowStyle = ProcessWindowStyle.Hidden,
78 UseShellExecute = true
79 });
80 }
81 catch (Win32Exception ex)
82 {
83 // User cancelled the UAC elevation prompt.
84 if (ex.NativeErrorCode == 1223)
85 return false;
86 }
87  
88 if (process == null)
89 return false;
90  
91 return process.WaitForExit(timeout);
9 office 92 }
93  
20 office 94 public bool Release()
9 office 95 {
20 office 96 System.Diagnostics.Process process = null;
97 try
9 office 98 {
20 office 99 process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
100 $@"http del urlacl url={URL}")
101 {
102 Verb = @"runas",
103 CreateNoWindow = true,
104 WindowStyle = ProcessWindowStyle.Hidden,
105 UseShellExecute = true
106 });
107 }
108 catch (Win32Exception ex)
109 {
110 // User cancelled the UAC elevation prompt.
111 if (ex.NativeErrorCode == 1223)
112 return false;
113 }
114  
115 if (process == null)
116 return false;
117  
118 return process.WaitForExit(timeout);
9 office 119 }
120 }
27 office 121 }