From ff945fabb4d9b74822e0af7a63d042b59428003a Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Fri, 3 Oct 2025 15:44:50 -0400 Subject: init --- Program.cs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ fortune-pit-net.csproj | 11 ++++++++ fortune-pit-net.sln | 24 ++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 Program.cs create mode 100644 fortune-pit-net.csproj create mode 100644 fortune-pit-net.sln diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..5f8de30 --- /dev/null +++ b/Program.cs @@ -0,0 +1,69 @@ +using System; +using System.Net; +using System.Net.Sockets; + +const char DELIM = ' '; +const int DELAY_DURATION_S = 990; + +short ReadPort() => args.Length != 0 && short.TryParse(args[0], out var port) ? port : (short)22; + +IList ReadBanner() => Console.In.ReadToEnd().Split(DELIM, StringSplitOptions.RemoveEmptyEntries); + +async Task SshClientHandler(TcpClient client, IEnumerable banner) +{ + var remote = client.Client.RemoteEndPoint; + Console.WriteLine($"{remote}: Client connected."); + + try + { + using (client) + { + var stream = client.GetStream(); + var writer = new StreamWriter(stream) { AutoFlush = true }; + + while (true) + { + foreach (var word in banner) + { + await writer.WriteAsync($"{word}{DELIM}"); + await Task.Delay(DELAY_DURATION_S); + } + + Console.WriteLine($"{remote}: Client read it all!"); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"{remote}: Client handler error: {ex.Message}"); + } + finally + { + Console.WriteLine($"{remote}: Client hung up"); + } +} + +// main +var banner = ReadBanner(); +var port = ReadPort(); +var listener = new TcpListener(IPAddress.Any, port); + +using (listener) +{ + listener.Start(); + Console.WriteLine($"Listening on port {port}..."); + + while (true) + { + try + { + var client = await listener.AcceptTcpClientAsync(); + + _ = Task.Run(() => SshClientHandler(client, banner)); + } + catch (Exception ex) + { + Console.WriteLine($"Accept error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/fortune-pit-net.csproj b/fortune-pit-net.csproj new file mode 100644 index 0000000..8c4111f --- /dev/null +++ b/fortune-pit-net.csproj @@ -0,0 +1,11 @@ + + + + Exe + net9.0 + fortune_pit_net + enable + enable + + + diff --git a/fortune-pit-net.sln b/fortune-pit-net.sln new file mode 100644 index 0000000..053ecd3 --- /dev/null +++ b/fortune-pit-net.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "fortune-pit-net", "fortune-pit-net.csproj", "{411EEB40-238B-141E-1601-4515284FC02F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {411EEB40-238B-141E-1601-4515284FC02F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {411EEB40-238B-141E-1601-4515284FC02F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {411EEB40-238B-141E-1601-4515284FC02F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {411EEB40-238B-141E-1601-4515284FC02F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CFB56DBE-DD5E-437D-B5AD-A79D112EE7BB} + EndGlobalSection +EndGlobal -- cgit v1.2.3