summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java
blob: d2667e42da3fa811dd03adee13d59d9378d308b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package se.leap.bitmaskclient.eip;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.ResultReceiver;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.v4.content.ContextCompat;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import se.leap.bitmaskclient.Provider;

import static se.leap.bitmaskclient.Constants.EIP_ACTION_CHECK_CERT_VALIDITY;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_CONFIGURE_TETHERING;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START_BLOCKING_VPN;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP;
import static se.leap.bitmaskclient.Constants.EIP_EARLY_ROUTES;
import static se.leap.bitmaskclient.Constants.EIP_N_CLOSEST_GATEWAY;
import static se.leap.bitmaskclient.Constants.EIP_RECEIVER;

/**
 * Use this class to send commands to EIP
 */

public class EipCommand {

    private static void execute(@NotNull Context context, @NotNull String action) {
        execute(context, action, null, null);
    }

    /**
     * Send a command to EIP
     * @param context the context to start the command from
     * @param action A valid String constant from EIP class representing an Intent
     *               filter for the EIP class
     * @param resultReceiver The resultreceiver to reply to
     */
    private static void execute(@NotNull Context context, @NotNull String action, @Nullable ResultReceiver resultReceiver, @Nullable Intent vpnIntent) {
        // TODO validate "action"...how do we get the list of intent-filters for a class via Android API?
        if (vpnIntent == null) {
            vpnIntent = new Intent();
        }
        vpnIntent.setComponent(new ComponentName(context.getApplicationContext(), EIP.class));
        vpnIntent.setAction(action);
        if (resultReceiver != null)
            vpnIntent.putExtra(EIP_RECEIVER, resultReceiver);
        EIP.enqueueWork(context, vpnIntent);
    }

    public static void startVPN(@NonNull Context context, boolean earlyRoutes) {
        Intent baseIntent = new Intent();
        baseIntent.putExtra(EIP_EARLY_ROUTES, earlyRoutes);
        baseIntent.putExtra(EIP_N_CLOSEST_GATEWAY, 0);
        execute(context, EIP_ACTION_START, null, baseIntent);
    }

    public static void startVPN(@NonNull Context context, boolean earlyRoutes, int nClosestGateway) {
        Intent baseIntent = new Intent();
        baseIntent.putExtra(EIP_EARLY_ROUTES, earlyRoutes);
        baseIntent.putExtra(EIP_N_CLOSEST_GATEWAY, nClosestGateway);
        execute(context, EIP_ACTION_START, null, baseIntent);
    }

    public static void startBlockingVPN(Context context) {
        execute(context, EIP_ACTION_START_BLOCKING_VPN);
    }

    @VisibleForTesting
    public static void startVPN(@NonNull Context context, ResultReceiver resultReceiver) {
        execute(context, EIP_ACTION_START, resultReceiver, null);
    }

    public static void stopVPN(@NonNull Context context) {
        execute(context, EIP_ACTION_STOP);
    }

    @VisibleForTesting
    public static void stopVPN(@NonNull Context context, ResultReceiver resultReceiver) {
        execute(context, EIP_ACTION_STOP, resultReceiver, null);
    }

    public static void checkVpnCertificate(@NonNull Context context) {
        execute(context, EIP_ACTION_CHECK_CERT_VALIDITY);
    }

    @VisibleForTesting
    public static void checkVpnCertificate(@NonNull Context context, ResultReceiver resultReceiver) {
        execute(context, EIP_ACTION_CHECK_CERT_VALIDITY, resultReceiver, null);
    }

    public static void configureTethering(@NonNull Context context) {
        execute(context, EIP_ACTION_CONFIGURE_TETHERING);
    }

    @VisibleForTesting
    public static void configureTethering(@NonNull Context context, ResultReceiver resultReceiver) {
        execute(context, EIP_ACTION_CONFIGURE_TETHERING);
    }

}