VpnService:更改允许的应用程序?

时间:2018-08-08 10:30:04

标签: android android-service vpn android-vpn-service

我有一个VpnService,其中包含一些允许的应用程序和一些不允许使用的应用程序,这些应用程序可以随时更改。

public void onCreate() {
    super.onCreate();
    isRunning = true;
    setupVPN();

    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_VPN_STATE).putExtra("running", true));
    Log.i(TAG, "Started");

    registerReceiver(stopBr, new IntentFilter("stop_vpn"));

    setupNotification();
}

private void setupVPN() {
    if (vpnInterface == null) {
        Builder builder = new Builder();
        builder.addAddress(VPN_ADDRESS, 32);
        builder.addRoute(VPN_ROUTE, 0);
        // Deciding which applications will be able to access the internet
        try {
            builder.addDisallowedApplication("com.android.chrome");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        vpnInterface = builder.setSession(getString(R.string.app_name)).setConfigureIntent(pendingIntent).establish();
    }
}

在这种情况下,应用程序仅禁止使用“ com.android.chrome”。 该VPN实际上不处理任何传入的数据包,因此我用它来阻止流量,这意味着任何非“禁止”列表的应用都将无法访问互联网。

问题:如果要在运行时更改不允许的应用程序列表怎么办? 我是否需要外部服务或某些东西来控制VpnService?还是有另一种方法?

我首先想到的是一个外部服务,该服务可以使用不同的参数检测并重新启动Vpn,但是对于这样的事情似乎有点复杂。

我还认为可以通过另一种方式来实现此目的,即具有完全正常工作的Vpn并按应用程序过滤数据包。但是(假设有一种将数据包绑定到应用程序的方式),我将如何确定哪些数据包绑定到哪些应用程序?

0 个答案:

没有答案
相关问题