|
| 1 | +/* |
| 2 | + Copyright 2019-2020 Dmitry Isaenko |
| 3 | +
|
| 4 | + This file is part of NS-USBloader. |
| 5 | +
|
| 6 | + NS-USBloader is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + NS-USBloader is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | +package nsusbloader.COM.NET; |
| 20 | + |
| 21 | +import nsusbloader.ModelControllers.ILogPrinter; |
| 22 | +import nsusbloader.NSLDataTypes.EMsgType; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.io.FileNotFoundException; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.UnsupportedEncodingException; |
| 28 | +import java.net.*; |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | +public class NetworkSetupValidator { |
| 32 | + |
| 33 | + private String hostIP; |
| 34 | + private int hostPort; |
| 35 | + private final HashMap<String, UniFile> files; |
| 36 | + private ServerSocket serverSocket; |
| 37 | + private final boolean valid; |
| 38 | + private final ILogPrinter logPrinter; |
| 39 | + |
| 40 | + private final boolean doNotServe; |
| 41 | + |
| 42 | + NetworkSetupValidator(List<File> filesList, |
| 43 | + boolean doNotServe, |
| 44 | + String hostIP, |
| 45 | + String hostPortNum, |
| 46 | + ILogPrinter logPrinter) { |
| 47 | + this.files = new HashMap<>(); |
| 48 | + this.logPrinter = logPrinter; |
| 49 | + this.doNotServe = doNotServe; |
| 50 | + |
| 51 | + try { |
| 52 | + validateFiles(filesList); |
| 53 | + encodeAndAddFilesToMap(filesList); |
| 54 | + resolveIp(hostIP); |
| 55 | + resolvePort(hostPortNum); |
| 56 | + } |
| 57 | + catch (Exception e){ |
| 58 | + logPrinter.print(e.getMessage(), EMsgType.FAIL); |
| 59 | + valid = false; |
| 60 | + return; |
| 61 | + } |
| 62 | + valid = true; |
| 63 | + } |
| 64 | + |
| 65 | + private void validateFiles(List<File> filesList) { |
| 66 | + filesList.removeIf(f -> { |
| 67 | + if (f.isFile()) |
| 68 | + return false; |
| 69 | + |
| 70 | + File[] subFiles = f.listFiles((file, name) -> name.matches("[0-9]{2}")); |
| 71 | + |
| 72 | + if (subFiles == null || subFiles.length == 0) { |
| 73 | + logPrinter.print("NET: Exclude folder: " + f.getName(), EMsgType.WARNING); |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + Arrays.sort(subFiles, Comparator.comparingInt(file -> Integer.parseInt(file.getName()))); |
| 78 | + |
| 79 | + for (int i = subFiles.length - 2; i > 0 ; i--){ |
| 80 | + if (subFiles[i].length() < subFiles[i-1].length()) { |
| 81 | + logPrinter.print("NET: Exclude split file: "+f.getName()+ |
| 82 | + "\n Chunk sizes of the split file are not the same, but has to be.", EMsgType.WARNING); |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + return false; |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + private void encodeAndAddFilesToMap(List<File> filesList) throws UnsupportedEncodingException, FileNotFoundException { |
| 91 | + for (File file : filesList){ |
| 92 | + String encodedName = URLEncoder.encode(file.getName(), "UTF-8").replaceAll("\\+", "%20"); // replace '+' to '%20' |
| 93 | + UniFile uniFile = new UniFile(file); |
| 94 | + files.put(encodedName, uniFile); |
| 95 | + } |
| 96 | + |
| 97 | + if (files.size() == 0) { |
| 98 | + throw new FileNotFoundException("NET: No files to send."); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void resolveIp(String hostIPaddr) throws IOException{ |
| 103 | + if (! hostIPaddr.isEmpty()){ |
| 104 | + this.hostIP = hostIPaddr; |
| 105 | + logPrinter.print("NET: Host IP defined as: " + hostIP, EMsgType.PASS); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (findIpUsingHost("google.com")) |
| 110 | + return; |
| 111 | + |
| 112 | + if (findIpUsingHost("people.com.cn")) |
| 113 | + return; |
| 114 | + |
| 115 | + throw new IOException("Try using 'Expert mode' and set IP manually. " + getAvaliableIpExamples()); |
| 116 | + } |
| 117 | + |
| 118 | + private boolean findIpUsingHost(String host) { |
| 119 | + try { |
| 120 | + Socket scoketK; |
| 121 | + scoketK = new Socket(); |
| 122 | + scoketK.connect(new InetSocketAddress(host, 80)); |
| 123 | + hostIP = scoketK.getLocalAddress().getHostAddress(); |
| 124 | + scoketK.close(); |
| 125 | + |
| 126 | + logPrinter.print("NET: Host IP detected as: " + hostIP, EMsgType.PASS); |
| 127 | + return true; |
| 128 | + } |
| 129 | + catch (IOException e){ |
| 130 | + logPrinter.print("NET: Can't get your computer IP using " |
| 131 | + + host |
| 132 | + + " server (InetSocketAddress). Returned:\n\t"+e.getMessage(), EMsgType.INFO); |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private String getAvaliableIpExamples(){ |
| 138 | + try { |
| 139 | + StringBuilder builder = new StringBuilder("Check for:\n"); |
| 140 | + Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); |
| 141 | + while (enumeration.hasMoreElements()) { |
| 142 | + NetworkInterface n = enumeration.nextElement(); |
| 143 | + Enumeration<InetAddress> enumeration1 = n.getInetAddresses(); |
| 144 | + while (enumeration1.hasMoreElements()){ |
| 145 | + builder.append("- "); |
| 146 | + builder.append(enumeration1.nextElement().getHostAddress()); |
| 147 | + builder.append("\n"); |
| 148 | + } |
| 149 | + } |
| 150 | + return builder.toString(); |
| 151 | + } |
| 152 | + catch (SocketException socketException) { |
| 153 | + return ""; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private void resolvePort(String hostPortNum) throws Exception{ |
| 158 | + if (! hostPortNum.isEmpty()) { |
| 159 | + parsePort(hostPortNum); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + if (doNotServe) |
| 164 | + throw new Exception("NET: Port must be defined if 'Don't serve requests' option selected!"); |
| 165 | + |
| 166 | + findPort(); |
| 167 | + } |
| 168 | + |
| 169 | + private void findPort() throws Exception{ |
| 170 | + Random portRandomizer = new Random(); |
| 171 | + for (int i = 0; i < 5; i++) { |
| 172 | + try { |
| 173 | + this.hostPort = portRandomizer.nextInt(999) + 6000; |
| 174 | + serverSocket = new ServerSocket(hostPort); //System.out.println(serverSocket.getInetAddress()); 0.0.0.0 |
| 175 | + logPrinter.print("NET: Your port detected as: " + hostPort, EMsgType.PASS); |
| 176 | + break; |
| 177 | + } |
| 178 | + catch (IOException ioe) { |
| 179 | + if (i == 4) { |
| 180 | + throw new Exception("NET: Can't find good port\n" |
| 181 | + + "Set port by in settings ('Expert mode')."); |
| 182 | + } |
| 183 | + |
| 184 | + logPrinter.print("NET: Can't use port " + hostPort + "\nLooking for another one.", EMsgType.WARNING); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private void parsePort(String hostPortNum) throws Exception{ |
| 190 | + try { |
| 191 | + this.hostPort = Integer.parseInt(hostPortNum); |
| 192 | + serverSocket = new ServerSocket(hostPort); |
| 193 | + logPrinter.print("NET: Using defined port number: " + hostPort, EMsgType.PASS); |
| 194 | + } |
| 195 | + catch (IllegalArgumentException | IOException eee){ |
| 196 | + throw new Exception("NET: Can't use port defined in settings: " + hostPortNum + "\n\t"+eee.getMessage()); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + String getHostIP() { return hostIP; } |
| 201 | + int getHostPort() { return hostPort; } |
| 202 | + HashMap<String, UniFile> getFiles() { return files; } |
| 203 | + ServerSocket getServerSocket() { return serverSocket; } |
| 204 | + boolean isValid() { return valid; } |
| 205 | +} |
0 commit comments