-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
312 lines (259 loc) · 10.7 KB
/
Program.cs
File metadata and controls
312 lines (259 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ClusterSimulator
{
public class TelnetServer
{
private readonly TcpListener listener;
private bool isRunning;
private const int PORT = 2323;
static bool OWNSPOTS = false; // Set to false random spots of random calls
static readonly string OWNCALL = "SM7IUN"; // Set to your own callsign
static readonly double[][] bandlimits =
[
[1810.0, 1840.0], // 160m
[3500.0, 3560.0], // 80m
[7000.0, 7040.0], // 40m
[14000.0, 14050.0], // 20m
[21000.0, 21050.0], // 15m
[28000.0, 28060.0] // 10m
];
public TelnetServer()
{
listener = new TcpListener(IPAddress.Any, PORT);
}
public void Start()
{
listener.Start();
isRunning = true;
Console.WriteLine($"Telnet server started on port {PORT}");
Console.WriteLine("Press Ctrl+C to stop the server");
while (isRunning)
{
try
{
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine($"Client connected from: {client.Client.RemoteEndPoint}");
// Handle each client in a separate thread
Thread clientThread = new(() => HandleClient(client)) { IsBackground = true };
clientThread.Start();
}
catch (Exception ex)
{
if (isRunning) // Only show error if we're supposed to be running
{
Console.WriteLine($"Error accepting client: {ex.Message}");
}
}
}
}
private static string Randomcall()
{
Random random = new();
string suffix = new([.. Enumerable.Range(0, 3).Select(_ => (char)random.Next('A', 'Z' + 1))]);
string number = ((char)random.Next('0', '9' + 1)).ToString();
string prefix = new([.. Enumerable.Range(0, 2).Select(_ => (char)random.Next('A', 'Z' + 1))]);
return $"{prefix}{number}{suffix}";
}
private static string RandomFrequcy()
{
Random random = new();
int bandIndex = random.Next(bandlimits.Length);
double frequency = random.NextDouble() * (bandlimits[bandIndex][1] - bandlimits[bandIndex][0]) + bandlimits[bandIndex][0];
return frequency.ToString("0.0", CultureInfo.GetCultureInfo("en-US"));
}
private static string Randomspot(bool ownspot)
{
string frequency, spotted, spotter;
if (ownspot)
{
spotted = OWNCALL;
spotter = Randomcall() + "-#";
frequency = "14043.2";
}
else
{
spotted = Randomcall();
spotter = Randomcall() + "-#";
frequency = RandomFrequcy();
}
Random random = new();
string comment = $"CW {random.Next(11, 37)} dB {random.Next(28, 45)} WPM CQ";
string time = DateTime.UtcNow.ToString("HHmmZ");
// AK1A format
// 1 2 3 4 5 6 7
// 012345678901234567890123456789012345678901234567890123456789012345678901234
// DX de W3OA-#: 7031.5 W8KJP CW 12 dB 22 WPM CQ ? 1945Z
// DX de NN5ABC-#: 14065.00 SM8NIO CW 1844Z
// DX de PY2MKU-# 14065.0 SM7IUN CW 29dB Q:9* Z:14,15,20 1922Z
string line = $"DX de {spotter + ":",-10} {frequency,7} {spotted,-12} {comment,-30} {time}\r\n";
//Console.WriteLine("DX de W3OA-#: 7031.5 W8KJP CW 12 dB 22 WPM CQ ? 1945Z");
//Console.WriteLine(line);
return line;
}
private void HandleClient(TcpClient client)
{
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[1024];
try
{
Thread.Sleep(500);
// Send telnet negotiation commands
SendTelnetNegotiation(stream);
// Send welcome message
SendMessage(stream, "Welcome to Simple Telnet Server!\r\n");
SendMessage(stream, "Available commands: help, time, echo <message>, bye\r\n");
SendMessage(stream, "> ");
Thread.Sleep(1000);
while (client.Connected && isRunning)
{
Thread.Sleep(125);
string spotline = Randomspot(OWNSPOTS);
SendMessage(stream, spotline);
if (stream.DataAvailable)
{
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0) break; // Client disconnected
string input = Encoding.ASCII.GetString(buffer, 0, bytesRead).Trim();
// Handle telnet control characters
input = CleanTelnetInput(input);
if (string.IsNullOrEmpty(input))
{
SendMessage(stream, "> ");
continue;
}
Console.WriteLine($"Received: {input}");
// Process the command
string response = ProcessCommand(input);
if (input.Equals("bye", StringComparison.OrdinalIgnoreCase))
{
SendMessage(stream, "Goodbye!\r\n");
break;
}
SendMessage(stream, response + "\r\n> ");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Client error: {ex.Message}");
}
finally
{
Console.WriteLine($"Client disconnected: {client.Client?.RemoteEndPoint}");
client.Close();
}
}
private static void SendTelnetNegotiation(NetworkStream stream)
{
// Basic telnet negotiation to make clients happy
// IAC WILL ECHO - Server will echo characters
byte[] willEcho = [0xFF, 0xFB, 0x01];
stream.Write(willEcho, 0, willEcho.Length);
// IAC WILL SUPPRESS-GO-AHEAD - Suppress go-ahead
byte[] willSuppressGA = [0xFF, 0xFB, 0x03];
stream.Write(willSuppressGA, 0, willSuppressGA.Length);
stream.Flush();
}
private static string CleanTelnetInput(string input)
{
// Remove common telnet control sequences and non-printable characters
StringBuilder cleaned = new();
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
// Skip telnet IAC sequences (starts with 0xFF)
if (c == 0xFF && i + 2 < input.Length)
{
i += 2; // Skip the next two bytes
continue;
}
// Keep printable ASCII characters and common whitespace
if (c >= 32 && c <= 126 || c == '\t' || c == '\r' || c == '\n')
{
cleaned.Append(c);
}
}
return cleaned.ToString().Trim();
}
private static string ProcessCommand(string command)
{
string[] parts = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0) return "Type 'help' for available commands";
string cmd = parts[0].ToLower();
switch (cmd)
{
case "help":
return "Available commands:\r\n" +
" help - Show this help message\r\n" +
" time - Show current server time\r\n" +
" echo <message> - Echo back your message\r\n" +
" uptime - Show server uptime\r\n" +
" own - Produce own spots\r\n" +
" notown - Produce random spots\r\n" +
" bye - Disconnect from server";
case "time":
return $"Current server time: {DateTime.Now:yyyy-MM-dd HH:mm:ss}";
case "uptime":
TimeSpan uptime = DateTime.Now - System.Diagnostics.Process.GetCurrentProcess().StartTime;
return $"Server uptime: {uptime.Days}d {uptime.Hours}h {uptime.Minutes}m {uptime.Seconds}s";
case "own":
OWNSPOTS = true;
return "Producing own spots";
case "notown":
OWNSPOTS = false;
return "Producing own spots";
case "echo":
if (parts.Length > 1)
{
return "Echo: " + string.Join(" ", parts, 1, parts.Length - 1);
}
return "Echo: (no message provided)";
default:
return $"Unknown command: '{command}'. Type 'help' for available commands.";
}
}
private static void SendMessage(NetworkStream stream, string message)
{
try
{
byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
stream.Flush();
}
catch (Exception ex)
{
Console.WriteLine($"Error sending message: {ex.Message}");
}
}
public void Stop()
{
isRunning = false;
listener?.Stop();
Console.WriteLine("Server stopped.");
}
public static void Main()
{
TelnetServer server = new();
// Handle Ctrl+C gracefully
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
server.Stop();
Environment.Exit(0);
};
try
{
server.Start();
}
catch (Exception ex)
{
Console.WriteLine($"Server error: {ex.Message}");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
}