-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathTinfoilNetCli.java
More file actions
173 lines (140 loc) · 5.72 KB
/
TinfoilNetCli.java
File metadata and controls
173 lines (140 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
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.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TinfoilNetCli {
private final String[] arguments;
private String nsIp;
private String hostIp = "";
private String hostPortNum = "";
//private String hostExtras = ""; // TODO: Add 'don't serve requests' option or remove this
private int parseFileSince = 1;
private List<File> filesList;
TinfoilNetCli(String[] arguments) throws InterruptedException, IncorrectSetupException{
this.arguments = arguments;
checkArguments();
parseNsIP();
parseHostSettings();
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 (isHelpDirective(arguments[0])){
showHelp();
return;
}
else
throw new IncorrectSetupException("Not enough arguments.\n" +
"Try 'ns-usbloader -n help' for more information.");
}
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() throws IncorrectSetupException{
throw new IncorrectSetupException("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\tDefine NS IP address (mandatory)"
+ "\n\thostip=<ip[:port]>\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=<ip_address>'\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=<ip_address>' argument.\n" +
"Try 'ns-usbloader -n help' for more information.");
}
}
private void parseHostSettings(){
String argument2 = arguments[1];
if (! argument2.startsWith("hostip="))
return;
parseFileSince = 2;
hostIp = argument2.replaceAll("(^hostip=)|(:.+?$)|(:$)", "");
if (argument2.contains(":"))
hostPortNum = argument2.replaceAll("(^.+:)", "");
// 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()) {
continue;
}
if (file.isDirectory()) {
filesList.addAll(fillListOfFiles(file));
} else {
filesList.add(file);
}
}
if (filesList.size() == 0) {
throw new IncorrectSetupException("File(s) doesn't exist.\n" +
"Try 'ns-usbloader -n help' for more information.");
}
}
public List<File> fillListOfFiles(File rootDir) {
try (Stream<Path> stream = Files.walk(rootDir.toPath())) {
return stream
.map(Path::toFile)
.filter(File::isFile)
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
return Collections.emptyList();
}
private void runTinfoilNetBackend() throws InterruptedException{
NETCommunications netCommunications = new NETCommunications(
filesList,
nsIp,
false,
hostIp,
hostPortNum,
"");
Thread netCommThread = new Thread(netCommunications);
netCommThread.setDaemon(true);
netCommThread.start();
netCommThread.join();
}
}