From 9b6f1f8ff01dee31b16884111483805705bedcb9 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sun, 3 Jul 2022 18:19:52 +0200 Subject: [PATCH] add browser finger printing and getElementById --- index.html | 3 +++ src/de/tudbut/tryumph/example/Main.java | 18 ++++++++++++++++-- src/de/tudbut/tryumph/server/HTMLParsing.java | 16 ++++++++++++++++ src/de/tudbut/tryumph/server/Request.java | 11 +++++++++++ .../tryumph/server/http/HTTPRequestReader.java | 2 +- style.css | 1 + 6 files changed, 48 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index c0e16e5..a4cae9b 100644 --- a/index.html +++ b/index.html @@ -34,6 +34,9 @@ + +

Your fingerprint

+
diff --git a/src/de/tudbut/tryumph/example/Main.java b/src/de/tudbut/tryumph/example/Main.java index 20c360a..559296e 100644 --- a/src/de/tudbut/tryumph/example/Main.java +++ b/src/de/tudbut/tryumph/example/Main.java @@ -4,6 +4,8 @@ import java.net.Socket; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.Node; + import de.tudbut.async.Callback; import de.tudbut.async.ComposeCallback; import de.tudbut.async.TaskCallable; @@ -12,6 +14,7 @@ import de.tudbut.tryumph.events.EventListener; import de.tudbut.tryumph.events.GET; import de.tudbut.tryumph.events.POST; import de.tudbut.tryumph.events.Path; +import de.tudbut.tryumph.server.HTMLParsing; import de.tudbut.tryumph.server.Request; import de.tudbut.tryumph.server.Response; import tudbut.parsing.TCN; @@ -55,7 +58,13 @@ public class Main implements IRequestCatcher { @GET @Path("/") public void onIndex(Request request, Callback res, Callback rej) { - res.call(new Response(request, request.context.file("index.html"), 200, "OK")); + Response r = new Response(request, request.context.file("index.html"), 200, "OK"); + System.out.println(r.getHTMLData()); + Document html = r.getHTML(); + Node node = html.createTextNode(request.fingerPrint()); + HTMLParsing.getElementById(html, "fingerprint").appendChild(node); + r.updateHTMLData(); + res.call(r); } @POST @@ -64,7 +73,12 @@ public class Main implements IRequestCatcher { TCN query = request.bodyURLEncoded(); request.context.data.set(query.getString("name"), query.getString("value")); request.context.save(); - res.call(new Response(request, request.context.file("index.html"), 200, "OK")); + Response r = new Response(request, request.context.file("index.html"), 200, "OK"); + Document html = r.getHTML(); + Node node = html.createTextNode(request.fingerPrint()); + HTMLParsing.getElementById(html, "fingerprint").appendChild(node); + r.updateHTMLData(); + res.call(r); } } diff --git a/src/de/tudbut/tryumph/server/HTMLParsing.java b/src/de/tudbut/tryumph/server/HTMLParsing.java index 2e37077..430b9b8 100644 --- a/src/de/tudbut/tryumph/server/HTMLParsing.java +++ b/src/de/tudbut/tryumph/server/HTMLParsing.java @@ -5,7 +5,10 @@ import java.io.IOException; import java.io.StringReader; import java.io.Writer; +import org.w3c.dom.Attr; import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; import org.w3c.tidy.Tidy; import de.tudbut.tryumph.util.Bug; @@ -13,6 +16,19 @@ import de.tudbut.tryumph.util.Bug; public class HTMLParsing { private static Tidy tidy = new Tidy(); + + public static Element getElementById(Document document, String id) { + NodeList list = document.getElementsByTagName("*"); + for(int i = 0; i < list.getLength(); i++) { + Element el = (Element) list.item(i); + Attr attr = el.getAttributeNode("id"); + if(attr != null && attr.getNodeValue().equals(id)) { + return el; + } + } + return null; + } + public static Document parse(String html) { return (Document) tidy.parseDOM(new StringReader(html), (Writer) null); } diff --git a/src/de/tudbut/tryumph/server/Request.java b/src/de/tudbut/tryumph/server/Request.java index 6d1d39a..d77cd63 100644 --- a/src/de/tudbut/tryumph/server/Request.java +++ b/src/de/tudbut/tryumph/server/Request.java @@ -3,6 +3,7 @@ package de.tudbut.tryumph.server; import java.net.Socket; import java.util.HashMap; +import de.tudbut.tools.Hasher; import de.tudbut.tryumph.util.ReadOnlyHashMap; import tudbut.net.http.HTTPUtils; import tudbut.parsing.TCN; @@ -37,6 +38,16 @@ public class Request { splitPath = realPath.split("/"); } + public String fingerPrint() { + StringBuilder data = new StringBuilder(); + data.append(headers.get("User-Agent")); + data.append(headers.get("Accept")); + data.append(headers.get("Accept-Encoding")); + data.append(headers.get("Connection")); + data.append(headers.get("Accept-Language")); + return Hasher.sha512hex(data.toString()); + } + @Override public String toString() { return method + " " + path + " " + httpVersion + "\n" + diff --git a/src/de/tudbut/tryumph/server/http/HTTPRequestReader.java b/src/de/tudbut/tryumph/server/http/HTTPRequestReader.java index 67d77fa..dc029c5 100644 --- a/src/de/tudbut/tryumph/server/http/HTTPRequestReader.java +++ b/src/de/tudbut/tryumph/server/http/HTTPRequestReader.java @@ -57,7 +57,7 @@ public class HTTPRequestReader { assumeCRLF(); String header; while(!(header = readUntilCRLF()).isEmpty()) { - boolean hasParameters = header.indexOf(';') != -1; + boolean hasParameters = header.indexOf(';') != -1 && header.indexOf('=') != -1; String name = header.substring(0, header.indexOf(':')); String value = HTTPUtils.decodeUTF8(header.substring( header.indexOf(':') + 2, diff --git a/style.css b/style.css index 0e27bf1..ef6bef3 100644 --- a/style.css +++ b/style.css @@ -6,4 +6,5 @@ center { margin: auto; display: block; max-width: 700px; + word-break: break-all; }