summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java
blob: 501543b8a0b7edd4257cdc417bb1fd00510f71e5 (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
/**
 * 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 java.util.*;

import de.blinkt.openvpn.core.*;

public class EipStatus extends Observable implements VpnStatus.StateListener {
    public static String TAG = EipStatus.class.getSimpleName();
    private static EipStatus current_status;

    private static ConnectionStatus level = ConnectionStatus.LEVEL_NOTCONNECTED;
    private static boolean
            wants_to_disconnect = false,
            is_connecting = false;

    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) {
        updateStatus(state, logmessage, localizedResId, level);
        if (isConnected() || isDisconnected() || wantsToDisconnect()) {
            setConnectedOrDisconnected();
        } else
            setConnecting();
    }

    @Override
    public void setConnectedVPN(String uuid) {
    }

    private void updateStatus(final String state, final String logmessage, final int localizedResId, final ConnectionStatus level) {
        current_status = getInstance();
        current_status.setState(state);
        current_status.setLogMessage(logmessage);
        current_status.setLocalizedResId(localizedResId);
        current_status.setLevel(level);
        current_status.setChanged();
    }

    public boolean wantsToDisconnect() {
        return wants_to_disconnect;
    }

    public boolean isConnecting() {
        return is_connecting;
    }

    public boolean isConnected() {
        return level == ConnectionStatus.LEVEL_CONNECTED;
    }

    public boolean isDisconnected() {
        return level == ConnectionStatus.LEVEL_NOTCONNECTED;
    }

    public boolean isPaused() {
        return level == ConnectionStatus.LEVEL_VPNPAUSED;
    }

    public void setConnecting() {
        is_connecting = true;

        wants_to_disconnect = false;
        current_status.setChanged();
        current_status.notifyObservers();
    }

    public void setConnectedOrDisconnected() {
        is_connecting = false;
        wants_to_disconnect = false;
        current_status.setChanged();
        current_status.notifyObservers();
    }

    public void setDisconnecting() {
        wants_to_disconnect = true;
        is_connecting = false;
    }

    public String getState() {
        return state;
    }

    public String getLogMessage() {
        return log_message;
    }

    public int getLocalizedResId() {
        return localized_res_id;
    }

    public ConnectionStatus getLevel() {
        return 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) {
        EipStatus.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: " + level.toString();
    }

}