我在我的应用程序中获取传出数据包后,我想将它们转发到原始目标.我已设法从标头获取目标IP和端口,但我不知道如何与远程服务器通信,然后将响应写回源.我认为这是可能的,因为有这个 app.这是我的第一次尝试:
private voID runVpnConnection() throws Exception {configure();fileinputStream in = new fileinputStream(mInterface.getfileDescriptor());fileOutputStream out = new fileOutputStream( mInterface.getfileDescriptor());// Allocate the buffer for a single packet.ByteBuffer packet = ByteBuffer.allocate(32767);boolean ok = true;while (ok) { Socket tcpsocket = SocketChannel.open().socket(); try { // Read the outgoing packet from the input stream. int length = in.read(packet.array()); if (length > 0) { Log.i(TAG,"-------------------New packet: " + length); packet.limit(length); // here i get destIP and destIP InetAddress serverAddr = InetAddress.getByname(destIP); SocketAddress socketadd = new InetSocketAddress(serverAddr,destPort); protect(tcpsocket); OutputStream outBuffer = tcpsocket.getoutputStream(); outBuffer.write(packet.array()); outBuffer.flush(); // outBuffer.close(); packet.clear(); } if (tcpsocket.isConnected()) { inputStream inBuffer = tcpsocket.getinputStream(); DatainputStream inStream = new DatainputStream(inBuffer); Log.i(TAG,"Response length " + inStream.available()); if (inStream.available() > 0) { Log.i(TAG,"Server says " + inStream.readUTF()); inStream.readFully(packet.array()); out.write(packet.array()); inBuffer.close(); } out.flush(); } packet.clear(); // Thread.sleep(50); } catch (Exception e) { e.printstacktrace(); Log.e(TAG,e.toString()); ok = false; } tcpsocket.close();}in.close();out.close();}解决方法 显然tPacketCapture将流量路由到手机上运行的另一个界面,并将所有这些流量发送到互联网(这与使用VPNService的mobiwol,greyshirts和其他应用程序相同).
如果您运行(并了解)ToyVPN,您就会知道来自手机的所有流量都会进入服务器(您的计算机),您可以在其中配置iptables以将所有流量发送到互联网.
如果你想在没有服务器的情况下运行,你必须在手机上做同样的事情.从another question:起
When I look at mobiwol’s connection with “adb shell netcfg” it creates a tun0 interface with 10.2.3.4/32 address. It routes all packages to this private network and send to internet.
因此,基本上从您的应用程序,您将必须配置手机充当它自己的服务器.
总结以上是内存溢出为你收集整理的Android VpnService,包转发全部内容,希望文章能够帮你解决Android VpnService,包转发所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)