Allow unix domain sockets for client/daemon connection (not used by default)

This commit is contained in:
Guillaume Nodet
2021-07-21 22:06:35 +02:00
parent 4b75fec3e1
commit b67ab81c3d
19 changed files with 237 additions and 101 deletions

View File

@@ -108,13 +108,47 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<fork>true</fork>
<compilerArgs>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
</configuration>
</execution>
<execution>
<id>jdk16</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<fork>true</fork>
<compilerArgs>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
<release>16</release>
<multiReleaseOutput>true</multiReleaseOutput>
<compileSourceRoots>
<root>${project.basedir}/src/main/java16</root>
</compileSourceRoots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<fork>true</fork>
<compilerArgs>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.common;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
/**
* Trivial ByteChannel wrapper to avoid the read/write synchronization which
* happens when the channel implements SelectableChannel.
*/
public class ByteChannelWrapper implements ByteChannel {
private final ByteChannel socket;
public ByteChannelWrapper(ByteChannel socket) {
this.socket = socket;
}
@Override
public int read(ByteBuffer dst) throws IOException {
return socket.read(dst);
}
@Override
public int write(ByteBuffer src) throws IOException {
return socket.write(src);
}
@Override
public boolean isOpen() {
return socket.isOpen();
}
@Override
public void close() throws IOException {
socket.close();
}
}

View File

