Skip to content

Commit 0e46eba

Browse files
author
a.hosseini
committed
add tcp test
1 parent c78a1aa commit 0e46eba

File tree

2 files changed

+164
-15
lines changed

2 files changed

+164
-15
lines changed

fast cf ip scanner/Services/IPService.cs

Lines changed: 155 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Net;
66
using System.Net.Http;
77
using System.Net.NetworkInformation;
8+
using System.Net.Sockets;
89
using System.Net.WebSockets;
910
using System.Reflection;
1011
using System.Resources;
@@ -20,35 +21,169 @@ public IPService(FastCFIPScannerDatabase db)
2021
}
2122

2223

23-
public async Task<List<IPModel>> GetIpValid(string[]ips,int maxPing)
24+
public async Task<List<IPModel>> GetIpValid(string[] ips, int maxPing,string protcol)
2425
{
25-
SocketsHttpHandler SocketsHandler = new SocketsHttpHandler();
26-
HttpClient Client = new HttpClient(SocketsHandler)
26+
var validIps = new List<IPModel>();
27+
switch(protcol)
2728
{
28-
Timeout = TimeSpan.FromSeconds(maxPing),
29-
};
3029

30+
case "Http test":
31+
validIps = await GetValidٌIPWithHttpTest(ips, maxPing);
32+
break;
33+
34+
case "TCP test":
35+
validIps = await GetValidIPWithTCPTest(ips, maxPing);
36+
break;
37+
38+
case "UDP test":
39+
validIps = await GetValidIPWithUDPTest(ips, maxPing);
40+
break;
41+
42+
default:
43+
validIps = await GetValidIPWithTCPTest(ips,maxPing);
44+
break;
45+
}
46+
return validIps;
47+
}
48+
49+
public async Task<List<IPModel>> GetValidٌIPWithHttpTest(string[] ips, int maxPing)
50+
{
3151

3252
var validIp = new List<IPModel>();
3353
for (int i = 0; i < 20; i++)
3454
{
3555
var t = new Task(async () =>
3656
{
3757
var stopwatch = new Stopwatch();
38-
var ipAddresse = GetRandomIp(ips);
39-
HttpResponseMessage result = new HttpResponseMessage();
58+
var ipAddresse = IPAddress.Parse(GetRandomIp(ips).Split("/")[0]);
59+
60+
try
61+
{
62+
var SocketsHandler = new SocketsHttpHandler();
63+
var Client = new HttpClient(SocketsHandler)
64+
{
65+
Timeout = TimeSpan.FromSeconds(maxPing),
66+
};
67+
68+
69+
stopwatch.Start();
70+
71+
72+
var result = await Client.GetAsync($"http://{ipAddresse}/__down");
73+
74+
75+
stopwatch.Stop();
76+
var ping = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
77+
78+
lock (validIp)
79+
{
80+
validIp.Add(new IPModel
81+
{
82+
IP = ipAddresse.ToString(),
83+
Ping = ping,
84+
});
85+
}
86+
}
87+
catch
88+
{
89+
stopwatch.Reset();
90+
}
91+
});
92+
t.Start();
93+
}
94+
for (int i = 0; i < maxPing * 2 / 100; i++)
95+
{
96+
await Task.Delay(100);
97+
if (validIp.Count >= 10)
98+
{
99+
return validIp;
100+
}
101+
}
102+
return validIp;
103+
}
104+
105+
106+
public async Task<List<IPModel>> GetValidIPWithTCPTest(string[] ips, int maxPing)
107+
{
108+
109+
var validIp = new List<IPModel>();
110+
for (int i = 0; i < 20; i++)
111+
{
112+
var t = new Task(() =>
113+
{
114+
var stopwatch = new Stopwatch();
115+
var ipAddresse = IPAddress.Parse(GetRandomIp(ips).Split("/")[0]);
116+
try
117+
{
118+
var client = new TcpClient();
119+
120+
stopwatch.Start();
121+
122+
123+
client.Connect(ipAddresse, 443);
124+
125+
126+
stopwatch.Stop();
127+
128+
var ping = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
129+
130+
lock (validIp)
131+
{
132+
validIp.Add(new IPModel
133+
{
134+
IP = ipAddresse.ToString(),
135+
Ping = ping,
136+
});
137+
}
138+
}
139+
catch
140+
{
141+
stopwatch.Reset();
142+
}
143+
});
144+
t.Start();
145+
}
146+
for (int i = 0; i < maxPing * 2 / 100; i++)
147+
{
148+
await Task.Delay(100);
149+
if (validIp.Count >= 10)
150+
{
151+
return validIp;
152+
}
153+
}
154+
return validIp;
155+
}
156+
157+
public async Task<List<IPModel>> GetValidIPWithUDPTest(string[] ips, int maxPing)
158+
{
159+
160+
var validIp = new List<IPModel>();
161+
for (int i = 0; i < 20; i++)
162+
{
163+
var t = new Task(() =>
164+
{
165+
var stopwatch = new Stopwatch();
166+
var ipAddresse = IPAddress.Parse(GetRandomIp(ips).Split("/")[0]);
167+
40168
try
41169
{
170+
var client = new UdpClient();
171+
42172
stopwatch.Start();
43-
result = await Client.GetAsync($"http://{ipAddresse}/__down");
173+
174+
175+
client.Connect(ipAddresse, 443);
176+
177+
44178
stopwatch.Stop();
179+
45180
var ping = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
46181

47182
lock (validIp)
48183
{
49184
validIp.Add(new IPModel
50185
{
51-
IP = ipAddresse,
186+
IP = ipAddresse.ToString(),
52187
Ping = ping,
53188
});
54189
}
@@ -60,7 +195,7 @@ public async Task<List<IPModel>> GetIpValid(string[]ips,int maxPing)
60195
});
61196
t.Start();
62197
}
63-
for (int i = 0; i < maxPing / 100; i++)
198+
for (int i = 0; i < maxPing * 2 / 100; i++)
64199
{
65200
await Task.Delay(100);
66201
if (validIp.Count >= 10)
@@ -71,6 +206,10 @@ public async Task<List<IPModel>> GetIpValid(string[]ips,int maxPing)
71206
return validIp;
72207
}
73208

209+
210+
211+
212+
74213
public async Task<string[]> GetIps()
75214
{
76215
var IpAddresses = await _db.GetAllIPs();
@@ -92,5 +231,11 @@ public async Task addValidIpToDb(List<IPModel> ips)
92231
await _db.AddIP(ip);
93232
}
94233
}
234+
235+
236+
237+
238+
239+
95240
}
96241
}

