public OpenNetProtocolDriver(ClientSession session) : base("null") { _session = session; BinaryReader mreader = new BinaryReader(session.securedStream); while(true) { byte opcode = mreader.ReadByte(); if(opcode == 0) { return; } if(opcode == 1) { //XMIT int conid = mreader.ReadInt32(); Console.WriteLine(conid); sockets[conid].ntfyDgram(mreader.ReadBytes(mreader.ReadInt32())); Console.WriteLine("PACKET RECEIVED"); } if(opcode == 2) { //Establish network connection BinaryWriter mwriter = new BinaryWriter(session.securedStream); lock(session.securedStream) { Console.WriteLine("OPCODE 3"); mwriter.Write((byte)3); mwriter.Write(currentID); mwriter.Flush(); } lock(sockets) { ParallelSocket msock = new ParallelSocket(this,session.securedStream,currentID); sockets.Add(currentID,msock); System.Threading.ThreadPool.QueueUserWorkItem(thetar,msock); currentID++; } } } }
public void onRequest(ClientWebRequest request) { if(request.Method == "GET") { //Initiate session ClientHttpResponse response = new ClientHttpResponse(); response.ContentType = "text/html"; response.len = 1024*1024*300; response.StatusCode = "200 OK"; response.WriteHeader(request.stream); ClientSession session = new ClientSession(); session.sessionID = Guid.NewGuid(); TrashyStream garbage = new TrashyStream(request.stream); session.writer = garbage; BinaryWriter mwriter = new BinaryWriter(garbage); mwriter.Write(session.sessionID.ToByteArray()); mwriter.Flush(); lock(ClientSession.sessions) { ClientSession.sessions.Add(session.sessionID,session); } session.WaitHandle.WaitOne(); }else { try { TrashyStream reader = new TrashyStream(request.stream); BinaryReader mreader = new BinaryReader(reader); ClientSession currentSession = ClientSession.sessions[new Guid(mreader.ReadBytes(16))]; currentSession.reader = reader; BinaryWriter mwriter = new BinaryWriter(currentSession.writer); byte[] ourprivatekey = db[0]; byte[] ourpubkey = db.GetPublicKeyOnly(ourprivatekey); mwriter.Write(ourpubkey.Length); mwriter.Write(ourpubkey); mwriter.Flush(); byte[] theirpubkey = mreader.ReadBytes(mreader.ReadInt32()); if(!db.IsKeyTrusted(theirpubkey)) { db.AddPublicKey(theirpubkey); db.Commit(); } currentSession.securedStream = new TrashyStream(db.CreateAuthenticatedStream(ourprivatekey,new DualStream(currentSession.writer,currentSession.reader),32)); Console.WriteLine("Secure stream negotiated"); currentSession.pubKey = BitConverter.ToString(theirpubkey); OpenNetProtocolDriver driver = new OpenNetProtocolDriver(currentSession); }catch(Exception er) { Console.WriteLine(er); } } }