From 4b71e07b3fd63282aef05f8dca259da09ce0b1c8 Mon Sep 17 00:00:00 2001 From: rusefi Date: Thu, 23 Jul 2020 20:52:50 -0400 Subject: [PATCH] proxy progress - getting REALLY close?! --- .../com/rusefi/LocalApplicationProxy.java | 13 +++++---- .../main/java/com/rusefi/io/ByteReader.java | 2 +- .../com/rusefi/tools/online/HttpUtil.java | 7 +++-- .../com/rusefi/tools/online/ProxyClient.java | 3 +- .../main/java/com/rusefi/server/Backend.java | 5 +++- .../server/JsonUserDetailsResolver.java | 3 +- .../java/com/rusefi/ts_plugin/RemoteTab.java | 28 +++++++++++++------ 7 files changed, 40 insertions(+), 21 deletions(-) diff --git a/java_console/io/src/main/java/com/rusefi/LocalApplicationProxy.java b/java_console/io/src/main/java/com/rusefi/LocalApplicationProxy.java index 2d4f6a2a0d..8f50b1fd65 100644 --- a/java_console/io/src/main/java/com/rusefi/LocalApplicationProxy.java +++ b/java_console/io/src/main/java/com/rusefi/LocalApplicationProxy.java @@ -27,21 +27,22 @@ public class LocalApplicationProxy { /** * @param serverPortForRemoteUsers port on which rusEFI proxy accepts authenticator connections * @param applicationRequest remote session we want to connect to - * @param authenticatorPort local port we would bind for TunerStudio to connect to - * @param httpPort + * @param localApplicationPort local port we would bind for TunerStudio to connect to + * @param jsonHttpPort */ - public static ServerHolder startAndRun(Logger logger, int serverPortForRemoteUsers, ApplicationRequest applicationRequest, int authenticatorPort, int httpPort) throws IOException { - HttpResponse httpResponse = HttpUtil.executeGet(ProxyClient.getHttpAddress(httpPort) + ProxyClient.VERSION_PATH); + public static ServerHolder startAndRun(Logger logger, int serverPortForRemoteUsers, ApplicationRequest applicationRequest, int localApplicationPort, int jsonHttpPort) throws IOException { + HttpResponse httpResponse = HttpUtil.executeGet(logger,ProxyClient.getHttpAddress(jsonHttpPort) + ProxyClient.VERSION_PATH); String version = HttpUtil.getResponse(httpResponse); - logger.info("Version=" + version); + logger.info("Server says version=" + version); if (!version.contains(ProxyClient.BACKEND_VERSION)) throw new IOException("Unexpected backend version " + version + " while we want " + ProxyClient.BACKEND_VERSION); IoStream authenticatorToProxyStream = new TcpIoStream("authenticatorToProxyStream ", logger, rusEFISSLContext.getSSLSocket(HttpUtil.RUSEFI_PROXY_HOSTNAME, serverPortForRemoteUsers)); LocalApplicationProxy localApplicationProxy = new LocalApplicationProxy(logger, applicationRequest); + logger.info("Pushing " + applicationRequest); localApplicationProxy.run(authenticatorToProxyStream); - return BinaryProtocolProxy.createProxy(logger, authenticatorToProxyStream, authenticatorPort); + return BinaryProtocolProxy.createProxy(logger, authenticatorToProxyStream, localApplicationPort); } public void run(IoStream authenticatorToProxyStream) throws IOException { diff --git a/java_console/io/src/main/java/com/rusefi/io/ByteReader.java b/java_console/io/src/main/java/com/rusefi/io/ByteReader.java index 4942d9a057..d615ccf0c4 100644 --- a/java_console/io/src/main/java/com/rusefi/io/ByteReader.java +++ b/java_console/io/src/main/java/com/rusefi/io/ByteReader.java @@ -33,7 +33,7 @@ public interface ByteReader { throw new IOException("TcpIoStream: End of input?"); listener.onDataArrived(Arrays.copyOf(inputBuffer, result)); } catch (IOException e) { - System.err.println("TcpIoStream: End of connection"); + logger.error("TcpIoStream: End of connection " + e); return; } } diff --git a/java_console/io/src/main/java/com/rusefi/tools/online/HttpUtil.java b/java_console/io/src/main/java/com/rusefi/tools/online/HttpUtil.java index 702d656aac..ac7404ba72 100644 --- a/java_console/io/src/main/java/com/rusefi/tools/online/HttpUtil.java +++ b/java_console/io/src/main/java/com/rusefi/tools/online/HttpUtil.java @@ -1,5 +1,6 @@ package com.rusefi.tools.online; +import com.opensr5.Logger; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; @@ -33,13 +34,13 @@ public class HttpUtil { public static String getResponse(HttpResponse response) throws IOException { HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); - System.out.println("responseString=" + responseString); + Logger.CONSOLE.info("responseString=" + responseString); return responseString; } - public static HttpResponse executeGet(String url) throws IOException { + public static HttpResponse executeGet(Logger logger, String url) throws IOException { HttpClient httpclient = new DefaultHttpClient(); - System.out.println("Connecting to " + url); + logger.info("GET " + url); HttpGet httpget = new HttpGet(url); return httpclient.execute(httpget); } diff --git a/java_console/io/src/main/java/com/rusefi/tools/online/ProxyClient.java b/java_console/io/src/main/java/com/rusefi/tools/online/ProxyClient.java index c019192b5f..b145623a0f 100644 --- a/java_console/io/src/main/java/com/rusefi/tools/online/ProxyClient.java +++ b/java_console/io/src/main/java/com/rusefi/tools/online/ProxyClient.java @@ -1,5 +1,6 @@ package com.rusefi.tools.online; +import com.opensr5.Logger; import com.rusefi.server.ControllerInfo; import com.rusefi.server.UserDetails; import org.apache.http.HttpResponse; @@ -31,7 +32,7 @@ public class ProxyClient { @NotNull public static List getOnlineApplications(String url) throws IOException { - HttpResponse httpResponse = HttpUtil.executeGet(url); + HttpResponse httpResponse = HttpUtil.executeGet(Logger.CONSOLE, url); List userLists = new ArrayList<>(); try { 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 1f015308ea..cc231aa94b 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 @@ -146,6 +146,7 @@ public class Backend implements Closeable { // authenticator pushed hello packet on connect System.out.println("Starting application connector at " + serverPortForApplications); BinaryProtocolServer.tcpServerSocket(logger, applicationSocket -> () -> { + logger.info("new application connection!"); totalSessions.incrementAndGet(); // connection from authenticator app which proxies for Tuner Studio IoStream applicationClientStream = null; @@ -155,8 +156,10 @@ public class Backend implements Closeable { // authenticator pushed hello packet on connect String jsonString = HelloCommand.getHelloResponse(applicationClientStream.getDataBuffer(), logger); - if (jsonString == null) + if (jsonString == null) { + logger.info("ERROR: null HELLO"); return; + } ApplicationRequest applicationRequest = ApplicationRequest.valueOf(jsonString); logger.info("Application Connected: " + applicationRequest); String authToken = applicationRequest.getSessionDetails().getAuthToken(); 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 dfbb893eb6..a0e4387730 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 @@ -1,5 +1,6 @@ package com.rusefi.server; +import com.opensr5.Logger; import com.rusefi.tools.online.HttpUtil; import org.apache.http.HttpResponse; import org.jetbrains.annotations.Nullable; @@ -14,7 +15,7 @@ public class JsonUserDetailsResolver implements UserDetailsResolver { public UserDetails apply(String authToken) { try { - HttpResponse response = HttpUtil.executeGet(HttpUtil.RUSEFI_ONLINE_JSON_API_PREFIX + "getUserByToken&rusefi_token=" + authToken); + HttpResponse response = HttpUtil.executeGet(Logger.CONSOLE,HttpUtil.RUSEFI_ONLINE_JSON_API_PREFIX + "getUserByToken&rusefi_token=" + authToken); JSONObject json = HttpUtil.getJsonResponse(response); System.out.println("String " + json); Object getUserByToken = json.get("getUserByToken"); diff --git a/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/RemoteTab.java b/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/RemoteTab.java index 27286cecf9..e09604f41c 100644 --- a/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/RemoteTab.java +++ b/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/RemoteTab.java @@ -17,8 +17,6 @@ import org.putgemin.VerticalFlowLayout; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.io.IOException; import java.util.List; import java.util.concurrent.Executor; @@ -34,7 +32,14 @@ public class RemoteTab { private final JComponent content = new JPanel(new BorderLayout()); private final JPanel list = new JPanel(new VerticalFlowLayout()); - private final JTextField oneTimePasswordControl = new JTextField(); + private final JTextField oneTimePasswordControl = new JTextField("0") { + @Override + public Dimension getPreferredSize() { + Dimension size = super.getPreferredSize(); + // todo: dynamic calculation of desired with based on String width? + return new Dimension(100, size.height); + } + }; private final Executor listDownloadExecutor = Executors.newSingleThreadExecutor(new NamedThreadFactory("online list downloader")); @@ -42,7 +47,14 @@ public class RemoteTab { JButton refresh = new JButton("Refresh List"); refresh.addActionListener(e -> requestListDownload()); - JTextField applicationPort = new JTextField(); + JTextField applicationPort = new JTextField() { + @Override + public Dimension getPreferredSize() { + Dimension size = super.getPreferredSize(); + // todo: dynamic calculation of desired with based on String width? + return new Dimension(100, size.height); + } + }; String portProperty = getLocalPort(); applicationPort.setText(portProperty); @@ -59,14 +71,14 @@ public class RemoteTab { } private String getLocalPort() { - return getConfig().getRoot().getProperty(APPLICATION_PORT, Integer.toString(LocalApplicationProxy.SERVER_PORT_FOR_APPLICATIONS)); + return getConfig().getRoot().getProperty(APPLICATION_PORT, "8100"); } private void requestListDownload() { listDownloadExecutor.execute(() -> { List userDetails; try { - userDetails = ProxyClient.getOnlineApplications(HttpUtil.RUSEFI_PROXY_JSON_PROTOCOL); + userDetails = ProxyClient.getOnlineApplications(HttpUtil.PROXY_JSON_API_HTTP_PORT); SwingUtilities.invokeLater(() -> showList(userDetails)); } catch (IOException e) { e.printStackTrace(); @@ -104,8 +116,8 @@ public class RemoteTab { LocalApplicationProxy.startAndRun(Logger.CONSOLE, LocalApplicationProxy.SERVER_PORT_FOR_APPLICATIONS, applicationRequest, - HttpUtil.PROXY_JSON_API_HTTP_PORT, - Integer.parseInt(getLocalPort())); + Integer.parseInt(getLocalPort()), + HttpUtil.PROXY_JSON_API_HTTP_PORT); } catch (IOException e) { // todo: proper handling e.printStackTrace();