Skip to content

Commit 90651f9

Browse files
committed
.
1 parent 35848a3 commit 90651f9

File tree

7 files changed

+154
-84
lines changed

7 files changed

+154
-84
lines changed

fast cf ip scanner/Model/IpOptionModel.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
using System.Text;
55
using System.Threading.Tasks;
66

7-
namespace fast_cf_ip_scanner.Model
7+
namespace fast_cf_ip_scanner.Model;
8+
9+
public class IpOptionModel
810
{
9-
public class IpOptionModel
11+
public IpOptionModel()
1012
{
11-
public List<string> Ports { get; set; } = Constants.HttpPorts.Concat(Constants.HttpsPorts).ToList();
13+
Ports = new List<string>();
14+
Ports.AddRange(new List<string>() { "80", "443", "8080" });
15+
}
16+
public List<string> Ports { get; set; }
1217

13-
public int MaxPingOfIP { get; set; } = 1000;
18+
public int MaxPingOfIP { get; set; } = 1000;
1419

15-
public int MinimumCountOfValidIp { get; set; } = 5;
16-
public int CountOfRepeatTestForEachIp { get; set; } = 3;
17-
}
20+
public int MinimumCountOfValidIp { get; set; } = 5;
21+
public int CountOfRepeatTestForEachIp { get; set; } = 1;
22+
public int CountOfIpRanges { get; set; } = 4;
23+
public int CountOfIpForTest { get; set; } = 20;
1824
}

fast cf ip scanner/Services/IPService.cs

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ public IPService(FastCFIPScannerDatabase db)
1919

2020
#region Test Ips
2121

22-
public async Task<List<IPModel>> GetIpValid(string[] ips, int maxPing, string protcol)
22+
public async Task<List<IPModel>> GetIpValid(string[] ips, IpOptionModel ipOptions, string protcol)
2323
{
2424
var validIps = new List<IPModel>();
2525
switch (protcol)
2626
{
2727

2828
case "Http test":
29-
validIps = await GetValidٌIPWithHttpTest(ips, maxPing);
29+
validIps = await GetValidٌIPWithHttpTest(ips, ipOptions);
3030
break;
3131

3232
case "TCP test":
33-
validIps = await GetValidIPWithTCPTest(ips, maxPing);
33+
validIps = await GetValidIPWithTCPTest(ips, ipOptions);
3434
break;
3535

36-
case "UDP test":
37-
validIps = await GetValidIPWithUDPTest(ips, maxPing);
38-
break;
36+
//case "UDP test":
37+
// validIps = await GetValidIPWithUDPTest(ips, ipOptions);
38+
// break;
3939

4040
default:
41-
validIps = await GetValidIPWithTCPTest(ips, maxPing);
41+
validIps = await GetValidٌIPWithHttpTest(ips, ipOptions);
4242
break;
4343
}
4444
return validIps;
4545
}
4646

