Compare commits
No commits in common. "90d1d6b4291ae70b5a5156043e57acc467ea114a" and "eedca27a11125d267307b0024c709a009f552e39" have entirely different histories.
90d1d6b429
...
eedca27a11
@ -55,29 +55,26 @@ public class ContentFilter
|
||||
{
|
||||
if (_stop) break;
|
||||
|
||||
Unfiltered unfiltered = _dbHandler.ReadUnfilteredWithId(i);
|
||||
Unfiltered? unfiltered = _dbHandler.ReadUnfilteredWithId(i);
|
||||
|
||||
if (unfiltered.Filtered) continue;
|
||||
if (unfiltered is null || unfiltered.Filtered == 1) continue;
|
||||
|
||||
Ip ip = unfiltered.Ip;
|
||||
|
||||
unfiltered.Filtered = true;
|
||||
unfiltered.Filtered = 1;
|
||||
|
||||
QueueItem superUnfilteredObject = new()
|
||||
{
|
||||
Unfiltered = unfiltered,
|
||||
Operations = Operations.Update,
|
||||
DbType = DbType.Unfiltered
|
||||
Operations = Operations.Update
|
||||
};
|
||||
|
||||
_queue.Enqueue(superUnfilteredObject);
|
||||
|
||||
if (_dbHandler.FilteredIpExists(unfiltered.Ip))
|
||||
if (_dbHandler.GetFilteredIp(unfiltered.Ip))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Filtered filtered = GetSiteData(ip);
|
||||
Filtered filtered = GetSiteData(unfiltered.Ip);
|
||||
|
||||
filtered.Port1 = unfiltered.Port1;
|
||||
filtered.Port2 = unfiltered.Port2;
|
||||
@ -85,8 +82,7 @@ public class ContentFilter
|
||||
QueueItem superFilteredObject = new()
|
||||
{
|
||||
Filtered = filtered,
|
||||
Operations = Operations.Insert,
|
||||
DbType = DbType.Filtered
|
||||
Operations = Operations.Insert
|
||||
};
|
||||
|
||||
_queue.Enqueue(superFilteredObject);
|
||||
@ -98,7 +94,7 @@ public class ContentFilter
|
||||
((EventWaitHandle) obj).Set();
|
||||
}
|
||||
|
||||
private Filtered GetSiteData(Ip ip)
|
||||
private Filtered GetSiteData(string ip)
|
||||
{
|
||||
StartProcess(ip, 80);
|
||||
StartProcess(ip, 443);
|
||||
@ -226,15 +222,15 @@ public class ContentFilter
|
||||
return siteData;
|
||||
}
|
||||
|
||||
private void StartProcess(Ip ip, int port)
|
||||
private void StartProcess(string ip, int port)
|
||||
{
|
||||
string fileName = port == 80 ? _getDomainPort80 : _getDomainPort443;
|
||||
//Console.WriteLine($"{ip.Ip1}.{ip.Ip2}.{ip.Ip3}.{ip.Ip4}");
|
||||
|
||||
Process proc = new();
|
||||
proc.StartInfo = new()
|
||||
{
|
||||
FileName = "/bin/bash",
|
||||
Arguments = $"{fileName} {ip.Ip1}.{ip.Ip2}.{ip.Ip3}.{ip.Ip4} {_basePath}/Backend/Scripts/{port}Header.txt",
|
||||
Arguments = $"{fileName} {ip} {_basePath}/Backend/Scripts/{port}Header.txt",
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = false,
|
||||
RedirectStandardError = false,
|
||||
|
@ -128,13 +128,7 @@ public class IpScanner
|
||||
break;
|
||||
}
|
||||
|
||||
Ip ip = new()
|
||||
{
|
||||
Ip1 = i,
|
||||
Ip2 = j,
|
||||
Ip3 = k,
|
||||
Ip4 = l
|
||||
};
|
||||
string ip = $"{i}.{j}.{k}.{l}";
|
||||
|
||||
IPStatus responseCode = IPStatus.Unknown;
|
||||
|
||||
@ -142,7 +136,7 @@ public class IpScanner
|
||||
{
|
||||
// Sometimes, if the pinger gets a Destination Unreachable Communication administratively prohibited response, the pinger will throw an exception.
|
||||
// https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol?useskin=vector#Control_messages
|
||||
_ = IPAddress.TryParse(ip.ToString(), out IPAddress? address);
|
||||
_ = IPAddress.TryParse(ip, out IPAddress? address);
|
||||
if (address is not null)
|
||||
{
|
||||
responseCode = ping.Send(address, _timeout, buf, null).Status;
|
||||
@ -155,19 +149,19 @@ public class IpScanner
|
||||
|
||||
if (responseCode != IPStatus.Success)
|
||||
{
|
||||
_discardedQueue.Enqueue(CreateDiscardedQueueItem(ip.ToString(), (int)responseCode));
|
||||
_discardedQueue.Enqueue(CreateDiscardedQueueItem(ip, (int)responseCode));
|
||||
continue;
|
||||
}
|
||||
|
||||
(int, int) ports = TcpClientHelper.CheckPort(ip.ToString(), 80, 443);
|
||||
(int, int) ports = TcpClientHelper.CheckPort(ip, 80, 443);
|
||||
|
||||
if (ports is { Item1: 0, Item2: 0 })
|
||||
{
|
||||
_discardedQueue.Enqueue(CreateDiscardedQueueItem(ip.ToString(), (int)responseCode));
|
||||
_discardedQueue.Enqueue(CreateDiscardedQueueItem(ip, (int)responseCode));
|
||||
continue;
|
||||
}
|
||||
|
||||
_queue.Enqueue(CreateUnfilteredQueueItem(ip, ports));
|
||||
_queue.Enqueue(CreateUnfilteredQueueItem(ip, (int)responseCode, ports));
|
||||
}
|
||||
|
||||
if (_stop)
|
||||
@ -214,21 +208,21 @@ public class IpScanner
|
||||
return discarded;
|
||||
}
|
||||
|
||||
private static QueueItem CreateUnfilteredQueueItem(Ip ip, (int, int) ports)
|
||||
private static QueueItem CreateUnfilteredQueueItem(string ip, int responseCode, (int, int) ports)
|
||||
{
|
||||
Unfiltered unfiltered = new()
|
||||
{
|
||||
Ip = ip,
|
||||
ResponseCode = responseCode,
|
||||
Port1 = ports.Item1,
|
||||
Port2 = ports.Item2,
|
||||
Filtered = false
|
||||
Filtered = 0
|
||||
};
|
||||
|
||||
QueueItem superUnfilteredObject = new()
|
||||
{
|
||||
Unfiltered = unfiltered,
|
||||
Operations = Operations.Insert,
|
||||
DbType = DbType.Unfiltered
|
||||
Operations = Operations.Insert
|
||||
};
|
||||
|
||||
return superUnfilteredObject;
|
||||
|
@ -49,9 +49,9 @@ public class ThreadHandler
|
||||
|
||||
private void StartScanner()
|
||||
{
|
||||
Thread.Sleep(5000); // Let the database handler instantiate and warm up first.
|
||||
Thread.Sleep(10000); // Let the database handler instantiate and warm up first.
|
||||
|
||||
WaitHandle[] wait = _ipScanner.Start(64);
|
||||
WaitHandle[] wait = _ipScanner.Start(4);
|
||||
|
||||
WaitHandle.WaitAll(wait);
|
||||
|
||||
|
@ -1,13 +1,19 @@
|
||||
DROP TABLE Unfiltered;
|
||||
DROP TABLE Runtimes;
|
||||
|
||||
CREATE TABLE "Unfiltered" (
|
||||
"Id" INTEGER NOT NULL,
|
||||
"Ip1" INTEGER NOT NULL,
|
||||
"Ip2" INTEGER NOT NULL,
|
||||
"Ip3" INTEGER NOT NULL,
|
||||
"Ip4" INTEGER NOT NULL,
|
||||
"Ip" TEXT NOT NULL,
|
||||
"ResponseCode" INTEGER NOT NULL,
|
||||
"Port1" INTEGER NOT NULL,
|
||||
"Port2" INTEGER NOT NULL,
|
||||
"Filtered" INTEGER NOT NULL,
|
||||
CONSTRAINT "PK_Unfiltered" PRIMARY KEY("Id" AUTOINCREMENT)
|
||||
)
|
||||
);
|
||||
|
||||
CREATE TABLE "Runtimes" (
|
||||
"Id" INTEGER NOT NULL CONSTRAINT "PK_Runtimes" PRIMARY KEY AUTOINCREMENT,
|
||||
"StartTime" TEXT NOT NULL,
|
||||
"EndTime" TEXT NOT NULL,
|
||||
"ThreadNumber" INTEGER NOT NULL
|
||||
);
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,4 +1,3 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Models.Model.Backend;
|
||||
@ -17,8 +16,8 @@ public class DbHandler
|
||||
private readonly string _resumeConnectionString;
|
||||
private readonly List<string> _discardedConnectionStrings = [];
|
||||
|
||||
private const string InsertStatement = "PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; PRAGMA journal_mode = MEMORY; PRAGMA foreign_keys = off; INSERT INTO Unfiltered (Ip1, Ip2, Ip3, Ip4, Port1, Port2, Filtered) VALUES (@ip1, @ip2, @ip3, @ip4, @port1, @port2, @filtered)";
|
||||
private const string InsertIntoFiltered = "PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; PRAGMA journal_mode = MEMORY; PRAGMA foreign_keys = off; INSERT INTO Filtered (Ip1, Ip2, Ip3, Ip4, Port1, Port2, Title1, Title2, Description1, Description2, Url1, Url2, ServerType1, ServerType2, RobotsTXT1, RobotsTXT2, HttpVersion1, HttpVersion2, CertificateIssuerCountry, CertificateOrganizationName, IpV6, TlsVersion, CipherSuite, KeyExchangeAlgorithm, PublicKeyType1, PublicKeyType2, PublicKeyType3, AcceptEncoding1, AcceptEncoding2, ALPN, Connection1, Connection2) VALUES (@ip1, @ip2, @ip3, @ip4, @port1, @port2, @title1, @title2, @description1, @description2, @url1, @url2, @serverType1, @serverType2, @robotsTXT1, @robotsTXT2, @httpVersion1, @httpVersion2, @certificateIssuerCountry, @certificateOrganizationName, @ipV6, @tlsVersion, @cipherSuite, @keyExchangeAlgorithm, @publicKeyType1, @publicKeyType2, @publicKeyType3, @acceptEncoding1, @acceptEncoding2, @aLPN, @connection1, @connection2)";
|
||||
private const string InsertStatement = "PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; PRAGMA journal_mode = MEMORY; PRAGMA foreign_keys = off; INSERT INTO Unfiltered (Ip, ResponseCode, Port1, Port2, Filtered) VALUES (@ip, @responseCode, @port1, @port2, @filtered)";
|
||||
private const string InsertIntoFiltered = "PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; PRAGMA journal_mode = MEMORY; PRAGMA foreign_keys = off; INSERT INTO Filtered (Ip, Port1, Port2, Title1, Title2, Description1, Description2, Url1, Url2, ServerType1, ServerType2, RobotsTXT1, RobotsTXT2, HttpVersion1, HttpVersion2, CertificateIssuerCountry, CertificateOrganizationName, IpV6, TlsVersion, CipherSuite, KeyExchangeAlgorithm, PublicKeyType1, PublicKeyType2, PublicKeyType3, AcceptEncoding1, AcceptEncoding2, ALPN, Connection1, Connection2) VALUES (@ip, @port1, @port2, @title1, @title2, @description1, @description2, @url1, @url2, @serverType1, @serverType2, @robotsTXT1, @robotsTXT2, @httpVersion1, @httpVersion2, @certificateIssuerCountry, @certificateOrganizationName, @ipV6, @tlsVersion, @cipherSuite, @keyExchangeAlgorithm, @publicKeyType1, @publicKeyType2, @publicKeyType3, @acceptEncoding1, @acceptEncoding2, @aLPN, @connection1, @connection2)";
|
||||
private const string InsertIntoDiscarded = "PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; PRAGMA journal_mode = MEMORY; PRAGMA foreign_keys = off; INSERT INTO Discarded (Ip, ResponseCode) VALUES (@ip, @responseCode)";
|
||||
private const string InsertIntoResume = "PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; PRAGMA journal_mode = MEMORY; PRAGMA foreign_keys = off; INSERT INTO Resume (ThreadNumber, StartRange, EndRange, FirstByte, SecondByte, ThirdByte, FourthByte) VALUES (@threadNumber, @startRange, @endRange, @firstByte, @secondByte, @thirdByte, @fourthByte);";
|
||||
|
||||
@ -26,7 +25,7 @@ public class DbHandler
|
||||
private const string ReadUnfilteredIdsStatement = "SELECT Id FROM Unfiltered WHERE Id != 0 ORDER BY Id DESC LIMIT 1;";
|
||||
private const string ReadFilteredStatement = "SELECT Title2, Url2 FROM Filtered WHERE (Url2 NOT NULL AND Url2 != '') AND (Title2 NOT NULL AND Title2 != '') ORDER BY Url2 DESC;";
|
||||
private const string ReadFilteredIdsStatement = "SELECT Id FROM Filtered WHERE Id != 0 ORDER BY Id DESC LIMIT 1;";
|
||||
private const string ReadFilteredIpStatement = "SELECT Ip1, Ip2, Ip3, Ip4 FROM Filtered WHERE Ip1 == @ip1 AND Ip2 == @ip1 AND Ip3 == @ip1 AND Ip4 == @ip1 ORDER BY Ip1 DESC LIMIT 1;";
|
||||
private const string ReadFilteredIpStatement = "SELECT Ip FROM Filtered WHERE Ip == @ip ORDER BY Ip DESC LIMIT 1;";
|
||||
private const string ReadDiscardedSeqIdsStatement = "SELECT seq FROM sqlite_sequence;";
|
||||
private const string ReadAndDeleteResumeStatement = "SELECT * FROM Resume WHERE ThreadNumber == @threadNumber; DELETE FROM RESUME WHERE ThreadNumber == @threadNumber;";
|
||||
|
||||
@ -91,24 +90,20 @@ public class DbHandler
|
||||
|
||||
if (queueItem is null) { continue; }
|
||||
|
||||
if (queueItem.Operations == Operations.Insert && queueItem.DbType == DbType.Unfiltered)
|
||||
switch (queueItem.Operations)
|
||||
{
|
||||
case Operations.Insert when queueItem.Unfiltered is not null:
|
||||
InsertUnfiltered(queueItem.Unfiltered);
|
||||
}
|
||||
|
||||
else if (queueItem.Operations == Operations.Insert && queueItem.DbType == DbType.Filtered)
|
||||
{
|
||||
InsertFiltered(queueItem.Filtered!);
|
||||
}
|
||||
|
||||
else if (queueItem.Operations == Operations.Insert && queueItem.ResumeObject is not null)
|
||||
{
|
||||
break;
|
||||
case Operations.Insert when queueItem.Filtered is not null:
|
||||
InsertFiltered(queueItem.Filtered);
|
||||
break;
|
||||
case Operations.Insert when queueItem.ResumeObject is not null:
|
||||
InsertResumeObject(queueItem.ResumeObject);
|
||||
}
|
||||
|
||||
else if (queueItem.Operations == Operations.Update && queueItem.DbType == DbType.Unfiltered)
|
||||
{
|
||||
break;
|
||||
case Operations.Update when queueItem.Unfiltered is not null:
|
||||
UpdateUnfiltered(queueItem.Unfiltered);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -175,10 +170,8 @@ public class DbHandler
|
||||
|
||||
using SqliteCommand command = new(InsertStatement, connection);
|
||||
|
||||
command.Parameters.AddWithValue("@ip1", unfiltered.Ip.Ip1);
|
||||
command.Parameters.AddWithValue("@ip2", unfiltered.Ip.Ip2);
|
||||
command.Parameters.AddWithValue("@ip3", unfiltered.Ip.Ip3);
|
||||
command.Parameters.AddWithValue("@ip4", unfiltered.Ip.Ip4);
|
||||
command.Parameters.AddWithValue("@ip", unfiltered.Ip);
|
||||
command.Parameters.AddWithValue("@responseCode", unfiltered.ResponseCode);
|
||||
command.Parameters.AddWithValue("@port1", unfiltered.Port1);
|
||||
command.Parameters.AddWithValue("@port2", unfiltered.Port2);
|
||||
command.Parameters.AddWithValue("@filtered", unfiltered.Filtered);
|
||||
@ -208,10 +201,7 @@ public class DbHandler
|
||||
|
||||
using SqliteCommand command = new(InsertIntoFiltered, connection);
|
||||
|
||||
command.Parameters.AddWithValue("@ip1", filtered.Ip.Ip1);
|
||||
command.Parameters.AddWithValue("@ip2", filtered.Ip.Ip2);
|
||||
command.Parameters.AddWithValue("@ip3", filtered.Ip.Ip3);
|
||||
command.Parameters.AddWithValue("@ip4", filtered.Ip.Ip4);
|
||||
command.Parameters.AddWithValue("@ip", filtered.Ip);
|
||||
command.Parameters.AddWithValue("@port1", filtered.Port1);
|
||||
command.Parameters.AddWithValue("@port2", filtered.Port2);
|
||||
command.Parameters.AddWithValue("@url1", filtered.Url1);
|
||||
@ -277,7 +267,7 @@ public class DbHandler
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
public Unfiltered ReadUnfilteredWithId(long id)
|
||||
public Unfiltered? ReadUnfilteredWithId(long id)
|
||||
{
|
||||
using SqliteConnection connection = new(_unfilteredConnectionString);
|
||||
connection.Open();
|
||||
@ -286,28 +276,19 @@ public class DbHandler
|
||||
command.Parameters.AddWithValue("@id", id);
|
||||
|
||||
using SqliteDataReader reader = command.ExecuteReader();
|
||||
if (!reader.HasRows) return new();
|
||||
if (!reader.HasRows) return null;
|
||||
|
||||
Unfiltered unfiltered = new();
|
||||
Ip ip = new();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
unfiltered.Id = reader.GetInt32(0);
|
||||
ip.Ip1 = reader.GetInt32(1);
|
||||
ip.Ip2 = reader.GetInt32(2);
|
||||
ip.Ip3 = reader.GetInt32(3);
|
||||
ip.Ip4 = reader.GetInt32(4);
|
||||
//Console.WriteLine(ip + "lmo");
|
||||
unfiltered.Port1 = reader.GetInt32(5);
|
||||
unfiltered.Port2 = reader.GetInt32(6);
|
||||
unfiltered.Filtered = reader.GetBoolean(7);
|
||||
unfiltered.Ip = reader.GetString(1);
|
||||
unfiltered.Port1 = reader.GetInt32(3);
|
||||
unfiltered.Port2 = reader.GetInt32(4);
|
||||
unfiltered.Filtered = reader.GetInt32(5);
|
||||
}
|
||||
|
||||
//Console.WriteLine(ip + "aaaaa");
|
||||
unfiltered.Ip = ip;
|
||||
//Console.WriteLine(unfiltered.Ip + "adfgdgfdgfsdfgs");
|
||||
|
||||
return unfiltered;
|
||||
}
|
||||
|
||||
@ -383,16 +364,13 @@ public class DbHandler
|
||||
return rowId;
|
||||
}
|
||||
|
||||
public bool FilteredIpExists(Ip ip)
|
||||
public bool GetFilteredIp(string ip)
|
||||
{
|
||||
using SqliteConnection connection = new(_filteredConnectionString);
|
||||
connection.Open();
|
||||
|
||||
using SqliteCommand command = new(ReadFilteredIpStatement, connection);
|
||||
command.Parameters.AddWithValue("@ip1", ip.Ip1);
|
||||
command.Parameters.AddWithValue("@ip2", ip.Ip2);
|
||||
command.Parameters.AddWithValue("@ip3", ip.Ip3);
|
||||
command.Parameters.AddWithValue("@ip4", ip.Ip4);
|
||||
command.Parameters.AddWithValue("@ip", ip);
|
||||
|
||||
using SqliteDataReader reader = command.ExecuteReader();
|
||||
|
||||
@ -401,37 +379,14 @@ public class DbHandler
|
||||
return false;
|
||||
}
|
||||
|
||||
Ip tempIp = new();
|
||||
string ipAddress = "";
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
tempIp.Ip1 = reader.GetInt32(0);
|
||||
tempIp.Ip2 = reader.GetInt32(1);
|
||||
tempIp.Ip3 = reader.GetInt32(2);
|
||||
tempIp.Ip4 = reader.GetInt32(3);
|
||||
ipAddress = reader.GetString(0);
|
||||
}
|
||||
|
||||
if (tempIp.Ip1 != ip.Ip1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tempIp.Ip2 != ip.Ip2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tempIp.Ip3 != ip.Ip3)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tempIp.Ip4 != ip.Ip4)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return ipAddress == ip;
|
||||
}
|
||||
|
||||
public List<SearchResult?> GetSearchResults()
|
||||
|
@ -1,7 +0,0 @@
|
||||
namespace Models.Model.Backend;
|
||||
|
||||
public enum DbType
|
||||
{
|
||||
Unfiltered,
|
||||
Filtered,
|
||||
}
|
@ -2,7 +2,7 @@ namespace Models.Model.Backend;
|
||||
|
||||
public class Filtered
|
||||
{
|
||||
public Ip Ip { get; set; }
|
||||
public string Ip { get; set; } = "";
|
||||
public string Title1 { get; set; } = "";
|
||||
public string Title2 { get; set; } = "";
|
||||
public string Description1 { get; set; } = "";
|
||||
|
@ -1,17 +0,0 @@
|
||||
namespace Models.Model.Backend;
|
||||
|
||||
public struct Ip
|
||||
{
|
||||
public int Ip1 { get; set; }
|
||||
|
||||
public int Ip2 { get; set; }
|
||||
|
||||
public int Ip3 { get; set; }
|
||||
|
||||
public int Ip4 { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Ip1}.{Ip2}.{Ip3}.{Ip4}";
|
||||
}
|
||||
}
|
@ -2,9 +2,8 @@ namespace Models.Model.Backend;
|
||||
|
||||
public class QueueItem
|
||||
{
|
||||
public Unfiltered Unfiltered { get; init; }
|
||||
public Unfiltered? Unfiltered { get; init; }
|
||||
public Filtered? Filtered { get; init; }
|
||||
public ScannerResumeObject? ResumeObject { get; init; }
|
||||
public Operations Operations { get; init; }
|
||||
public DbType DbType { get; init; }
|
||||
}
|
@ -1,14 +1,16 @@
|
||||
namespace Models.Model.Backend;
|
||||
|
||||
public struct Unfiltered
|
||||
public class Unfiltered
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public Ip Ip { get; set; }
|
||||
public string Ip { get; set; } = "";
|
||||
|
||||
public int ResponseCode { get; init; }
|
||||
|
||||
public int Port1 { get; set; }
|
||||
|
||||
public int Port2 { get; set; }
|
||||
|
||||
public bool Filtered { get; set; }
|
||||
public int Filtered { get; set; }
|
||||
}
|
BIN
Models/mydb.db
BIN
Models/mydb.db
Binary file not shown.
Loading…
Reference in New Issue
Block a user