Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (104 loc) · 4.06 KB
/
Copy pathProgram.cs
File metadata and controls
117 lines (104 loc) · 4.06 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
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
namespace BlinkkShell
{
internal static class Program
{
private static async Task Main()
{
const int port = 8020;
Console.Write("Enter server password: ");
string authPassword = ReadPassword();
if (string.IsNullOrWhiteSpace(authPassword))
{
Console.Error.WriteLine("A non-empty server password is required.");
WaitForExit();
return;
}
Console.WriteLine($"BlinkkShell Server v1.0.5 - {Environment.OSVersion}");
Console.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}");
RemoteShell shell = new RemoteShell(port, authPassword);
Task serverTask = shell.StartAsync();
Task inputTask = Task.Run(Console.ReadKey);
IPAddress[] addresses = GetLocalIPv4Addresses();
if (addresses.Length == 0)
{
Console.WriteLine("No active local IPv4 network address was found.");
}
else
{
Console.WriteLine("Available local IPv4 network addresses:");
foreach (IPAddress address in addresses)
Console.WriteLine($" {address}:{port}");
}
Console.WriteLine("Press ENTER to stop the server.");
if (await Task.WhenAny(serverTask, inputTask) == inputTask)
{
await inputTask;
shell.Stop();
await serverTask;
return;
}
try
{
await serverTask;
}
catch (SocketException exception)
{
Console.Error.WriteLine($"Failed to listen on port {port}: {exception.Message}");
WaitForExit();
}
catch (UnauthorizedAccessException exception)
{
Console.Error.WriteLine($"Permission denied while starting the server: {exception.Message}");
WaitForExit();
}
}
private static void WaitForExit()
{
Console.WriteLine("Press ENTER to close.");
Console.ReadLine();
}
private static string ReadPassword()
{
if (Console.IsInputRedirected)
return Console.ReadLine() ?? string.Empty;
StringBuilder password = new StringBuilder();
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(intercept: true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
return password.ToString();
}
if (key.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
password.Length--;
continue;
}
if (!char.IsControl(key.KeyChar))
password.Append(key.KeyChar);
}
}
private static IPAddress[] GetLocalIPv4Addresses()
{
return NetworkInterface.GetAllNetworkInterfaces()
.Where(networkInterface =>
networkInterface.OperationalStatus == OperationalStatus.Up &&
networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.SelectMany(networkInterface => networkInterface.GetIPProperties().UnicastAddresses)
.Select(unicastAddress => unicastAddress.Address)
.Where(address =>
address.AddressFamily == AddressFamily.InterNetwork &&
!IPAddress.IsLoopback(address) &&
!address.ToString().StartsWith("169.254.", StringComparison.Ordinal))
.Distinct()
.ToArray();
}
}
}