fast cf ip scanner/ViewModels/ScanPageViewModel.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class ScanPageViewModel : BaseViewModel
1616

1717
[ObservableProperty]
1818
bool isBusy;
19-
19+
2020
readonly IPService _iPServices;
2121
readonly WorkerService _workerServices;
2222
public ScanPageViewModel(IPService iPServices,WorkerService workerService)
@@ -29,12 +29,16 @@ public ScanPageViewModel(IPService iPServices,WorkerService workerService)
2929
this.Title = "scan";
3030

3131
}
32-
32+
3333

3434
[RelayCommand]
3535
async void GetValidIPs()
3636
{
37-
37+
var protocol = await App.Current.MainPage.DisplayActionSheet("which protocol", "Cancel", null,new string[]{"Http test","TCP test"});
38+
if(protocol == "Cancel" || protocol ==null)
39+
{
40+
return;
41+
}
3842
StartBtnEnable = false;
3943

4044
IsBusy = true;
@@ -53,7 +57,7 @@ async void GetValidIPs()
5357
{
5458
while (validIp.Count == 0)
5559
{
56-
validIp.AddRange(await _iPServices.GetIpValid(ips, maxping));
60+
validIp.AddRange(await _iPServices.GetIpValid(ips, maxping,protocol));
5761
}
5862

5963
validIp.Sort((x, y) => x.Ping.CompareTo(y.Ping));
@@ -112,7 +116,7 @@ async Task CopySelectedIPAsync(IPModel ipModel)
112116
{
113117
workersForShow[i] = workers[i-1].Url;
114118
}
115-
var reslut = await App.Current.MainPage.DisplayActionSheet($"What to Do With {ipModel.IP}", null, null, workersForShow); ;
119+
var reslut = await App.Current.MainPage.DisplayActionSheet($"What to Do With {ipModel.IP}", null, null, workersForShow);
116120

117121
if (reslut != null)
118122
{

0 commit comments

Comments
 (0)