add browser finger printing and getElementById

This commit is contained in:
Daniella / Tove 2022-07-03 18:19:52 +02:00
parent 2cc71500fd
commit 9b6f1f8ff0
6 changed files with 48 additions and 3 deletions

View file

@ -34,6 +34,9 @@
<input type="submit" value="Set!"> <input type="submit" value="Set!">
<button onclick="data.save()" type="button">Save!</button> <button onclick="data.save()" type="button">Save!</button>
</form> </form>
<h2>Your fingerprint</h2>
<div id="fingerprint"></div>
</div> </div>
</body> </body>
</html> </html>

View file

@ -4,6 +4,8 @@ import java.net.Socket;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node;
import de.tudbut.async.Callback; import de.tudbut.async.Callback;
import de.tudbut.async.ComposeCallback; import de.tudbut.async.ComposeCallback;
import de.tudbut.async.TaskCallable; 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.GET;
import de.tudbut.tryumph.events.POST; import de.tudbut.tryumph.events.POST;
import de.tudbut.tryumph.events.Path; import de.tudbut.tryumph.events.Path;
import de.tudbut.tryumph.server.HTMLParsing;
import de.tudbut.tryumph.server.Request; import de.tudbut.tryumph.server.Request;
import de.tudbut.tryumph.server.Response; import de.tudbut.tryumph.server.Response;
import tudbut.parsing.TCN; import tudbut.parsing.TCN;
@ -55,7 +58,13 @@ public class Main implements IRequestCatcher {
@GET @GET
@Path("/") @Path("/")
public void onIndex(Request request, Callback<Response> res, Callback<Throwable> rej) { public void onIndex(Request request, Callback<Response> res, Callback<Throwable> 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 @POST
@ -64,7 +73,12 @@ public class Main implements IRequestCatcher {
TCN query = request.bodyURLEncoded(); TCN query = request.bodyURLEncoded();
request.context.data.set(query.getString("name"), query.getString("value")); request.context.data.set(query.getString("name"), query.getString("value"));
request.context.save(); 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);
} }
} }

View file

@ -5,7 +5,10 @@ import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.io.Writer; import java.io.Writer;
import org.w3c.dom.Attr;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy; import org.w3c.tidy.Tidy;
import de.tudbut.tryumph.util.Bug; import de.tudbut.tryumph.util.Bug;
@ -13,6 +16,19 @@ import de.tudbut.tryumph.util.Bug;
public class HTMLParsing { public class HTMLParsing {
private static Tidy tidy = new Tidy(); 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) { public static Document parse(String html) {
return (Document) tidy.parseDOM(new StringReader(html), (Writer) null); return (Document) tidy.parseDOM(new StringReader(html), (Writer) null);
} }

View file

@ -3,6 +3,7 @@ package de.tudbut.tryumph.server;
import java.net.Socket; import java.net.Socket;
import java.util.HashMap; import java.util.HashMap;
import de.tudbut.tools.Hasher;
import de.tudbut.tryumph.util.ReadOnlyHashMap; import de.tudbut.tryumph.util.ReadOnlyHashMap;
import tudbut.net.http.HTTPUtils; import tudbut.net.http.HTTPUtils;
import tudbut.parsing.TCN; import tudbut.parsing.TCN;
@ -37,6 +38,16 @@ public class Request {
splitPath = realPath.split("/"); 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 @Override
public String toString() { public String toString() {
return method + " " + path + " " + httpVersion + "\n" + return method + " " + path + " " + httpVersion + "\n" +

View file

@ -57,7 +57,7 @@ public class HTTPRequestReader {
assumeCRLF(); assumeCRLF();
String header; String header;
while(!(header = readUntilCRLF()).isEmpty()) { 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 name = header.substring(0, header.indexOf(':'));
String value = HTTPUtils.decodeUTF8(header.substring( String value = HTTPUtils.decodeUTF8(header.substring(
header.indexOf(':') + 2, header.indexOf(':') + 2,

View file

@ -6,4 +6,5 @@ center {
margin: auto; margin: auto;
display: block; display: block;
max-width: 700px; max-width: 700px;
word-break: break-all;
} }