47-
public async Task<List<IPModel>> GetValidٌIPWithHttpTest(string[] ips, int maxPing)
47+
public async Task<List<IPModel>> GetValidٌIPWithHttpTest(string[] ips, IpOptionModel ipOptions)
4848
{
4949

5050
var validIp = new List<IPModel>();
@@ -56,40 +56,41 @@ async Task HttpTest(string ipAddresse)
5656
var SocketsHandler = new SocketsHttpHandler();
5757
var Client = new HttpClient(SocketsHandler)
5858
{
59-
Timeout = TimeSpan.FromSeconds(maxPing),
59+
Timeout = TimeSpan.FromSeconds(ipOptions.MaxPingOfIP),
6060
};
6161

62-
var randomPorts = GetRandomPort();
6362
int totalPing = 0;
6463
var ports = new List<string>();
6564
var ipIsConnected = false;
66-
67-
foreach (var port in randomPorts)
65+
for (int i = 0; i < ipOptions.CountOfRepeatTestForEachIp; i++)
6866
{
69-
70-
try
67+
foreach (var port in ipOptions.Ports)
7168
{
72-
stopwatch.Start();
73-
var result = await Client.GetAsync($"http://{ipAddresse}/__down:{port}");
74-
stopwatch.Stop();
7569

76-
if (result != null)
70+
try
7771
{
78-
ipIsConnected = true;
79-
ports.Add(port);
72+
stopwatch.Start();
73+
var result = await Client.GetAsync($"http://{ipAddresse}/__down:{port}");
74+
stopwatch.Stop();
75+
76+
if (result != null)
77+
{
78+
ipIsConnected = true;
79+
ports.Add(port);
80+
}
81+
}
82+
catch
83+
{
84+
ipIsConnected = false;
85+
stopwatch.Stop();
8086
}
81-
}
82-
catch
83-
{
84-
ipIsConnected = false;
85-
stopwatch.Stop();
86-
}
8787

8888

89-
var currentPing = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
90-
totalPing += currentPing;
89+
var currentPing = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
90+
totalPing += currentPing;
91+
}
9192
}
92-
int ping = totalPing / 3;
93+
int ping = totalPing / (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count);
9394
if (ipIsConnected)
9495
{
9596
lock (validIp)
@@ -107,22 +108,22 @@ async Task HttpTest(string ipAddresse)
107108
#region repeat test
108109
while (true)
109110
{
110-
if (validIp.Count >= 5)
111+
if (validIp.Count >= ipOptions.MinimumCountOfValidIp)
111112
{
112113
return validIp;
113114
}
114-
var newRandomIps = GetRandomIp(ips);
115+
var newRandomIps = GetRandomIp(ips, ipOptions.CountOfIpForTest, ipOptions.CountOfIpRanges);
115116

116117
List<Task> tasks = new List<Task>();
117118

118119
foreach (var ip in newRandomIps)
119120
{
120121
tasks.Add(HttpTest(ip));
121122
}
122-
for (int i = 0; i < maxPing / 100; i++)
123+
for (int i = 0; i < ipOptions.MaxPingOfIP / 100; i++)
123124
{
124125
await Task.Delay(100);
125-
if (validIp.Count >= 5)
126+
if (validIp.Count >= ipOptions.MinimumCountOfValidIp)
126127
{
127128
return validIp;
128129
}
@@ -131,32 +132,36 @@ async Task HttpTest(string ipAddresse)
131132
#endregion
132133
}
133134

134-
public async Task<List<IPModel>> GetValidIPWithTCPTest(string[] ips, int maxPing)
135+
public async Task<List<IPModel>> GetValidIPWithTCPTest(string[] ips, IpOptionModel ipOptions)
135136
{
136137

137138
var validIp = new List<IPModel>();
138139

139140
async Task TestConnectionAsync(string ip)
140141
{
141-
var randomPort = GetRandomPort();
142+
142143
var ports = new List<string>();
143144
var ipIsConnected = false;
144-
foreach (var port in randomPort)
145+
for (int i = 0; i < ipOptions.CountOfRepeatTestForEachIp; i++)
145146
{
146-
try
147+
148+
foreach (var port in ipOptions.Ports)
147149
{
148-
TcpClient tcpClient = new TcpClient();
149-
tcpClient.ConnectAsync(ip, int.Parse(port));
150-
await Task.Delay(maxPing);
151-
if (tcpClient.Connected)
150+
try
152151
{
153-
ipIsConnected = true;
154-
ports.Add(port);
152+
TcpClient tcpClient = new TcpClient();
153+
tcpClient.ConnectAsync(ip, int.Parse(port));
154+
await Task.Delay(ipOptions.MaxPingOfIP);
155+
if (tcpClient.Connected)
156+
{
157+
ipIsConnected = true;
158+
ports.Add(port);
159+
}
155160
}
156-
}
157-
catch
158-
{
161+
catch
162+
{
159163

164+
}
160165
}
161166
}
162167

@@ -175,19 +180,19 @@ async Task TestConnectionAsync(string ip)
175180
}
176181
while (true)
177182
{
178-
if (validIp.Count >= 5)
183+
if (validIp.Count >= ipOptions.MinimumCountOfValidIp)
179184
{
180185
break;
181186
}
182-
var newRandomIps = GetRandomIp(ips);
187+
var newRandomIps = GetRandomIp(ips, ipOptions.CountOfIpForTest, ipOptions.CountOfIpRanges);
183188

184189
List<Task> tasks = new List<Task>();
185190

186191
foreach (var ip in newRandomIps)
187192
{
188193
tasks.Add(TestConnectionAsync(ip));
189194
}
190-
await Task.Delay(maxPing + 100);
195+
await Task.Delay(ipOptions.MaxPingOfIP * (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count));
191196
}
192197
return validIp;
193198
}
@@ -265,7 +270,7 @@ public List<string> GetRandomIp(string[] ips, int ipCount = 20, int ipRangeCount
265270
randomIpRange.Add(strIp);
266271
}
267272
var randomIps = new List<string>();
268-
var countOfEachRange = (ipCount / ipRangeCount) + 1;
273+
var countOfEachRange = ipCount / ipRangeCount;
269274
foreach (var iprange in randomIpRange)
270275
{
271276
for (int i = 1; i < countOfEachRange; i++)

fast cf ip scanner/ViewModels/IpOptionViewModel.cs

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,47 @@ public partial class IpOptionViewModel : BaseViewModel
2626
[ObservableProperty]
2727
int countOfRepeatTestForEachIp;
2828

29+
[ObservableProperty]
30+
int countOfIpRanges;
31+
2932

30-
bool Saved = false;
33+
bool saved = false;
3134

3235

3336
public IpOptionViewModel()
3437
{
3538
ports = new ObservableCollection<PortForShow>();
3639
}
3740

41+
[ObservableProperty]
42+
int countOfIpForTest;
43+
44+
int totalTestForEachIp = 0;
3845
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
3946
{
4047
base.OnPropertyChanged(e);
41-
if (e.PropertyName == "IpOptions" && !Saved)
48+
if (e.PropertyName == "IpOptions" && !saved)
4249
{
43-
foreach (var port in IpOptions.Ports)
50+
var allPorts = Constants.HttpPorts.Concat(Constants.HttpsPorts);
51+
var selectedPort = IpOptions.Ports;
52+
foreach (var port in allPorts)
4453
{
45-
Ports.Add(new PortForShow(port, false));
54+
Ports.Add(new PortForShow(port, selectedPort.Any(p => p == port)));
4655
}
4756
MaxPingOfIP = IpOptions.MaxPingOfIP;
4857
MinimumCountOfValidIp = IpOptions.MinimumCountOfValidIp;
4958
CountOfRepeatTestForEachIp = IpOptions.CountOfRepeatTestForEachIp;
59+
CountOfIpRanges = IpOptions.CountOfIpRanges;
60+
CountOfIpForTest = IpOptions.CountOfIpForTest;
5061
}
5162
}
5263

5364
[RelayCommand]
5465
async void Save()
5566
{
56-
Saved = true;
67+
saved = true;
5768
IpOptions.Ports.Clear();
58-
foreach (var portForShow in Ports)
59-
{
60-
if (portForShow.IsChecked)
61-
{
62-
IpOptions.Ports.Add(portForShow.Port);
63-
}
64-
}
69+
IpOptions.Ports.AddRange(Ports.Where(p => p.IsChecked).Select(p => p.Port));
6570
if (IpOptions.Ports.Count == 0)
6671
{
6772
IpOptions.Ports.AddRange(GetRandomPort());
@@ -73,6 +78,7 @@ async void Save()
7378
else
7479
{
7580
await App.Current.MainPage.DisplayAlert("error", "Minimum Count is 1", "ok");
81+
saved = false;
7682
}
7783
if (MinimumCountOfValidIp > 0)
7884
{
@@ -81,6 +87,7 @@ async void Save()
8187
else
8288
{
8389
await App.Current.MainPage.DisplayAlert("error", "Minimum Count is 1", "ok");
90+
saved = false;
8491

8592
}
8693
if (MaxPingOfIP > 100)
@@ -90,9 +97,44 @@ async void Save()
9097
else
9198
{
9299
await App.Current.MainPage.DisplayAlert("error", "Minimum ping is 100", "ok");
100+
saved = false;
101+
102+
}
103+
if (CountOfIpRanges > 0)
104+
{
105+
IpOptions.CountOfIpRanges = CountOfIpRanges;
106+
}
107+
else
108+
{
109+
await App.Current.MainPage.DisplayAlert("error", "Minimum Count is 1", "ok");
110+
saved = false;
111+
112+
}
113+
if (CountOfIpForTest > 0)
114+
{
115+
try
116+
{
117+
118+
}
119+
catch
120+
{
121+
122+
}
93123
}
124+
else
125+
{
126+
saved = false;
127+
}
128+
129+
130+
if (saved)
131+
{
132+
totalTestForEachIp = CountOfRepeatTestForEachIp * (Ports.Where(p=>p.IsChecked)).Count();
133+
var countOfIpInEachRange = CountOfIpForTest / CountOfIpRanges;
94134

95-
await Shell.Current.GoToAsync("../");
135+
await App.Current.MainPage.DisplayAlert("Info", $"{CountOfIpForTest} ips in {CountOfIpRanges} ranges will be tested {totalTestForEachIp} times", "ok");
136+
await Shell.Current.GoToAsync("../");
137+
}
96138
}
97139
public string[] GetRandomPort(int count = 3)
98140
{
@@ -108,5 +150,8 @@ public string[] GetRandomPort(int count = 3)
108150
return randomPorts;
109151
}
110152

111-
153+
public void UpdateVar()
154+
{
155+
totalTestForEachIp = CountOfRepeatTestForEachIp * (Ports.Where(p => p.IsChecked)).Count();
156+
}
112157
}

0 commit comments

Comments
 (0)