summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java
blob: b1d7c994791c076d3dbee1faf9a9692598eef7d9 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
 * 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 android.content.*;
import android.os.AsyncTask;
import android.support.annotation.VisibleForTesting;

import java.util.*;

import de.blinkt.openvpn.core.*;

/**
 * EipStatus is a Singleton that represents a reduced set of a vpn's ConnectionStatus.
 * EipStatus changes it's state (EipLevel) when ConnectionStatus gets updated by OpenVpnService or
 * by VoidVpnService.
 */
public class EipStatus extends Observable implements VpnStatus.StateListener {
    public static String TAG = EipStatus.class.getSimpleName();
    private static EipStatus current_status;
    public enum EipLevel {
        CONNECTING,
        DISCONNECTING,
        CONNECTED,
        DISCONNECTED,
        BLOCKING,
        UNKNOWN
    }

    /**
     * vpn_level holds the connection status of the openvpn vpn and the traffic blocking
     * void vpn. LEVEL_BLOCKING is set when the latter vpn is up. All other states are set by
     * openvpn.
     */
    private ConnectionStatus vpn_level = ConnectionStatus.LEVEL_NOTCONNECTED;
    private EipLevel current_eip_level = EipLevel.DISCONNECTED;

    int last_error_line = 0;
    private String state, log_message;
    private int localized_res_id;

    public static EipStatus getInstance() {
        if (current_status == null) {
            current_status = new EipStatus();
            VpnStatus.addStateListener(current_status);
        }
        return current_status;
    }

    private EipStatus() {
    }

    @Override
    public void updateState(final String state, final String logmessage, final int localizedResId, final ConnectionStatus level) {
        ConnectionStatus tmp = current_status.getLevel();
        current_status = getInstance();
        current_status.setState(state);
        current_status.setLogMessage(logmessage);
        current_status.setLocalizedResId(localizedResId);
        current_status.setLevel(level);
        current_status.setEipLevel(level);
        if (tmp != current_status.getLevel()) {
            current_status.setChanged();
            current_status.notifyObservers();
        }
    }

    @Override
    public void setConnectedVPN(String uuid) {
    }


    private void setEipLevel(ConnectionStatus level) {
        switch (level) {
            case LEVEL_CONNECTED:
                current_eip_level = EipLevel.CONNECTED;
                break;
            case LEVEL_VPNPAUSED:
                throw new IllegalStateException("Ics-Openvpn's VPNPAUSED state is not supported by Bitmask");
            case LEVEL_CONNECTING_SERVER_REPLIED:
            case LEVEL_CONNECTING_NO_SERVER_REPLY_YET:
            case LEVEL_WAITING_FOR_USER_INPUT:
            case LEVEL_START:
                current_eip_level = EipLevel.CONNECTING;
                break;
            case LEVEL_AUTH_FAILED:
            case LEVEL_NOTCONNECTED:
                current_eip_level = EipLevel.DISCONNECTED;
                break;
            case LEVEL_NONETWORK:
            case LEVEL_BLOCKING:
                setEipLevelWithDelay(level);
                break;
            case UNKNOWN_LEVEL:
                current_eip_level = EipLevel.UNKNOWN; //??
                break;
        }
    }

    @VisibleForTesting
    EipLevel getEipLevel() {
        return current_eip_level;
    }

    /**
     * This is a debouncing method ignoring states that are valid for less than a second.
     * This way flickering UI changes can be avoided.
     *
     * @param futureLevel
     */
    private void setEipLevelWithDelay(ConnectionStatus futureLevel) {
        new DelayTask(current_status.getLevel(), futureLevel).execute();
    }

    private class DelayTask extends AsyncTask<Void, Void, Void> {

        private final ConnectionStatus currentLevel;
        private final ConnectionStatus futureLevel;

        public DelayTask(ConnectionStatus currentLevel, ConnectionStatus futureLevel) {
            this.currentLevel = currentLevel;
            this.futureLevel = futureLevel;
        }
        protected Void doInBackground(Void... levels) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.interrupted();
            }
            return null;
        }

        protected void onPostExecute(Void result) {;
            if (currentLevel == current_status.getLevel()) {
                switch (futureLevel) {
                    case LEVEL_NONETWORK:
                        current_eip_level = EipLevel.DISCONNECTED;
                        break;
                    case LEVEL_BLOCKING:
                        current_eip_level = EipLevel.BLOCKING;
                        break;
                    default:
                        break;
                }
                current_status.setChanged();
                current_status.notifyObservers();
            }
        }
    }

    public boolean isConnecting() {
        return current_eip_level == EipLevel.CONNECTING;
    }

    public boolean isConnected() {
        return current_eip_level == EipLevel.CONNECTED;
    }

    /**
     * @return true if current_eip_level is for at least a second {@link EipLevel#BLOCKING}.
     * See {@link #setEipLevelWithDelay(ConnectionStatus)}.
     */
    public boolean isBlocking() {
        return current_eip_level == EipLevel.BLOCKING;
    }

    /**
     *
     * @return true immediately after traffic blocking VoidVpn was established.
     */
    public boolean isBlockingVpnEstablished() {
        return vpn_level == ConnectionStatus.LEVEL_BLOCKING;
    }

    public boolean isDisconnected() {
        return current_eip_level == EipLevel.DISCONNECTED;
    }

    /**
     * ics-openvpn's paused state is not implemented yet
     * @return
     */
    @Deprecated
    public boolean isPaused() {
        return vpn_level == ConnectionStatus.LEVEL_VPNPAUSED;
    }

    public String getState() {
        return state;
    }

    public String getLogMessage() {
        return log_message;
    }

    public int getLocalizedResId() {
        return localized_res_id;
    }

    public ConnectionStatus getLevel() {
        return vpn_level;
    }

    private void setState(String state) {
        this.state = state;
    }

    private void setLogMessage(String log_message) {
        this.log_message = log_message;
    }

    private void setLocalizedResId(int localized_res_id) {
        this.localized_res_id = localized_res_id;
    }

    private void setLevel(ConnectionStatus level) {
        this.vpn_level = level;
    }

    public boolean errorInLast(int lines, Context context) {
        return !lastError(lines, context).isEmpty();
    }

    public String lastError(int lines, Context context) {
        String error = "";

        String[] error_keywords = {"error", "ERROR", "fatal", "FATAL"};

        LogItem[] log = VpnStatus.getlogbuffer();
        if(log.length < last_error_line)
            last_error_line = 0;
        String message = "";
        for (int i = 1; i <= lines && log.length > i; i++) {
            int line = log.length - i;
            LogItem log_item = log[line];
            message = log_item.getString(context);
            for (int j = 0; j < error_keywords.length; j++)
                if (message.contains(error_keywords[j]) && line > last_error_line) {
                    error = message;
                    last_error_line = line;
                }
        }

        return error;
    }

    @Override
    public String toString() {
        return "State: " + state + " Level: " + vpn_level.toString();
    }

}