-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
108 lines (88 loc) · 3.04 KB
/
Program.cs
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
using System;
using System.Net;
namespace TelnetServer
{
class Program
{
private static Server s;
static void Main(string[] args)
{
s = new Server(IPAddress.Any);
s.ClientConnected += clientConnected;
s.ClientDisconnected += clientDisconnected;
s.ConnectionBlocked += connectionBlocked;
s.MessageReceived += messageReceived;
s.start();
Console.WriteLine("SERVER STARTED: " + DateTime.Now);
char read = Console.ReadKey(true).KeyChar;
do
{
if (read == 'b')
{
s.sendMessageToAll(Console.ReadLine());
}
} while ((read = Console.ReadKey(true).KeyChar) != 'q');
s.stop();
}
private static void clientConnected(Client c)
{
Console.WriteLine("CONNECTED: " + c);
s.sendMessageToClient(c, "Telnet Server" + Server.END_LINE + "Login: ");
}
private static void clientDisconnected(Client c)
{
Console.WriteLine("DISCONNECTED: " + c);
}
private static void connectionBlocked(IPEndPoint ep)
{
Console.WriteLine(string.Format("BLOCKED: {0}:{1} at {2}", ep.Address, ep.Port, DateTime.Now));
}
private static void messageReceived(Client c, string message)
{
if (c.getCurrentStatus() != EClientStatus.LoggedIn)
{
handleLogin(c, message);
return;
}
Console.WriteLine("MESSAGE: " + message);
if (message == "kickmyass" || message == "logout" ||
message == "exit")
{
s.kickClient(c);
s.sendMessageToClient(c, Server.END_LINE + Server.CURSOR);
}
else if (message == "clear")
{
s.clearClientScreen(c);
s.sendMessageToClient(c, Server.CURSOR);
}
else
s.sendMessageToClient(c, Server.END_LINE + Server.CURSOR);
}
private static void handleLogin(Client c, string message)
{
EClientStatus status = c.getCurrentStatus();
if (status == EClientStatus.Guest)
{
if (message == "root")
{
s.sendMessageToClient(c, Server.END_LINE + "Password: ");
c.setStatus(EClientStatus.Authenticating);
}
else
s.kickClient(c);
}
else if (status == EClientStatus.Authenticating)
{
if (message == "r00t")
{
s.clearClientScreen(c);
s.sendMessageToClient(c, "Successfully authenticated." + Server.END_LINE + Server.CURSOR);
c.setStatus(EClientStatus.LoggedIn);
}
else
s.kickClient(c);
}
}
}
}