1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
| public void testSftpListDir() { new UploadAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }
public static class UploadAsyncTask extends AsyncTask<Void, Void, Void> { private String TAG = "UploadAsyncTask"; private long totalLen = 0; private long sendByteLen = 0; private long lastSendByteLen = 0; private String user = "himalayas"; private String pwd = "1q2w3e4r5t"; private String fileName = "a.txt"; private boolean isExited = false; private String path = "ssh://himalayas:1q2w3e4r5t@10.168.66.1:22/bigdata/pullstashroot/ST202302230001007/test0";
private CountDownTimer timer;
public UploadAsyncTask() { timer = new CountDownTimer(1000 * 10000, 1000) { @Override public void onTick(long millisUntilFinished) { Log.i(TAG, "ssh send speed:" + (sendByteLen - lastSendByteLen) + "(byte/s)");
lastSendByteLen = sendByteLen; }
@Override public void onFinish() { Log.i(TAG, "ssh send speed:" + (sendByteLen - lastSendByteLen) + "(byte/s)"); lastSendByteLen = sendByteLen; } }; timer.start(); }
private void download() { CustomSshJConfig config = new CustomSshJConfig(); SSHClient client = new SSHClient((Config) config); try { client.addHostKeyVerifier(new HostKeyVerifier() { @Override public boolean verify(String hostname, int port, PublicKey key) { return true; }
@Override public List<String> findExistingAlgorithms(String hostname, int port) { return null; } }); client.connect("10.168.66.1", 22); client.authPassword(user, pwd);
SFTPClient sftp = client.newSFTPClient(); String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "com.xinan.ubox.debug"; // String srcPath = rootPath + "/de.apk"; // String srcPath = rootPath + "/de.m4a"; // String srcPath = rootPath + "/de.apk"; String srcPath = rootPath + File.separator + fileName; String destPath = "/bigdata/test" + File.separator + fileName; final RemoteFile file = sftp.getSFTPEngine().open(destPath, EnumSet.of(OpenMode.CREAT, OpenMode.WRITE)); OutputStream os = file.new RemoteFileOutputStream(0, 10) { @Override public void close() throws IOException { try { super.close(); } finally { file.close(); } } };
totalLen = new File(srcPath).length(); ReadableByteChannel inChannel = new RandomAccessFile(new File(srcPath), "r").getChannel(); WritableByteChannel outChannel = Channels.newChannel(os); doCopy(inChannel, outChannel);
inChannel.close(); outChannel.close();
// sftp.put(filePath, "/bigdata/test"); } catch (UserAuthException e) { e.printStackTrace(); } catch (TransportException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
private void listFolder() { CustomSshJConfig config = new CustomSshJConfig(); SSHClient client = new SSHClient((Config) config); try { client.addHostKeyVerifier(new HostKeyVerifier() { @Override public boolean verify(String hostname, int port, PublicKey key) { return true; }
@Override public List<String> findExistingAlgorithms(String hostname, int port) { return null; } }); client.connect("10.168.66.1", 22); client.authPassword(user, pwd);
SFTPClient sftp = client.newSFTPClient(); String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "com.xinan.ubox.debug"; HybridFileParcelable fileParcelable = new HybridFileParcelable(); fileParcelable.path = path; fileParcelable.isDirectory = true; List<HybridFileParcelable> sources = new ArrayList<>(); sources.add(fileParcelable); _doListDir(sftp, sources, new OnFileFound() { @Override public void onFileFound(HybridFileParcelable file) { Log.i(TAG, "onFileFound, name:" + file.name + " path:" + file.path + " dir:" + file.isDirectory + " size:" + file.size); }
@Override public void cancel() {
} }); } catch (UserAuthException e) { e.printStackTrace(); } catch (TransportException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
private void _doListDir(SFTPClient sftp, List<HybridFileParcelable> sources, OnFileFound fileFound) { ArrayList<HybridFileParcelable> list = new ArrayList<>(); for (HybridFileParcelable file: sources) { if (!file.isDirectory) { continue; }
String path = file.path; try { for (RemoteResourceInfo info : sftp.ls(extractRemotePathFrom(path))) { boolean isDirectory = false; try { isDirectory = isDirectory(sftp, info); } catch (IOException ifBrokenSymlink) { Log.e(TAG, "IOException checking isDirectory(): " + info.getPath()); continue; } // Log.i(TAG, "path:" + path + " isDir:" + isDirectory + " info:" + info.getPath());
HybridFileParcelable fileParcelable = new HybridFileParcelable(); fileParcelable.path = String.format("%s/%s", path, info.getName()); fileParcelable.name = info.getName(); fileParcelable.isDirectory = isDirectory; fileParcelable.size = isDirectory ? 0 : info.getAttributes().getSize(); if (isDirectory) { list.add(fileParcelable); } else { fileFound.onFileFound(fileParcelable); } } } catch (IOException e) { Log.e(TAG, "IOException", e); } }
if (list.size() > 0) { _doListDir(sftp, list, fileFound); } }
interface OnFileFound { void onFileFound(HybridFileParcelable file); void cancel(); }
public class HybridFileParcelable { private boolean isDirectory; public String path; public String name; public long date, size; }
public boolean isDirectory(@NonNull SFTPClient client, @NonNull RemoteResourceInfo info) throws IOException { boolean isDirectory = info.isDirectory(); if (info.getAttributes().getType().equals(FileMode.Type.SYMLINK)) { try { FileAttributes symlinkAttrs = client.stat(info.getPath()); isDirectory = symlinkAttrs.getType().equals(FileMode.Type.DIRECTORY); } catch (IOException ifSymlinkIsBroken) { Log.e(TAG, String.format("Symbolic link %s is broken, skipping", info.getPath())); throw ifSymlinkIsBroken; } } return isDirectory; }
public String extractRemotePathFrom(@NonNull String fullUri) { String hostPath = fullUri.substring(fullUri.indexOf('@')); return hostPath.indexOf('/') == -1 ? "/" : hostPath.substring(hostPath.indexOf('/')); }
@Override protected Void doInBackground(Void... voids) { listFolder();
return null; }
@Override protected void onPostExecute(Void unused) { super.onPostExecute(unused);
timer.cancel(); Log.i(TAG, "upload file finish:" + sendByteLen + " totalLen:" + totalLen); }
void doCopy( @NonNull ReadableByteChannel from, @NonNull WritableByteChannel to) throws IOException { final int DEFAULT_TRANSFER_QUANTUM = 65536; ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_TRANSFER_QUANTUM); long count = 0;
while ((from.read(buffer) != -1 || buffer.position() > 0) && !isExited) { buffer.flip(); count = to.write(buffer);
sendByteLen += count;
buffer.compact(); }
buffer.flip(); while (buffer.hasRemaining() && !isExited) { count = to.write(buffer); // Log.i("Copy", "buffer count:" + count); sendByteLen += count; }
from.close(); to.close(); } }
public static class CustomSshJConfig extends DefaultConfig { static { Security.removeProvider("BC"); Security.insertProviderAt(new BouncyCastleProvider(), 0); }
@Override protected void initCipherFactories() { List<Factory.Named<Cipher>> avail = new LinkedList<Factory.Named<Cipher>>(Arrays.<Factory.Named<Cipher>>asList( StreamCiphers.Arcfour(), BlockCiphers.BlowfishCBC(), ChachaPolyCiphers.CHACHA_POLY_OPENSSH(), BlockCiphers.AES128CBC(), BlockCiphers.AES128CTR(), BlockCiphers.AES192CBC(), BlockCiphers.AES192CTR(), BlockCiphers.AES256CBC(), BlockCiphers.AES256CTR(), GcmCiphers.AES128GCM(), GcmCiphers.AES256GCM(), BlockCiphers.BlowfishCTR(), BlockCiphers.Cast128CBC(), BlockCiphers.Cast128CTR(), BlockCiphers.IDEACBC(), BlockCiphers.IDEACTR(), BlockCiphers.Serpent128CBC(), BlockCiphers.Serpent128CTR(), BlockCiphers.Serpent192CBC(), BlockCiphers.Serpent192CTR(), BlockCiphers.Serpent256CBC(), BlockCiphers.Serpent256CTR(), BlockCiphers.TripleDESCBC(), BlockCiphers.TripleDESCTR(), BlockCiphers.Twofish128CBC(), BlockCiphers.Twofish128CTR(), BlockCiphers.Twofish192CBC(), BlockCiphers.Twofish192CTR(), BlockCiphers.Twofish256CBC(), BlockCiphers.Twofish256CTR(), BlockCiphers.TwofishCBC(), StreamCiphers.Arcfour128(), StreamCiphers.Arcfour256()) );
boolean warn = false; // Ref. https://issues.apache.org/jira/browse/SSHD-24 // "AES256 and AES192 requires unlimited cryptography extension" for (Iterator<Factory.Named<Cipher>> i = avail.iterator(); i.hasNext(); ) { final Factory.Named<Cipher> f = i.next(); try { final Cipher c = f.create(); final byte[] key = new byte[c.getBlockSize()]; final byte[] iv = new byte[c.getIVSize()]; c.init(Cipher.Mode.Encrypt, key, iv); } catch (Exception e) { warn = true; Log.e("CustomSshJConfig", e.getCause().getMessage()); i.remove(); } } if (warn) Log.w("CustomSshJConfig", "Disabling high-strength ciphers: cipher strengths apparently limited by JCE policy");
setCipherFactories(avail); Log.d("CustomSshJConfig", "Available cipher factories:" + avail); } }
|