Skip to content

Commit 73dd138

Browse files
committed
hidfuzzer: modernization 3/N
- improved error handling - move all UI code into Activity - favor synchronous SU with no synchronization when possible - remove back references in favor of callbacks - simplify lua interface - refactor code style
1 parent 516b37e commit 73dd138

23 files changed

+634
-789
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ android {
44
compileSdkVersion 30
55
defaultConfig {
66
applicationId "org.netdex.hidfuzzer"
7-
minSdkVersion 25
7+
minSdkVersion 26
88
targetSdkVersion 30
99
versionCode 100
1010
versionName '1.0.0'

app/src/main/assets/scripts/a.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
---
77

88
while not cancelled() do
9-
progress("idle")
9+
log("idle")
1010

1111
-- poll until /dev/hidg0 is writable
1212
while not cancelled() and not test() do delay(1000) end
1313
if cancelled() then break end
1414

15-
progress("running")
15+
log("running")
1616
delay(1000)
1717

1818
send_string("test")
1919

20-
progress("done")
20+
log("done")
2121
while not cancelled() and test() do
2222
delay(1000)
2323
end

app/src/main/assets/scripts/downloadrun.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ local file = ask("File to download?", "https://github.com/Netdex/FlyingCursors/r
1010
local runAs = should("Task UAC", "Launch exe as admin?");
1111

1212
while not cancelled() do
13-
progress("idle")
13+
log("idle")
1414

1515
-- poll until /dev/hidg0 is writable
1616
while not cancelled() and not test() do delay(1000) end
1717
if cancelled() then break end
1818

19-
progress("running")
19+
log("running")
2020
delay(1000)
2121

2222
log("opening powershell, runAs=" .. tostring(runAs))
@@ -49,7 +49,7 @@ while not cancelled() do
4949
"exit;\n"
5050
)
5151

52-
progress("done")
52+
log("done")
5353
while not cancelled() and test() do
5454
delay(1000)
5555
end

app/src/main/assets/scripts/fuzzer.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@
88
math.randomseed(os.time())
99

1010
while not cancelled() do
11-
progress("idle")
11+
log("idle")
1212

1313
-- poll until /dev/hidg0 is writable
1414
while not cancelled() and not test() do delay(1000) end
1515
if cancelled() then break end
1616

17-
progress("running")
17+
log("running")
1818
delay(1000)
1919

2020
while test() and not isCancelled() do
2121
kbuf = {}
2222
mbuf = {}
23-
for i=1,7 do kbuf[i] = math.random(0, )end
24-
for i=1,4 do end
25-
end
23+
--for i=1,7 do kbuf[i] = math.random(0, )end
24+
--for i=1,4 do end
25+
end
2626
kbuf = {}
2727
mbuf = {}
28-
progress("done")
28+
log("done")
2929
while not cancelled() and test() do
3030
delay(1000)
3131
end

app/src/main/assets/scripts/template.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
---
77

88
while not cancelled() do
9-
progress("idle")
9+
log("idle")
1010

1111
-- poll until /dev/hidg0 is writable
1212
while not cancelled() and not test() do delay(1000) end
1313
if cancelled() then break end
1414

15-
progress("running")
15+
log("running")
1616
delay(1000)
1717

18-
progress("done")
18+
log("done")
1919
while not cancelled() and test() do
2020
delay(1000)
2121
end

app/src/main/assets/scripts/wallpaper.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
--- wallpaper.lua: Changes a Windows 10 desktop wallpaper
66
---
77
---
8-
local file = ask("Wallpaper to download?", "https://i.imgur.com/UhjsQ3x.jpg")
8+
local file = ask("Wallpaper to download?", "https://i.redd.it/ur1mqcbpxou51.png")
99

1010
while not cancelled() do
11-
progress("idle")
11+
log("idle")
1212

1313
-- poll until /dev/hidg0 is writable
1414
while not cancelled() and not test() do delay(1000) end
1515
if cancelled() then break end
1616

17-
progress("running")
17+
log("running")
1818
delay(1000)
1919

2020
press_keys(kb.LSUPER, kb.R)
@@ -34,7 +34,7 @@ while not cancelled() do
3434
"[W.S]::SW(\"$Env:Temp\\b.jpg\")\n" ..
3535
"exit\n")
3636

37-
progress("done")
37+
log("done")
3838
while not cancelled() and test() do
3939
delay(1000)
4040
end

app/src/main/java/org/netdex/hidfuzzer/MainActivity.java

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
package org.netdex.hidfuzzer;
22

33
import android.os.Bundle;
4+
import android.os.Handler;
5+
import android.os.Looper;
6+
import android.text.Html;
7+
import android.view.View;
48
import android.widget.ArrayAdapter;
9+
import android.widget.ScrollView;
510
import android.widget.Spinner;
11+
import android.widget.TextView;
612
import android.widget.ToggleButton;
713

814
import androidx.appcompat.app.AppCompatActivity;
15+
import androidx.core.os.HandlerCompat;
916

10-
import org.netdex.hidfuzzer.ltask.HIDTask;
11-
import org.netdex.hidfuzzer.ltask.LuaTaskLoader;
17+
import org.netdex.hidfuzzer.gui.ConfirmDialog;
18+
import org.netdex.hidfuzzer.gui.PromptDialog;
19+
import org.netdex.hidfuzzer.ltask.AsyncIOBridge;
20+
import org.netdex.hidfuzzer.ltask.LuaHIDTask;
21+
import org.netdex.hidfuzzer.ltask.LuaHIDTaskFactory;
1222

1323
import java.io.File;
1424
import java.io.IOException;
1525
import java.util.ArrayList;
1626
import java.util.HashMap;
27+
import java.util.concurrent.ExecutorService;
28+
import java.util.concurrent.Executors;
1729

1830

1931
public class MainActivity extends AppCompatActivity {
@@ -25,7 +37,7 @@ public class MainActivity extends AppCompatActivity {
2537
static final HashMap<String, String> fTaskMap = new HashMap<>();
2638
static final ArrayList<String> fTaskSpinnerItems = new ArrayList<>();
2739

28-
private HIDTask mRunningTask;
40+
private LuaHIDTask mRunningTask;
2941

3042
protected void onCreate(Bundle savedInstanceState) {
3143
super.onCreate(savedInstanceState);
@@ -46,25 +58,77 @@ protected void onCreate(Bundle savedInstanceState) {
4658
e.printStackTrace();
4759
}
4860

61+
Handler handler = HandlerCompat.createAsync(Looper.getMainLooper());
62+
4963
// sort out the UI
50-
final ToggleButton btnPoll = findViewById(R.id.btnPoll);
51-
final Spinner spnTask = findViewById(R.id.spnTask);
64+
ToggleButton btnPoll = findViewById(R.id.btnPoll);
65+
Spinner spnTask = findViewById(R.id.spnTask);
66+
TextView logView = findViewById(R.id.txtLog);
67+
ScrollView scrollView = findViewById(R.id.scrollview);
5268

5369
ArrayAdapter<String> adapter = new ArrayAdapter<>(
5470
this, android.R.layout.simple_spinner_item, fTaskSpinnerItems);
5571
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
5672
spnTask.setAdapter(adapter);
5773

74+
AsyncIOBridge dialogIO = new AsyncIOBridge() {
75+
@Override
76+
public void onLogMessage(String s) {
77+
handler.post(() -> {
78+
logView.append(Html.fromHtml(s));
79+
logView.append("\n");
80+
scrollView.fullScroll(View.FOCUS_DOWN);
81+
});
82+
}
83+
84+
@Override
85+
public void onLogClear() {
86+
handler.post(() -> {
87+
logView.setText("");
88+
});
89+
}
90+
91+
@Override
92+
public void onSignal(Signal signal) {
93+
handler.post(()->{
94+
switch(signal){
95+
case DONE:
96+
btnPoll.setChecked(false);
97+
btnPoll.setEnabled(true);
98+
break;
99+
default:
100+
throw new IllegalStateException("Unexpected value: " + signal);
101+
}
102+
});
103+
}
104+
105+
@Override
106+
public boolean onConfirm(String title, String prompt) {
107+
ConfirmDialog dialog = new ConfirmDialog(MainActivity.this, title, prompt);
108+
return dialog.show();
109+
}
110+
111+
@Override
112+
public String onPrompt(String title, String def) {
113+
PromptDialog dialog = new PromptDialog(MainActivity.this, title, def);
114+
return dialog.show();
115+
}
116+
};
117+
118+
LuaHIDTaskFactory luaHIDTaskFactory = new LuaHIDTaskFactory(dialogIO);
119+
120+
ExecutorService executorService = Executors.newCachedThreadPool();
121+
58122
btnPoll.setOnCheckedChangeListener((buttonView, isChecked) -> {
59123
if (isChecked) {
60124
String selectedTask = (String) spnTask.getSelectedItem();
61125
String taskSrcFilePath = fTaskMap.get(selectedTask);
62-
mRunningTask = LuaTaskLoader.createTaskFromLuaFile(this, selectedTask, taskSrcFilePath);
63-
mRunningTask.execute();
126+
mRunningTask = luaHIDTaskFactory.createTaskFromLuaFile(this, selectedTask, taskSrcFilePath);
127+
executorService.execute(mRunningTask);
64128
} else {
65129
if (mRunningTask != null) {
66130
btnPoll.setEnabled(false);
67-
mRunningTask.cancel(false);
131+
mRunningTask.setCancelled(true);
68132
}
69133
}
70134
});

0 commit comments

Comments
 (0)