我已经设置了一个 vpn 服务,然后配置了一个 vpn 接口。
private void setupVPN() {
try {
if (vpnInterface == null) {
Builder builder = new Builder();
builder.addAddress(VPN_ADDRESS, 32);
builder.addRoute(VPN_ROUTE, 0);
builder.setMtu(1499);
vpnInterface = builder.setSession(getString(R.string.app_name)).setConfigureIntent(pendingIntent).establish();
}
} catch (Exception e) {
Log.e(TAG, "error", e);
System.exit(0);
}
}
我可以从文件描述符中读取字节,但如何将它们写回。
FileChannel vpnInput = new FileInputStream(vpnFileDescriptor).getChannel();
FileChannel vpnOutput = new FileOutputStream(vpnFileDescriptor).getChannel();
Thread t = new Thread(new WriteVpnThread(vpnOutput, networkToDeviceQueue));
t.start();
try {
ByteBuffer bufferToNetwork = null;
while (!Thread.interrupted()) {
bufferToNetwork = ByteBufferPool.acquire();
int readBytes = vpnInput.read(bufferToNetwork);
if (readBytes > 0) {
bufferToNetwork.flip();
}
}
}
我必须解析它们然后将其写回还是有其他方法?