private static void ServerThread(object data) { NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, numThreads); int threadId = Thread.CurrentThread.ManagedThreadId; // Wait for a client to connect pipeServer.WaitForConnection(); Console.WriteLine("Client connected on thread[{0}].", threadId); try { // Read the request from the client. Once the client has // written to the pipe its security token will be available. StreamString ss = new StreamString(pipeServer); // Verify our identity to the connected client using a // string that the client anticipates. ss.WriteString("I am the one true server!"); string filename = ss.ReadString(); // Read in the contents of the file while impersonating the client. ReadFileToStream fileReader = new ReadFileToStream(ss, filename); // Display the name of the user we are impersonating. Console.WriteLine("Reading file: {0} on thread[{1}] as user: {2}.", filename, threadId, pipeServer.GetImpersonationUserName()); pipeServer.RunAsClient(fileReader.Start); } // Catch the IOException that is raised if the pipe is broken // or disconnected. catch (IOException e) { Console.WriteLine("ERROR: {0}", e.Message); } pipeServer.Close(); }
private static void ServerThread(object data) { NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, numThreads); int threadId = Thread.CurrentThread.ManagedThreadId; //wait for client to connect pipeServer.WaitForConnection(); Console.WriteLine("Client connected on thread[{0}].", threadId); try { //Read the request from the client. Once the client has //written to the pipe its security token will be available. //We might not need this security token ? //We need interprocess communication on the same computer StreamString ss = new StreamString(pipeServer); //Verify our identity to the connected client using a //string that the client anticipates ss.WriteString("I am the one true server!"); string filename = ss.ReadString(); //Read in the contents of the file while impersonating the client. ReadFileToStream fileReader = new ReadFileToStream(ss, filename); //Display the name of the user we are impersonating. //Impersonating ? Console.WriteLine("Reading file: {0} on thread{1} as user: {2}.", filename, threadId, pipeServer.GetImpersonationUserName()); //So impersonation is nothing but name of client at the other end ? Cool! pipeServer.RunAsClient(fileReader.Start); } catch(IOException e) { Console.WriteLine("ERROR: {0}", e.Message); } pipeServer.Close(); }
private static void ServerThread(object data) { var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, numThreads); int threadId = Thread.CurrentThread.ManagedThreadId; pipeServer.WaitForConnection(); Console.WriteLine("Client connected on thread[{0}].", threadId); try { StreamString ss = new StreamString(pipeServer); ss.WriteString("I am the one true server!"); string filename = ss.ReadString(); ReadFileToStream fileReader = new ReadFileToStream(ss, filename); Console.WriteLine("Reading file: {0} on thread[{1}] as user: {2}.", filename, threadId, pipeServer.GetImpersonationUserName()); pipeServer.RunAsClient(fileReader.Start); } catch (IOException e) { Console.WriteLine("ERROR: {0}", e.Message); } pipeServer.Close(); }
public void ServerThread() { NamedPipeServerStream pipeServer = new NamedPipeServerStream("FTPbox Server", PipeDirection.InOut, 5); int threadID = Thread.CurrentThread.ManagedThreadId; pipeServer.WaitForConnection(); Log.Write(l.Client, "Client connected, id: {0}", threadID); try { StreamString ss = new StreamString(pipeServer); ss.WriteString("ftpbox"); string args = ss.ReadString(); ReadMessageSent fReader = new ReadMessageSent(ss, "All done!"); Log.Write(l.Client, "Reading file: \n {0} \non thread [{1}] as user {2}.", args, threadID, pipeServer.GetImpersonationUserName()); List<string> li = new List<string>(); li = ReadCombinedParameters(args); CheckClientArgs(li.ToArray()); pipeServer.RunAsClient(fReader.Start); } catch (IOException e) { Common.LogError(e); } pipeServer.Close(); }
public static void ClientServerMessages() { byte[] msg1 = new byte[] { 5, 7, 9, 10 }; byte[] msg2 = new byte[] { 2, 4 }; byte[] received1 = new byte[] { 0, 0, 0, 0 }; byte[] received2 = new byte[] { 0, 0 }; byte[] received3 = new byte[] { 0, 0, 0, 0}; ; using (NamedPipeServerStream server = new NamedPipeServerStream("foomsg", PipeDirection.InOut, 1, PipeTransmissionMode.Message)) { using (NamedPipeClientStream client = new NamedPipeClientStream(".", "foomsg", PipeDirection.InOut, PipeOptions.None, System.Security.Principal.TokenImpersonationLevel.Identification)) { Task clientTask = Task.Run(() => { client.Connect(); client.Write(msg1, 0, msg1.Length); client.Write(msg2, 0, msg2.Length); client.Write(msg1, 0, msg1.Length); int serverCount = client.NumberOfServerInstances; Assert.Equal(1, serverCount); }); server.WaitForConnection(); int len1 = server.Read(received1, 0, msg1.Length); Assert.True(server.IsMessageComplete); Assert.Equal(msg1.Length, len1); Assert.Equal(msg1, received1); int len2 = server.Read(received2, 0, msg2.Length); Assert.True(server.IsMessageComplete); Assert.Equal(msg2.Length, len2); Assert.Equal(msg2, received2); int len3 = server.Read(received3, 0, msg1.Length - 1); // read one less than message Assert.False(server.IsMessageComplete); Assert.Equal(msg1.Length - 1, len3); int len4 = server.Read(received3, len3, received3.Length - len3); Assert.True(server.IsMessageComplete); Assert.Equal(msg1.Length, len3 + len4); Assert.Equal(msg1, received3); string userName = server.GetImpersonationUserName(); // not sure what to test here that will work in all cases } } }
private static void ServerThread(object data) { PipeSecurity ps = new PipeSecurity(); ps.AddAccessRule(new PipeAccessRule("IIS_IUSRS", PipeAccessRights.ReadWrite, AccessControlType.Allow)); ps.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().Owner, PipeAccessRights.FullControl, AccessControlType.Allow)); using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(RemoteDemoControlPipe.PIPE_NAME, PipeDirection.InOut, numThreads, PipeTransmissionMode.Message, PipeOptions.WriteThrough, 1024, 1024, ps)) { int threadId = Thread.CurrentThread.ManagedThreadId; Console.WriteLine("Created thread[{0}].", threadId); // Wait for a client to connect pipeServer.WaitForConnection(); Console.WriteLine("Client connected on thread[{0}].", threadId); try { // Read the request from the client. Once the client has // written to the pipe its security token will be available. StreamString ss = new StreamString(pipeServer); // Verify our identity to the connected client using a // string that the client anticipates. ss.WriteString(RemoteDemoControlPipe.PIPE_SIGNATURE); string filename = ss.ReadString(); if (WorkstationLockedUtil.IsWorkstationLocked()) { ss.WriteString("Workstation is locked. Scripts can not be run."); } else { // Display the name of the user we are impersonating. Console.WriteLine("Launching script: {0} on thread[{1}] as user: {2}.", filename, threadId, pipeServer.GetImpersonationUserName()); // Launch the specified script System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.UseShellExecute = false; if (File.Exists(filename)) { Process.Start(filename); ss.WriteString("Process started: " + filename); } else { ss.WriteString("File does not exist: " + filename); } } } // Catch the IOException that is raised if the pipe is broken // or disconnected. catch (IOException e) { Console.WriteLine("ERROR: {0}", e.Message); } pipeServer.Close(); } }