Skip to content

Commit 2ce5856

Browse files
committed
chore: update ssh lib to 3.1.2 from 3.0.6
- updated ssh library - few bug fixes - removed deprecated apis - experimental logging of native logs in js logger
1 parent e1d829e commit 2ce5856

File tree

4 files changed

+77
-27
lines changed

4 files changed

+77
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
.DS_Store
1212
pnpm-lock.yaml
1313
.zed
14+
.idea

src/plugins/sftp/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
</platform>
2626

2727
<framework src="commons-io:commons-io:2.11.0" />
28-
<framework src="com.sshtools:maverick-synergy-client:3.0.6" />
28+
<framework src="com.sshtools:maverick-synergy-client:3.1.2" />
2929
</plugin>

src/plugins/sftp/src/com/foxdebug/sftp/Sftp.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import android.net.Uri;
77
import android.util.Log;
88
import androidx.documentfile.provider.DocumentFile;
9-
import com.sshtools.client.PublicKeyAuthenticator;
109
import com.sshtools.client.SshClient;
10+
import com.sshtools.client.SshClient.SshClientBuilder;
1111
import com.sshtools.client.sftp.SftpClient;
12+
import com.sshtools.client.sftp.SftpClient.SftpClientBuilder;
1213
import com.sshtools.client.sftp.SftpFile;
1314
import com.sshtools.client.sftp.TransferCancelledException;
1415
import com.sshtools.common.permissions.PermissionDeniedException;
@@ -51,6 +52,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
5152
super.initialize(cordova, webView);
5253
context = cordova.getContext();
5354
activity = cordova.getActivity();
55+
System.setProperty("maverick.log.nothread", "true");
5456
}
5557

