0

在我们的应用程序中,我们有许多不同的模块可以自己发出网络请求。应用程序可以选择使用 VPN 进行安全连接。VPN 服务仅为我们的应用程序处理网络。我的问题是:在应用程序冷启动时,启用 VPN 功能时,如何防止模块在 VPN 服务启动之前发出网络请求。

4

1 回答 1

1

我的项目中有类似的问题,但是我有一个模块需要等待建立 VPN。无论如何,我有一个小代码来检测是否连接了 VPN。

这是代码:

class VpnMonitor {
    private final String VPN_INTERFACE = "tun0";

    private OnVpnStatusChange mCallback;
    private MonitorThread mThread = null;
    private Handler mHandler = new Handler(Looper.getMainLooper());

    public interface OnVpnStatusChange {
        void onVpnConnect();

        void onVpnDisconnect();
    }

    VpnMonitor() {
        this(null);
    }

    VpnMonitor(@Nullable OnVpnStatusChange callback) {
        mCallback = callback;
    }

    void startMonitor() {
        if ((mThread == null) || !mThread.isAlive()) {
            mThread = new MonitorThread();
            mThread.start();
        }
    }

    void stopMonitor() {
        if (mThread != null) {
            mThread.terminate();
            try {
                mThread.join();
            } catch (Exception e) {
            }
            mThread = null;
        }
    }

    boolean isVpnConnectionUp() {
        try {
            ArrayList<NetworkInterface> infs = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface inf : infs) {
                if (inf.getName().equals(VPN_INTERFACE) && inf.isUp() && inf.getInterfaceAddresses().size() > 0) {
                    return true;
                }
            }
        } catch (SocketException e) {
            return false;
        }

        return false;
    }

    private class MonitorThread extends Thread {
        private volatile boolean mRunning = true;

        void terminate() {
            mRunning = false;
        }

        @Override
        public void run() {
            mRunning = true;

            for (int i = 0; i < 5; i++) {
                try {
                    delay(100);

                    ArrayList<NetworkInterface> infs = Collections.list(NetworkInterface.getNetworkInterfaces());

                    for (NetworkInterface inf : infs) {
                        if (inf.getName().equals(VPN_INTERFACE)) {
                            for (int r = 0; r < 10; r++) {
                                if (!mRunning) {
                                    mHandler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (mCallback != null) {
                                                mCallback.onVpnDisconnect();
                                            }
                                        }
                                    });

                                    return;
                                }

                                if (inf.isUp() && inf.getInterfaceAddresses().size() > 0) {
                                    delay(1000);

                                    mHandler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (mCallback != null) {
                                                mCallback.onVpnConnect();
                                            }
                                        }
                                    });

                                    return;
                                }

                                delay(50);
                            }
                        }
                    }
                } catch (SocketException e) {
                }
            }

            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (mCallback != null) {
                        mCallback.onVpnDisconnect();
                    }
                }
            });

        }

        private void delay(int time) {
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

也许它也可以帮助你。随意更改它。

于 2020-02-06T17:08:47.323 回答