@@ -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 \n Host: " + 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