-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathServer.cs
153 lines (127 loc) · 5.03 KB
/
Server.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
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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Collections.Generic;
using KSSHServer.KexAlgorithms;
using KSSHServer.HostKeyAlgorithms;
using KSSHServer.Packets;
namespace KSSHServer
{
public static class ServerConstants
{
public const string ProtocolVersionExchange = "SSH-2.0-ksshserver";
}
public class Server
{
private IConfigurationRoot _Configuration;
private LoggerFactory _LoggerFactory;
private ILogger _Logger;
private const int DefaultPort = 22;
private const int ConectionBacklog = 64;
private TcpListener _Listener;
private List<Client> _Clients = new List<Client>();
private static Dictionary<string, string> _HostKeys = new Dictionary<string, string>();
public Server()
{
_Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("sshserver.json", optional: false)
.Build();
_LoggerFactory = new LoggerFactory();
_LoggerFactory.AddConsole(_Configuration.GetSection("Logging"));
_Logger = _LoggerFactory.CreateLogger("KSSHServer");
IConfigurationSection keys = _Configuration.GetSection("keys");
foreach (IConfigurationSection key in keys.GetChildren())
{
_HostKeys[key.Key] = key.Value;
}
}
public static IReadOnlyList<Type> SupportedHostKeyAlgorithms { get; private set; } = new List<Type>()
{
typeof(SSHRSA)
};
public static T GetType<T>(IReadOnlyList<Type> types, string selected) where T : class
{
foreach (Type type in types)
{
IAlgorithm algo = Activator.CreateInstance(type) as IAlgorithm;
if (algo.Name.Equals(selected, StringComparison.OrdinalIgnoreCase))
{
if (algo is IHostKeyAlgorithm)
{
((IHostKeyAlgorithm)algo).ImportKey(_HostKeys[algo.Name]);
}
return algo as T;
}
}
return default(T);
}
public void Start()
{
Stop();
_Logger.LogInformation("Starting up...");
int port = _Configuration.GetValue<int>("port", DefaultPort);
_Listener = new TcpListener(IPAddress.Any, port);
_Listener.Start(ConectionBacklog);
_Logger.LogInformation($"Listening on port: {port}");
}
public void Stop()
{
if (_Listener != null)
{
_Logger.LogInformation("Shutting down...");
_Listener.Stop();
_Listener = null;
// Disconnect each client and clear list
_Clients.ForEach(c => c.Disconnect(DisconnectReason.SSH_DISCONNECT_BY_APPLICATION, "The server is getting shutdown."));
_Clients.Clear();
_Logger.LogInformation("Shutting down...");
}
}
public void Poll()
{
// Check for new connections
while (_Listener.Pending())
{
Task<Socket> acceptTask = _Listener.AcceptSocketAsync();
acceptTask.Wait();
Socket socket = acceptTask.Result;
_Logger.LogDebug($"New Client: {socket.RemoteEndPoint}");
_Clients.Add(new Client(socket, _LoggerFactory.CreateLogger(socket.RemoteEndPoint.ToString())));
}
// Poll each client
_Clients.ForEach(c => c.Poll());
// Remove all disconnected clients
_Clients.RemoveAll(c => c.IsConnected() == false);
}
public static IReadOnlyList<Type> SupportedCompressions { get; private set; } = new List<Type>()
{
typeof(Compressions.NoCompression)
};
public static IReadOnlyList<Type> SupportedMACAlgorithms { get; private set; } = new List<Type>()
{
typeof(MACAlgorithms.HMACSHA1)
};
public static IReadOnlyList<Type> SupportedCiphers { get; private set; } = new List<Type>()
{
// typeof(Ciphers.NoCipher),
typeof(Ciphers.TripleDESCBC)
};
public static IReadOnlyList<Type> SupportedKexAlgorithms { get; private set; } = new List<Type>()
{
typeof(DiffieHellmanGroup14SHA1)
};
public static IEnumerable<string> GetNames(IReadOnlyList<Type> types)
{
foreach (Type type in types)
{
IAlgorithm algo = Activator.CreateInstance(type) as IAlgorithm;
yield return type.Name;
}
}
}
}