@@ -23,7 +23,7 @@ import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedSelectorException;
import java.nio.channels.SelectionKey;
@@ -47,10 +47,10 @@ public class DaemonConnection implements AutoCloseable {
private final SocketChannel socket;
private final DataInputStream input;
private final DataOutputStream output;
private final InetSocketAddress localAddress;
private final InetSocketAddress remoteAddress;
private final SocketAddress localAddress;
private final SocketAddress remoteAddress;
public DaemonConnection(SocketChannel socket) {
public DaemonConnection(SocketChannel socket) throws IOException {
this.socket = socket;
try {
// NOTE: we use non-blocking IO as there is no reliable way when using blocking IO to shutdown reads while
@@ -61,8 +61,8 @@ public class DaemonConnection implements AutoCloseable {
} catch (IOException e) {
throw new DaemonException.InterruptedException(e);
}
localAddress = (InetSocketAddress) socket.socket().getLocalSocketAddress();
remoteAddress = (InetSocketAddress) socket.socket().getRemoteSocketAddress();
localAddress = socket.getLocalAddress();
remoteAddress = socket.getRemoteAddress();
}
@Override

View File

@@ -32,7 +32,7 @@ public class DaemonInfo {
private final String javaHome;
private final String mvndHome;
private final int pid;
private final int address;
private final String address;
private final byte[] token;
private final String locale;
private final List<String> options;
@@ -41,7 +41,7 @@ public class DaemonInfo {
private final long lastBusy;
public DaemonInfo(String id, String javaHome, String mavenHome,
int pid, int address, byte[] token,
int pid, String address, byte[] token,
String locale, List<String> options,
DaemonState state, long lastIdle, long lastBusy) {
this.id = id;
@@ -73,7 +73,7 @@ public class DaemonInfo {
return pid;
}
public int getAddress() {
public String getAddress() {
return address;
}

View File

@@ -185,7 +185,7 @@ public class DaemonRegistry implements AutoCloseable {
String javaHome = readString();
String mavenHome = readString();
int pid = buffer.getInt();
int address = buffer.getInt();
String address = readString();
byte[] token = new byte[DaemonInfo.TOKEN_SIZE];
buffer.get(token);
@@ -224,7 +224,7 @@ public class DaemonRegistry implements AutoCloseable {
writeString(di.getJavaHome());
writeString(di.getMvndHome());
buffer.putInt(di.getPid());
buffer.putInt(di.getAddress());
writeString(di.getAddress());
buffer.put(di.getToken());
writeString(di.getLocale());
buffer.putInt(di.getOptions().size());

View File

@@ -239,7 +239,11 @@ public enum Environment {
/**
* Log mojos execution time at the end of the build.
*/
MVND_BUILD_TIME("mvnd.buildTime", null, null, OptionType.BOOLEAN, Flags.NONE);
MVND_BUILD_TIME("mvnd.buildTime", null, null, OptionType.BOOLEAN, Flags.NONE),
/**
* Socket family to use
*/
MVND_SOCKET_FAMILY("mvnd.socketFamily", null, "INET", OptionType.STRING, Flags.DISCRIMINATING);
static Properties properties;

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.common;
public class JavaVersion {
private static float javaSpec = 0.0f;
public static float getJavaSpec() {
if (javaSpec <= 0.0f) {
javaSpec = Float.parseFloat(System.getProperty("java.specification.version"));
}
return javaSpec;
}
}

View File

@@ -0,0 +1,180 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.common;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.UnknownHostException;
import java.nio.channels.ByteChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Objects;
import static java.net.StandardProtocolFamily.INET;
import static java.net.StandardProtocolFamily.INET6;
public class SocketHelper {
public static StandardProtocolFamily getSocketFamily(SocketAddress address) {
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
InetAddress ia = isa.getAddress();
if (ia instanceof Inet6Address) {
return INET6;
} else {
return INET;
}
// } else if (address instanceof UnixDomainSocketAddress) {
// return UNIX;
} else {
throw new IllegalArgumentException("Unsupported socket address '" + address + "'");
}
}
public static void checkFamily(StandardProtocolFamily family, SocketAddress address) {
Objects.requireNonNull(family);
Objects.requireNonNull(address);
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
InetAddress ia = isa.getAddress();
if (ia != null
&& !(ia instanceof Inet4Address && family == INET)
&& !(ia instanceof Inet6Address && family == INET6)) {
throw new IllegalArgumentException(
"Socket address '" + address + "' does not match required family '" + family + "'");
}
// } else if (address instanceof UnixDomainSocketAddress) {
// if (family != StandardProtocolFamily.UNIX) {
// throw new IllegalArgumentException("Socket address '" + address + "' does not match required family '" + family + "'");
// }
} else {
throw new IllegalArgumentException(
"Socket address '" + address + "' does not match required family '" + family + "'");
}
}
public static SocketAddress socketAddressFromString(String str) {
if (str.startsWith("inet:")) {
String s = str.substring("inet:".length());
int ic = s.lastIndexOf(':');
String ia = s.substring(0, ic);
int is = ia.indexOf('/');
String h = ia.substring(0, is);
String a = ia.substring(is + 1);
String p = s.substring(ic + 1);
InetAddress addr;
if ("<unresolved>".equals(a)) {
return InetSocketAddress.createUnresolved(h, Integer.parseInt(p));
} else {
if (a.indexOf('.') > 0) {
String[] as = a.split("\\.");
if (as.length != 4) {
throw new IllegalArgumentException("Unsupported address: " + str);
}
byte[] ab = new byte[4];
for (int i = 0; i < 4; i++) {
ab[i] = (byte) Integer.parseInt(as[i]);
}
try {
addr = InetAddress.getByAddress(h.isEmpty() ? null : h, ab);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported address: " + str, e);
}
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
return new InetSocketAddress(addr, Integer.parseInt(p));
}
// } else if (str.startsWith("unix:")) {
// return UnixDomainSocketAddress.of(str.substring("unix:".length()));
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
}
public static String socketAddressToString(SocketAddress address) {
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
String host = isa.getHostString();
InetAddress addr = isa.getAddress();
int port = isa.getPort();
String formatted;
if (addr == null) {
formatted = host + "/<unresolved>";
} else {
formatted = addr.toString();
if (addr instanceof Inet6Address) {
int i = formatted.lastIndexOf("/");
formatted = formatted.substring(0, i + 1) + "[" + formatted.substring(i + 1) + "]";
}
}
return "inet:" + formatted + ":" + port;
// } else if (address instanceof UnixDomainSocketAddress) {
// return "unix:" + address;
} else {
throw new IllegalArgumentException("Unsupported socket address: '" + address + "'");
}
}
public static SocketChannel openSocket(StandardProtocolFamily family) throws IOException {
Objects.requireNonNull(family);
if (family == INET || family == INET6) {
return SocketChannel.open();
} else {
throw new IllegalArgumentException("Unsupported socket family: " + family);
}
// return SocketChannel.open(family);
}
public static ServerSocketChannel openServerSocket(StandardProtocolFamily family) throws IOException {
Objects.requireNonNull(family);
if (family == INET || family == INET6) {
return ServerSocketChannel.open().bind(getLoopbackAddress(family), 0);
} else {
throw new IllegalArgumentException("Unsupported socket family: " + family);
}
// return ServerSocketChannel.open(family).bind(getLoopbackAddress(family), 0);
}
private static SocketAddress getLoopbackAddress(StandardProtocolFamily family) {
try {
Objects.requireNonNull(family);
switch (family) {
case INET:
return new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), 0);
case INET6:
return new InetSocketAddress(
Inet6Address.getByAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }), 0);
// case UNIX:
// return null;
default:
throw new IllegalArgumentException("Unsupported family: " + family);
}
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported family: " + family, e);
}
}
public static ByteChannel wrapChannel(ByteChannel channel) {
return new ByteChannelWrapper(channel);
}
}

View File

@@ -0,0 +1,172 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.common;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.ByteChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Objects;
import static java.net.StandardProtocolFamily.INET;
import static java.net.StandardProtocolFamily.INET6;
import static java.net.StandardProtocolFamily.UNIX;
public class SocketHelper {
public static StandardProtocolFamily getSocketFamily(SocketAddress address) {
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
InetAddress ia = isa.getAddress();
if (ia instanceof Inet6Address) {
return INET6;
} else {
return INET;
}
} else if (address instanceof UnixDomainSocketAddress) {
return UNIX;
} else {
throw new IllegalArgumentException("Unsupported socket address '" + address + "'");
}
}
public static void checkFamily(StandardProtocolFamily family, SocketAddress address) {
Objects.requireNonNull(family);
Objects.requireNonNull(address);
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
InetAddress ia = isa.getAddress();
if (ia != null
&& !(ia instanceof Inet4Address && family == INET)
&& !(ia instanceof Inet6Address && family == INET6)) {
throw new IllegalArgumentException(
"Socket address '" + address + "' does not match required family '" + family + "'");
}
} else if (address instanceof UnixDomainSocketAddress) {
if (family != StandardProtocolFamily.UNIX) {
throw new IllegalArgumentException("Socket address '" + address + "' does not match required family '" + family + "'");
}
} else {
throw new IllegalArgumentException(
"Socket address '" + address + "' does not match required family '" + family + "'");
}
}
public static SocketAddress socketAddressFromString(String str) {
if (str.startsWith("inet:")) {
String s = str.substring("inet:".length());
int ic = s.lastIndexOf(':');
String ia = s.substring(0, ic);
int is = ia.indexOf('/');
String h = ia.substring(0, is);
String a = ia.substring(is + 1);
String p = s.substring(ic + 1);
InetAddress addr;
if ("<unresolved>".equals(a)) {
return InetSocketAddress.createUnresolved(h, Integer.parseInt(p));
} else {
if (a.indexOf('.') > 0) {
String[] as = a.split("\\.");
if (as.length != 4) {
throw new IllegalArgumentException("Unsupported address: " + str);
}
byte[] ab = new byte[4];
for (int i = 0; i < 4; i++) {
ab[i] = (byte) Integer.parseInt(as[i]);
}
try {
addr = InetAddress.getByAddress(h.isEmpty() ? null : h, ab);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported address: " + str, e);
}
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
return new InetSocketAddress(addr, Integer.parseInt(p));
}
} else if (str.startsWith("unix:")) {
return UnixDomainSocketAddress.of(str.substring("unix:".length()));
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
}
public static String socketAddressToString(SocketAddress address) {
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
String host = isa.getHostString();
InetAddress addr = isa.getAddress();
int port = isa.getPort();
String formatted;
if (addr == null) {
formatted = host + "/<unresolved>";
} else {
formatted = addr.toString();
if (addr instanceof Inet6Address) {
int i = formatted.lastIndexOf("/");
formatted = formatted.substring(0, i + 1) + "[" + formatted.substring(i + 1) + "]";
}
}
return "inet:" + formatted + ":" + port;
} else if (address instanceof UnixDomainSocketAddress) {
return "unix:" + address;
} else {
throw new IllegalArgumentException("Unsupported socket address: '" + address + "'");
}
}
public static SocketChannel openSocket(StandardProtocolFamily family) throws IOException {
Objects.requireNonNull(family);
return SocketChannel.open(family);
}
public static ServerSocketChannel openServerSocket(StandardProtocolFamily family) throws IOException {
Objects.requireNonNull(family);
return ServerSocketChannel.open(family).bind(getLoopbackAddress(family), 0);
}
private static SocketAddress getLoopbackAddress(StandardProtocolFamily family) {
try {
Objects.requireNonNull(family);
switch (family) {
case INET:
return new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), 0);
case INET6:
return new InetSocketAddress(
Inet6Address.getByAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }), 0);
case UNIX:
return null;
default:
throw new IllegalArgumentException("Unsupported family: " + family);
}
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported family: " + family, e);
}
}
public static ByteChannel wrapChannel(ByteChannel channel) {
return new ByteChannelWrapper(channel);
}
}

