From 61ce495482ab5197dc87e6b26f42fb437fdd64ff Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 18 Jul 2020 16:06:08 -0400 Subject: [PATCH] proxy progress --- java_console/ui/src/test/java/com/rusefi/ServerTest.java | 9 ++++----- .../src/main/java/com/rusefi/server/Backend.java | 6 +++--- .../src/main/java/com/rusefi/server/BackendLauncher.java | 3 +-- .../com/rusefi/server/ControllerConnectionState.java | 8 +++++--- .../java/com/rusefi/server/JsonUserDetailsResolver.java | 3 +-- .../main/java/com/rusefi/server/UserDetailsResolver.java | 6 ++++++ 6 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 java_tools/proxy_server/src/main/java/com/rusefi/server/UserDetailsResolver.java diff --git a/java_console/ui/src/test/java/com/rusefi/ServerTest.java b/java_console/ui/src/test/java/com/rusefi/ServerTest.java index 942f8b3a1f..080b22a193 100644 --- a/java_console/ui/src/test/java/com/rusefi/ServerTest.java +++ b/java_console/ui/src/test/java/com/rusefi/ServerTest.java @@ -22,7 +22,6 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.function.Function; import static com.rusefi.TestHelper.createIniField; import static com.rusefi.TestHelper.prepareImage; @@ -50,7 +49,7 @@ public class ServerTest { public void testControllerSessionTimeout() throws InterruptedException, IOException { int serverPortForControllers = 7000; int httpPort = 8000; - Function userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), authToken.charAt(6)); + UserDetailsResolver userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), authToken.charAt(6)); CountDownLatch serverCreated = new CountDownLatch(1); CountDownLatch allClientsDisconnected = new CountDownLatch(1); @@ -92,7 +91,7 @@ public class ServerTest { @Test public void testInvalidApplicationRequest() throws InterruptedException, IOException { - Function userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), authToken.charAt(6)); + UserDetailsResolver userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), authToken.charAt(6)); int httpPort = 8001; int serverPortForRemoteUsers = 6801; CountDownLatch disconnectedCountDownLatch = new CountDownLatch(1); @@ -120,7 +119,7 @@ public class ServerTest { public void testAuthenticatorRequestUnknownSession() throws InterruptedException, IOException { int serverPortForRemoteUsers = 6800; - Function userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), authToken.charAt(6)); + UserDetailsResolver userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), authToken.charAt(6)); int httpPort = 8001; CountDownLatch disconnectedCountDownLatch = new CountDownLatch(1); @@ -157,7 +156,7 @@ public class ServerTest { CountDownLatch controllerRegistered = new CountDownLatch(1); - Function userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), userId); + UserDetailsResolver userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), userId); int httpPort = 8001; Backend backend = new Backend(userDetailsResolver, httpPort, logger) { @Override diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java index 7f73aa68a9..681b6c9ebd 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java @@ -42,10 +42,10 @@ public class Backend { // guarded by clients private HashMap byId = new HashMap<>(); // private final int clientTimeout; - private final Function userDetailsResolver; + private final UserDetailsResolver userDetailsResolver; private final Logger logger; - public Backend(Function userDetailsResolver, int httpPort, Logger logger) { + public Backend(UserDetailsResolver userDetailsResolver, int httpPort, Logger logger) { // this.clientTimeout = clientTimeout; this.userDetailsResolver = userDetailsResolver; this.logger = logger; @@ -166,7 +166,7 @@ public class Backend { return new RsJson(builder.build()); } - public Function getUserDetailsResolver() { + public UserDetailsResolver getUserDetailsResolver() { return userDetailsResolver; } diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/BackendLauncher.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/BackendLauncher.java index 46fa23f431..9dd49add44 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/BackendLauncher.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/BackendLauncher.java @@ -4,7 +4,6 @@ import com.opensr5.Logger; import java.io.File; import java.net.MalformedURLException; -import java.util.function.Function; public class BackendLauncher { /** @@ -13,7 +12,7 @@ public class BackendLauncher { public static void start(String[] args) throws MalformedURLException { rusEFISSLContext.setupCertificates(new File("keystore.jks"), System.getProperty("RUSEFI_PROXY_PASSWORD")); - Function userDetailsFunction = new JsonUserDetailsResolver(); + UserDetailsResolver userDetailsFunction = new JsonUserDetailsResolver(); Backend backend = new Backend(userDetailsFunction, Backend.HTTP_PORT, Logger.CONSOLE); backend.runApplicationConnector(Backend.SERVER_PORT_FOR_APPLICATIONS, parameter -> { diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java index d6a24f2199..c1653c33af 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java @@ -11,12 +11,11 @@ import com.rusefi.io.tcp.TcpIoStream; import java.io.Closeable; import java.io.IOException; import java.net.Socket; -import java.util.function.Function; public class ControllerConnectionState { private final Socket clientSocket; private final Logger logger; - private final Function userDetailsResolver; + private final UserDetailsResolver userDetailsResolver; private boolean isClosed; private IoStream stream; @@ -31,7 +30,7 @@ public class ControllerConnectionState { private UserDetails userDetails; private ControllerKey controllerKey; - public ControllerConnectionState(Socket clientSocket, Logger logger, Function userDetailsResolver) { + public ControllerConnectionState(Socket clientSocket, Logger logger, UserDetailsResolver userDetailsResolver) { this.clientSocket = clientSocket; this.logger = logger; this.userDetailsResolver = userDetailsResolver; @@ -71,6 +70,9 @@ public class ControllerConnectionState { logger.info(sessionDetails.getAuthToken() + " New client: " + sessionDetails.getControllerInfo()); userDetails = userDetailsResolver.apply(sessionDetails.getAuthToken()); + if (userDetails == null) { + throw new IOException("Unable to resolve " + sessionDetails.getAuthToken()); + } controllerKey = new ControllerKey(userDetails.getUserId(), sessionDetails.getControllerInfo()); logger.info("User " + userDetails); } diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/JsonUserDetailsResolver.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/JsonUserDetailsResolver.java index 36b6e1edf9..dfbb893eb6 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/JsonUserDetailsResolver.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/JsonUserDetailsResolver.java @@ -7,9 +7,8 @@ import org.json.simple.JSONObject; import org.json.simple.parser.ParseException; import java.io.IOException; -import java.util.function.Function; -public class JsonUserDetailsResolver implements Function { +public class JsonUserDetailsResolver implements UserDetailsResolver { @Override @Nullable public UserDetails apply(String authToken) { diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/UserDetailsResolver.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/UserDetailsResolver.java new file mode 100644 index 0000000000..e2c567c9e7 --- /dev/null +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/UserDetailsResolver.java @@ -0,0 +1,6 @@ +package com.rusefi.server; + +import java.util.function.Function; + +public interface UserDetailsResolver extends Function { +}