Skip to content

Commit a62a374

Browse files
committed
System Touch
1 parent 1c6d7a8 commit a62a374

8 files changed

Lines changed: 113 additions & 1 deletion

File tree

configuration/protocol-handlers.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,22 @@
5858
<expect>HTTP/</expect>
5959
<template>GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n</template>
6060
</handler>
61+
<!--
62+
HTTP Method Commands
63+
====================
64+
"set method http get" - wraps subsequent user messages in an HTTP GET request
65+
"set method http post" - wraps subsequent user messages in an HTTP POST request body
66+
"break method" - unsets HTTP method, reverts to raw binary passthrough
67+
-->
68+
<method-commands>
69+
<command name="set method http get">
70+
<template>GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: keep-alive\r\n\r\n</template>
71+
</command>
72+
<command name="set method http post">
73+
<template>POST {path} HTTP/1.1\r\nHost: {host}\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: {content-length}\r\nConnection: keep-alive\r\n\r\n{message}</template>
74+
</command>
75+
<command name="break method">
76+
<description>Unsets active HTTP method, returns to raw binary passthrough</description>
77+
</command>
78+
</method-commands>
6179
</protocol-handlers>
2.67 MB
Loading
2.61 MB
Loading
2.6 MB
Loading
2.34 MB
Loading
2.26 MB
Loading

source/connections/Connection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class Connection implements AutoCloseable
4848

4949
public long nationalId = -1;
5050

51+
/** Active session protocol (HTTP, HTTPS, FTP, etc.) — null means RAW/no wrapping. */
52+
public String protocol;
53+
5154
public telnet.TelnetLineEditor lineEditor;
5255

5356
public Connection()

source/national/NationalFinanceIDFeeder.java

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,22 @@ else if (cmd.equals("disconnect"))
239239
write(CONN, line + " < Disconnected from proxy relay. Back to local.");
240240
logSessionEvent(NFID.nationalId, "disconnect", "", 0);
241241
}
242+
else if (cmd.startsWith("set protocol"))
243+
{
244+
write(CONN, line + " < " + handleSetProtocol(CONN, input));
245+
}
246+
else if (cmd.equals("show protocol"))
247+
{
248+
String p = CONN.protocol != null ? CONN.protocol : "RAW (no protocol wrapping)";
249+
write(CONN, line + " < Protocol: " + p);
250+
}
242251
else
243252
{
244-
write(CONN, line + " < " + trade(input, NFID));
253+
// Wrap message in selected protocol before sending
254+
String outMsg = input;
255+
if (CONN.protocol != null)
256+
outMsg = wrapInProtocol(CONN.protocol, input, CONN);
257+
write(CONN, line + " < " + trade(outMsg, NFID));
245258
}
246259
line++;
247260
}
@@ -607,4 +620,82 @@ private static String prompt(final Connection CONN, final String QUESTION)
607620
private static int parseInt(final String S, final int DEF) { try { return Integer.parseInt(S.replaceAll("[^\\d]","")); } catch (Exception e) { return DEF; } }
608621
private static double parseDouble(final String S, final double D) { try { return Double.parseDouble(S); } catch (Exception e) { return D; } }
609622
private static String defaultStr(final String S, final String D) { return (S == null || S.isEmpty()) ? D : S; }
623+
624+
// ── Protocol selection ────────────────────────────────────────────────────
625+
626+
private static final java.util.Set<String> SUPPORTED_PROTOCOLS = java.util.Set.of(
627+
"HTTP", "HTTPS", "FTP", "SSH", "SMTP", "POP3", "IMAP", "RAW"
628+
);
629+
630+
/**
631+
* Handles "set protocol <name>" — sets the session protocol for message wrapping.
632+
* Supported: HTTP, HTTPS, FTP, SSH, SMTP, POP3, IMAP, RAW (no wrapping).
633+
*/
634+
private static String handleSetProtocol(final Connection CONN, final String INPUT)
635+
{
636+
String[] parts = INPUT.trim().split("\\s+");
637+
if (parts.length < 3)
638+
return "Usage: set protocol <HTTP|HTTPS|FTP|SSH|SMTP|POP3|IMAP|RAW>";
639+
640+
String proto = parts[2].toUpperCase();
641+
if (!SUPPORTED_PROTOCOLS.contains(proto))
642+
return "✗ Unknown protocol '" + proto + "'. Supported: " + SUPPORTED_PROTOCOLS;
643+
644+
if ("RAW".equals(proto))
645+
{
646+
CONN.protocol = null;
647+
return "✔ Protocol cleared — messages sent raw (no wrapping).";
648+
}
649+
650+
CONN.protocol = proto;
651+
return "✔ Protocol set to " + proto + ". All messages will be wrapped accordingly.";
652+
}
653+
654+
/**
655+
* Wraps user message in the selected protocol framing.
656+
* HTTP/HTTPS: GET / HTTP/1.1\r\nHost: {host}\r\n\r\n{message}\r\n\r\n
657+
* Others: delegated to ProtocolHandlerRegistry template.
658+
*/
659+
private static String wrapInProtocol(final String PROTOCOL, final String MESSAGE, final Connection CONN)
660+
{
661+
String host = "localhost";
662+
if (CONN.internet_address != null)
663+
host = CONN.internet_address.getHostAddress();
664+
665+
switch (PROTOCOL)
666+
{
667+
case "HTTP", "HTTPS" ->
668+
{
669+
return "GET / HTTP/1.1\r\nHost: " + host + "\r\n\r\n" + MESSAGE + "\r\n\r\n";
670+
}
671+
default ->
672+
{
673+
// Use ProtocolHandlerRegistry template if available
674+
java.util.Map<String, String> params = new java.util.HashMap<>();
675+
params.put("host", host);
676+
params.put("path", "/");
677+
params.put("command", MESSAGE);
678+
// Find port for this protocol from the registry
679+
configuration.ProtocolHandlerRegistry.ProtocolHandler ph = findByProtocol(PROTOCOL);
680+
if (ph != null)
681+
return configuration.ProtocolHandlerRegistry.wrapMessage(ph.port, MESSAGE, params);
682+
return MESSAGE;
683+
}
684+
}
685+
}
686+
687+
/** Lookup a protocol handler by protocol name rather than port. */
688+
private static configuration.ProtocolHandlerRegistry.ProtocolHandler findByProtocol(final String PROTOCOL)
689+
{
690+
// Check well-known port mappings
691+
int port = switch (PROTOCOL) {
692+
case "FTP" -> 21;
693+
case "SSH" -> 22;
694+
case "SMTP" -> 25;
695+
case "POP3" -> 110;
696+
case "IMAP" -> 143;
697+
default -> -1;
698+
};
699+
return port > 0 ? configuration.ProtocolHandlerRegistry.get(port) : null;
700+
}
610701
}

0 commit comments

Comments
 (0)