From 3877b90c9bca58f1f2b2c882c5a000b035fe8a8a Mon Sep 17 00:00:00 2001 From: Andreas Venizelou Date: Sat, 10 Aug 2024 14:36:24 +0300 Subject: [PATCH] DNS-SD, display remote ip --- Protest/Front/dns-sd.js | 7 +++++-- Protest/Front/tools.css | 11 ++++++++++ Protest/Protocols/Mdns.cs | 43 +++++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/Protest/Front/dns-sd.js b/Protest/Front/dns-sd.js index 3857ecd0..f3087bca 100644 --- a/Protest/Front/dns-sd.js +++ b/Protest/Front/dns-sd.js @@ -17,7 +17,7 @@ class DnsSD extends Console { this.args = args ?? { entries : [], - type : "A", + type : "ANY", timeout : 2000 }; @@ -38,7 +38,7 @@ class DnsSD extends Console { this.toolbar.appendChild(this.AddToolbarSeparator()); this.AddSendToChatButton(); - this.inputBox.placeholder = "query"; + this.inputBox.placeholder = "_http._tcp.local"; if (this.args.entries) { //restore entries from previous session let temp = this.args.entries; @@ -382,6 +382,8 @@ class DnsSD extends Console { for (let i = 0; i < json.answer.length; i++) { const box = document.createElement("div"); + box.setAttribute("after-label", json.answer[i].remote); + box.className = "tool-after-label-far"; result.appendChild(box); const label = document.createElement("div"); @@ -400,6 +402,7 @@ class DnsSD extends Console { const string = document.createElement("div"); string.style.display = "inline-block"; + string.style.paddingRight = "8px"; string.textContent = json.answer[i].name; box.append(label, string); diff --git a/Protest/Front/tools.css b/Protest/Front/tools.css index 20205be9..4993bb97 100644 --- a/Protest/Front/tools.css +++ b/Protest/Front/tools.css @@ -127,6 +127,17 @@ text-overflow: ellipsis; } +.tool-element > .expanded .tool-after-label-far[after-label]::after { + position: sticky; + left: max(60%, 300px); + right: 64px; + content: attr(after-label); + color: #c0c0c0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + .tool-element > .expanded { overflow-y:auto; } .tool-element > .expanded > div { width:33%; } .tool-element > .collapsed > div { width:auto; } diff --git a/Protest/Protocols/Mdns.cs b/Protest/Protocols/Mdns.cs index 74d58392..bbabbd63 100644 --- a/Protest/Protocols/Mdns.cs +++ b/Protest/Protocols/Mdns.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.AspNetCore.Identity.UI.Services; +using System; using System.Collections.Generic; using System.IO; using System.Net; @@ -23,6 +24,7 @@ private struct Answer { public byte[] data; public string questionString; public string answerString; + public IPAddress remote; public bool isAuthoritative; public bool isAdditional; public byte error; @@ -63,6 +65,7 @@ public static byte[] Resolve(Dictionary parameters) { public static byte[] Resolve(string queryString, int timeout = 2000, RecordType type = RecordType.A) { byte[] query = ConstructQuery(queryString, type); List receivedData = new List(); + List sender = new List(); IPAddress[] nics = IpTools.GetIpAddresses(); for (int i = 0; i < nics.Length && receivedData.Count == 0; i++) { @@ -101,14 +104,15 @@ public static byte[] Resolve(string queryString, int timeout = 2000, RecordType byte[] reply = new byte[1024]; try { - //EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); - //int length = socket.ReceiveFrom(reply, ref remoteEP); + EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); + int length = socket.ReceiveFrom(reply, ref remoteEP); - int length = socket.Receive(reply); if (length > 0) { byte[] actualReply = new byte[length]; Array.Copy(reply, actualReply, length); + receivedData.Add(actualReply); + sender.Add(((IPEndPoint)remoteEP).Address); } } catch { } @@ -123,26 +127,29 @@ public static byte[] Resolve(string queryString, int timeout = 2000, RecordType List matchingData = new List(); List answers = new List(); - /*try*/ { - foreach (byte[] response in receivedData) { + try { + for (int i = 0; i < receivedData.Count; i++) { + byte[] response = receivedData[i]; ushort answerCount, authorityCount, additionalCount; - Answer[] answer = DeconstructResponse(response, type, out answerCount, out authorityCount, out additionalCount); + Answer[] answer = DeconstructResponse(response, type, sender[i], out answerCount, out authorityCount, out additionalCount); bool matched = false; - for (int i = 0; i < answer.Length; i++) { - if (type != RecordType.ANY && answer[i].type != type) { continue; } - if (!answer[i].questionString.Equals(queryString, StringComparison.OrdinalIgnoreCase)) { continue; } - answers.Add(answer[i]); + for (int j = 0; j < answer.Length; j++) { + if (type != RecordType.ANY && answer[j].type != type) { continue; } + if (!answer[j].questionString.Equals(queryString, StringComparison.OrdinalIgnoreCase)) { continue; } + + answers.Add(answer[j]); if (!matched) { matched = true; matchingData.Add(response); } } } + } - /*catch { + catch { return "{\"error\":\"unknown error\",\"errorcode\":\"0\"}"u8.ToArray(); - }*/ + } return Serialize(query, matchingData, answers); } @@ -203,7 +210,7 @@ private static byte[] ConstructQuery(string queryString, RecordType type) { return query; } - private static Answer[] DeconstructResponse(byte[] response, RecordType queryType, out ushort answerCount, out ushort authorityCount, out ushort additionalCount) { + private static Answer[] DeconstructResponse(byte[] response, RecordType queryType, IPAddress remoteEndPoint, out ushort answerCount, out ushort authorityCount, out ushort additionalCount) { if (response.Length < 12) { answerCount = 0; authorityCount = 0; @@ -242,6 +249,7 @@ private static Answer[] DeconstructResponse(byte[] response, RecordType queryTyp } Answer ans = new Answer(); + ans.remote = remoteEndPoint; int nameStartIndex = index; @@ -394,12 +402,12 @@ private static byte[] Serialize(byte[] query, List data, List an for (int i = 0; i < data.Count; i++) { if (!first) builder.Append(','); - builder.Append("["); + builder.Append('['); for (int j = 0; j < data[i].Length; j++) { if (j > 0) builder.Append(','); builder.Append(data[i][j]); } - builder.Append("]"); + builder.Append(']'); first = false; } builder.Append("],"); @@ -473,7 +481,8 @@ private static byte[] Serialize(byte[] query, List data, List an break; } - builder.Append($"\"ttl\":\"{answers[i].ttl}\""); + builder.Append($"\"ttl\":\"{answers[i].ttl}\","); + builder.Append($"\"remote\":\"{answers[i].remote.ToString()}\""); builder.Append('}');