mirror of
https://github.com/developersu/ns-usbloader.git
synced 2025-05-14 15:15:05 -04:00
Frontend complete
This commit is contained in:
parent
863c69affa
commit
65f084591a
5 changed files with 130 additions and 107 deletions
|
@ -10,9 +10,6 @@ import java.net.*;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
/*
|
||||
* Add option: don't serve replies
|
||||
* */
|
||||
|
||||
public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
||||
|
||||
|
@ -33,13 +30,15 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
/**
|
||||
* Simple constructor that everybody uses
|
||||
* */
|
||||
public NETCommunications(List<File> filesList, String switchIP){
|
||||
this.doNotServeRequests = false;
|
||||
this.extras = "";
|
||||
this.logPrinter = new LogPrinter();
|
||||
public NETCommunications(List<File> filesList, String switchIP, boolean doNotServeRequests, String hostIPaddr, String hostPortNum, String extras){
|
||||
this.doNotServeRequests = doNotServeRequests;
|
||||
if (doNotServeRequests)
|
||||
this.extras = extras;
|
||||
else
|
||||
this.extras = "";
|
||||
this.switchIP = switchIP;
|
||||
this.logPrinter = new LogPrinter();
|
||||
this.nspMap = new HashMap<>();
|
||||
logPrinter.print("\tPrepare chain", EMsgType.INFO);
|
||||
// Collect and encode NSP files list
|
||||
try {
|
||||
for (File nspFile : filesList)
|
||||
|
@ -47,94 +46,106 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
}
|
||||
catch (UnsupportedEncodingException uee){
|
||||
isValid = false;
|
||||
logPrinter.print("NET: Unsupported encoding for 'URLEncoder'. Internal issue you can't fix. Please report.", EMsgType.FAIL);
|
||||
logPrinter.print("NET: Unsupported encoding for 'URLEncoder'. Internal issue you can't fix. Please report. . Returned:\n\t"+uee.getMessage(), EMsgType.FAIL);
|
||||
for (File nspFile : filesList)
|
||||
nspMap.put(nspFile.getName(), nspFile);
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
}
|
||||
// Resolve IP
|
||||
DatagramSocket socket;
|
||||
try{ // todo: check other method if internet unavaliable
|
||||
socket = new DatagramSocket();
|
||||
socket.connect(InetAddress.getByName("8.8.8.8"), 10002); // Google
|
||||
hostIP = socket.getLocalAddress().getHostAddress();
|
||||
socket.close();
|
||||
}
|
||||
catch (SocketException | UnknownHostException e){
|
||||
logPrinter.print("NET: Can't get your computer IP using Google DNS server", EMsgType.INFO);
|
||||
try {
|
||||
if (hostIPaddr.isEmpty()) {
|
||||
DatagramSocket socket;
|
||||
try { // todo: check other method if internet unavaliable
|
||||
socket = new DatagramSocket();
|
||||
socket.connect(InetAddress.getByName("193.0.14.129"), 10002); // RIPE NCC
|
||||
socket.connect(InetAddress.getByName("8.8.8.8"), 10002); // Google
|
||||
hostIP = socket.getLocalAddress().getHostAddress();
|
||||
socket.close();
|
||||
}
|
||||
catch (SocketException | UnknownHostException e1){
|
||||
logPrinter.print("NET: Can't get your computer IP using RIPE NCC root server", EMsgType.INFO);
|
||||
} catch (SocketException | UnknownHostException e) {
|
||||
logPrinter.print("NET: Can't get your computer IP using Google DNS server. Returned:\n\t"+e.getMessage(), EMsgType.INFO);
|
||||
try {
|
||||
socket = new DatagramSocket();
|
||||
socket.connect(InetAddress.getByName("people.com.cn"), 10002); // Renmin Ribao
|
||||
socket.connect(InetAddress.getByName("193.0.14.129"), 10002); // RIPE NCC
|
||||
hostIP = socket.getLocalAddress().getHostAddress();
|
||||
socket.close();
|
||||
}
|
||||
catch (SocketException | UnknownHostException e2){
|
||||
logPrinter.print("NET: Can't get your computer IP using Renmin Ribao server", EMsgType.FAIL);
|
||||
logPrinter.print("Try using 'Expert mode' and set IP by yourself", EMsgType.INFO);
|
||||
} catch (SocketException | UnknownHostException e1) {
|
||||
logPrinter.print("NET: Can't get your computer IP using RIPE NCC root server. Returned:\n\t"+e1.getMessage(), EMsgType.INFO);
|
||||
try {
|
||||
Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
NetworkInterface n = (NetworkInterface) enumeration.nextElement();
|
||||
Enumeration enumeration1 = n.getInetAddresses();
|
||||
while (enumeration1.hasMoreElements()) {
|
||||
InetAddress i = (InetAddress) enumeration1.nextElement();
|
||||
logPrinter.print("Check for: "+i.getHostAddress(), EMsgType.INFO);
|
||||
socket = new DatagramSocket();
|
||||
socket.connect(InetAddress.getByName("people.com.cn"), 10002); // Renmin Ribao
|
||||
hostIP = socket.getLocalAddress().getHostAddress();
|
||||
socket.close();
|
||||
} catch (SocketException | UnknownHostException e2) {
|
||||
logPrinter.print("NET: Can't get your computer IP using Renmin Ribao server. Returned:\n\t"+e2.getMessage(), EMsgType.FAIL);
|
||||
logPrinter.print("Try using 'Expert mode' and set IP manually.", EMsgType.INFO);
|
||||
try {
|
||||
Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
NetworkInterface n = (NetworkInterface) enumeration.nextElement();
|
||||
Enumeration enumeration1 = n.getInetAddresses();
|
||||
while (enumeration1.hasMoreElements()) {
|
||||
InetAddress i = (InetAddress) enumeration1.nextElement();
|
||||
logPrinter.print("Check for: " + i.getHostAddress(), EMsgType.INFO);
|
||||
}
|
||||
}
|
||||
} catch (SocketException socketException) { // Good block.
|
||||
logPrinter.print("Can't determine possible variants. Returned:\n\t"+socketException.getMessage(), EMsgType.FAIL);
|
||||
}
|
||||
} catch (SocketException socketException) {
|
||||
logPrinter.print("Can't determine possible variants.", EMsgType.FAIL);
|
||||
isValid = false;
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
}
|
||||
isValid = false;
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
logPrinter.print("NET: Your IP detected as: " + hostIP, EMsgType.PASS);
|
||||
}
|
||||
else {
|
||||
this.hostIP = hostIPaddr;
|
||||
logPrinter.print("NET: Your IP defined as: " + hostIP, EMsgType.PASS);
|
||||
}
|
||||
|
||||
// Get port
|
||||
if (hostPortNum.isEmpty()) {
|
||||
Random portRandomizer = new Random();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
try {
|
||||
this.hostPort = portRandomizer.nextInt(999) + 6000;
|
||||
serverSocket = new ServerSocket(hostPort); //System.out.println(serverSocket.getInetAddress()); 0.0.0.0
|
||||
logPrinter.print("NET: Your port detected as: " + hostPort, EMsgType.PASS);
|
||||
break;
|
||||
} catch (IOException ioe) {
|
||||
if (i == 4) {
|
||||
logPrinter.print("NET: Can't find good port", EMsgType.FAIL);
|
||||
logPrinter.print("Try using 'Expert mode' and set port by yourself.", EMsgType.INFO);
|
||||
isValid = false;
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
} else
|
||||
logPrinter.print("NET: Can't use port " + hostPort + "\nLooking for another one.", EMsgType.WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
logPrinter.print("NET: Your IP detected as: "+hostIP, EMsgType.PASS);
|
||||
// Detect port
|
||||
Random portRandomizer = new Random();
|
||||
for (int i=0; i<5; i++){
|
||||
else {
|
||||
try {
|
||||
this.hostPort = portRandomizer.nextInt(999)+6000;
|
||||
serverSocket = new ServerSocket(hostPort); //System.out.println(serverSocket.getInetAddress()); 0.0.0.0
|
||||
logPrinter.print("NET: Your port detected as: "+hostPort, EMsgType.PASS);
|
||||
break;
|
||||
this.hostPort = Integer.parseInt(hostPortNum);
|
||||
logPrinter.print("NET: Using defined port number: " + hostPort, EMsgType.PASS);
|
||||
}
|
||||
catch (IOException ioe){
|
||||
if (i==4){
|
||||
logPrinter.print("NET: Can't find good port", EMsgType.FAIL);
|
||||
logPrinter.print("Try using 'Expert mode' and set port by yourself.", EMsgType.INFO);
|
||||
isValid = false;
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
}
|
||||
else
|
||||
logPrinter.print("NET: Can't use port "+hostPort+"\nLooking for another one.", EMsgType.WARNING);
|
||||
catch (NumberFormatException exeption){ // Literally never happens.
|
||||
isValid = false;
|
||||
logPrinter.print("NET: Can't use port defined in settings: " + hostPortNum + "\nIt's not a valid number!", EMsgType.WARNING);
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
isValid = true;
|
||||
}
|
||||
/**
|
||||
* Do it hard way (expert mode selected)
|
||||
* Override cancel block to close connection by ourselves
|
||||
* */
|
||||
public NETCommunications(List<File> filesList, String switchIP, boolean doNotServeRequests, String hostIP, int hostPort, String extras){
|
||||
this.doNotServeRequests = doNotServeRequests;
|
||||
if (doNotServeRequests)
|
||||
this.extras = extras;
|
||||
else
|
||||
this.extras = "";
|
||||
this.switchIP = switchIP;
|
||||
@Override
|
||||
protected void cancelled() {
|
||||
this.close(EFileStatus.UNKNOWN);
|
||||
super.cancelled();
|
||||
}
|
||||
|
||||
/*
|
||||
Replace everything to ASCII (WEB representation)
|
||||
calculate
|
||||
|
@ -142,16 +153,10 @@ write in first 4 bytes
|
|||
* */
|
||||
@Override
|
||||
protected Void call() {
|
||||
if (!isValid)
|
||||
if (!isValid | isCancelled())
|
||||
return null;
|
||||
if (isCancelled()){
|
||||
logPrinter.print("NET: interrupted by user", EMsgType.INFO);
|
||||
close(EFileStatus.FAILED);
|
||||
return null;
|
||||
}
|
||||
|
||||
logPrinter.print("\tStart chain", EMsgType.INFO);
|
||||
// Get files list length
|
||||
// Create string that we'll send to TF and which initiates chain
|
||||
StringBuilder myStrBuilder;
|
||||
|
||||
myStrBuilder = new StringBuilder();
|
||||
|
@ -179,9 +184,17 @@ write in first 4 bytes
|
|||
handShakeSocket.close();
|
||||
}
|
||||
catch (IOException uhe){
|
||||
uhe.printStackTrace(); // TODO: FIX: could be [UnknownHostException]
|
||||
logPrinter.print("NET: Unable to connect to NS and send files list. Returned:\n\t"+uhe.getMessage(), EMsgType.FAIL);
|
||||
close(EFileStatus.UNKNOWN);
|
||||
return null;
|
||||
}
|
||||
// Check if we should serve requests
|
||||
if (this.doNotServeRequests){
|
||||
logPrinter.print("NET: List of files transferred. Replies won't be served.", EMsgType.PASS);
|
||||
close(EFileStatus.UNKNOWN);
|
||||
return null;
|
||||
}
|
||||
logPrinter.print("NET: Initiation files list has been sent to NS.", EMsgType.PASS);
|
||||
|
||||
// Go transfer
|
||||
try {
|
||||
|
@ -198,8 +211,8 @@ write in first 4 bytes
|
|||
String line;
|
||||
LinkedList<String> tcpPackeet = new LinkedList<>();
|
||||
while ((line = br.readLine()) != null) {
|
||||
if (isCancelled()) // TODO: notice everywhere
|
||||
break;
|
||||
// if (isCancelled()) // TODO: notice everywhere
|
||||
// break;
|
||||
System.out.println(line); // TODO: remove DBG
|
||||
if (line.trim().isEmpty()) { // If TCP packet is ended
|
||||
handleRequest(tcpPackeet, pw); // Proceed required things
|
||||
|
@ -210,13 +223,12 @@ write in first 4 bytes
|
|||
}
|
||||
System.out.println("\nDone!"); // reopen client sock
|
||||
clientSocket.close();
|
||||
serverSocket.close();
|
||||
}
|
||||
catch (IOException ioe){
|
||||
ioe.printStackTrace(); // TODO: fix
|
||||
System.out.println("client sock closed");
|
||||
}
|
||||
logPrinter.update(nspMap, EFileStatus.UNKNOWN);
|
||||
logPrinter.close();
|
||||
if (!isCancelled())
|
||||
close(EFileStatus.UNKNOWN);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -271,11 +283,12 @@ write in first 4 bytes
|
|||
* Close when done
|
||||
* */
|
||||
private void close(EFileStatus status){
|
||||
System.out.println("called");
|
||||
try {
|
||||
serverSocket.close();
|
||||
logPrinter.print("NET: Closing server socket.", EMsgType.PASS);
|
||||
}
|
||||
catch (IOException ioe){
|
||||
catch (IOException | NullPointerException ioe){
|
||||
logPrinter.print("NET: Closing server socket failed. Sometimes it's not an issue.", EMsgType.WARNING);
|
||||
}
|
||||
if (status != null) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue