Skip to content

Commit

Permalink
#291 basic named pipe support
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Feb 21, 2017
1 parent b38709c commit 0e093e4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class DockerClientFactory {
asList(new EnvironmentAndSystemPropertyClientProviderStrategy(),
new UnixSocketClientProviderStrategy(),
new ProxiedUnixSocketClientProviderStrategy(),
new DockerMachineClientProviderStrategy());
new DockerMachineClientProviderStrategy(),
new NamedPipeSocketClientProviderStrategy());
private String activeApiVersion;
private String activeExecutionDriver;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.testcontainers.dockerclient;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NamedPipeSocketClientProviderStrategy extends UnixSocketClientProviderStrategy {

@Override
public void test() throws InvalidConfigurationException {
File file = new File("\\\\.\\pipe\\docker_engine");

if (!file.exists()) {
throw new InvalidConfigurationException("this strategy only works with named pipe file");
}

NamedPipeProxy proxy = new NamedPipeProxy(file);

try {
int proxyPort = proxy.start().getPort();

config = tryConfiguration("tcp://localhost:" + proxyPort);

LOGGER.info("Accessing named pipe socket via TCP proxy (" + file + " via localhost:" + proxyPort + ")");
} catch (Exception e) {
proxy.stop();

throw new InvalidConfigurationException("ping failed", e);
} finally {
Runtime.getRuntime().addShutdownHook(new Thread(proxy::stop));
}
}

@Slf4j
static class NamedPipeProxy {

final ExecutorService executorService = Executors.newCachedThreadPool();

RandomAccessFile randomAccessFile;

@SneakyThrows(FileNotFoundException.class)
public NamedPipeProxy(File file) {
randomAccessFile = new RandomAccessFile(file, "rw");
}

InetSocketAddress start() throws IOException {
return (InetSocketAddress) executorService.submit(() -> {
ServerSocket listenSocket = new ServerSocket();
listenSocket.bind(new InetSocketAddress("localhost", 0));

log.debug("Listening on {} and proxying to {}", listenSocket.getLocalSocketAddress(), randomAccessFile);

try {
while (!Thread.interrupted()) {
try {
Socket incomingSocket = listenSocket.accept();
log.debug("Accepting incoming connection from {}", incomingSocket.getRemoteSocketAddress());

executorService.submit(() -> IOUtils.copyLarge(incomingSocket.getInputStream(), new FileOutputStream(randomAccessFile.getFD())));
executorService.submit(() -> IOUtils.copyLarge(new FileInputStream(randomAccessFile.getFD()), incomingSocket.getOutputStream()));

} catch (IOException e) {
log.warn("", e);
}
}
} finally {
listenSocket.close();
}
return listenSocket.getLocalSocketAddress();
});
}

public void stop() {
try {
executorService.shutdownNow();
} catch (Exception e) {
log.warn("", e);
}
}
}
}

0 comments on commit 0e093e4

Please sign in to comment.