Created
May 22, 2022 08:33
-
-
Save Gamer08YT/38ccb0f9cc9ffbcd0a3b4f1e3f89f069 to your computer and use it in GitHub Desktop.
Simple Java Socket Shell using MyTrioX Framework.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (C) Byte-Store.DE, Inc - All Rights Reserved | |
| * | |
| * Unauthorized copying of this storage, via any medium is strictly prohibited. | |
| */ | |
| import de.bytestore.mytriox.cache.CacheHandler; | |
| import de.bytestore.mytriox.network.server.ServerSocket; | |
| import de.bytestore.mytriox.network.utils.Socket; | |
| import java.io.IOException; | |
| import java.net.InetSocketAddress; | |
| import java.util.HashMap; | |
| import static de.bytestore.mytriox.cache.CacheHandler.charsetIO; | |
| /** | |
| * Basic Socket Shell Example. | |
| * @author Jan Heil | |
| * @see Socket Shell using MyTrioX Framework. | |
| */ | |
| public class Shell extends ServerSocket { | |
| // Store Strings from Sockets as Buffer. | |
| private HashMap<java.net.Socket, StringBuilder> bufferIO = new HashMap<>(); | |
| public static void main(String[] args) { | |
| // Print Debug Message. | |
| System.out.println("For testing use 'ncat -z -v 127.0.0.1 465' or 'telnet 127.0.0.1 465', you need to install Netcat."); | |
| new Shell(new InetSocketAddress("127.0.0.1", 465)); | |
| } | |
| /** | |
| * Create ServerSocket with InetSocketAddress. | |
| * | |
| * @param networkIO | |
| */ | |
| public Shell(InetSocketAddress networkIO) { | |
| // Init InetSocketAddress for Hostname. | |
| super(networkIO); | |
| // Enable MyTrioX Debugging. | |
| CacheHandler.debugIO = true; | |
| // Enable Binary Support for Socket. | |
| this.setBinary(true); | |
| // Enable Blocking Socket (Multithreading) | |
| this.setBlocking(true); | |
| // Start Server Socket. | |
| this.start(); | |
| } | |
| @Override | |
| public void connect(Socket clientIO) { | |
| try { | |
| // Write Welcome Message. | |
| clientIO.write("Welcome to this Shell Example, you can quit by using CTRL+C and use help to get some example Response.\n\r# ".getBytes()); | |
| } catch (IOException e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| @Override | |
| public void binary(Socket clientIO, byte[] binaryIO) { | |
| // Print Debug Content of Socket. | |
| System.out.println(new String(binaryIO)); | |
| try { | |
| // Convert current Binary Stream to String. | |
| String dataIO = new String(binaryIO, charsetIO); | |
| if (this.listen(clientIO, dataIO)) { | |
| // Put Socket into Buffer. | |
| if (!bufferIO.containsKey(clientIO.getSocket())) | |
| bufferIO.put(clientIO.getSocket(), new StringBuilder()); | |
| // Breakdown Line and write Buffer to Shell. | |
| if (dataIO.contains("\n")) { | |
| // Build Line from Buffered Chars. | |
| String lineIO = bufferIO.get(clientIO.getSocket()).toString().replaceAll(System.lineSeparator(), ""); | |
| // Write back to Shell. | |
| clientIO.write(("\n\r> '" + lineIO + "'").getBytes(charsetIO)); | |
| // Listen for Commands. | |
| if(this.listen(clientIO, lineIO)) | |
| clientIO.write(" - Can't find anything for your input, use help."); | |
| // Write Line Initiator. | |
| clientIO.write("\n\r# "); | |
| // Clear Line Buffer. | |
| bufferIO.remove(clientIO.getSocket()); | |
| } else | |
| // Append Line Data to Buffer. | |
| bufferIO.get(clientIO.getSocket()).append(dataIO); | |
| } | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Listen for Console Keys. | |
| * | |
| * @param socketIO | |
| * @param dataIO | |
| * @return | |
| */ | |
| private boolean listen(Socket socketIO, String dataIO) throws IOException { | |
| switch (dataIO.toLowerCase()) { | |
| case "\u0003": | |
| socketIO.write("Goodbye :)"); | |
| socketIO.close(); | |
| return false; | |
| case "help": | |
| socketIO.write("\n\rVersion 1.0.10\n\rSample Shell Socket Application\n\rby Jan Heil\n\r"); | |
| return false; | |
| } | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment