-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeagueProxy.cs
110 lines (99 loc) · 3.33 KB
/
LeagueProxy.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
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices.Marshalling;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace LionsGate;
public class LeagueProxy
{
private CancellationTokenSource? _ServerCTS;
private readonly ConfigProxy _ConfigProxy;
public static int ConfigPort { get; private set; }
public LeagueProxy()
{
_ConfigProxy = new ConfigProxy();
}
private static void TerminateRiotServices()
{
string[] riotProcesses = ["RiotClientServices"];
foreach (var processName in riotProcesses)
{
try
{
var processes = Process.GetProcessesByName(processName);
foreach (var process in processes)
{
process.Kill();
Console.WriteLine(" [INFO] Stopping RCS process...");
process.WaitForExit();
Console.WriteLine(" [INFO] Stopped RCS process, resarting it now...");
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($" [ERROR] Failed to stop {processName} - {ex.Message}. Try running this app is administrator if this issue persists.");
Console.ResetColor();
}
}
}
public async Task Start()
{
if (_ServerCTS is not null)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(" [WARN] Proxy is already running. Attempting to restart.");
Console.ResetColor();
Stop();
}
TerminateRiotServices();
await FindAvailablePortsAsync();
_ServerCTS = new CancellationTokenSource();
_ConfigProxy?.RunAsync(_ServerCTS.Token);
}
private static async Task FindAvailablePortsAsync()
{
int[] ports = new int[1];
for (int i = 0; i < ports.Length; i++)
{
ports[i] = GetFreePort();
await Task.Delay(10);
}
ConfigPort = ports[0];
}
private static int GetFreePort()
{
using var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
public void Stop()
{
if (_ServerCTS is null)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(" [WARN] Unable to stop proxy service: Service is not running.");
Console.ResetColor();
return;
}
_ServerCTS?.Cancel();
_ConfigProxy.Stop();
_ServerCTS?.Dispose();
_ServerCTS = null;
Console.WriteLine(" [DEBUG] Proxy services successfully stopped.");
}
public Process? LaunchRCS(IEnumerable<string>? args = null)
{
if (_ServerCTS is null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" [ERROR] RCS launch failed: Proxies were not started due to an error.");
Console.ResetColor();
}
return RiotClient.Launch(args);
}
}