5658
public boolean execute(
@@ -91,10 +93,18 @@ public void run() {
9193
int port = args.optInt(1);
9294
String username = args.optString(2);
9395
String password = args.optString(3);
94-
ssh = new SshClient(host, port, username, password.toCharArray());
96+
ssh = SshClientBuilder.create()
97+
.withHostname(host)
98+
.withPort(port)
99+
.withUsername(username)
100+
.withPassword(password)
101+
.build();
102+
95103
if (ssh.isConnected()) {
96104
connectionID = username + "@" + host;
97-
sftp = new SftpClient(ssh);
105+
106+
sftp = SftpClientBuilder.create().withClient(ssh).build();
107+
98108
try {
99109
sftp.getSubsystemChannel().setCharsetEncoding("UTF-8");
100110
} catch (UnsupportedEncodingException | SshException e) {
@@ -139,22 +149,18 @@ public void run() {
139149
Uri uri = file.getUri();
140150
ContentResolver contentResolver = context.getContentResolver();
141151
InputStream in = contentResolver.openInputStream(uri);
142-
SshKeyPair pair = SshKeyUtils.getPrivateKey(in, passphrase);
143-
144-
try {
145-
pair = SshKeyUtils.makeRSAWithSHA256Signature(pair);
146-
pair = SshKeyUtils.makeRSAWithSHA512Signature(pair);
147-
} catch (Exception e) {
148-
// ignore
149-
}
150-
151-
ssh = new SshClient(host, port, username);
152152

153-
ssh.authenticate(new PublicKeyAuthenticator(pair), 30000);
153+
ssh = SshClientBuilder.create()
154+
.withHostname(host)
155+
.withPort(port)
156+
.withUsername(username)
157+
.withIdentities(SshKeyUtils.getPrivateKey(in, passphrase))
158+
.build();
154159

155160
if (ssh.isConnected()) {
156161
connectionID = username + "@" + host;
157-
sftp = new SftpClient(ssh);
162+
sftp = SftpClientBuilder.create().withClient(ssh).build();
163+
158164
try {
159165
sftp.getSubsystemChannel().setCharsetEncoding("UTF-8");
160166
} catch (UnsupportedEncodingException | SshException e) {
@@ -190,7 +196,6 @@ public void run() {
190196
try {
191197
String command = args.optString(0);
192198
if (ssh != null) {
193-
callback.success("hiiii");
194199
JSONObject res = new JSONObject();
195200
StringBuffer buffer = new StringBuffer();
196201
int code = ssh.executeCommandWithResult(command, buffer);
@@ -331,21 +336,23 @@ public void run() {
331336
if (filename.equals(".") || filename.equals("..")) {
332337
continue;
333338
}
334-
SftpFileAttributes fileAttributes = file.getAttributes();
339+
SftpFileAttributes fileAttributes = file.attributes();
335340
JSONObject fileInfo = new JSONObject();
336341
fileInfo.put("name", filename);
337342
fileInfo.put("exists", true);
338-
fileInfo.put("canRead", file.canRead());
339-
fileInfo.put("canWrite", file.canWrite());
340343

341344
if (fileAttributes != null) {
342-
String permissions = fileAttributes.getPermissionsString();
345+
String permissions = fileAttributes.toPermissionsString();
346+
boolean canRead = permissions.charAt(1) == 'r';
347+
boolean canWrite = permissions.charAt(2) == 'w';
348+
fileInfo.put("canRead", canRead);
349+
fileInfo.put("canWrite", canWrite);
343350
fileInfo.put("permissions", permissions);
344-
fileInfo.put("length", fileAttributes.getSize());
351+
fileInfo.put("length", fileAttributes.size());
345352
fileInfo.put("url", file.getAbsolutePath());
346353
fileInfo.put(
347354
"lastModified",
348-
fileAttributes.getModifiedDateTime()
355+
fileAttributes.lastModifiedTime()
349356
);
350357

351358
if (permissions.charAt(0) == 'l') {
@@ -405,7 +412,7 @@ public void run() {
405412
try {
406413
SftpFileAttributes fileAttributes = sftp.stat(uri.getPath());
407414
if (fileAttributes != null) {
408-
String permissions = fileAttributes.getPermissionsString();
415+
String permissions = fileAttributes.toPermissionsString();
409416
boolean canRead = permissions.charAt(1) == 'r';
410417
boolean canWrite = permissions.charAt(2) == 'w';
411418

@@ -415,14 +422,14 @@ public void run() {
415422
fileStat.put("isLink", fileAttributes.isLink());
416423
fileStat.put("isDirectory", fileAttributes.isDirectory());
417424
fileStat.put("isFile", fileAttributes.isFile());
418-
fileStat.put("length", fileAttributes.getSize());
425+
fileStat.put("length", fileAttributes.size());
419426
fileStat.put(
420427
"permissions",
421-
fileAttributes.getPermissionsString()
428+
fileAttributes.toPermissionsString()
422429
);
423430
fileStat.put(
424431
"lastModified",
425-
fileAttributes.getModifiedDateTime()
432+
fileAttributes.lastModifiedTime()
426433
);
427434
String[] pathSegments = uri.getPath().split("/");
428435
String filename = pathSegments[pathSegments.length - 1];

src/plugins/system/android/com/foxdebug/system/System.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import androidx.core.graphics.drawable.IconCompat;
3232
import com.foxdebug.system.Ui.Theme;
3333
import java.io.File;
34+
import java.io.PrintWriter;
35+
import java.io.StringWriter;
3436
import java.lang.reflect.InvocationTargetException;
3537
import java.lang.reflect.Method;
3638
import java.nio.ByteBuffer;
@@ -65,6 +67,31 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
6567
this.context = cordova.getContext();
6668
this.activity = cordova.getActivity();
6769
this.webView = webView;
70+
71+
// Set up global exception handler
72+
Thread.setDefaultUncaughtExceptionHandler(
73+
new Thread.UncaughtExceptionHandler() {
74+
@Override
75+
public void uncaughtException(Thread thread, Throwable ex) {
76+
StringWriter sw = new StringWriter();
77+
PrintWriter pw = new PrintWriter(sw);
78+
ex.printStackTrace(pw);
79+
String stackTrace = sw.toString();
80+
81+
String errorMsg = String.format(
82+
"Uncaught Exception: %s\nStack trace: %s",
83+
ex.getMessage(),
84+
stackTrace
85+
);
86+
87+
sendLogToJavaScript("error", errorMsg);
88+
89+
// rethrow to the default handler
90+
Thread.getDefaultUncaughtExceptionHandler()
91+
.uncaughtException(thread, ex);
92+
}
93+
}
94+
);
6895
}
6996

7097
public boolean execute(
@@ -210,6 +237,21 @@ public void run() {
210237
return true;
211238
}
212239

240+
private void sendLogToJavaScript(String level, String message) {
241+
final String js =
242+
"window.log('" + level + "', " + JSONObject.quote(message) + ");";
243+
cordova
244+
.getActivity()
245+
.runOnUiThread(
246+
new Runnable() {
247+
@Override
248+
public void run() {
249+
webView.loadUrl("javascript:" + js);
250+
}
251+
}
252+
);
253+
}
254+
213255
private void getConfiguration(CallbackContext callback) {
214256
try {
215257
JSONObject result = new JSONObject();

0 commit comments

Comments
 (0)