A packet interceptor for Android built on top of VpnService package xyz.hexene.localvpn;import android.app.PendingIntent;import android.content.Intent;import android.net.VpnService;import android.os.ParcelFileDescriptor;import android.support.v4.content.LocalBroadcastManager;import android.util.Log;import java.io.Closeable;import java.io.FileDescriptor;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.nio.channels.Selector;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class LocalVPNService extends VpnService{ private static final String TAG = LocalVPNService.class.getSimpleName(); private static final String VPN_ADDRESS = "10.0.0.2"; // Only IPv4 support for now private static final String VPN_ROUTE = "0.0.0.0"; // Intercept everything public static final String BROADCAST_VPN_STATE = "xyz.hexene.localvpn.VPN_STATE"; private static boolean isRunning = false; private ParcelFileDescriptor vpnInterface = null; private PendingIntent pendingIntent; private ConcurrentLinkedQueue<Packet> deviceToNetworkUDPQueue; private ConcurrentLinkedQueue<Packet> deviceToNetworkTCPQueue; private ConcurrentLinkedQueue<ByteBuffer> networkToDeviceQueue; private ExecutorService executorService; private Selector udpSelector; private Selector tcpSelector; @Override public void onCreate() { super.onCreate(); isRunning = true; setupVPN(); try { udpSelector = Selector.open(); tcpSelector = Selector.open(); deviceToNetworkUDPQueue = new ConcurrentLinkedQueue<>(); deviceToNetworkTCPQueue = new ConcurrentLinkedQueue<>(); networkToDeviceQueue = new ConcurrentLinkedQueue<>(); executorService = Executors.newFixedThreadPool(5); executorService.submit(new UDPInput(networkToDeviceQueue, udpSelector)); executorService.submit(new UDPOutput(deviceToNetworkUDPQueue, udpSelector, this)); executorService.submit(new TCPInput(networkToDeviceQueue, tcpSelector)); executorService.submit(new TCPOutput(deviceToNetworkTCPQueue, networkToDeviceQueue, tcpSelector, this)); executorService.submit(new VPNRunnable(vpnInterface.getFileDescriptor(), deviceToNetworkUDPQueue, deviceToNetworkTCPQueue, networkToDeviceQueue)); LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_VPN_STATE).putExtra("running", true)); Log.i(TAG, "Started"); } catch (IOException e) { // TODO: Here and elsewhere, we should explicitly notify the user of any errors // and suggest that they stop the service, since we can't do it ourselves Log.e(TAG, "Error starting service", e); cleanup(); } } private void setupVPN() { if (vpnInterface == null) { Builder builder = new Builder(); builder.addAddress(VPN_ADDRESS, 32); builder.addRoute(VPN_ROUTE, 0); vpnInterface = builder.setSession(getString(R.string.app_name)).setConfigureIntent(pendingIntent).establish(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } public static boolean isRunning() { return isRunning; } @Override public void onDestroy() { super.onDestroy(); isRunning = false; executorService.shutdownNow(); cleanup(); Log.i(TAG, "Stopped"); } private void cleanup() { deviceToNetworkTCPQueue = null; deviceToNetworkUDPQueue = null; networkToDeviceQueue = null; ByteBufferPool.clear(); closeResources(udpSelector, tcpSelector, vpnInterface); } // TODO: Move this to a "utils" class for reuse private static void closeResources(Closeable... resources) { for (Closeable resource : resources) { try { resource.close(); } catch (IOException e) { // Ignore } } } private static class VPNRunnable implements Runnable { private static final String TAG = VPNRunnable.class.getSimpleName(); private FileDescriptor vpnFileDescriptor; private ConcurrentLinkedQueue<Packet> deviceToNetworkUDPQueue; private ConcurrentLinkedQueue<Packet> deviceToNetworkTCPQueue; private ConcurrentLinkedQueue<ByteBuffer> networkToDeviceQueue; public VPNRunnable(FileDescriptor vpnFileDescriptor, ConcurrentLinkedQueue<Packet> deviceToNetworkUDPQueue, ConcurrentLinkedQueue<Packet> deviceToNetworkTCPQueue, ConcurrentLinkedQueue<ByteBuffer> networkToDeviceQueue) { this.vpnFileDescriptor = vpnFileDescriptor; this.deviceToNetworkUDPQueue = deviceToNetworkUDPQueue; this.deviceToNetworkTCPQueue = deviceToNetworkTCPQueue; this.networkToDeviceQueue = networkToDeviceQueue; } @Override public void run() { Log.i(TAG, "Started"); FileChannel vpnInput = new FileInputStream(vpnFileDescriptor).getChannel(); FileChannel vpnOutput = new FileOutputStream(vpnFileDescriptor).getChannel(); try { ByteBuffer bufferToNetwork = null; boolean dataSent = true; boolean dataReceived; while (!Thread.interrupted()) { if (dataSent) bufferToNetwork = ByteBufferPool.acquire(); else bufferToNetwork.clear(); // TODO: Block when not connected int readBytes = vpnInput.read(bufferToNetwork); if (readBytes > 0) { dataSent = true; bufferToNetwork.flip(); Packet packet = new Packet(bufferToNetwork); if (packet.isUDP()) { deviceToNetworkUDPQueue.offer(packet); } else if (packet.isTCP()) { deviceToNetworkTCPQueue.offer(packet); } else { Log.w(TAG, "Unknown packet type"); Log.w(TAG, packet.ip4Header.toString()); dataSent = false; } } else { dataSent = false; } ByteBuffer bufferFromNetwork = networkToDeviceQueue.poll(); if (bufferFromNetwork != null) { bufferFromNetwork.flip(); while (bufferFromNetwork.hasRemaining()) vpnOutput.write(bufferFromNetwork); dataReceived = true; ByteBufferPool.release(bufferFromNetwork); } else { dataReceived = false; } // TODO: Sleep-looping is not very battery-friendly, consider blocking instead // Confirm if throughput with ConcurrentQueue is really higher compared to BlockingQueue if (!dataSent && !dataReceived) Thread.sleep(10); } } catch (InterruptedException e) { Log.i(TAG, "Stopping"); } catch (IOException e) { Log.w(TAG, e.toString(), e); } finally { closeResources(vpnInput, vpnOutput); } } }
下载基于VpnService的二次开发用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论