-
Notifications
You must be signed in to change notification settings - Fork 5
/
BotInstance.cs
276 lines (239 loc) · 9.42 KB
/
BotInstance.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
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
using KickLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using KickLib.Helpers;
using KickLib.Models;
namespace KickFuckerApi.Models
{
public enum BotStatus
{
Starting,
Started,
Stopping,
Stopped
}
public class BotInstance
{
private const bool BYPASS_ENABLED = false;
public string Channel { get; set; }
public int KickViewTaskId { get; set; }
public int Count { get; set; }
public List<KickClient> KickClients { get; }
public BotStatus Status { get; private set; }
public int WorkingClients => KickClients.Count;
public event EventHandler BotStopped;
private BypassWrapper _wrapper;
public BotInstance()
{
_wrapper = new BypassWrapper("bypass.py");
KickClients = new List<KickClient>();
Status = BotStatus.Stopped;
}
private ChannelApiResponse ChannelInfo = null;
private bool _live = false;
public async Task UpdateChannelInfoTaskAsync(string channel)
{
int errorCount = 0;
bool crashDetect = false;
while (errorCount < 15 && (Status == BotStatus.Starting || Status == BotStatus.Started))
{
try
{
using (HttpClient client = new HttpClient())
{
bool channelInfoGathered = false;
try
{
if (ChannelInfo == null)
{
var channelInfos = await KickClient.GetChannelInfosAsync(channel, 1);
ChannelInfo = channelInfos[0];
if (!string.IsNullOrEmpty(ChannelInfo?.playback_url))
{
channelInfoGathered = true;
}
}
//we try to gather info from playback url
var response = await client.GetAsync(ChannelInfo.playback_url);
response.EnsureSuccessStatusCode();
ChannelInfo.Active = true;
_live = true;
errorCount = 0;
if (crashDetect)
{
Console.WriteLine("Crash bypassed stream is live");
crashDetect = false;
}
}
catch (Exception e)
{
if (!crashDetect)
{
Console.WriteLine("Possible stream crash deteced!.");
crashDetect = true;
}
if (channelInfoGathered)
{
errorCount++;
ChannelInfo.Active = false;
}
//if the playback url is not accessible we try to gather the new one. (new one can be not accessible too, we will try it up to 10 times until it's accessible maybe stream is just lagged)
var channelInfos = await KickClient.GetChannelInfosAsync(channel, 1);
ChannelInfo.playback_url = channelInfos[0].playback_url;
ChannelInfo.id = channelInfos[0].id;
//if this block throws error than we just think that stream is ended
}
}
}
catch (Exception e)
{
// ignored
}
await Task.Delay(10000);
}
Console.WriteLine("Stream looks like ended.");
if (Status is BotStatus.Starting or BotStatus.Started)
{
await StopAsync(0);
}
}
public async Task Start(string channel, int count, int delay)
{
try
{
Channel = channel;
Count = count;
Status = BotStatus.Starting;
Task.Run(() => UpdateChannelInfoTaskAsync(channel));
int waiter = 0;
while (true)
{
waiter++;
if (ChannelInfo != null && _live)
{
break;
}
if (waiter > 20)
{
Status = BotStatus.Stopped;
throw new Exception("Stream looks like not live.");
}
await Task.Delay(1000);
}
if (BYPASS_ENABLED)
{
//Start's bypass
Task.Run(() => _wrapper.StartScript(ChannelInfo.id, count, "bypass_proxies.txt"));
}
for (int i = 0; i < count; i++)
{
var kickClient = new KickClient();
kickClient.BotStopped += (sender, e) => RemoveFailedClient((KickClient)sender);
KickClients.Add(kickClient);
var stopwatch = new Stopwatch();
if (Status != BotStatus.Stopping || Status != BotStatus.Stopped)
{
stopwatch.Start();
var i1 = i;
Task.Run(() => kickClient.WatchChannelReadyAsync(ChannelInfo));
stopwatch.Stop();
}
var elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
if (i < count - 1)
{
await Task.Delay(TimeSpan.FromMilliseconds(delay));
}
}
Status = BotStatus.Started;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public async Task IncreaseViewersAsync(int count, int delay)
{
if (BYPASS_ENABLED)
{
_ = Task.Run(() => _wrapper.AddThreads(count));
}
if(Status != BotStatus.Started)
{
throw new InvalidOperationException("BotInstance must be started before increasing viewers.");
}
for (int i = 0; i < count; i++)
{
await AddNewViewerAsync(ChannelInfo, delay);
}
}
private async Task AddNewViewerAsync(ChannelApiResponse channelInfo, int delay)
{
var kickClient = new KickClient();
kickClient.BotStopped += (sender, e) => RemoveFailedClient((KickClient)sender);
KickClients.Add(kickClient);
var stopwatch = new Stopwatch();
if (Status != BotStatus.Stopping || Status != BotStatus.Stopped)
{
stopwatch.Start();
_ = Task.Run(() => kickClient.WatchChannelReadyAsync(channelInfo));
stopwatch.Stop();
}
var elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
await Task.Delay(TimeSpan.FromMilliseconds(delay));
}
public async Task StopAsync(int delay)
{
Status = BotStatus.Stopping;
foreach (var client in KickClients.ToList())
{
client.Stop();
await Task.Delay(delay);
}
}
public async Task DecreaseViewersAsync(int count, int delay)
{
if(Status != BotStatus.Started)
{
throw new InvalidOperationException("BotInstance must be started before decreasing viewers.");
}
var random = new Random();
for (int i = 0; i < count; i++)
{
var clientsSnapshot = KickClients.ToList();
if (clientsSnapshot.Count == 0)
break; // No more clients to remove
var client = clientsSnapshot[random.Next(clientsSnapshot.Count)];
// Check if the client is still in the original list
if (!KickClients.Contains(client))
continue;
client.Stop();
KickClients.Remove(client);
if (i < count - 1) // If there are more clients to stop, delay the next operation
{
await Task.Delay(TimeSpan.FromMilliseconds(delay));
}
}
if (BYPASS_ENABLED)
{
_ = Task.Run(() => _wrapper.RemoveThreads(count));
}
}
private void RemoveFailedClient(KickClient kickClient)
{
KickClients.Remove(kickClient);
if (KickClients.Count == 0)
{
Status = BotStatus.Stopped;
BotStopped?.Invoke(this, EventArgs.Empty);
if (BYPASS_ENABLED)
{
_wrapper.StopScript();
}
}
}
}
}