/Diagnostics/ExceptionExtensions.cs |
@@ -15,32 +15,31 @@ |
{ |
public static class ExceptionExtensions |
{ |
public static string PrettyPrint(this Exception x) |
public static string PrettyPrint(this Exception ex) |
{ |
var st = new StackTrace(x, true); |
if(ex == null) |
return string.Empty; |
|
var st = new StackTrace(ex, true); |
var frames = st.GetFrames(); |
if (frames == null) |
return string.Empty; |
|
StringBuilder sb = new StringBuilder(); |
sb.Append(Enumerable.Repeat('-', 75).ToArray()); |
var sb = new StringBuilder(); |
sb.Append(Environment.NewLine); |
|
int indent = 0; |
foreach (var frame in frames) |
{ |
if (frame.GetFileLineNumber() < 1) |
var fileName = frame?.GetFileName(); |
if (string.IsNullOrEmpty(fileName)) |
continue; |
|
sb.Append(Enumerable.Repeat(' ', indent).ToArray()); |
sb.Append(@" -> "); |
sb.Append(Enumerable |
.Repeat('-', System.Console.WindowWidth) |
.Concat(Environment.NewLine) |
.ToArray()); |
sb.Append("File: "); |
sb.Append(string.Join( |
Path.DirectorySeparatorChar.ToString(), |
frame.GetFileName() |
.Split(Path.DirectorySeparatorChar) |
.Reverse() |
.Take(2) |
.Reverse() |
) |
); |
sb.Append(fileName); |
sb.Append(@" Method: "); |
sb.Append(frame.GetMethod().Name); |
sb.Append(@" Line and Column : "); |
@@ -48,13 +47,18 @@ |
sb.Append(@":"); |
sb.Append(frame.GetFileColumnNumber()); |
sb.Append(Environment.NewLine); |
|
indent += 1; |
} |
|
sb.Append(Enumerable.Repeat('-', 75).ToArray()); |
sb.Append(x); |
sb.Append(Enumerable.Repeat('-', 75).ToArray()); |
sb.Append(Enumerable |
.Repeat('-', System.Console.WindowWidth) |
.Concat(Environment.NewLine) |
.ToArray()); |
sb.Append(ex); |
sb.Append(Environment.NewLine); |
sb.Append(Enumerable |
.Repeat('-', System.Console.WindowWidth) |
.Concat(Environment.NewLine) |
.ToArray()); |
|
return sb.ToString(); |
} |
/Network/HTTP/HTTPServer.cs |
@@ -15,7 +15,7 @@ |
{ |
private int activeRequests; |
|
private HttpListener HTTPListener = null; |
private HttpListener HTTPListener; |
|
private int processedRequests; |
|
@@ -24,9 +24,11 @@ |
|
public bool IsRunning => HTTPListener != null && HTTPListener.IsListening; |
|
public bool Start(IEnumerable<string> prefixes) |
private readonly ManualResetEventSlim stopListen = new ManualResetEventSlim(false); |
|
public bool Start(List<string> prefixes) |
{ |
HTTPListener = new HttpListener() |
HTTPListener = new HttpListener |
{ |
AuthenticationSchemes = AuthenticationSchemes |
}; |
@@ -41,23 +43,27 @@ |
// Do not bomb if the client disconnects. |
HTTPListener.IgnoreWriteExceptions = true; |
HTTPListener.Start(); |
ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); |
return true; |
return ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); |
} |
|
public void Stop() |
{ |
HTTPListener.Stop(); |
stopListen.Set(); |
} |
|
private void Listen(object state) |
{ |
var callbackState = (HTTPServerCallbackState)state; |
HTTPServerCallbackState callbackState = (HTTPServerCallbackState)state; |
|
while (callbackState.Listener.IsListening) |
{ |
callbackState.Listener.BeginGetContext(ContextCallback, callbackState); |
callbackState.ContextRetrieved.WaitOne(); |
|
if (WaitHandle.WaitAny(new[] { callbackState.ContextRetrieved, stopListen.WaitHandle }) != 1) |
continue; |
|
callbackState.Listener.Stop(); |
break; |
} |
} |
|
@@ -66,7 +72,7 @@ |
private void ContextCallback(IAsyncResult ar) |
{ |
var callbackState = (HTTPServerCallbackState)ar.AsyncState; |
HttpListenerContext httpContext = null; |
HttpListenerContext httpContext; |
|
Interlocked.Increment(ref processedRequests); |
Interlocked.Increment(ref activeRequests); |
@@ -85,9 +91,6 @@ |
callbackState.ContextRetrieved.Set(); |
} |
|
if (httpContext == null) |
return; |
|
try |
{ |
ProcessHTTPContext(httpContext); |
@@ -101,7 +104,6 @@ |
public void Dispose() |
{ |
Stop(); |
HTTPListener = null; |
} |
|
private class HTTPServerCallbackState |
@@ -109,7 +111,7 @@ |
public HTTPServerCallbackState(HttpListener listener) |
{ |
if (listener == null) |
throw new ArgumentNullException("listener"); |
throw new ArgumentNullException(nameof(listener)); |
Listener = listener; |
ContextRetrieved = new AutoResetEvent(false); |
} |
/Platform/Windows/Commands/NetSH/URLACL.cs |
@@ -4,9 +4,12 @@ |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System.ComponentModel; |
using System.Diagnostics; |
using System.IO; |
using System.Text; |
using System.Text.RegularExpressions; |
using Process = System.Diagnostics.Process; |
|
namespace wasSharpNET.Platform.Windows.Commands.NetSH |
{ |
@@ -28,8 +31,7 @@ |
|
URLReservationRegex = |
new Regex( |
string.Format(@"{0}.*{1}", Regex.Escape(URL), |
Regex.Escape(string.Format(@"{0}\{1}", domain, username))), |
$@"{Regex.Escape(URL)}.*{Regex.Escape(string.Format(@"{0}\{1}", domain, username))}", |
RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | |
RegexOptions.Compiled); |
} |
@@ -64,28 +66,62 @@ |
} |
} |
|
public void Reserve() |
public bool Reserve() |
{ |
System.Diagnostics.Process.Start(new ProcessStartInfo("netsh", |
string.Format(@"http add urlacl url={0} user={1}\{2}", URL, domain, username)) |
System.Diagnostics.Process process = null; |
try |
{ |
Verb = @"runas", |
CreateNoWindow = true, |
WindowStyle = ProcessWindowStyle.Hidden, |
UseShellExecute = false |
}).WaitForExit(timeout); |
process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh", |
$@"http add urlacl url={URL} user={domain}\{username}") |
{ |
Verb = @"runas", |
CreateNoWindow = true, |
WindowStyle = ProcessWindowStyle.Hidden, |
UseShellExecute = true |
}); |
} |
catch (Win32Exception ex) |
{ |
// User cancelled the UAC elevation prompt. |
if (ex.NativeErrorCode == 1223) |
{ |
return false; |
} |
} |
|
if (process == null) |
return false; |
|
return process.WaitForExit(timeout); |
} |
|
public void Release() |
public bool Release() |
{ |
System.Diagnostics.Process.Start(new ProcessStartInfo("netsh", |
string.Format(@"http del urlacl url={0}", URL)) |
System.Diagnostics.Process process = null; |
try |
{ |
Verb = @"runas", |
CreateNoWindow = true, |
WindowStyle = ProcessWindowStyle.Hidden, |
UseShellExecute = false |
}).WaitForExit(timeout); |
process = System.Diagnostics.Process.Start(new ProcessStartInfo("netsh", |
$@"http del urlacl url={URL}") |
{ |
Verb = @"runas", |
CreateNoWindow = true, |
WindowStyle = ProcessWindowStyle.Hidden, |
UseShellExecute = true |
}); |
} |
catch (Win32Exception ex) |
{ |
// User cancelled the UAC elevation prompt. |
if (ex.NativeErrorCode == 1223) |
{ |
return false; |
} |
} |
|
if (process == null) |
return false; |
|
return process.WaitForExit(timeout); |
} |
} |
} |