summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java
blob: e92816091b139a82d019c5c9576f1281224065d9 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
 * Copyright (c) 2013 LEAP Encryption Access Project and contributers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package se.leap.bitmaskclient.eip;

import static se.leap.bitmaskclient.base.models.Constants.FULLNESS;
import static se.leap.bitmaskclient.base.models.Constants.HOST;
import static se.leap.bitmaskclient.base.models.Constants.IP_ADDRESS;
import static se.leap.bitmaskclient.base.models.Constants.LOCATION;
import static se.leap.bitmaskclient.base.models.Constants.LOCATIONS;
import static se.leap.bitmaskclient.base.models.Constants.NAME;
import static se.leap.bitmaskclient.base.models.Constants.OPENVPN_CONFIGURATION;
import static se.leap.bitmaskclient.base.models.Constants.OVERLOAD;
import static se.leap.bitmaskclient.base.models.Constants.TIMEZONE;
import static se.leap.bitmaskclient.base.models.Constants.VERSION;

import android.content.Context;

import androidx.annotation.NonNull;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.ConfigParser;
import de.blinkt.openvpn.core.connection.Connection;
import se.leap.bitmaskclient.base.utils.ConfigHelper;
import se.leap.bitmaskclient.base.utils.PreferenceHelper;

/**
 * Gateway provides objects defining gateways and their metadata.
 * Each instance contains a VpnProfile for OpenVPN specific data and member
 * variables describing capabilities and location (name)
 *
 * @author Sean Leonard <meanderingcode@aetherislands.net>
 * @author Parménides GV <parmegv@sdf.org>
 * @author cyberta
 */
public class Gateway {

    public final static String TAG = Gateway.class.getSimpleName();

    private JSONObject generalConfiguration;
    private JSONObject secrets;
    private JSONObject gateway;
    private JSONObject load;

    // the location of a gateway is its name
    private String name;
    private int timezone;
    private int apiVersion;
    private HashMap<Connection.TransportType, VpnProfile> vpnProfiles;

    /**
     * Build a gateway object from a JSON OpenVPN gateway definition in eip-service.json
     * and create a VpnProfile belonging to it.
     */
    public Gateway(JSONObject eipDefinition, JSONObject secrets, JSONObject gateway, Context context)
            throws ConfigParser.ConfigParseError, JSONException, IOException {
        this(eipDefinition, secrets, gateway, null, context);
    }

    public Gateway(JSONObject eipDefinition, JSONObject secrets, JSONObject gateway, JSONObject load, Context context)
            throws ConfigParser.ConfigParseError, JSONException, IOException {

        this.gateway = gateway;
        this.secrets = secrets;
        this.load = load;

        generalConfiguration = getGeneralConfiguration(eipDefinition);
        timezone = getTimezone(eipDefinition);
        name = locationAsName(eipDefinition);
        apiVersion = getApiVersion(eipDefinition);
        vpnProfiles = createVPNProfiles(context);
    }

    public void updateLoad(JSONObject load) {
        this.load = load;
    }

    private void addProfileInfos(Context context, HashMap<Connection.TransportType, VpnProfile> profiles) {
        Set<String> excludedAppsVpn = PreferenceHelper.getExcludedApps(context);
        for (VpnProfile profile : profiles.values()) {
            profile.mName = name;
            profile.mGatewayIp = gateway.optString(IP_ADDRESS);
            if (excludedAppsVpn != null) {
                profile.mAllowedAppsVpn = new HashSet<>(excludedAppsVpn);
            }
        }
    }

    private JSONObject getGeneralConfiguration(JSONObject eipDefinition) {
        try {
            return eipDefinition.getJSONObject(OPENVPN_CONFIGURATION);
        } catch (JSONException e) {
            return new JSONObject();
        }
    }

    private int getTimezone(JSONObject eipDefinition) {
        JSONObject location = getLocationInfo(eipDefinition);
        return location.optInt(TIMEZONE);
    }

    private int getApiVersion(JSONObject eipDefinition) {
        return eipDefinition.optInt(VERSION);
    }

    public String getRemoteIP() {
        return gateway.optString(IP_ADDRESS);
    }

    public String getHost() {
        return gateway.optString(HOST);
    }

    private String locationAsName(JSONObject eipDefinition) {
        JSONObject location = getLocationInfo(eipDefinition);
        return location.optString(NAME);
    }

    private JSONObject getLocationInfo(JSONObject eipDefinition) {
        try {
            JSONObject locations = eipDefinition.getJSONObject(LOCATIONS);

            return locations.getJSONObject(gateway.getString(LOCATION));
        } catch (JSONException e) {
            return new JSONObject();
        }
    }

    public boolean hasLoadInfo() {
        return load != null;
    }

    public double getFullness() {
        try {
            return load.getDouble(FULLNESS);
        } catch (JSONException | NullPointerException e) {
            return ConfigHelper.getConnectionQualityFromTimezoneDistance(timezone);
        }
    }

    public boolean isOverloaded() {
        try {
            return load.getBoolean(OVERLOAD);
        } catch (JSONException | NullPointerException e) {
            return false;
        }
    }

    /**
     * Create and attach the VpnProfile to our gateway object
     */
    private @NonNull HashMap<Connection.TransportType, VpnProfile> createVPNProfiles(Context context)
            throws ConfigParser.ConfigParseError, IOException, JSONException {
        boolean preferUDP = PreferenceHelper.getPreferUDP(context);
        boolean allowExperimentalTransports = PreferenceHelper.allowExperimentalTransports(context);
        VpnConfigGenerator vpnConfigurationGenerator = new VpnConfigGenerator(generalConfiguration, secrets, gateway, apiVersion, preferUDP, allowExperimentalTransports);
        HashMap<Connection.TransportType, VpnProfile> profiles = vpnConfigurationGenerator.generateVpnProfiles();
        addProfileInfos(context, profiles);
        return profiles;
    }

    public String getName() {
        return name;
    }

    public HashMap<Connection.TransportType, VpnProfile> getProfiles() {
        return vpnProfiles;
    }

    public VpnProfile getProfile(Connection.TransportType transportType) {
        return vpnProfiles.get(transportType);
    }

    public boolean supportsTransport(Connection.TransportType transportType) {
        if (transportType == Connection.TransportType.PT) {
            return supportsPluggableTransports();
        }
        return vpnProfiles.get(transportType) != null;
    }

    public HashSet<Connection.TransportType> getSupportedTransports() {
        return new HashSet<>(vpnProfiles.keySet());
    }

    public boolean supportsPluggableTransports() {
        for (Connection.TransportType transportType : vpnProfiles.keySet()) {
            if (transportType.isPluggableTransport() && vpnProfiles.get(transportType) != null) {
                return true;
            }
        }
        return false;
    }

    public int getTimezone() {
        return timezone;
    }

    @Override
    public String toString() {
        return new Gson().toJson(this, Gateway.class);
    }

}