now with some SSL

This commit is contained in:
rusefi 2020-07-17 21:36:48 -04:00
parent 5caf428d2f
commit 1bcd038ea6
5 changed files with 49 additions and 34 deletions

View File

@ -8,6 +8,7 @@ import com.rusefi.config.generated.Fields;
import com.rusefi.io.LinkManager;
import com.rusefi.io.commands.HelloCommand;
import javax.net.ssl.SSLServerSocketFactory;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
@ -34,6 +35,23 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
public AtomicInteger unknownCommands = new AtomicInteger();
public static final Function<Integer, ServerSocket> SECURE_SOCKET_FACTORY = port -> {
try {
return SSLServerSocketFactory.getDefault().createServerSocket(port);
} catch (IOException e) {
throw new IllegalStateException("Error binding secure server socket " + port, e);
}
};
public static final Function<Integer, ServerSocket> PLAIN_SOCKET_FACTORY = port -> {
try {
return new ServerSocket(port);
} catch (IOException e) {
throw new IllegalStateException("Error binding server socket " + port, e);
}
};
public BinaryProtocolServer(Logger logger) {
this.logger = logger;
}
@ -61,35 +79,29 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
*
* @param port server port to accept connections
* @param threadName
* @param clientSocketRunnableFactory method to invoke on a new thread for each new client connection
* @param socketRunnableFactory method to invoke on a new thread for each new client connection
* @param logger
* @param serverSocketCreationCallback this callback is invoked once we open the server socket
*/
public static void tcpServerSocket(int port, String threadName, Function<Socket, Runnable> clientSocketRunnableFactory, final Logger logger, Listener serverSocketCreationCallback) {
Runnable runnable = new Runnable() {
@SuppressWarnings("InfiniteLoopStatement")
@Override
public void run() {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(port, 1);
} catch (IOException e) {
logger.error(threadName + ": Error binding server socket " + port + ": " + e);
throw new IllegalStateException(e);
}
if (serverSocketCreationCallback != null)
serverSocketCreationCallback.onResult(null);
public static void tcpServerSocket(int port, String threadName, Function<Socket, Runnable> socketRunnableFactory, final Logger logger, Listener serverSocketCreationCallback) {
tcpServerSocket(logger, socketRunnableFactory, port, threadName, serverSocketCreationCallback, PLAIN_SOCKET_FACTORY);
}
try {
while (true) {
// Wait for a connection
final Socket clientSocket = serverSocket.accept();
logger.info("Binary protocol proxy port connection");
new Thread(clientSocketRunnableFactory.apply(clientSocket), "proxy connection").start();
}
} catch (IOException e) {
throw new IllegalStateException(e);
public static void tcpServerSocket(Logger logger, Function<Socket, Runnable> clientSocketRunnableFactory, int port, String threadName, Listener serverSocketCreationCallback, Function<Integer, ServerSocket> nonSecureSocketFunction) {
Runnable runnable = () -> {
ServerSocket serverSocket = nonSecureSocketFunction.apply(port);
if (serverSocketCreationCallback != null)
serverSocketCreationCallback.onResult(null);
try {
while (true) {
// Wait for a connection
final Socket clientSocket = serverSocket.accept();
logger.info("Binary protocol proxy port connection");
new Thread(clientSocketRunnableFactory.apply(clientSocket), "proxy connection").start();
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
};
new Thread(runnable, threadName).start();

View File

@ -9,6 +9,7 @@ import com.rusefi.server.ControllerInfo;
import com.rusefi.server.SessionDetails;
import org.jetbrains.annotations.NotNull;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.Socket;
@ -28,7 +29,7 @@ public class NetworkConnector {
SessionDetails deviceSessionDetails = new SessionDetails(ci, authToken, SessionDetails.createOneTimeCode());
BaseBroadcastingThread baseBroadcastingThread = new BaseBroadcastingThread(new Socket(LOCALHOST, serverPortForControllers),
BaseBroadcastingThread baseBroadcastingThread = new BaseBroadcastingThread(SSLSocketFactory.getDefault().createSocket(LOCALHOST, serverPortForControllers),
deviceSessionDetails,
logger) {
@Override

View File

@ -4,6 +4,7 @@ import com.opensr5.Logger;
import com.rusefi.server.ControllerInfo;
import com.rusefi.server.SessionDetails;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.Socket;
@ -27,7 +28,8 @@ public class MockRusEfiDevice {
}
public void connect(int serverPort) throws IOException {
BaseBroadcastingThread baseBroadcastingThread = new BaseBroadcastingThread(new Socket(LOCALHOST, serverPort),
Socket socket = SSLSocketFactory.getDefault().createSocket(LOCALHOST, serverPort);
BaseBroadcastingThread baseBroadcastingThread = new BaseBroadcastingThread(socket,
sessionDetails,
logger);
baseBroadcastingThread.start();

View File

@ -54,7 +54,7 @@ public class ServerTest {
@Test
public void testControllerSessionTimeout() throws InterruptedException, IOException {
int serverPort = 7000;
int serverPortForControllers = 7000;
int httpPort = 8000;
Function<String, UserDetails> userDetailsResolver = authToken -> new UserDetails(authToken.substring(0, 5), authToken.charAt(6));
@ -77,15 +77,15 @@ public class ServerTest {
}
};
backend.runControllerConnector(serverPort, parameter -> serverCreated.countDown());
backend.runControllerConnector(serverPortForControllers, parameter -> serverCreated.countDown());
assertTrue(serverCreated.await(30, TimeUnit.SECONDS));
assertEquals(0, backend.getCount());
new MockRusEfiDevice(MockRusEfiDevice.TEST_TOKEN_1, "rusEFI 2020.07.06.frankenso_na6.2468827536", logger).connect(serverPort);
new MockRusEfiDevice("12345678-1234-1234-1234-123456789012", "rusEFI 2020.07.11.proteus_f4.1986715563", logger).connect(serverPort);
new MockRusEfiDevice(MockRusEfiDevice.TEST_TOKEN_1, "rusEFI 2020.07.06.frankenso_na6.2468827536", logger).connect(serverPortForControllers);
new MockRusEfiDevice("12345678-1234-1234-1234-123456789012", "rusEFI 2020.07.11.proteus_f4.1986715563", logger).connect(serverPortForControllers);
assertTrue(onConnected.await(30, TimeUnit.SECONDS));
assertTrue("onConnected", onConnected.await(30, TimeUnit.SECONDS));
List<ControllerConnectionState> clients = backend.getClients();
assertEquals(2, clients.size());
@ -93,7 +93,7 @@ public class ServerTest {
List<UserDetails> onlineUsers = ProxyClient.getOnlineUsers(httpPort);
assertEquals(2, onlineUsers.size());
assertTrue(allClientsDisconnected.await(30, TimeUnit.SECONDS));
assertTrue("allClientsDisconnected", allClientsDisconnected.await(30, TimeUnit.SECONDS));
}
@Test

View File

@ -122,7 +122,7 @@ public class Backend {
}
public void runControllerConnector(int serverPortForControllers, Listener serverSocketCreationCallback) {
BinaryProtocolServer.tcpServerSocket(serverPortForControllers, "ControllerServer", new Function<Socket, Runnable>() {
BinaryProtocolServer.tcpServerSocket(logger, new Function<Socket, Runnable>() {
@Override
public Runnable apply(Socket controllerSocket) {
return new Runnable() {
@ -141,7 +141,7 @@ public class Backend {
}
};
}
}, logger, serverSocketCreationCallback);
}, serverPortForControllers, "ControllerServer", serverSocketCreationCallback, BinaryProtocolServer.SECURE_SOCKET_FACTORY);
}
@NotNull