Skip to content

Commit

Permalink
DNS-SD, display remote ip
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Aug 10, 2024
1 parent ac7699e commit 3877b90
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
7 changes: 5 additions & 2 deletions Protest/Front/dns-sd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DnsSD extends Console {

this.args = args ?? {
entries : [],
type : "A",
type : "ANY",
timeout : 2000
};

Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions Protest/Front/tools.css
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
43 changes: 26 additions & 17 deletions Protest/Protocols/Mdns.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.AspNetCore.Identity.UI.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
Expand All @@ -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;
Expand Down Expand Up @@ -63,6 +65,7 @@ public static byte[] Resolve(Dictionary<string, string> parameters) {
public static byte[] Resolve(string queryString, int timeout = 2000, RecordType type = RecordType.A) {
byte[] query = ConstructQuery(queryString, type);
List<byte[]> receivedData = new List<byte[]>();
List<IPAddress> sender = new List<IPAddress>();

IPAddress[] nics = IpTools.GetIpAddresses();
for (int i = 0; i < nics.Length && receivedData.Count == 0; i++) {
Expand Down Expand Up @@ -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 { }
Expand All @@ -123,26 +127,29 @@ public static byte[] Resolve(string queryString, int timeout = 2000, RecordType
List<byte[]> matchingData = new List<byte[]>();
List<Answer> answers = new List<Answer>();

/*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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -242,6 +249,7 @@ private static Answer[] DeconstructResponse(byte[] response, RecordType queryTyp
}

Answer ans = new Answer();
ans.remote = remoteEndPoint;

int nameStartIndex = index;

Expand Down Expand Up @@ -394,12 +402,12 @@ private static byte[] Serialize(byte[] query, List<byte[]> data, List<Answer> 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("],");
Expand Down Expand Up @@ -473,7 +481,8 @@ private static byte[] Serialize(byte[] query, List<byte[]> data, List<Answer> an
break;
}

builder.Append($"\"ttl\":\"{answers[i].ttl}\"");
builder.Append($"\"ttl\":\"{answers[i].ttl}\",");
builder.Append($"\"remote\":\"{answers[i].remote.ToString()}\"");

builder.Append('}');

Expand Down

0 comments on commit 3877b90

Please sign in to comment.