Created
September 30, 2018 17:44
-
-
Save abfo/b40dec188e5a0f69193f9cf38f0cfeaa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Net; | |
| using System.Net.Sockets; | |
| using System.Text; | |
| using System.Threading; | |
| namespace fingerd | |
| { | |
| static class Program | |
| { | |
| private const int FingerPort = 79; | |
| private const int MaxFingerCommand = 256; | |
| private const string PlanFile = ".plan"; | |
| private static readonly TcpListener _tcpListener = new TcpListener( | |
| IPAddress.Any, FingerPort); | |
| static void Main() | |
| { | |
| _tcpListener.Start(); | |
| while (true) | |
| { | |
| TcpClient tcpClient = _tcpListener.AcceptTcpClient(); | |
| Thread clientThread = new Thread(ClientThread); | |
| clientThread.Start(tcpClient); | |
| } | |
| } | |
| static void ClientThread(object client) | |
| { | |
| NetworkStream clientStream = null; | |
| TcpClient tcpClient = client as TcpClient; | |
| if (tcpClient == null) { return; } | |
| try | |
| { | |
| byte[] command = new byte[MaxFingerCommand]; | |
| clientStream = tcpClient.GetStream(); | |
| int read = clientStream.Read(command, 0, command.Length); | |
| if (read == 0) { return; } | |
| ASCIIEncoding asciiEncoding = new ASCIIEncoding(); | |
| string commandText = asciiEncoding.GetString(command); | |
| int endOfCommand = commandText.IndexOf("\r\n", | |
| StringComparison.InvariantCultureIgnoreCase); | |
| if (endOfCommand <= 0) { return; } | |
| string user = commandText.Substring(0, endOfCommand); | |
| if (string.Compare(user, Environment.UserName, | |
| StringComparison.InvariantCultureIgnoreCase) != 0) { return; } | |
| string planPath = Path.Combine(Environment.GetFolderPath( | |
| Environment.SpecialFolder.UserProfile), | |
| PlanFile); | |
| if (!File.Exists(planPath)) { return; } | |
| string plan = File.ReadAllText(planPath) + "\r\n"; | |
| byte[] planBytes = asciiEncoding.GetBytes(plan); | |
| clientStream.Write(planBytes, 0, planBytes.Length); | |
| } | |
| catch (Exception ex) | |
| { | |
| Debug.WriteLine(ex); | |
| } | |
| finally | |
| { | |
| if (clientStream != null) | |
| { | |
| clientStream.Close(); | |
| } | |
| tcpClient.Close(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment