简体   繁体   English

在 Android 应用冷启动时,在 VPN 启动之前阻止网络请求

[英]On Android app cold start, prevent network requests until VPN started

In our app we have many different modules that can make network requests on their own.在我们的应用程序中,我们有许多不同的模块可以自行发出网络请求。 App has option to use VPN for secure connection.应用程序可以选择使用 VPN 进行安全连接。 VPN service is only handling network for our app. VPN 服务仅为我们的应用程序处理网络。 My problem is: on app cold start, when VPN feature is enabled, how to prevent modules from making network requests until VPN service is started.我的问题是:在应用冷启动时,当启用 VPN 功能时,如何防止模块在 VPN 服务启动之前发出网络请求。

I had similar problem in my project, but I had a single module that need to wait for establishing VPN.我在我的项目中遇到了类似的问题,但是我有一个模块需要等待建立 VPN。 Anyway, I have a small code to detect if a VPN is connected or not.无论如何,我有一个小代码来检测是否连接了 VPN。

Here is the code:这是代码:

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();
            }
        }
    }
}

Maybe it can help you too.也许它也可以帮助你。 Feel free to change it as you please.随意更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM