Skip to content

Commit 2f38a34

Browse files
committed
.
1 parent 797d021 commit 2f38a34

File tree

7 files changed

+109
-59
lines changed

7 files changed

+109
-59
lines changed

fast cf ip scanner/Constants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public static class Constants
1313
SQLite.SQLiteOpenFlags.Create |
1414
// enable multi-threaded database access
1515
SQLite.SQLiteOpenFlags.SharedCache;
16+
1617
public static string DatabasePath =>
17-
Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);
18+
Path.Combine("C:\\Users\\azata\\Desktop\\db test", DatabaseFilename);
1819

1920
public static List<string> HttpPorts
2021
{

fast cf ip scanner/Data/FastCFIPScannerDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async Task Init()
1111
{
1212
if (DataBase is not null)
1313
return;
14-
14+
1515
DataBase = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
1616
await DataBase.CreateTableAsync<IPModel>();
1717
await DataBase.CreateTableAsync<WorkerModel>();

fast cf ip scanner/Model/IPModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ namespace fast_cf_ip_scanner.Model
1111
{
1212
public class IPModel
1313
{
14-
[PrimaryKey,AutoIncrement]
14+
[PrimaryKey, AutoIncrement]
1515
public int Id { get; set; }
16-
16+
1717
public string IP { get; set; }
18-
18+
1919
public int Ping { get; set; }
20+
21+
public string Ports { get; set; }
2022
}
2123
}

fast cf ip scanner/Resources/Styles/Colors.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@
4141
<Color x:Key="Blue200Accent">#72ACF1</Color>
4242
<Color x:Key="Blue300Accent">#A7CBF6</Color>
4343

44+
45+
<Color x:Key="PrimaryTextColorDarkMode">#fff</Color>
46+
<Color x:Key="SecondaryTextColorDarkMode">#ddd</Color>
47+
<Color x:Key="DarkGrayBackgroundColor">#333333</Color>
48+
<Color x:Key="DarkCardBackgroundColor">#444444</Color>
49+
<Color x:Key="BorderColorDarkMode">#555555</Color>
4450
</ResourceDictionary>

fast cf ip scanner/Services/IPService.cs

Lines changed: 90 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -49,61 +49,86 @@ public async Task<List<IPModel>> GetValidٌIPWithHttpTest(string[] ips, int maxP
4949

5050
var validIp = new List<IPModel>();
5151

52-
53-
var randomIps = GetRandomIp(ips);
54-
foreach (var ipAddresse in randomIps)
52+
async Task HttpTest(string ipAddresse)
5553
{
56-
var t = new Task(async () =>
57-
{
58-
var stopwatch = new Stopwatch();
54+
var stopwatch = new Stopwatch();
5955

60-
try
61-
{
62-
var SocketsHandler = new SocketsHttpHandler();
63-
var Client = new HttpClient(SocketsHandler)
64-
{
65-
Timeout = TimeSpan.FromSeconds(maxPing),
66-
};
67-
int totalPing = 0;
68-
var randomPorts = GetRandomPort();
69-
foreach (var port in randomPorts)
70-
{
71-
stopwatch.Start();
56+
var SocketsHandler = new SocketsHttpHandler();
57+
var Client = new HttpClient(SocketsHandler)
58+
{
59+
Timeout = TimeSpan.FromSeconds(maxPing),
60+
};
7261

62+
var randomPorts = GetRandomPort();
63+
int totalPing = 0;
64+
var ports = new List<string>();
65+
var ipIsConnected = false;
7366

74-
var result = await Client.GetAsync($"http://{ipAddresse}/__down:{port}");
67+
foreach (var port in randomPorts)
68+
{
7569

70+
try
71+
{
72+
stopwatch.Start();
73+
var result = await Client.GetAsync($"http://{ipAddresse}/__down:{port}");
74+
stopwatch.Stop();
7675

77-
stopwatch.Stop();
78-
var currentPing = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
79-
totalPing += currentPing;
80-
}
81-
int ping = totalPing / 3;
82-
lock (validIp)
76+
if (result != null)
8377
{
84-
validIp.Add(new IPModel
85-
{
86-
IP = ipAddresse.ToString(),
87-
Ping = ping,
88-
});
78+
ipIsConnected = true;
79+
ports.Add(port);
8980
}
9081
}
9182
catch
9283
{
93-
stopwatch.Reset();
84+
ipIsConnected = false;
85+
stopwatch.Stop();
9486
}
95-
});
96-
t.Start();
87+
88+
89+
var currentPing = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
90+
totalPing += currentPing;
91+
}
92+
int ping = totalPing / 3;
93+
if (ipIsConnected)
94+
{
95+
lock (validIp)
96+
{
97+
validIp.Add(new IPModel
98+
{
99+
IP = ipAddresse.ToString(),
100+
Ping = ping,
101+
Ports = string.Join(",", ports)
102+
});
103+
}
104+
}
105+
97106
}
98-
for (int i = 0; i < maxPing * 2 / 100; i++)
107+
#region repeat test
108+
while (true)
99109
{
100-
await Task.Delay(100);
101110
if (validIp.Count >= 5)
102111
{
103112
return validIp;
104113
}
114+
var newRandomIps = GetRandomIp(ips);
115+
116+
List<Task> tasks = new List<Task>();
117+
118+
foreach (var ip in newRandomIps)
119+
{
120+
tasks.Add(HttpTest(ip));
121+
}
122+
for (int i = 0; i < maxPing / 100; i++)
123+
{
124+
await Task.Delay(100);
125+
if (validIp.Count >= 5)
126+
{
127+
return validIp;
128+
}
129+
}
105130
}
106-
return validIp;
131+
#endregion
107132
}
108133

109134
public async Task<List<IPModel>> GetValidIPWithTCPTest(string[] ips, int maxPing)
@@ -113,27 +138,39 @@ public async Task<List<IPModel>> GetValidIPWithTCPTest(string[] ips, int maxPing
113138

114139
async Task TestConnectionAsync(string ip)
115140
{
116-
TcpClient tcpClient = new TcpClient();
117-
var randomPort = GetRandomPort(1);
118-
try
141+
var randomPort = GetRandomPort();
142+
var ports = new List<string>();
143+
var ipIsConnected = false;
144+
foreach (var port in randomPort)
119145
{
120-
tcpClient.ConnectAsync(ip, int.Parse(randomPort.First()));
121-
await Task.Delay(maxPing);
122-
if (tcpClient.Connected)
146+
try
123147
{
124-
lock (validIp)
148+
TcpClient tcpClient = new TcpClient();
149+
tcpClient.ConnectAsync(ip, int.Parse(port));
150+
await Task.Delay(maxPing);
151+
if (tcpClient.Connected)
125152
{
126-
validIp.Add(new IPModel
127-
{
128-
IP = ip,
129-
Ping = 0,
130-
});
153+
ipIsConnected = true;
154+
ports.Add(port);
131155
}
132156
}
157+
catch
158+
{
159+
160+
}
133161
}
134-
catch
135-
{
136162

163+
if (ipIsConnected)
164+
{
165+
lock (validIp)
166+
{
167+
validIp.Add(new IPModel
168+
{
169+
IP = ip,
170+
Ping = 0,
171+
Ports = string.Join(",", ports)
172+
});
173+
}
137174
}
138175
}
139176
while (true)
@@ -238,16 +275,16 @@ public List<string> GetRandomIp(string[] ips, int ipCount = 20, int ipRangeCount
238275
}
239276
return randomIps;
240277
}
241-
public List<string> GetRandomPort(int count = 3)
278+
public string[] GetRandomPort(int count = 3)
242279
{
243280
Random random = new Random();
244-
var randomPorts = new List<string>();
281+
string[] randomPorts = new string[count];
245282
var allPorts = Constants.HttpPorts.Concat(Constants.HttpsPorts).ToList();
246283

247284
for (int i = 0; i < count; i++)
248285
{
249286
int randomIndex = random.Next(allPorts.Count);
250-
randomPorts.Add(allPorts[randomIndex]);
287+
randomPorts[i] = allPorts[randomIndex];
251288
}
252289
return randomPorts;
253290
}

fast cf ip scanner/ViewModels/ScanPageViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ScanPageViewModel(IPService iPServices,WorkerService workerService)
2626
_iPServices = iPServices;
2727
_workerServices = workerService;
2828

29-
maxPingOfIP = "5000";
29+
maxPingOfIP = "1000";
3030
}
3131

3232

fast cf ip scanner/Views/ScanPage.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
TextColor="White"></Label>
7575
<Label Text="{Binding Ping}"
7676
TextColor="White"></Label>
77+
<Label Text=" ping : "
78+
TextColor="White"></Label>
79+
<Label Text="{Binding Ports}"
80+
TextColor="White"></Label>
7781
</HorizontalStackLayout>
7882
</DataTemplate>
7983
</CollectionView.ItemTemplate>

0 commit comments

Comments
 (0)