From 3690c63399172dc9d254942d0cd6fea6eff6c189 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Mon, 6 Jul 2020 13:35:45 +0300 Subject: [PATCH] CLI support for Tinfoil/Awoo Net-install mode. --- .../nsusbloader/cli/CommandLineInterface.java | 3 + .../cli/IncorrectSetupException.java | 7 + src/main/java/nsusbloader/cli/TinfoilNet.java | 151 ++++++++++-------- 3 files changed, 92 insertions(+), 69 deletions(-) create mode 100644 src/main/java/nsusbloader/cli/IncorrectSetupException.java diff --git a/src/main/java/nsusbloader/cli/CommandLineInterface.java b/src/main/java/nsusbloader/cli/CommandLineInterface.java index 4ac568e..7541e30 100644 --- a/src/main/java/nsusbloader/cli/CommandLineInterface.java +++ b/src/main/java/nsusbloader/cli/CommandLineInterface.java @@ -66,6 +66,9 @@ public class CommandLineInterface { System.out.println(pe.getLocalizedMessage() + "\nTry 'ns-usbloader --help' for more information."); } + catch (IncorrectSetupException iee){ + System.out.println(iee.getLocalizedMessage()); + } catch (InterruptedException ignore){} catch (Exception e){ System.out.println("CLI error"); diff --git a/src/main/java/nsusbloader/cli/IncorrectSetupException.java b/src/main/java/nsusbloader/cli/IncorrectSetupException.java new file mode 100644 index 0000000..6077b75 --- /dev/null +++ b/src/main/java/nsusbloader/cli/IncorrectSetupException.java @@ -0,0 +1,7 @@ +package nsusbloader.cli; + +public class IncorrectSetupException extends Exception { + public IncorrectSetupException(String errorMessage){ + super(errorMessage); + } +} diff --git a/src/main/java/nsusbloader/cli/TinfoilNet.java b/src/main/java/nsusbloader/cli/TinfoilNet.java index 2a4c025..58502d0 100644 --- a/src/main/java/nsusbloader/cli/TinfoilNet.java +++ b/src/main/java/nsusbloader/cli/TinfoilNet.java @@ -25,9 +25,10 @@ import java.util.ArrayList; import java.util.List; // TODO: Add 'don't serve requests' option -// TODO: Refactor: remove duplicates; make logic flow more 'exception-driven' public class TinfoilNet { + private String[] arguments; + private String nsIp; private String hostIp = ""; @@ -36,36 +37,98 @@ public class TinfoilNet { private int parseFileSince = 1; - TinfoilNet(String[] arguments) throws InterruptedException{ + private List filesList; - if (arguments == null) { - showIncorrectCommandMessage(); - return; + TinfoilNet(String[] arguments) throws InterruptedException, IncorrectSetupException{ + this.arguments = arguments; + checkArguments(); + parseNsIP(); + parseHostIPAndExtras(); + parseFilesArguments(); + runTinfoilNetBackend(); + } + + private void checkArguments() throws IncorrectSetupException{ + if (arguments == null || arguments.length == 0) { + throw new IncorrectSetupException("No arguments.\n" + + "Try 'ns-usbloader -n help' for more information."); } if (arguments.length == 1){ - if (isHelp(arguments[0])) + if (isHelpDirective(arguments[0])){ showHelp(); + return; + } else - showIncorrectCommandMessage(); - return; + throw new IncorrectSetupException("Not enough arguments.\n" + + "Try 'ns-usbloader -n help' for more information."); } - if (arguments.length < 2){ - showIncorrectCommandMessage(); + if (arguments.length == 2 && arguments[1].startsWith("hostip=")) { + throw new IncorrectSetupException("Not enough arguments.\n" + + "Try 'ns-usbloader -n help' for more information."); + } + } + + private boolean isHelpDirective(String argument){ + return argument.equals("help"); + } + private void showHelp(){ + System.out.println("Usage:\n" + + "\tns-usbloader -n nsip= [hostip=] FILE1 ...\n" + + "\tns-usbloader --tfn nsip= [hostip=] FILE1 ..." + + "\n\nOptions:" + + "\n\tnsip=\t\t\tDefine NS IP address (mandatory)" + + "\n\thostip=\tDefine this host IP address. Will be obtained automatically if not set."); + } + + private void parseNsIP() throws IncorrectSetupException{ + String argument1 = arguments[0]; + + if (! argument1.startsWith("nsip=")) + throw new IncorrectSetupException("First argument must be 'nsip='\n" + + "Try 'ns-usbloader -n help' for more information."); + + nsIp = argument1.replaceAll("^nsip=", ""); + + if (nsIp.isEmpty()) + throw new IncorrectSetupException("No spaces allowed before or after 'nsip=' argument.\n" + + "Try 'ns-usbloader -n help' for more information."); + } + + private void parseHostIPAndExtras(){ + String argument2 = arguments[1]; + + if (! argument2.startsWith("hostip=")) return; + + parseFileSince = 2; + hostIp = argument2.replaceAll("(^hostip=)|(:.+?$)|(:$)", ""); + + if (argument2.contains(":")) + hostPortNum = argument2.replaceAll("(^.+:)|(/.+?$)|(/$)", ""); + + if (argument2.contains("/")) + hostExtras = argument2.replaceAll("^[^/]*/", ""); + } + + private void parseFilesArguments() throws IncorrectSetupException{ + filesList = new ArrayList<>(); + File file; + + for (; parseFileSince < arguments.length; parseFileSince++) { + file = new File(arguments[parseFileSince]); + if (file.exists()) + filesList.add(file); } - if (parseNsIP(arguments[0])) - return; - parseHostIPAndExtras(arguments[1]); - if (checkArgumentsCount(arguments.length)) - return; - - List filesList = new ArrayList<>(); - for (; parseFileSince < arguments.length; parseFileSince++) - filesList.add(new File(arguments[parseFileSince])); + if (filesList.size() == 0) { + throw new IncorrectSetupException("File(s) doesn't exists.\n" + + "Try 'ns-usbloader -n help' for more information."); + } + } + private void runTinfoilNetBackend() throws InterruptedException{ NETCommunications netCommunications = new NETCommunications( filesList, nsIp, @@ -77,54 +140,4 @@ public class TinfoilNet { netCommThread.start(); netCommThread.join(); } - - private boolean isHelp(String argument){ - return argument.equals("help"); - } - private void showHelp(){ - System.out.println("Usage:\n" - + "\tns-usbloader -n nsip= [hostip=] FILE1 ...\n" - + "\tns-usbloader --tfn nsip= [hostip=] FILE1 ..." - + "\n\nOptions:" - + "\n\tnsip=\t\t\tDefine NS IP address (mandatory)" - + "\n\thostip=\tDefine this host IP address. Will be obtained automatically if not set."); - } - private void showIncorrectCommandMessage(){ - System.out.println("Try 'ns-usbloader -n help' for more information."); - } - - private boolean parseNsIP(String argument1){ - if (argument1.startsWith("nsip=")){ - nsIp = argument1.replaceAll("^nsip=", ""); - - if (nsIp.isEmpty()) { - showIncorrectCommandMessage(); - return true; - } - } - else{ - showIncorrectCommandMessage(); - return true; - } - return false; - } - private void parseHostIPAndExtras(String argument2){ - if (argument2.startsWith("hostip=")){ - parseFileSince = 2; - hostIp = argument2.replaceAll("(^hostip=)|(:.+?$)|(:$)", ""); - - if (argument2.contains(":")) - hostPortNum = argument2.replaceAll("(^.+:)|(/.+?$)|(/$)", ""); - - if (argument2.contains("/")) - hostExtras = argument2.replaceAll("^[^/]*/", ""); - } - } - private boolean checkArgumentsCount(int argumentsLength){ - if (argumentsLength == parseFileSince){ - showIncorrectCommandMessage(); - return true; - } - return false; - } }