Skip to content

Commit

Permalink
migrate shell to v2 and make termonal onexit optional
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Aug 1, 2024
1 parent 6af65d0 commit 6cd5e69
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 111 deletions.
49 changes: 49 additions & 0 deletions Android/app/src/main/java/com/dergoogler/core/NativeShell.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,72 @@
package com.dergoogler.core;

import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.dergoogler.plugin.TerminalPlugin;
import com.dergoogler.util.Json;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class NativeShell {
private static final String TAG = "NativeShell";
private final WebView wv;
ArrayList<String> output = new ArrayList<>();

public NativeShell(WebView wv) {
this.wv = wv;
}

@JavascriptInterface
public Object v2(String jsonArr) {
String[] cmds;
try {
cmds = Json.getStringArray(new JSONArray(jsonArr));
} catch (JSONException e) {
Log.e(TAG, e.toString());
return null;
}
Shell.Job shell = Shell.cmd(cmds);

final Shell.Result[] result = new Shell.Result[1];

return new Object() {

@JavascriptInterface
public void exec() {
result[0] = shell.exec();
}

@JavascriptInterface
public String result() {
List<String> out = shell.to(new ArrayList<>(), null).exec().getOut();
return ShellUtils.isValidOutput(out) ? out.get(out.size() - 1) : "";
}

@JavascriptInterface
public boolean isSuccess() {
return result[0].isSuccess();
}

@JavascriptInterface
public int getCode(String command) {
return result[0].getCode();
}


};
}


@JavascriptInterface
public void exec(String command) {
Shell.cmd(command).exec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.util.Log;

import com.dergoogler.util.Json;
import com.topjohnwu.superuser.io.SuFile;

import org.apache.cordova.CallbackContext;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void run(JSONObject envp, String cwd, String... command) throws IOExcepti

if (envp != null) {
Map<String, String> m = pb.environment();
m.putAll(toMap(envp));
m.putAll(Json.toMap(envp));
}

pb.directory(new SuFile(cwd));
Expand Down Expand Up @@ -112,30 +113,4 @@ private void sendUpdate(PluginResult.Status status, String line) {
this.terminalCallbackContext.sendPluginResult(result);
}
}

public static Map<String, String> toMap(JSONObject jsonobj) throws JSONException {
Map<String, String> map = new HashMap<String, String>();
Iterator<String> keys = jsonobj.keys();
while (keys.hasNext()) {
String key = keys.next();
String value = jsonobj.getString(key);
map.put(key, value);
}
return map;
}

public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if (value instanceof JSONArray) {
value = toList((JSONArray) value);
} else if (value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}

}
50 changes: 50 additions & 0 deletions Android/app/src/main/java/com/dergoogler/util/Json.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.dergoogler.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Json {
public static Map<String, String> toMap(JSONObject jsonobj) throws JSONException {
Map<String, String> map = new HashMap<String, String>();
Iterator<String> keys = jsonobj.keys();
while (keys.hasNext()) {
String key = keys.next();
String value = jsonobj.getString(key);
map.put(key, value);
}
return map;
}

public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if (value instanceof JSONArray) {
value = toList((JSONArray) value);
} else if (value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}

public static String[] getStringArray(JSONArray jsonArray) {
String[] stringArray = null;
if (jsonArray != null) {
int length = jsonArray.length();
stringArray = new String[length];
for (int i = 0; i < length; i++) {
stringArray[i] = jsonArray.optString(i);
}
}
return stringArray;
}
}
6 changes: 6 additions & 0 deletions Website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"react-render-tools": "^1.0.1",
"react-syntax-highlighter": "^15.5.0",
"react-zoom-pan-pinch": "^3.3.0",
"reflect-metadata": "^0.2.2",
"underscore": "^1.13.6",
"usehooks-ts": "^3.1.0",
"yaml": "^2.3.4"
Expand Down
12 changes: 6 additions & 6 deletions Website/src/native/IsolatedEval/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Chooser } from "@Native/Chooser";
import { SuZip } from "@Native/SuZip";
import { Terminal } from "@Native/Terminal";
import { Path } from "@Util/path.js";
import { transform } from "@babel/standalone";
Expand All @@ -11,16 +12,15 @@ import { Build } from "../Build";
import { BuildConfig, BuildConfigClass } from "../BuildConfig";
import { Native } from "../Native";
import { OsClass, os } from "../Os";
import { Shell, ShellClass } from "../Shell";
import { SuFile, SuFileConstuctor } from "../SuFile";
import { Shell } from "../Shell";
import { SuFile } from "../SuFile";
import { View, view } from "../View";
import { IsoAudio } from "./IsoAudio";
import { IsoDOMParser } from "./IsoDOMParser";
import { IsoDocument } from "./IsoDocument";
import { IsoXMLSerializer } from "./IsoXMLSerializer";
import { IsolatedEvalError } from "./IsolatedEvalError";
import { IsolatedFunctionBlockError } from "./IsolatedFunctionBlockError";
import { IsoDOMParser } from "./IsoDOMParser";
import { IsoXMLSerializer } from "./IsoXMLSerializer";
import { NativeSuZip, SuZip, SuZipConstuctor } from "@Native/SuZip";

type IsoModule = {
exports: {
Expand Down Expand Up @@ -123,12 +123,12 @@ class IsolatedEval<T = any> {
this._prototypeWhitelist.set(XMLSerializer, new Set());
this._prototypeWhitelist.set(SuFile, new Set());
this._prototypeWhitelist.set(SuZip, new Set());
this._prototypeWhitelist.set(ShellClass, new Set());
this._prototypeWhitelist.set(View, new Set());
this._prototypeWhitelist.set(OsClass, new Set());
this._prototypeWhitelist.set(BuildConfigClass, new Set());
this._prototypeWhitelist.set(Build, new Set());
this._prototypeWhitelist.set(Native, new Set());
this._prototypeWhitelist.set(Shell, new Set());
this._prototypeWhitelist.set(Terminal, new Set());
this._prototypeWhitelist.set(Chooser, new Set());
this._prototypeWhitelist.set(Path, new Set());
Expand Down
18 changes: 14 additions & 4 deletions Website/src/native/Native.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface INative<T = any> {
get interface(): T;
get userAgent(): string;
}

export type NativeArgumentTypes<F extends Function> = F extends (...args: infer A) => any ? A : never;
Expand All @@ -19,22 +18,33 @@ export class Native<I = any> implements INative<I> {
this._internal_interface = i;
}

private get userAgentRegex(): RegExp {
private static get userAgentRegex(): RegExp {
return /MMRL\/(.+)\s\(Linux;\sAndroid\s(.+);\s(.+)\sBuild\/(.+)\)/gs;
}

public get userAgent(): string {
public static get userAgent(): string {
return window.navigator.userAgent;
}

/**
* Determine if MMRL runs on a Android device
*/
public get isAndroid(): boolean {
public static get isAndroid(): boolean {
return this.userAgentRegex.test(this.userAgent) || window.hasOwnProperty("cordova") ? true : false;
}

/**
* Determine if MMRL runs on a Android device
*/
public get isAndroid(): boolean {
return Native.isAndroid;
}

public get interface(): I {
return this._internal_interface;
}

public static get interface() {
return Native.prototype.interface;
}
}
Loading

0 comments on commit 6cd5e69

Please sign in to comment.