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