public static async void TempAction (Object obj) { string url = "https://www.peterglotfelty.com:8000/"; while (true) { Console.WriteLine ("What application are you running?"); string app = Console.ReadLine (); Console.WriteLine ("Please enter your fingerprint :)"); string fingerprint = Console.ReadLine (); Console.WriteLine ("Sending Request to url:"); var remote = new RemoteConnectionManager (url); var req = new RequestObject (RequestType.GetAuthenticationRequest, app); req.Fields.Add ("fingerprint", fingerprint); req.Fields.Add ("application-password", "justice-league"); var res = await remote.SendDatabaseRequest (req); Console.WriteLine ("Response received"); Console.WriteLine ("The session id is: {0}", res.SessionID); Console.WriteLine ("What field would you like to retrieve?"); string field = Console.ReadLine (); req = new RequestObject (RequestType.GetFieldRequest, app); req.Fields.Add ("fieldname", field); req.Fields.Add ("sessionID", res.SessionID); Console.WriteLine (); Console.WriteLine ("Sending request for field"); res = await remote.SendDatabaseRequest (req); Console.WriteLine ("The response contains:"); foreach (var response in res.Responses) { Console.WriteLine (response); } string cont = "y"; Console.WriteLine (); Console.WriteLine ("Would you like to continue (y/n)?"); cont = Console.ReadLine (); if (cont.ToLower () == "n") { break; } } ((Semaphore)obj).Release (); }
//Disable the "async" warning until we actually implement the async parts. #pragma warning disable 1998 private static async void HandleRequest (IAsyncResult result) { // Note for all of this: // We're good as long as async operators work fine across platforms. // We will enter into callback hell if they don't. HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext (result); var httpRequest = context.Request; RequestObject requestObject = new RequestObject (); requestObject.RequestType = RequestType.InvalidRequest; ResponseObject databaseResponse = new ResponseObject (); try { requestObject = ParseRequest (httpRequest); if (requestObject.RequestType == RequestType.InvalidRequest) { // Handle us receiving a malformed request. throw new Exception("The request could not be properly parsed"); } // Console.WriteLine (requestObject.RequestType); // ================================================================= // Open a connection to the peripheral so that we can receive // a finger print from the user. // ================================================================= if (requestObject.RequestType == RequestType.GetAuthenticationRequest) { // Console.WriteLine ("Please enter your fingerprint:"); // var user = Console.ReadLine (); // var temp = new List<string>() {"Batman", "Superman", "Flash", "Wonderwoman", "Green Lantern", "Spiderman", "Green Arrow"}; // string user; // lock (o) { // user = temp [r.Next (temp.Count)]; // } // // // Randomly authenticate one of our users. // requestObject.Fields.Add ("fingerprint", user); PeripheralConnection peripheral = PeripheralConnection.Instance; // This either opens a new connection or simply verifies the existing // connection is good. await m.WaitAsync(); await peripheral.TryOpenConnection (); m.Release(); await peripheral.RequestTemplate(requestObject); } RemoteConnectionManager databaseManager = new RemoteConnectionManager (connectionString); databaseResponse = await databaseManager.SendDatabaseRequest (requestObject); } catch (Exception ex) { Console.WriteLine (ex.Message); } finally { var responseText = GenerateResponse (requestObject, databaseResponse); var responseBytes = System.Text.Encoding.UTF8.GetBytes (responseText); // Pretty simple, get a handle to the response packet and the output stream. var httpResponse = context.Response; httpResponse.ContentLength64 = responseBytes.Length; var outputStream = httpResponse.OutputStream; // Write the response to the buffer and then close the connection. Note that // These might take a while to do. outputStream.Write (responseBytes, 0, responseBytes.Length); outputStream.Close (); } }