Skip to content

Commit 6baa449

Browse files
committed
[cleanup] general bug fix and code cleanup
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
1 parent 6a9b1d1 commit 6baa449

File tree

8 files changed

+38
-278
lines changed

8 files changed

+38
-278
lines changed

base/src/main/java/io/vproxy/base/util/Utils.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -563,13 +563,18 @@ public static ExecuteResult execute(String script, int timeout, boolean getResul
563563
} else {
564564
Logger.alert("trying to execute script: " + script);
565565
}
566-
File file = File.createTempFile("script", ".sh");
566+
File file = File.createTempFile("script", OS.isWindows() ? ".bat" : ".sh");
567567
try {
568568
Files.writeString(file.toPath(), script);
569569
if (!file.setExecutable(true)) {
570570
throw new Exception("setting executable to script " + file.getAbsolutePath() + " failed");
571571
}
572-
ProcessBuilder pb = new ProcessBuilder(file.getAbsolutePath());
572+
ProcessBuilder pb;
573+
if (OS.isWindows()) {
574+
pb = new ProcessBuilder("cmd.exe", "/c", file.getAbsolutePath());
575+
} else {
576+
pb = new ProcessBuilder(file.getAbsolutePath());
577+
}
573578
return execute(pb, timeout, getResult);
574579
} finally {
575580
//noinspection ResultOfMethodCallIgnored
@@ -951,6 +956,17 @@ public static List<Nic> getNetworkInterfaces() throws IOException {
951956
}
952957

953958
public static void loadDynamicLibrary(String name) throws UnsatisfiedLinkError {
959+
loadDynamicLibrary(name, Utils.class.getClassLoader(), "io/vproxy/");
960+
}
961+
962+
public static void loadDynamicLibrary(String name, ClassLoader cl, String basePath) throws UnsatisfiedLinkError {
963+
// format basePath
964+
if (basePath.startsWith("/")) {
965+
basePath = basePath.substring(1);
966+
}
967+
if (!basePath.endsWith("/")) {
968+
basePath += "/";
969+
}
954970
// check and use bundled binaries
955971
String filename = "lib" + name + "-" + OS.arch();
956972
String suffix;
@@ -963,7 +979,7 @@ public static void loadDynamicLibrary(String name) throws UnsatisfiedLinkError {
963979
suffix = ".so";
964980
}
965981

966-
InputStream is = Utils.class.getClassLoader().getResourceAsStream("io/vproxy/" + filename + suffix);
982+
InputStream is = cl.getResourceAsStream(basePath + filename + suffix);
967983
if (is == null) {
968984
System.out.println("System.loadLibrary(" + name + ")");
969985
System.loadLibrary(name);

base/src/main/java/io/vproxy/base/util/bytearray/MemorySegmentByteArray.java

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public MemorySegmentByteArray(MemorySegment seg) {
1414
this.seg = seg;
1515
}
1616

17+
public MemorySegment getMemorySegment() {
18+
return seg;
19+
}
20+
1721
@Override
1822
public byte get(int idx) {
1923
return seg.get(ValueLayout.JAVA_BYTE, idx);
@@ -138,4 +142,10 @@ public ByteArray int64ReverseNetworkByteOrder(int offset, long val) {
138142
seg.set(LONG_LITTLE_ENDIAN, offset, val);
139143
return this;
140144
}
145+
146+
@Override
147+
public ByteArray sub(int fromInclusive, int len) {
148+
var newSeg = seg.asSlice(fromInclusive, len);
149+
return new MemorySegmentByteArray(newSeg);
150+
}
141151
}

base/src/main/java/io/vproxy/vfd/ReadableByteStream.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public interface ReadableByteStream {
77
int read(ByteBuffer dst) throws IOException;
88

9-
default int readBlocking(ByteBuffer buf) throws IOException {
10-
return read(buf);
9+
default int readBlocking(ByteBuffer dst) throws IOException {
10+
return read(dst);
1111
}
1212
}

base/src/main/java/io/vproxy/vpacket/Ipv4Packet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public Ipv4Packet copy() {
258258
ret.options = options;
259259
ret.packet = packet.copy();
260260
ret.packet.recordParent(ret);
261-
return this;
261+
return ret;
262262
}
263263

264264
@Override

base/src/main/java/io/vproxy/vpacket/UdpPacket.java

+6
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ public void setData(AbstractPacket data) {
235235
this.data = data;
236236
}
237237

238+
public void setData(ByteArray data) {
239+
clearRawPacket();
240+
this.data = new PacketBytes(data);
241+
this.length = data.length() + 8;
242+
}
243+
238244
@Override
239245
public void clearAllRawPackets() {
240246
clearRawPacket();

base/src/main/java/io/vproxy/vpacket/VProxyEncryptedPacket.java

-196
This file was deleted.

core/src/main/java/io/vproxy/vswitch/iface/AbstractBaseEncryptedSwitchSocketIface.java

-69
This file was deleted.

core/src/main/java/io/vproxy/vswitch/iface/IfaceCanSendVProxyPacket.java

-7
This file was deleted.

0 commit comments

Comments
 (0)