Updated frontend to new Protocol

This commit is contained in:
UnlegitDqrk
2026-02-22 17:21:23 +01:00
parent 2d829fe341
commit 09dd207bb1
5 changed files with 355 additions and 221 deletions

View File

@@ -2,22 +2,24 @@ package ins.frontend;
import ins.frontend.utils.RegistrarDao;
import ins.frontend.utils.WebApp;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
import org.openautonomousconnection.protocol.packets.v1_0_1.beta.web.impl.resource.WebResourceResponsePacket;
import org.openautonomousconnection.protocol.versions.v1_0_1.beta.WebPacketFlags;
import org.openautonomousconnection.protocol.versions.v1_0_1.beta.WebPacketHeader;
import org.openautonomousconnection.webserver.api.Route;
import org.openautonomousconnection.webserver.api.SessionContext;
import org.openautonomousconnection.webserver.api.WebPage;
import org.openautonomousconnection.webserver.api.WebPageContext;
import org.openautonomousconnection.webserver.utils.HeaderMaps;
import org.openautonomousconnection.webserver.utils.Html;
import org.openautonomousconnection.webserver.utils.MergedRequestParams;
import org.openautonomousconnection.webserver.utils.QuerySupport;
import org.openautonomousconnection.webserver.utils.WebUrlUtil;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* INS registrar ins.frontend (TLN / InfoName / Records) with proper POST parameter parsing.
* INS registrar dashboard (TLN / InfoName / Records) for protocol v1.0.1 resource packets.
*
* <p>Supported actions (POST recommended for mutations):</p>
* <ul>
@@ -28,11 +30,8 @@ import java.util.Map;
* <li>delete_infoname</li>
* <li>add_record</li>
* </ul>
*
* <p>Important: Listing/editing/deleting records requires DAO methods that are not part of the provided snippet.
* This page currently supports adding records only.</p>
*/
@Route(path = "dashboard.html")
@Route(path = "/dashboard.html")
public final class dashboard implements WebPage {
private static Integer normalizeNullableInt(String s) {
@@ -47,30 +46,26 @@ public final class dashboard implements WebPage {
}
@Override
public WebResponsePacket handle(WebPageContext ctx) throws Exception {
public WebResourceResponsePacket handle(WebPageContext ctx) throws Exception {
WebApp.init();
SessionContext session = SessionContext.from(
ctx.client,
(ProtocolWebServer) ctx.client.getServer(),
ctx.request.getHeaders()
);
if (!session.isValid() || session.getUser() == null) {
return plain(401, "Authentication required (session).");
if (ctx.session == null || !ctx.session.isValid() || ctx.session.getUser() == null) {
return plain(ctx, 401, "Authentication required (session).");
}
int userId;
try {
userId = Integer.parseInt(session.getUser());
userId = Integer.parseInt(ctx.session.getUser());
} catch (Exception e) {
return plain(401, "Invalid session user.");
return plain(ctx, 401, "Invalid session user.");
}
RegistrarDao dao = WebApp.get().dao();
// Raw target and merged params (GET + POST).
String rawTarget = org.openautonomousconnection.webserver.utils.QuerySupport.extractRawTarget(ctx.request);
// Build target for param merging: "/path?query"
String rawTarget = WebUrlUtil.extractPathAndQuery(ctx.request.getUrl());
if (rawTarget == null) rawTarget = "/dashboard.html";
Map<String, String> headers = ctx.request.getHeaders();
byte[] body = ctx.request.getBody();
@@ -90,7 +85,7 @@ public final class dashboard implements WebPage {
}
}
return render(userId, msg, err, dao);
return render(ctx, userId, msg, err, dao);
}
private ActionResult executeAction(String action, MergedRequestParams p, int userId, RegistrarDao dao) throws Exception {
@@ -171,7 +166,6 @@ public final class dashboard implements WebPage {
if (!dao.isOwnerOfInfoName(infonameId, userId)) return ActionResult.err("Not owner of this infoname.");
if (type.isBlank() || value == null || value.isBlank()) return ActionResult.err("Missing type/value.");
// Validate allow_subdomains against TLN of this infoname (owned list contains TLN metadata).
RegistrarDao.InfoNameRow[] owned = dao.listOwnedInfoNames(userId);
RegistrarDao.InfoNameRow row = null;
for (RegistrarDao.InfoNameRow r : owned) {
@@ -194,7 +188,7 @@ public final class dashboard implements WebPage {
return ActionResult.err("Unknown action: " + action);
}
private WebResponsePacket render(int userId, String msg, String err, RegistrarDao dao) throws Exception {
private WebResourceResponsePacket render(WebPageContext ctx, int userId, String msg, String err, RegistrarDao dao) throws Exception {
RegistrarDao.TlnRow[] tlns = dao.listVisibleTlns(userId);
RegistrarDao.InfoNameRow[] owned = dao.listOwnedInfoNames(userId);
@@ -206,7 +200,7 @@ public final class dashboard implements WebPage {
<h2>INS Registrar</h2>
%s
%s
<h3>Create TLN</h3>
<form method="post" action="dashboard.html" class="form">
<input type="hidden" name="action" value="create_tln">
@@ -216,10 +210,10 @@ public final class dashboard implements WebPage {
<label><span class="muted">allow_subdomains</span><input type="text" name="allow_subdomains" value="1" required></label>
<button type="submit">Create TLN</button>
</form>
<h3>TLNs (public + owned)</h3>
%s
<h3>Create InfoName</h3>
<p class="muted">Allowed if TLN is public or owned by you.</p>
<form method="post" action="dashboard.html" class="form">
@@ -228,7 +222,7 @@ public final class dashboard implements WebPage {
<label><span class="muted">info</span><input type="text" name="info" placeholder="example" required></label>
<button type="submit">Create InfoName</button>
</form>
<h3>Add Record</h3>
<p class="muted">Subname requires allow_subdomains=1 unless you own the TLN. Root (no sub) always allowed.</p>
<form method="post" action="dashboard.html" class="form">
@@ -243,10 +237,10 @@ public final class dashboard implements WebPage {
<label><span class="muted">weight (SRV)</span><input type="number" name="weight"></label>
<button type="submit">Add Record</button>
</form>
<h3>Your InfoNames</h3>
%s
<div class="row">
<div class="col"><a href="index.html">Home</a></div>
</div>
@@ -259,7 +253,7 @@ public final class dashboard implements WebPage {
);
String html = Html.page("INS Registrar", body);
return new WebResponsePacket(200, "text/html", new HashMap<>(), Html.utf8(html));
return html(ctx, 200, html);
}
private String renderTlnSection(RegistrarDao.TlnRow[] tlns, int userId) {
@@ -300,7 +294,7 @@ public final class dashboard implements WebPage {
</label>
<button type="submit">Update</button>
</form>
<form method="post" action="dashboard.html" style="margin:0;">
<input type="hidden" name="action" value="delete_tln">
<input type="hidden" name="id" value="%d">
@@ -348,8 +342,33 @@ public final class dashboard implements WebPage {
return sb.toString();
}
private WebResponsePacket plain(int code, String text) {
return new WebResponsePacket(code, "text/plain", new HashMap<>(), Html.utf8(text));
private WebResourceResponsePacket plain(WebPageContext ctx, int code, String text) {
byte[] body = (text == null ? "" : text).getBytes(StandardCharsets.UTF_8);
Map<String, String> headers = HeaderMaps.mutable();
headers.put("content-length", String.valueOf(body.length));
return new WebResourceResponsePacket(outHeader(ctx), code, "text/plain; charset=utf-8", headers, body, null);
}
private WebResourceResponsePacket html(WebPageContext ctx, int code, String html) {
byte[] body = Html.utf8(html);
Map<String, String> headers = HeaderMaps.mutable();
headers.put("content-length", String.valueOf(body.length));
return new WebResourceResponsePacket(outHeader(ctx), code, "text/html; charset=utf-8", headers, body, null);
}
private WebPacketHeader outHeader(WebPageContext ctx) {
WebPacketHeader in = (ctx != null && ctx.request != null) ? ctx.request.getHeader() : null;
if (in == null) {
return new WebPacketHeader(0, 0, 0, 0, WebPacketFlags.RESOURCE, System.currentTimeMillis());
}
return new WebPacketHeader(
in.getRequestId(),
in.getTabId(),
in.getPageId(),
in.getFrameId(),
in.getFlags() | WebPacketFlags.RESOURCE,
System.currentTimeMillis()
);
}
private String safeMsg(Exception e) {