我的应用程序实现了VpnService来拦截网络流量并提供量身定制的响应.目标是处理特定地址的流量,并丢弃其他请求.
目前,我成功地解析了传入的请求以及构建和发送响应.然而,问题是这些答复并未作为对原始请求的实际答复而到达; 使用套接字连接测试只是超时.
为了做出这种区分,我现在正在从VpnService的输入流中解析原始IP数据包,如下所示:
VpnService.Builder b = new VpnService.Builder();
b.addAddress("10.2.3.4", 28);
b.addRoute("0.0.0.0", 0);
b.setMtu(1500);
...
ParcelFileDescriptor vpnInterface = b.establish();
final FileInputStream in = new FileInputStream(
vpnInterface.getFileDescriptor());
final FileOutputStream out = new FileOutputStream(
vpnInterface.getFileDescriptor());
// Allocate the buffer for a single packet.
ByteBuffer packet = ByteBuffer.allocate(32767);
// We keep forwarding packets till something goes wrong.
try {
while (vpnInterface != null && vpnInterface.getFileDescriptor() != null
&& vpnInterface.getFileDescriptor().valid()) {
packet.clear();
SystemClock.sleep(10);
// Read the outgoing packet from the input stream.
final byte[] …Run Code Online (Sandbox Code Playgroud)