Skip to content

Commit

Permalink
IP discovery, dns name and front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Aug 30, 2024
1 parent f6c850b commit 909dad7
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 46 deletions.
31 changes: 31 additions & 0 deletions Protest/Front/ipdiscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ class IpDiscovery extends List {

this.stopButton.disabled = true;

this.statusLabel = document.createElement("div");
this.statusLabel.className = "issues-status-label";
this.statusLabel.textContent = "";
this.statusLabel.style.position = "absolute";
this.statusLabel.style.right = "8px";
this.statusLabel.style.bottom = "4px";
this.statusLabel.style.color = "var(--clr-pane)";
this.statusLabel.style.fontWeight = "600";
this.content.appendChild(this.statusLabel);

this.startButton.onclick = ()=> this.StartDialog();
this.stopButton.onclick = ()=> this.Stop();

Expand Down Expand Up @@ -81,6 +91,21 @@ class IpDiscovery extends List {
innerBox.parentElement.style.maxWidth = "640px";
innerBox.style.border = "var(--clr-control) solid 1.5px";
innerBox.style.overflowY = "auto";

const spinner = document.createElement("div");
spinner.className = "spinner";
spinner.style.textAlign = "left";
spinner.style.marginTop = "32px";
spinner.style.marginBottom = "16px";
spinner.appendChild(document.createElement("div"));
innerBox.appendChild(spinner);

const status = document.createElement("div");
status.textContent = "Retrieving network interfaces...";
status.style.textAlign = "center";
status.style.fontWeight = "bold";
status.style.animation = "delayed-fade-in 1.5s ease-in 1";
innerBox.appendChild(status);

let selectedNic = null;

Expand All @@ -92,6 +117,8 @@ class IpDiscovery extends List {
const json = await response.json();
if (json.error) throw (json.error);

innerBox.textContent = "";

for (let i=0; i<json.length; i++) {
const newNic = document.createElement("div");
newNic.className = "list-element";
Expand Down Expand Up @@ -166,6 +193,8 @@ class IpDiscovery extends List {
this.ws.send(id);
this.startButton.disabled = true;
this.stopButton.disabled = false;
this.statusLabel.textContent = "Discovering...";
this.statusLabel.classList.add("list-working-spinner");
};

this.ws.onmessage = event=> {
Expand Down Expand Up @@ -244,6 +273,8 @@ class IpDiscovery extends List {
this.ws.onclose = ()=> {
this.startButton.disabled = false;
this.stopButton.disabled = true;
this.statusLabel.textContent = "";
this.statusLabel.classList.remove("list-working-spinner");
this.ws = null;
};

Expand Down
16 changes: 0 additions & 16 deletions Protest/Front/issues.css
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,3 @@
color: var(--clr-pane);
font-weight: 600;
}

.issues-scanning::before {
content: "";
display: inline-block;
margin-right: 8px;
margin-bottom: -4px;
width: 18px;
height: 18px;
border-radius: 50%;
border-style: solid;
border-color: var(--clr-pane) var(--clr-pane) transparent transparent;
border-width: 2px 3px 3px 0px;
box-sizing: border-box;

animation: spin 1s linear infinite;
}
4 changes: 2 additions & 2 deletions Protest/Front/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class Issues extends List {
this.ws.onopen = ()=> {
this.scanButton.disabled = true;
this.statusLabel.textContent = "Scanning...";
this.statusLabel.classList.add("issues-scanning");
this.statusLabel.classList.add("list-working-spinner");
};

this.ws.onmessage = event=> {
Expand All @@ -370,7 +370,7 @@ class Issues extends List {
this.ws.onclose = ()=> {
this.scanButton.disabled = false;
this.statusLabel.textContent = "";
this.statusLabel.classList.remove("issues-scanning");
this.statusLabel.classList.remove("list-working-spinner");
this.ws = null;
};

Expand Down
16 changes: 16 additions & 0 deletions Protest/Front/list.css
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,20 @@
padding: 0px 8px;
border: 1px solid var(--clr-pane);
border-radius: 2px;
}

.list-working-spinner::before {
content: "";
display: inline-block;
margin-right: 8px;
margin-bottom: -4px;
width: 18px;
height: 18px;
border-radius: 50%;
border-style: solid;
border-color: var(--clr-pane) var(--clr-pane) transparent transparent;
border-width: 2px 3px 3px 0px;
box-sizing: border-box;

animation: spin 1s linear infinite;
}
78 changes: 50 additions & 28 deletions Protest/Tools/IpDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ internal struct HostEntry {
internal string mac;
internal string manufacturer;
internal string services;
internal string source;
}

private static readonly JsonSerializerOptions hostSerializerOptions;
Expand Down Expand Up @@ -170,7 +169,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {

if (phase == 1) {
threads = new Thread[] {
new Thread(()=> DiscoverHostname(dic, nic, ws, mutex, tokenSource.Token)),
new Thread(async ()=> await DiscoverHostnameAsync(dic, nic, ws, mutex, tokenSource.Token)),
new Thread(()=> DiscoverServices(dic, nic, ws, mutex, tokenSource.Token)),
};

Expand Down Expand Up @@ -233,7 +232,6 @@ private static void DiscoverAdapter(ConcurrentDictionary<string, HostEntry> dic,
mac = mac,
manufacturer = MacLookup.LookupToString(mac),
services = String.Empty,
source = "nic"
};

dic.AddOrUpdate(ipv4String, host, (key, value) => value);
Expand Down Expand Up @@ -265,7 +263,6 @@ private static void DiscoverAdapter(ConcurrentDictionary<string, HostEntry> dic,
mac = mac,
manufacturer = MacLookup.LookupToString(mac),
services = String.Empty,
source = "nic"
};

dic.AddOrUpdate(ipv4String, gwHost, (key, value) => value);
Expand Down Expand Up @@ -327,7 +324,6 @@ private static void DiscoverIcmp(ConcurrentDictionary<string, HostEntry> dic, Ne
mac = mac,
manufacturer = MacLookup.LookupToString(mac),
services = String.Empty,
source = "icmp"
};

dic.AddOrUpdate(ipString, gwHost, (key, value) => value);
Expand All @@ -341,35 +337,63 @@ private static void DiscoverMdns(ConcurrentDictionary<string, HostEntry> dic, Ne
//MdnsMulticast(dic, nic, ws, mutex, token, HTTP_QUERY, Protocols.Dns.RecordType.ANY, 1000);
}

private static void DiscoverHostname(ConcurrentDictionary<string, HostEntry> dic, NetworkInterface nic, WebSocket ws, object mutex, CancellationToken token) {
private static async Task DiscoverHostnameAsync(ConcurrentDictionary<string, HostEntry> dic, NetworkInterface nic, WebSocket ws, object mutex, CancellationToken token) {
List<Task> tasks = new List<Task>();

foreach (KeyValuePair<string, HostEntry> pair in dic) {
if (token.IsCancellationRequested) {
return;
break;
}

HostEntry host = pair.Value;
string name = NetBios.GetBiosName(pair.Key, 200);
if (name is not null) {
WsWriteText(ws, JsonSerializer.SerializeToUtf8Bytes(new {
ip = pair.Key,
name = name,
}), mutex);
}
else {
try {
IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(host.ip);
if (!string.IsNullOrEmpty(hostEntry.HostName)) {
WsWriteText(ws, JsonSerializer.SerializeToUtf8Bytes(new {
ip = pair.Key,
name = name,
}), mutex);
tasks.Add(Task.Run(async () => {
if (token.IsCancellationRequested) {
return;
}

HostEntry host = pair.Value;
string name = NetBios.GetBiosName(pair.Key, 200);

if (name is not null) {
WsWriteText(ws, JsonSerializer.SerializeToUtf8Bytes(new {
ip = pair.Key,
name = name,
}), mutex);
}
else {
try {
IPHostEntry hostEntry = await System.Net.Dns.GetHostEntryAsync(host.ip);
name = hostEntry.HostName;

if (!string.IsNullOrEmpty(name)) {
Mdns.Answer[] answer = Mdns.ResolveToArray($"{name}.local", 500, RecordType.AAAA, false);
Mdns.Answer[] filtered = answer.Where(o=> o.type == RecordType.AAAA).ToArray();

if (filtered.Length > 0 && !String.IsNullOrEmpty(filtered[0].answerString)) {
WsWriteText(ws, JsonSerializer.SerializeToUtf8Bytes(new {
ip = pair.Key,
ipv6 = filtered[0].answerString,
name = name,
}), mutex);
}
else {
WsWriteText(ws, JsonSerializer.SerializeToUtf8Bytes(new {
ip = pair.Key,
name = name,
}), mutex);
}
}
}
catch { }
}
catch { }
}
}, token));
}

try {
await Task.WhenAll(tasks);
}
catch { }
}

private static void DiscoverServices(ConcurrentDictionary<string, HostEntry> dic, NetworkInterface nic, WebSocket ws, object mutex, CancellationToken token) {
short[] ports = { 22, 23, 53, 80, 443, 445, 3389, 9100 };

Expand Down Expand Up @@ -560,7 +584,6 @@ private static void MdnsResponse(ConcurrentDictionary<string, HostEntry> dic, We
mac = mac,
manufacturer = MacLookup.LookupToString(mac),
services = services.ToString(),
source = "mdns"
};

dic.AddOrUpdate(ipString, host, (key, value) => value);
Expand Down Expand Up @@ -588,7 +611,6 @@ public override void Write(Utf8JsonWriter writer, IpDiscovery.HostEntry value, J
ReadOnlySpan<byte> _services = "services"u8;

writer.WriteStartObject();
writer.WriteString("source", value.source);
writer.WriteString(_name, value.name);
writer.WriteString(_ip, value.ip);
writer.WriteString(_ipv6, value.ipv6);
Expand Down

0 comments on commit 909dad7

Please sign in to comment.