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
|
package se.leap.bitmaskclient.providersetup;
import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_PROVIDER_API_EVENT;
import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_RESULT_CODE;
import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_RESULT_KEY;
import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_KEY;
import static se.leap.bitmaskclient.base.utils.ConfigHelper.getProviderFormattedString;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORID;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORS;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_DOWNLOADED_GEOIP_JSON;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_DOWNLOADED_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.INITIAL_ACTION;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.PROVIDER_NOK;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.TOR_EXCEPTION;
import static se.leap.bitmaskclient.providersetup.ProviderAPI.TOR_TIMEOUT;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.ResultReceiver;
import org.json.JSONException;
import org.json.JSONObject;
import de.blinkt.openvpn.core.VpnStatus;
import se.leap.bitmaskclient.base.models.Provider;
public class ProviderApiEventSender {
private final Resources resources;
private final ProviderApiManagerBase.ProviderApiServiceCallback serviceCallback;
public ProviderApiEventSender(Resources resources, ProviderApiManagerBase.ProviderApiServiceCallback callback) {
this.resources = resources;
this.serviceCallback = callback;
}
/**
* Interprets the error message as a JSON object and extract the "errors" keyword pair.
* If the error message is not a JSON object, then it is returned untouched.
*
* @param stringJsonErrorMessage
* @return final error message
*/
protected String pickErrorMessage(String stringJsonErrorMessage) {
String errorMessage = "";
try {
JSONObject jsonErrorMessage = new JSONObject(stringJsonErrorMessage);
errorMessage = jsonErrorMessage.getString(ERRORS);
} catch (JSONException e) {
// TODO Auto-generated catch block
errorMessage = stringJsonErrorMessage;
} catch (NullPointerException e) {
//do nothing
}
return errorMessage;
}
protected Bundle setErrorResult(Bundle result, String stringJsonErrorMessage) {
String reasonToFail = pickErrorMessage(stringJsonErrorMessage);
VpnStatus.logWarning("[API] error: " + reasonToFail);
result.putString(ERRORS, reasonToFail);
result.putBoolean(BROADCAST_RESULT_KEY, false);
return result;
}
Bundle setErrorResultAction(Bundle result, String initialAction) {
JSONObject errorJson = new JSONObject();
addErrorMessageToJson(errorJson, null, null, initialAction);
VpnStatus.logWarning("[API] error: " + initialAction + " failed.");
result.putString(ERRORS, errorJson.toString());
result.putBoolean(BROADCAST_RESULT_KEY, false);
return result;
}
Bundle setErrorResult(Bundle result, int errorMessageId, String errorId) {
return setErrorResult(result, errorMessageId, errorId, null);
}
Bundle setErrorResult(Bundle result, int errorMessageId, String errorId, String initialAction) {
JSONObject errorJson = new JSONObject();
String errorMessage = getProviderFormattedString(resources, errorMessageId);
addErrorMessageToJson(errorJson, errorMessage, errorId, initialAction);
VpnStatus.logWarning("[API] error: " + errorMessage);
result.putString(ERRORS, errorJson.toString());
result.putBoolean(BROADCAST_RESULT_KEY, false);
return result;
}
private void addErrorMessageToJson(JSONObject jsonObject, String errorMessage) {
try {
jsonObject.put(ERRORS, errorMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void addErrorMessageToJson(JSONObject jsonObject, String errorMessage, String errorId, String initialAction) {
try {
jsonObject.putOpt(ERRORS, errorMessage);
jsonObject.putOpt(ERRORID, errorId);
jsonObject.putOpt(INITIAL_ACTION, initialAction);
} catch (JSONException e) {
e.printStackTrace();
}
}
protected void sendToReceiverOrBroadcast(ResultReceiver receiver, int resultCode, Bundle resultData, Provider provider) {
if (resultData == null || resultData == Bundle.EMPTY) {
resultData = new Bundle();
}
resultData.putParcelable(PROVIDER_KEY, provider);
if (receiver != null) {
receiver.send(resultCode, resultData);
} else {
broadcastEvent(resultCode, resultData);
}
handleEventSummaryErrorLog(resultCode);
}
private void broadcastEvent(int resultCode , Bundle resultData) {
Intent intentUpdate = new Intent(BROADCAST_PROVIDER_API_EVENT);
intentUpdate.addCategory(Intent.CATEGORY_DEFAULT);
intentUpdate.putExtra(BROADCAST_RESULT_CODE, resultCode);
intentUpdate.putExtra(BROADCAST_RESULT_KEY, resultData);
serviceCallback.broadcastEvent(intentUpdate);
}
private void handleEventSummaryErrorLog(int resultCode) {
String event = null;
switch (resultCode) {
case INCORRECTLY_DOWNLOADED_VPN_CERTIFICATE:
event = "download of vpn certificate.";
break;
case PROVIDER_NOK:
event = "setup or update provider details.";
break;
case INCORRECTLY_DOWNLOADED_EIP_SERVICE:
event = "update eip-service.json";
break;
case INCORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE:
event = "update invalid vpn certificate.";
break;
case INCORRECTLY_DOWNLOADED_GEOIP_JSON:
event = "download menshen service json.";
break;
case TOR_TIMEOUT:
case TOR_EXCEPTION:
event = "start tor for censorship circumvention";
break;
default:
break;
}
if (event != null) {
VpnStatus.logWarning("[API] failed provider API event: " + event);
}
}
String formatErrorMessage(final int errorStringId) {
return formatErrorMessage(getProviderFormattedString(resources, errorStringId));
}
private String formatErrorMessage(String errorMessage) {
return "{ \"" + ERRORS + "\" : \"" + errorMessage + "\" }";
}
private JSONObject getErrorMessageAsJson(final int toastStringId) {
try {
return new JSONObject(formatErrorMessage(toastStringId));
} catch (JSONException e) {
e.printStackTrace();
return new JSONObject();
}
}
}
|