mirror of
https://github.com/developersu/ns-usbloader.git
synced 2025-05-13 06:34:43 -04:00
Refactoring:
* Replace UI-friendly Tasks to Runnable in all TF/GL NET/USB processes; implement similar interface for this. * Add Apache Commons CLI to handle CLI. * Extend/update CLI commands keys/functions, descriptions etc. * Add draft CLI command for Tinfoil/Awoo Net-install mode.
This commit is contained in:
parent
6b65c74c9d
commit
3c89df9dcd
19 changed files with 404 additions and 109 deletions
167
src/main/java/nsusbloader/cli/CommandLineInterface.java
Normal file
167
src/main/java/nsusbloader/cli/CommandLineInterface.java
Normal file
|
@ -0,0 +1,167 @@
|
|||
/*
|
||||
Copyright 2019-2020 Dmitry Isaenko
|
||||
|
||||
This file is part of NS-USBloader.
|
||||
|
||||
NS-USBloader is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
NS-USBloader is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package nsusbloader.cli;
|
||||
|
||||
import nsusbloader.NSLMain;
|
||||
import nsusbloader.Utilities.Rcm;
|
||||
import org.apache.commons.cli.*;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
public class CommandLineInterface {
|
||||
|
||||
public CommandLineInterface(String[] args) {
|
||||
if (noRealKeys(args)){
|
||||
System.out.println("Try 'ns-usbloader --help' for more information.");
|
||||
return;
|
||||
}
|
||||
|
||||
final Options cliOptions = createCliOptions();
|
||||
|
||||
CommandLineParser cliParser = new DefaultParser();
|
||||
try{
|
||||
CommandLine cli = cliParser.parse(cliOptions, args);
|
||||
if (cli.hasOption('v') || cli.hasOption("version")){
|
||||
handleVersion();
|
||||
return;
|
||||
}
|
||||
if (cli.hasOption('h') || cli.hasOption("help")){
|
||||
handleHelp(cliOptions);
|
||||
return;
|
||||
}
|
||||
if (cli.hasOption('r') || cli.hasOption("rcm")){
|
||||
final String payloadArgument = cli.getOptionValue("rcm");
|
||||
handleRcm(payloadArgument);
|
||||
return;
|
||||
}
|
||||
if (cli.hasOption("c") || cli.hasOption("clean")){
|
||||
handleSettingClean();
|
||||
return;
|
||||
}
|
||||
if (cli.hasOption("n") || cli.hasOption("tfn")){
|
||||
final String[] tfnArguments = cli.getOptionValues("tfn");
|
||||
new TinfoilNet(tfnArguments);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (ParseException pe){
|
||||
System.out.println(pe.getLocalizedMessage() +
|
||||
"\nTry 'ns-usbloader --help' for more information.");
|
||||
}
|
||||
catch (InterruptedException ignore){}
|
||||
catch (Exception e){
|
||||
System.out.println("CLI error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean noRealKeys(String[] args){
|
||||
return (args.length > 0 && ! args[0].startsWith("-"));
|
||||
}
|
||||
|
||||
private Options createCliOptions(){
|
||||
final Options options = new Options();
|
||||
|
||||
final Option rcmOption = Option.builder("r")
|
||||
.longOpt("rcm")
|
||||
.desc("Send payload")
|
||||
.hasArg(true)
|
||||
.argName("[PATH/]payload.bin")
|
||||
.numberOfArgs(1)
|
||||
.build();
|
||||
|
||||
final Option cleanSettingsOption = Option.builder("c")
|
||||
.longOpt("clean")
|
||||
.desc("Remove/reset settings and exit")
|
||||
.hasArg(false)
|
||||
.build();
|
||||
|
||||
final Option versionOption = Option.builder("v")
|
||||
.longOpt("version")
|
||||
.desc("Show application version")
|
||||
.hasArg(false)
|
||||
.build();
|
||||
|
||||
final Option helpOption = Option.builder("h")
|
||||
.longOpt("help")
|
||||
.desc("Show this help")
|
||||
.hasArg(false)
|
||||
.build();
|
||||
|
||||
/* Tinfoil network mode options */
|
||||
final Option tinfoilNetOption = Option.builder("n")
|
||||
.longOpt("tfn")
|
||||
.desc("Install via Tinfoil/Awoo Network mode. Check '-n help' for information.")
|
||||
.hasArgs()
|
||||
.argName("...")
|
||||
.build();
|
||||
|
||||
|
||||
final OptionGroup group = new OptionGroup();
|
||||
group.addOption(rcmOption);
|
||||
group.addOption(tinfoilNetOption);
|
||||
group.addOption(cleanSettingsOption);
|
||||
group.addOption(versionOption);
|
||||
group.addOption(helpOption);
|
||||
|
||||
options.addOptionGroup(group);
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
private void handleVersion(){
|
||||
System.out.println("NS-USBloader " + NSLMain.appVersion);
|
||||
}
|
||||
private void handleSettingClean() throws Exception {
|
||||
if (Preferences.userRoot().nodeExists("NS-USBloader")) {
|
||||
Preferences.userRoot().node("NS-USBloader").removeNode();
|
||||
System.out.println("Settings removed");
|
||||
}
|
||||
else
|
||||
System.out.println("There are no settings in system to remove");
|
||||
}
|
||||
private void handleRcm(String payload) throws InterruptedException{
|
||||
boolean isWindows = System.getProperty("os.name").toLowerCase().replace(" ", "").contains("windows");
|
||||
|
||||
if (isWindows) {
|
||||
if (! payload.matches("^.:\\\\.*$"))
|
||||
payload = System.getProperty("user.dir") + File.separator + payload;
|
||||
}
|
||||
else {
|
||||
if (! payload.startsWith("/"))
|
||||
payload = System.getProperty("user.dir") + File.separator + payload;
|
||||
}
|
||||
|
||||
Rcm rcm = new Rcm(payload);
|
||||
Thread rcmThread = new Thread(rcm);
|
||||
rcmThread.start();
|
||||
rcmThread.join();
|
||||
}
|
||||
private void handleHelp(Options cliOptions){
|
||||
new HelpFormatter().printHelp(
|
||||
120,
|
||||
"NS-USBloader.jar [OPTION]... [FILE]...",
|
||||
"options:",
|
||||
cliOptions,
|
||||
"\n");
|
||||
}
|
||||
|
||||
}
|
130
src/main/java/nsusbloader/cli/TinfoilNet.java
Normal file
130
src/main/java/nsusbloader/cli/TinfoilNet.java
Normal file
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
Copyright 2019-2020 Dmitry Isaenko
|
||||
|
||||
This file is part of NS-USBloader.
|
||||
|
||||
NS-USBloader is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
NS-USBloader is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package nsusbloader.cli;
|
||||
|
||||
import nsusbloader.COM.NET.NETCommunications;
|
||||
|
||||
import java.io.File;
|
||||
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 nsIp;
|
||||
|
||||
private String hostIp = "";
|
||||
private String hostPortNum = "";
|
||||
private String hostExtras = "";
|
||||
|
||||
private int parseFileSince = 1;
|
||||
|
||||
TinfoilNet(String[] arguments) throws InterruptedException{
|
||||
|
||||
if (arguments == null) {
|
||||
showIncorrectCommandMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
if (arguments.length == 1){
|
||||
if (isHelp(arguments[0]))
|
||||
showHelp();
|
||||
else
|
||||
showIncorrectCommandMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
if (arguments.length < 2){
|
||||
showIncorrectCommandMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
if (parseNsIP(arguments[0]))
|
||||
return;
|
||||
parseHostIPAndExtras(arguments[1]);
|
||||
if (checkArgumentsCount(arguments.length))
|
||||
return;
|
||||
|
||||
List<File> filesList = new ArrayList<>();
|
||||
for (; parseFileSince < arguments.length; parseFileSince++)
|
||||
filesList.add(new File(arguments[parseFileSince]));
|
||||
|
||||
NETCommunications netCommunications = new NETCommunications(
|
||||
filesList,
|
||||
nsIp,
|
||||
false,
|
||||
hostIp,
|
||||
hostPortNum,
|
||||
hostExtras);
|
||||
Thread netCommThread = new Thread(netCommunications);
|
||||
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=<arg1> [hostip=<arg2>] FILE1 ...\n"
|
||||
+ "\tns-usbloader --tfn nsip=<arg1> [hostip=<arg2>] FILE1 ..."
|
||||
+ "\n\nOptions:"
|
||||
+ "\n\tnsip=<ip>\t\t\tDefine NS IP address (mandatory)"
|
||||
+ "\n\thostip=<ip[:port][/extra]>\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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue