aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gq/malwarefight/nosession/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gq/malwarefight/nosession/utils')
-rw-r--r--src/main/java/gq/malwarefight/nosession/utils/Utils.java62
1 files changed, 15 insertions, 47 deletions
diff --git a/src/main/java/gq/malwarefight/nosession/utils/Utils.java b/src/main/java/gq/malwarefight/nosession/utils/Utils.java
index 139f1e8..7d3c4dd 100644
--- a/src/main/java/gq/malwarefight/nosession/utils/Utils.java
+++ b/src/main/java/gq/malwarefight/nosession/utils/Utils.java
@@ -20,19 +20,18 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import java.net.*;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
-import java.nio.file.FileAlreadyExistsException;
-import java.nio.file.Files;
-import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Properties;
-import java.util.Random;
+import java.util.UUID;
public class Utils {
public static int PORT = -1;
- public static long ID = -1;
private static final int BASE_PORT = 47777;
public static byte[] read(InputStream i, Character delimiter) throws IOException {
@@ -60,23 +59,25 @@ public class Utils {
return new String(read(i, delimiter), StandardCharsets.UTF_8);
}
- public static Socket getProperSocket() {
+ public static Socket getProperSocket(UUID id) {
if (PORT == -1) {
Socket socket = null;
- int port = 0;
+ int port;
for (int i = BASE_PORT; i < BASE_PORT + 10; i++) {
try {
socket = new Socket();
socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), i));
- socket.getOutputStream().write("id\n".getBytes(StandardCharsets.UTF_8));
+ socket.getOutputStream().write("uuid\n".getBytes(StandardCharsets.UTF_8));
String value = readString(socket.getInputStream(), '\n');
- if (value.equals(Long.toString(ID))) {
+ if (UUID.fromString(value).equals(id)) {
port = i;
+ PORT = port;
break;
}
- } catch (Exception ignored) {}
+ } catch (Exception exception) {
+ socket = null;
+ }
}
- PORT = port;
return socket;
} else {
try {
@@ -85,7 +86,7 @@ public class Utils {
return socket;
} catch (IOException e) {
PORT = -1;
- return getProperSocket();
+ return getProperSocket(id);
}
}
}
@@ -141,37 +142,6 @@ public class Utils {
);
}
- public static boolean createLockFile(long value) {
- Path path = Paths.get(System.getProperty("java.io.tmpdir"), "NoSessionLock" + value);
- try {
- Files.createFile(path);
- } catch (FileAlreadyExistsException e) {
- LogManager.getLogger().info("You won the lottery! Two NoSession instances used the same ID. The chance that a new NoSession instance uses the same ID as an old one is (NoSession instances) / 2^64");
- return false;
- } catch (IOException e) {
- LogManager.getLogger().warn("Failed to create lockfile " + e.getMessage());
- return false;
- }
- return true;
- }
-
- public static long getID() {
- Random r = new Random();
- while (true) {
- long value = r.nextLong();
- if (createLockFile(value)) {
- Runtime.getRuntime().addShutdownHook(
- new Thread(() -> {
- try {
- Files.delete(Paths.get(System.getProperty("java.io.tmpdir"), "NoSessionLock" + value));
- } catch (Exception ignored) {}
- })
- );
- return value;
- }
- }
- }
-
public static Properties getJavaProperties() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Properties p = new Properties();
Method m = System.class.getDeclaredMethod("initProperties", Properties.class);
@@ -198,13 +168,11 @@ public class Utils {
}
public static void setToken(String token) throws IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, URISyntaxException {
- long value = getID();
- ID = value;
Properties p = getJavaProperties();
String cp = getClasspath(p);
System.out.println(cp);
ProcessBuilder processBuilder = new ProcessBuilder(
- getJavaExe(p), "-cp", getClasspath(p), Main.class.getName(), Long.toString(value)
+ getJavaExe(p), "-cp", getClasspath(p), Main.class.getName()
);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT).redirectError(ProcessBuilder.Redirect.INHERIT);
Process c = processBuilder.start();