wasSharpNET – Blame information for rev 22

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;
17 private readonly string URL;
9 office 18  
19 private readonly Regex URLReservationRegex;
22 office 20 private readonly string username;
21 private readonly int timeout;
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 {
57 netSHOutput.Append(output.Data);
58 }
59 };
60  
61 checkProcess.Start();
62 checkProcess.BeginOutputReadLine();
16 office 63 checkProcess.WaitForExit(60000);
9 office 64  
65 return URLReservationRegex.IsMatch(netSHOutput.ToString());
66 }
67 }
68  
20 office 69 public bool Reserve()
9 office 70 {
20 office 71 System.Diagnostics.Process process = null;
72 try
9 office 73 {
20 office 74 process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
75 $@"http add urlacl url={URL} user={domain}\{username}")
76 {
77 Verb = @"runas",
78 CreateNoWindow = true,
79 WindowStyle = ProcessWindowStyle.Hidden,
80 UseShellExecute = true
81 });
82 }
83 catch (Win32Exception ex)
84 {
85 // User cancelled the UAC elevation prompt.
86 if (ex.NativeErrorCode == 1223)
87 {
88 return false;
89 }
90 }
91  
92 if (process == null)
93 return false;
94  
95 return process.WaitForExit(timeout);
9 office 96 }
97  
20 office 98 public bool Release()
9 office 99 {
20 office 100 System.Diagnostics.Process process = null;
101 try
9 office 102 {
20 office 103 process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh",
104 $@"http del urlacl url={URL}")
105 {
106 Verb = @"runas",
107 CreateNoWindow = true,
108 WindowStyle = ProcessWindowStyle.Hidden,
109 UseShellExecute = true
110 });
111 }
112 catch (Win32Exception ex)
113 {
114 // User cancelled the UAC elevation prompt.
115 if (ex.NativeErrorCode == 1223)
116 {
117 return false;
118 }
119 }
120  
121 if (process == null)
122 return false;
123  
124 return process.WaitForExit(timeout);
9 office 125 }
126 }
11 office 127 }