From f3613f8eb0e2cbaafca231407a97ebbd88ff7665 Mon Sep 17 00:00:00 2001 From: jens Date: Wed, 3 Jan 2024 16:24:21 +0100 Subject: [PATCH] triggerUrl with POST params --- .../java/com/jens/automation2/Action.java | 19 ++- .../java/com/jens/automation2/Actions.java | 34 ++--- .../com/jens/automation2/Miscellaneous.java | 124 +++++++++++++----- .../activity_manage_action_start_activity.xml | 15 ++- .../activity_manage_action_trigger_url.xml | 3 +- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values-it/strings.xml | 2 +- app/src/main/res/values-nl/strings.xml | 2 +- app/src/main/res/values-pl/strings.xml | 2 +- app/src/main/res/values-ru/strings.xml | 2 +- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- .../metadata/android/en-US/changelogs/138.txt | 6 +- 15 files changed, 136 insertions(+), 83 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index c2907826..0aa93f0d 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -23,6 +23,7 @@ public class Action public static final String actionParameters2SeparatorInner = "a2splitInner"; public static final String actionParameters2SeparatorOuter = "a2splitOuter"; public static final String vibrateSeparator = ","; + public static final String httpErrorDefaultText = "HTTP_ERROR"; public enum Action_Enum { @@ -691,9 +692,9 @@ public class Action String[] components; if(getParameter2().contains(Action.actionParameter2Split)) - components = getParameter2().split(Action.actionParameter2Split); + components = getParameter2().split(Action.actionParameter2Split, -1); else - components = getParameter2().split(";"); + components = getParameter2().split(";", -1); if(components.length >= 3) { @@ -720,11 +721,15 @@ public class Action Actions myAction = new Actions(); Miscellaneous.logEvent("i", "HTTP", "Attempting download of " + url, 4); //getResources().getString("attemptingDownloadOf"); - + + /* + Theoretically credentials could be saved, but authentication has been turned off afterwards. + The following if clause is there to force username and password to be null. + */ if(this.getParameter1()) // use authentication new DownloadTask().execute(url, username, password, method, params); else - new DownloadTask().execute(url, null, null, params); + new DownloadTask().execute(url, null, null, method, params); } catch(Exception e) { @@ -754,7 +759,7 @@ public class Action if(parameters.length >= 4) method = parameters[3]; -empty params are omitted instead of being empty + if(parameters.length >= 5) { // has params @@ -768,12 +773,12 @@ empty params are omitted instead of being empty } - String response = "HTTP_ERROR"; + String response = httpErrorDefaultText; if(Settings.httpAttempts < 1) Miscellaneous.logEvent("w", "HTTP Request", Miscellaneous.getAnyContext().getResources().getString(R.string.cantDownloadTooFewRequestsInSettings), 3); - while(attempts <= Settings.httpAttempts && response.equals("httpError")) + while(attempts <= Settings.httpAttempts && response.equals(httpErrorDefaultText)) { Miscellaneous.logEvent("i", "HTTP Request", "Attempt " + String.valueOf(attempts++) + " of " + String.valueOf(Settings.httpAttempts), 3); diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index 773894a8..b2dd8331 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -986,7 +986,7 @@ public class Actions public void useDownloadedWebpage(String result) { // Toast.makeText(context, "Result: " + result, Toast.LENGTH_LONG).show(); - Actions.setVariable("LAST_TRIGGERURL_RESULT" + Action.actionParameter2Split + result); + Actions.setVariable("last_triggerurl_result" + Action.actionParameter2Split + result); } public static HttpClient getInsecureSslClient(HttpClient client) @@ -1984,7 +1984,6 @@ public class Actions boolean suAvailable = false; String suVersion = null; String suVersionInternal = null; -// List suResult = null; int suResult; boolean success = false; @@ -1998,12 +1997,15 @@ public class Actions suVersionInternal = Shell.SU.version(true); Miscellaneous.logEvent("i", "executeCommandViaSu()", "suVersion: " + suVersion + ", suVersionInternal: " + suVersionInternal, 5); + Miscellaneous.logEvent("i", "executeCommandViaSu()", "calling method: " + Miscellaneous.getCallingMethodName(), 5); -// suResult = Shell.SU.run(commands); suResult = Shell.Pool.SU.run(commands); -// if (suResult != null) -// success = true; + if(Miscellaneous.getCallingMethodName().equals("runExecutable")) + { + Actions.setVariable("last_run_executable_exit_code" + Action.actionParameter2Split + String.valueOf(suResult)); +// Actions.setVariable("last_run_executable_output" + Action.actionParameter2Split + (String) result[1]); + } Miscellaneous.logEvent("i", "executeCommandViaSu()", "RC=" + String.valueOf(suResult), 3); @@ -2067,7 +2069,10 @@ public class Actions else result = runExternalApplication(path, 0, workingDir, null); - boolean execResult = (boolean) result[0]; + boolean execResult = ((int) result[0] == 0); + + Actions.setVariable("last_run_executable_exit_code" + Action.actionParameter2Split + String.valueOf((int) result[0])); + Actions.setVariable("last_run_executable_output" + Action.actionParameter2Split + (String) result[1]); return execResult; } @@ -2115,19 +2120,6 @@ public class Actions stderr = process.getErrorStream (); stdout = process.getInputStream (); - // "write" the parms into stdin - /*line = "param1" + "\n"; - stdin.write(line.getBytes() ); - stdin.flush(); - - line = "param2" + "\n"; - stdin.write(line.getBytes() ); - stdin.flush(); - - line = "param3" + "\n"; - stdin.write(line.getBytes() ); - stdin.flush();*/ - stdin.close(); // clean up if any output in stdout @@ -2192,10 +2184,6 @@ public class Actions Miscellaneous.logEvent("i", "Running executable", "Error running external application.", 1); -// if(slotMap != null) -// for(String key : slotMap.keySet()) -// System.clearProperty(key); - return null; } diff --git a/app/src/main/java/com/jens/automation2/Miscellaneous.java b/app/src/main/java/com/jens/automation2/Miscellaneous.java index b33b8ac8..1c838822 100644 --- a/app/src/main/java/com/jens/automation2/Miscellaneous.java +++ b/app/src/main/java/com/jens/automation2/Miscellaneous.java @@ -43,12 +43,15 @@ import com.jens.automation2.receivers.PhoneStatusListener; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; +import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; @@ -71,6 +74,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.lang.Thread.UncaughtExceptionHandler; @@ -116,6 +120,8 @@ import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import androidx.documentfile.provider.DocumentFile; +import eu.chainfire.libsuperuser.Shell; + public class Miscellaneous extends Service { protected static String writeableFolderStringCache = null; @@ -153,7 +159,25 @@ public class Miscellaneous extends Service } else if(method.equals(ActivityManageActionTriggerUrl.methodPost)) connection.setRequestMethod("POST"); - + + if(httpParams.size() > 0) + { + connection.setRequestMethod("POST"); + connection.setDoInput(true); + connection.setDoOutput(true); + + List paramPairs = new ArrayList(); + + for(String key : httpParams.keySet()) + paramPairs.add(new BasicNameValuePair(key, httpParams.get(key))); + + OutputStream os = connection.getOutputStream(); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); + writer.write(getQuery(paramPairs)); + writer.flush(); + writer.close(); + } + InputStream content = (InputStream)connection.getInputStream(); BufferedReader in = new BufferedReader (new InputStreamReader (content)); String line; @@ -178,6 +202,26 @@ public class Miscellaneous extends Service return responseBody.toString(); } } + + private static String getQuery(List params) throws UnsupportedEncodingException + { + StringBuilder result = new StringBuilder(); + boolean first = true; + + for (NameValuePair pair : params) + { + if (first) + first = false; + else + result.append("&"); + + result.append(URLEncoder.encode(pair.getName(), "UTF-8")); + result.append("="); + result.append(URLEncoder.encode(pair.getValue(), "UTF-8")); + } + + return result.toString(); + } public static String downloadURLwithoutCertificateChecking(String url, String username, String password, String method, Map httpParams) { @@ -194,18 +238,12 @@ public class Miscellaneous extends Service method.equals(ActivityManageActionTriggerUrl.methodPost) || (username != null && password != null) + || + httpParams.size() > 0 ) httpRequest = new HttpPost(url); else httpRequest = new HttpGet(url); - /*httpRequest = new HttpEntityEnclosingRequestBase() - { - @Override - public String getMethod() - { - return "GET"; - } - };*/ // Add http simple authentication if specified if(username != null && password != null) @@ -213,6 +251,16 @@ public class Miscellaneous extends Service String encodedCredentials = Base64.encodeToString(new String(username + ":" + password).getBytes(), Base64.DEFAULT); httpRequest.addHeader("Authorization", "Basic " + encodedCredentials); } + + if(httpParams.size() > 0) + { + List paramPairs = new ArrayList(); + + for(String key : httpParams.keySet()) + paramPairs.add(new BasicNameValuePair(key, httpParams.get(key))); + + ((HttpPost)httpRequest).setEntity(new UrlEncodedFormEntity(paramPairs, "UTF-8")); + } HttpResponse response = httpclient.execute(httpRequest); HttpEntity entity = response.getEntity(); @@ -841,36 +889,40 @@ public class Miscellaneous extends Service */ public static boolean isPhoneRooted() { -// if(true) -// return true; - - // get from build info - String buildTags = Build.TAGS; - if (buildTags != null && buildTags.contains("test-keys")) - { - return true; - } - - // check if /system/app/Superuser.apk is present try { - File file = new File("/system/app/Superuser.apk"); - if (file.exists()) + return Shell.SU.available(); + } + catch(Exception e) + { + // get from build info + String buildTags = Build.TAGS; + if (buildTags != null && buildTags.contains("test-keys")) { return true; } - } - catch (Exception e1) - { - // ignore - } - // try executing commands - return canExecuteCommand("/system/xbin/which su") - || - canExecuteCommand("/system/bin/which su") - || - canExecuteCommand("which su"); + // check if /system/app/Superuser.apk is present + try + { + File file = new File("/system/app/Superuser.apk"); + if (file.exists()) + { + return true; + } + } + catch (Exception e1) + { + // ignore + } + + // try executing commands + return canExecuteCommand("/system/xbin/which su") + || + canExecuteCommand("/system/bin/which su") + || + canExecuteCommand("which su"); + } } // executes a command on the system @@ -2079,4 +2131,10 @@ public class Miscellaneous extends Service return output; } + + public static String getCallingMethodName() + { + StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[4]; + return callingFrame.getMethodName(); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_manage_action_start_activity.xml b/app/src/main/res/layout/activity_manage_action_start_activity.xml index a273b501..a812cbab 100644 --- a/app/src/main/res/layout/activity_manage_action_start_activity.xml +++ b/app/src/main/res/layout/activity_manage_action_start_activity.xml @@ -335,6 +335,14 @@ android:layout_marginVertical="@dimen/default_margin" android:text="@string/intentParametersHint" /> + + - -