View File

@@ -0,0 +1,2 @@
Manifest-Version: 1.0
Multi-Release: true

View File

@@ -43,7 +43,7 @@ public class DaemonRegistryTest {
byte[] token = new byte[16];
new Random().nextBytes(token);
reg1.store(new DaemonInfo("12345678", "/java/home/",
"/data/reg/", 0x12345678, 7502, token,
"/data/reg/", 0x12345678, "inet:/127.0.0.1:7502", token,
Locale.getDefault().toLanguageTag(), Arrays.asList("-Xmx"),
DaemonState.Idle, System.currentTimeMillis(), System.currentTimeMillis()));
@@ -63,7 +63,7 @@ public class DaemonRegistryTest {
byte[] token = new byte[16];
new Random().nextBytes(token);
reg1.store(new DaemonInfo("12345678", "/java/home/",
"/data/reg/", 0x12345678, 7502, token,
"/data/reg/", 0x12345678, "inet:/127.0.0.1:7502", token,
Locale.getDefault().toLanguageTag(), Arrays.asList("-Xmx"),
DaemonState.Idle, System.currentTimeMillis(), System.currentTimeMillis()));
assertEquals(1, reg1.getAll().size());

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.common;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.StandardProtocolFamily;
import java.net.UnknownHostException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SocketHelperTest {
@Test
void testIpv4NullHost() throws UnknownHostException {
InetSocketAddress i4a = new InetSocketAddress(
InetAddress.getByAddress(null, new byte[] { (byte) 192, (byte) 168, 0, 1 }), 8080);
assertEquals("inet:/192.168.0.1:8080", SocketHelper.socketAddressToString(i4a));
assertEquals(i4a, SocketHelper.socketAddressFromString("inet:/192.168.0.1:8080"));
}
@Test
void testIpv4DummyHost() throws UnknownHostException {
InetSocketAddress i4a = new InetSocketAddress(
InetAddress.getByAddress("dummy.org", new byte[] { (byte) 192, (byte) 168, 0, 1 }), 8080);
assertEquals("inet:dummy.org/192.168.0.1:8080", SocketHelper.socketAddressToString(i4a));
assertEquals(i4a, SocketHelper.socketAddressFromString("inet:dummy.org/192.168.0.1:8080"));
}
@Test
void testIpv4Loopback() throws UnknownHostException {
InetSocketAddress i4a = new InetSocketAddress(8080);
assertEquals("inet:0.0.0.0/0.0.0.0:8080", SocketHelper.socketAddressToString(i4a));
assertEquals(i4a, SocketHelper.socketAddressFromString("inet:0.0.0.0/0.0.0.0:8080"));
}
@Test
void testIpv4Unresolved() throws UnknownHostException {
InetSocketAddress i4a = InetSocketAddress.createUnresolved("google.com", 8080);
assertEquals("inet:google.com/<unresolved>:8080", SocketHelper.socketAddressToString(i4a));
assertEquals(i4a, SocketHelper.socketAddressFromString("inet:google.com/<unresolved>:8080"));
}
@Test
void testCheckInetAddress() {
String family = "INET";
String address = "inet:/127.0.0.1:8192";
SocketHelper.checkFamily(StandardProtocolFamily.valueOf(family),
SocketHelper.socketAddressFromString(address));
}
}