diff options
author | Adam T. Carpenter <atc@53hor.net> | 2025-10-03 15:44:50 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2025-10-03 15:44:50 -0400 |
commit | ff945fabb4d9b74822e0af7a63d042b59428003a (patch) | |
tree | 8c71e6bee217160cfa8300c25939eb4025782061 | |
download | fortune-pit-net-master.tar.xz fortune-pit-net-master.zip |
-rw-r--r-- | Program.cs | 69 | ||||
-rw-r--r-- | fortune-pit-net.csproj | 11 | ||||
-rw-r--r-- | fortune-pit-net.sln | 24 |
3 files changed, 104 insertions, 0 deletions
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<string> ReadBanner() => Console.In.ReadToEnd().Split(DELIM, StringSplitOptions.RemoveEmptyEntries); + +async Task SshClientHandler(TcpClient client, IEnumerable<string> 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 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net9.0</TargetFramework> + <RootNamespace>fortune_pit_net</RootNamespace> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> 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 |