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