Skip to content

Commit

Permalink
[cleanup] general bug fix and code cleanup
Browse files Browse the repository at this point in the history
1. support windows .bat when executing
2. improve shared library loading
2. use memorySegment asSlice for ByteArray#sub
3. fix ipv4 copy
4. modify length in udp packet
5. remove unused classes
  • Loading branch information
wkgcass committed Apr 27, 2024
1 parent 6a9b1d1 commit 6baa449
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 278 deletions.
22 changes: 19 additions & 3 deletions base/src/main/java/io/vproxy/base/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,18 @@ public static ExecuteResult execute(String script, int timeout, boolean getResul
} else {
Logger.alert("trying to execute script: " + script);
}
File file = File.createTempFile("script", ".sh");
File file = File.createTempFile("script", OS.isWindows() ? ".bat" : ".sh");
try {
Files.writeString(file.toPath(), script);
if (!file.setExecutable(true)) {
throw new Exception("setting executable to script " + file.getAbsolutePath() + " failed");
}
ProcessBuilder pb = new ProcessBuilder(file.getAbsolutePath());
ProcessBuilder pb;
if (OS.isWindows()) {
pb = new ProcessBuilder("cmd.exe", "/c", file.getAbsolutePath());
} else {
pb = new ProcessBuilder(file.getAbsolutePath());
}
return execute(pb, timeout, getResult);
} finally {
//noinspection ResultOfMethodCallIgnored
Expand Down Expand Up @@ -951,6 +956,17 @@ public static List<Nic> getNetworkInterfaces() throws IOException {
}

public static void loadDynamicLibrary(String name) throws UnsatisfiedLinkError {
loadDynamicLibrary(name, Utils.class.getClassLoader(), "io/vproxy/");
}

public static void loadDynamicLibrary(String name, ClassLoader cl, String basePath) throws UnsatisfiedLinkError {
// format basePath
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
}
if (!basePath.endsWith("/")) {
basePath += "/";
}
// check and use bundled binaries
String filename = "lib" + name + "-" + OS.arch();
String suffix;
Expand All @@ -963,7 +979,7 @@ public static void loadDynamicLibrary(String name) throws UnsatisfiedLinkError {
suffix = ".so";
}

InputStream is = Utils.class.getClassLoader().getResourceAsStream("io/vproxy/" + filename + suffix);
InputStream is = cl.getResourceAsStream(basePath + filename + suffix);
if (is == null) {
System.out.println("System.loadLibrary(" + name + ")");
System.loadLibrary(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public MemorySegmentByteArray(MemorySegment seg) {
this.seg = seg;
}

public MemorySegment getMemorySegment() {
return seg;
}

@Override
public byte get(int idx) {
return seg.get(ValueLayout.JAVA_BYTE, idx);
Expand Down Expand Up @@ -138,4 +142,10 @@ public ByteArray int64ReverseNetworkByteOrder(int offset, long val) {
seg.set(LONG_LITTLE_ENDIAN, offset, val);
return this;
}

@Override
public ByteArray sub(int fromInclusive, int len) {
var newSeg = seg.asSlice(fromInclusive, len);
return new MemorySegmentByteArray(newSeg);
}
}
4 changes: 2 additions & 2 deletions base/src/main/java/io/vproxy/vfd/ReadableByteStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public interface ReadableByteStream {
int read(ByteBuffer dst) throws IOException;

default int readBlocking(ByteBuffer buf) throws IOException {
return read(buf);
default int readBlocking(ByteBuffer dst) throws IOException {
return read(dst);
}
}
2 changes: 1 addition & 1 deletion base/src/main/java/io/vproxy/vpacket/Ipv4Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public Ipv4Packet copy() {
ret.options = options;
ret.packet = packet.copy();
ret.packet.recordParent(ret);
return this;
return ret;
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions base/src/main/java/io/vproxy/vpacket/UdpPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ public void setData(AbstractPacket data) {
this.data = data;
}

public void setData(ByteArray data) {
clearRawPacket();
this.data = new PacketBytes(data);
this.length = data.length() + 8;
}

@Override
public void clearAllRawPackets() {
clearRawPacket();
Expand Down
196 changes: 0 additions & 196 deletions base/src/main/java/io/vproxy/vpacket/VProxyEncryptedPacket.java

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 6baa449

Please sign in to comment.