wasSharpNET – Diff between revs 16 and 20
?pathlinks?
Rev 16 | Rev 20 | |||
---|---|---|---|---|
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; |
|
- | 8 | using System.Diagnostics; |
||
7 | using System.Diagnostics; |
9 | using System.IO; |
|
8 | using System.Text; |
10 | using System.Text; |
|
- | 11 | using System.Text.RegularExpressions; |
||
9 | using System.Text.RegularExpressions; |
12 | using Process = System.Diagnostics.Process; |
|
10 | |
13 | |
|
11 | namespace wasSharpNET.Platform.Windows.Commands.NetSH |
14 | namespace wasSharpNET.Platform.Windows.Commands.NetSH |
|
12 | { |
15 | { |
|
13 | public class URLACL |
16 | public class URLACL |
|
14 | { |
17 | { |
|
15 | private string domain; |
18 | private string domain; |
|
16 | private string URL; |
19 | private string URL; |
|
17 | |
20 | |
|
18 | private readonly Regex URLReservationRegex; |
21 | private readonly Regex URLReservationRegex; |
|
19 | private string username; |
22 | private string username; |
|
20 | private int timeout; |
23 | private int timeout; |
|
21 | |
24 | |
|
22 | public URLACL(string URL, string username, string domain, int timeout) |
25 | public URLACL(string URL, string username, string domain, int timeout) |
|
23 | { |
26 | { |
|
24 | this.URL = URL; |
27 | this.URL = URL; |
|
25 | this.username = username; |
28 | this.username = username; |
|
26 | this.domain = domain; |
29 | this.domain = domain; |
|
27 | this.timeout = timeout; |
30 | this.timeout = timeout; |
|
28 | |
31 | |
|
29 | URLReservationRegex = |
32 | URLReservationRegex = |
|
30 | new Regex( |
33 | new Regex( |
|
31 | string.Format(@"{0}.*{1}", Regex.Escape(URL), |
- | ||
32 | Regex.Escape(string.Format(@"{0}\{1}", domain, username))), |
34 | $@"{Regex.Escape(URL)}.*{Regex.Escape(string.Format(@"{0}\{1}", domain, username))}", |
|
33 | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | |
35 | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | |
|
34 | RegexOptions.Compiled); |
36 | RegexOptions.Compiled); |
|
35 | } |
37 | } |
|
36 | |
38 | |
|
37 | public bool isReserved |
39 | public bool isReserved |
|
38 | { |
40 | { |
|
39 | get |
41 | get |
|
40 | { |
42 | { |
|
41 | var netSHOutput = new StringBuilder(); |
43 | var netSHOutput = new StringBuilder(); |
|
42 | var checkProcess = new System.Diagnostics.Process(); |
44 | var checkProcess = new System.Diagnostics.Process(); |
|
43 | checkProcess.StartInfo = new ProcessStartInfo("netsh", @"http show urlacl") |
45 | checkProcess.StartInfo = new ProcessStartInfo("netsh", @"http show urlacl") |
|
44 | { |
46 | { |
|
45 | RedirectStandardOutput = true, |
47 | RedirectStandardOutput = true, |
|
46 | CreateNoWindow = true, |
48 | CreateNoWindow = true, |
|
47 | WindowStyle = ProcessWindowStyle.Hidden, |
49 | WindowStyle = ProcessWindowStyle.Hidden, |
|
48 | UseShellExecute = false |
50 | UseShellExecute = false |
|
49 | }; |
51 | }; |
|
50 | |
52 | |
|
51 | checkProcess.OutputDataReceived += (sender, output) => |
53 | checkProcess.OutputDataReceived += (sender, output) => |
|
52 | { |
54 | { |
|
53 | if (output?.Data != null) |
55 | if (output?.Data != null) |
|
54 | { |
56 | { |
|
55 | netSHOutput.Append(output.Data); |
57 | netSHOutput.Append(output.Data); |
|
56 | } |
58 | } |
|
57 | }; |
59 | }; |
|
58 | |
60 | |
|
59 | checkProcess.Start(); |
61 | checkProcess.Start(); |
|
60 | checkProcess.BeginOutputReadLine(); |
62 | checkProcess.BeginOutputReadLine(); |
|
61 | checkProcess.WaitForExit(60000); |
63 | checkProcess.WaitForExit(60000); |
|
62 | |
64 | |
|
63 | return URLReservationRegex.IsMatch(netSHOutput.ToString()); |
65 | return URLReservationRegex.IsMatch(netSHOutput.ToString()); |
|
64 | } |
66 | } |
|
65 | } |
67 | } |
|
66 | |
68 | |
|
67 | public void Reserve() |
69 | public bool Reserve() |
|
68 | { |
70 | { |
|
69 | System.Diagnostics.Process.Start(new ProcessStartInfo("netsh", |
71 | System.Diagnostics.Process process = null; |
|
70 | string.Format(@"http add urlacl url={0} user={1}\{2}", URL, domain, username)) |
72 | try |
|
- | 73 | { |
||
- | 74 | process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh", |
||
- | 75 | $@"http add urlacl url={URL} user={domain}\{username}") |
||
71 | { |
76 | { |
|
72 | Verb = @"runas", |
77 | Verb = @"runas", |
|
73 | CreateNoWindow = true, |
78 | CreateNoWindow = true, |
|
74 | WindowStyle = ProcessWindowStyle.Hidden, |
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; |
||
75 | UseShellExecute = false |
94 | |
|
76 | }).WaitForExit(timeout); |
95 | return process.WaitForExit(timeout); |
|
77 | } |
96 | } |
|
78 | |
97 | |
|
79 | public void Release() |
98 | public bool Release() |
|
80 | { |
99 | { |
|
81 | System.Diagnostics.Process.Start(new ProcessStartInfo("netsh", |
100 | System.Diagnostics.Process process = null; |
|
- | 101 | try |
||
- | 102 | { |
||
- | 103 | process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh", |
||
82 | string.Format(@"http del urlacl url={0}", URL)) |
104 | $@"http del urlacl url={URL}") |
|
83 | { |
105 | { |
|
84 | Verb = @"runas", |
106 | Verb = @"runas", |
|
85 | CreateNoWindow = true, |
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) |
||
86 | WindowStyle = ProcessWindowStyle.Hidden, |
122 | return false; |
|
87 | UseShellExecute = false |
123 | |
|
88 | }).WaitForExit(timeout); |
124 | return process.WaitForExit(timeout); |
|
89 | } |
125 | } |
|
90 | } |
126 | } |
|
91 | } |
127 | } |
|
92 | |
128 | |