Fixes live notifications and some issues with Tor onion URLs

This commit is contained in:
stom79 2018-01-24 16:27:52 +01:00
parent b058df8353
commit 4ec2f1af82
5 changed files with 123 additions and 44 deletions

View File

@ -340,10 +340,11 @@ public class LoginActivity extends BaseActivity {
editor.apply();
//Update the account with the token;
new UpdateAccountInfoAsyncTask(LoginActivity.this, token, instance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (JSONException ignored) {}
} catch (JSONException ignored) {ignored.printStackTrace();}
}
});
}catch (final Exception e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
connectionButton.setEnabled(true);

View File

@ -55,7 +55,7 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
try {
//At the state the instance can be encoded
instance = URLDecoder.decode(instance, "utf-8");
} catch (UnsupportedEncodingException ignored) {}
} catch (UnsupportedEncodingException ignored) {ignored.printStackTrace();}
SharedPreferences sharedpreferences = this.contextReference.get().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( token == null) {
token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);

View File

@ -189,8 +189,10 @@ public class API {
account = parseAccountResponse(context, new JSONObject(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
e.printStackTrace();
}catch (Exception e) {
setDefaultError(e);
e.printStackTrace();
}
return account;
}

View File

@ -107,47 +107,88 @@ public class HttpsConnection {
@SuppressWarnings("ConstantConditions")
public String get(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
Map<String,Object> params = new LinkedHashMap<>();
if( paramaters != null) {
Iterator it = paramaters.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
params.put(pair.getKey().toString(), pair.getValue());
it.remove();
if( urlConnection.startsWith("https://")) {
Map<String, Object> params = new LinkedHashMap<>();
if (paramaters != null) {
Iterator it = paramaters.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
params.put(pair.getKey().toString(), pair.getValue());
it.remove();
}
}
}
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(param.getKey());
postData.append('=');
postData.append(String.valueOf(param.getValue()));
}
URL url = new URL(urlConnection + "?" + postData);
if( proxy !=null )
httpsURLConnection = (HttpsURLConnection)url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection)url.openConnection();
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
if( token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
httpsURLConnection.setRequestMethod("GET");
String response;
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
response = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
}else {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
int responseCode = httpsURLConnection.getResponseCode();
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(param.getKey());
postData.append('=');
postData.append(String.valueOf(param.getValue()));
}
URL url = new URL(urlConnection + "?" + postData);
if (proxy != null)
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
httpsURLConnection.setRequestMethod("GET");
String response;
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
response = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
} else {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
}
getSinceMaxId();
httpsURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
return response;
}else {
Map<String,Object> params = new LinkedHashMap<>();
if( paramaters != null) {
Iterator it = paramaters.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
params.put(pair.getKey().toString(), pair.getValue());
it.remove();
}
}
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(param.getKey());
postData.append('=');
postData.append(String.valueOf(param.getValue()));
}
URL url = new URL(urlConnection + "?" + postData);
if( proxy !=null )
httpURLConnection = (HttpURLConnection)url.openConnection(proxy);
else
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(timeout * 1000);
httpURLConnection.setRequestProperty("http.keepAlive", "false");
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
if( token != null)
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
httpURLConnection.setRequestMethod("GET");
String response;
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
response = new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
}else {
String error = new String(ByteStreams.toByteArray(httpURLConnection.getErrorStream()));
int responseCode = httpURLConnection.getResponseCode();
httpURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
}
getSinceMaxId();
httpURLConnection.getInputStream().close();
return response;
}
getSinceMaxId();
httpsURLConnection.getInputStream().close();
return response;
}

View File

@ -45,7 +45,11 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@ -84,6 +88,8 @@ public class LiveNotificationService extends Service {
protected Account account;
private boolean stop = false;
private static HashMap<String, Boolean> isRunning = new HashMap<>();
private Proxy proxy;
public void onCreate() {
super.onCreate();
}
@ -95,11 +101,34 @@ public class LiveNotificationService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean proxyEnabled = sharedpreferences.getBoolean(Helper.SET_PROXY_ENABLED, false);
int type = sharedpreferences.getInt(Helper.SET_PROXY_TYPE, 0);
proxy = null;
if( proxyEnabled ){
String host = sharedpreferences.getString(Helper.SET_PROXY_HOST, "127.0.0.1");
int port = sharedpreferences.getInt(Helper.SET_PROXY_PORT, 8118);
if( type == 0 )
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
else
proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port));
final String login = sharedpreferences.getString(Helper.SET_PROXY_LOGIN, null);
final String pwd = sharedpreferences.getString(Helper.SET_PROXY_PASSWORD, null);
if( login != null) {
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
assert pwd != null;
return (new PasswordAuthentication(login,
pwd.toCharArray()));
}
};
Authenticator.setDefault(authenticator);
}
}
if( intent == null || intent.getBooleanExtra("stop", false) ) {
stop = true;
stopSelf();
}
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
String userId;
@ -166,7 +195,10 @@ public class LiveNotificationService extends Service {
if (Helper.instanceWithProtocol(account.getInstance()).startsWith("https")) {
try {
URL url = new URL("https://" + account.getInstance() + "/api/v1/streaming/user");
httpsURLConnection = (HttpsURLConnection) url.openConnection();
if( proxy != null)
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + account.getToken());
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
@ -252,8 +284,11 @@ public class LiveNotificationService extends Service {
}
}else {
try {
URL url = new URL("https://" + account.getInstance() + "/api/v1/streaming/user");
httpURLConnection = (HttpURLConnection) url.openConnection();
URL url = new URL("http://" + account.getInstance() + "/api/v1/streaming/user");
if( proxy != null)
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
else
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Authorization", "Bearer " + account.getToken());
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");