4242
4343public class Sftp extends CordovaPlugin {
4444
45+ private static final String TAG = "SFTP" ;
4546 private SshClient ssh ;
4647 private SftpClient sftp ;
4748 private Context context ;
@@ -93,6 +94,10 @@ public void run() {
9394 int port = args .optInt (1 );
9495 String username = args .optString (2 );
9596 String password = args .optString (3 );
97+ Log .d (
98+ TAG ,
99+ "Connecting to " + host + ":" + port + " as " + username
100+ );
96101 ssh = SshClientBuilder .create ()
97102 .withHostname (host )
98103 .withPort (port )
@@ -103,27 +108,48 @@ public void run() {
103108 if (ssh .isConnected ()) {
104109 connectionID = username + "@" + host ;
105110
106- sftp = SftpClientBuilder .create ().withClient (ssh ).build ();
111+ try {
112+ sftp = SftpClientBuilder .create ().withClient (ssh ).build ();
113+ } catch (IOException | SshException e ) {
114+ ssh .close ();
115+ callback .error (
116+ "Failed to initialize SFTP subsystem: " + errMessage (e )
117+ );
118+ Log .e (TAG , "Failed to initialize SFTP subsystem" , e );
119+ return ;
120+ }
107121
108122 try {
109123 sftp .getSubsystemChannel ().setCharsetEncoding ("UTF-8" );
110124 } catch (UnsupportedEncodingException | SshException e ) {
111125 // Fallback to default encoding if UTF-8 fails
126+ Log .w (
127+ TAG ,
128+ "Failed to set UTF-8 encoding, falling back to default" ,
129+ e
130+ );
112131 }
113132 callback .success ();
114- Log .d ("connectUsingPassword" , "Connected successfully" );
133+ Log .d (TAG , "Connected successfully to " + connectionID );
115134 return ;
116135 }
117136
118- callback .error ("Cannot connect" );
119- } catch (
120- UnresolvedAddressException
121- | SshException
122- | IOException
123- | PermissionDeniedException e
124- ) {
125- callback .error (errMessage (e ));
126- Log .e ("connectUsingPassword" , "Cannot connect" , e );
137+ callback .error ("Failed to establish SSH connection" );
138+ } catch (UnresolvedAddressException e ) {
139+ callback .error ("Cannot resolve host address" );
140+ Log .e (TAG , "Cannot resolve host address" , e );
141+ } catch (PermissionDeniedException e ) {
142+ callback .error ("Authentication failed: " + e .getMessage ());
143+ Log .e (TAG , "Authentication failed" , e );
144+ } catch (SshException e ) {
145+ callback .error ("SSH error: " + errMessage (e ));
146+ Log .e (TAG , "SSH error" , e );
147+ } catch (IOException e ) {
148+ callback .error ("I/O error: " + errMessage (e ));
149+ Log .e (TAG , "I/O error" , e );
150+ } catch (Exception e ) {
151+ callback .error ("Unexpected error: " + errMessage (e ));
152+ Log .e (TAG , "Unexpected error" , e );
127153 }
128154 }
129155 }
@@ -150,37 +176,73 @@ public void run() {
150176 ContentResolver contentResolver = context .getContentResolver ();
151177 InputStream in = contentResolver .openInputStream (uri );
152178
179+ SshKeyPair keyPair = null ;
180+ try {
181+ keyPair = SshKeyUtils .getPrivateKey (in , passphrase );
182+ } catch (InvalidPassphraseException e ) {
183+ callback .error ("Invalid passphrase for key file" );
184+ Log .e (TAG , "Invalid passphrase for key file" , e );
185+ return ;
186+ } catch (IOException e ) {
187+ callback .error ("Could not read key file: " + errMessage (e ));
188+ Log .e (TAG , "Could not read key file" , e );
189+ return ;
190+ }
191+
153192 ssh = SshClientBuilder .create ()
154193 .withHostname (host )
155194 .withPort (port )
156195 .withUsername (username )
157- .withIdentities (SshKeyUtils . getPrivateKey ( in , passphrase ) )
196+ .withIdentities (keyPair )
158197 .build ();
159198
160199 if (ssh .isConnected ()) {
161200 connectionID = username + "@" + host ;
162- sftp = SftpClientBuilder .create ().withClient (ssh ).build ();
201+ try {
202+ sftp = SftpClientBuilder .create ().withClient (ssh ).build ();
203+ } catch (IOException | SshException e ) {
204+ ssh .close ();
205+ callback .error (
206+ "Failed to initialize SFTP subsystem: " + errMessage (e )
207+ );
208+ Log .e (TAG , "Failed to initialize SFTP subsystem" , e );
209+ return ;
210+ }
163211
164212 try {
165213 sftp .getSubsystemChannel ().setCharsetEncoding ("UTF-8" );
166214 } catch (UnsupportedEncodingException | SshException e ) {
167215 // Fallback to default encoding if UTF-8 fails
216+ Log .w (
217+ TAG ,
218+ "Failed to set UTF-8 encoding, falling back to default" ,
219+ e
220+ );
168221 }
169222 callback .success ();
223+ Log .d (TAG , "Connected successfully to " + connectionID );
170224 return ;
171225 }
172226
173- callback .error ("Cannot connect" );
174- } catch (
175- InvalidPassphraseException
176- | UnresolvedAddressException
177- | SshException
178- | IOException
179- | SecurityException
180- | PermissionDeniedException e
181- ) {
182- callback .error (errMessage (e ));
183- Log .e ("connectUsingKeyFile" , "Cannot connect" , e );
227+ callback .error ("Failed to establish SSH connection" );
228+ } catch (UnresolvedAddressException e ) {
229+ callback .error ("Cannot resolve host address" );
230+ Log .e (TAG , "Cannot resolve host address" , e );
231+ } catch (PermissionDeniedException e ) {
232+ callback .error ("Authentication failed: " + e .getMessage ());
233+ Log .e (TAG , "Authentication failed" , e );
234+ } catch (SshException e ) {
235+ callback .error ("SSH error: " + errMessage (e ));
236+ Log .e (TAG , "SSH error" , e );
237+ } catch (IOException e ) {
238+ callback .error ("I/O error: " + errMessage (e ));
239+ Log .e (TAG , "I/O error" , e );
240+ } catch (SecurityException e ) {
241+ callback .error ("Security error: " + errMessage (e ));
242+ Log .e (TAG , "Security error" , e );
243+ } catch (Exception e ) {
244+ callback .error ("Unexpected error: " + errMessage (e ));
245+ Log .e (TAG , "Unexpected error" , e );
184246 }
185247 }
186248 }
0 commit comments