wasSharpNET – Diff between revs 20 and 22

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