Skip to content
Snippets Groups Projects
Commit 65ce4677 authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- refactoring, extracted socket methods

parent 249763e8
No related branches found
No related tags found
No related merge requests found
package org.slub.rosetta.dps.repository.plugin; package org.slub.rosetta.dps.repository.plugin;
import java.io.File; import com.exlibris.dps.repository.plugin.virusChcek.VirusCheckPlugin;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Iterator;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import com.exlibris.core.infra.common.exceptions.logging.ExLogger;
// import com.exlibris.dps.repository.plugin.virusCheck; // import com.exlibris.dps.repository.plugin.virusCheck;
import com.exlibris.dps.repository.plugin.virusChcek.VirusCheckPlugin;
/** /**
* SLUBVirusCheckClamAVPlugin * SLUBVirusCheckClamAVPlugin
* * <p/>
* ClamScan, should use clamdscan variant to avoid initialization overhead * ClamScan, should use clamdscan variant to avoid initialization overhead
* * <p/>
* clamd-client opens a TCP-connection, see p18 in clamdoc.pdf * clamd-client opens a TCP-connection, see p18 in clamdoc.pdf
* or source at https://github.com/vrtadmin/clamav-devel/blob/master/clamdscan/client.c * or source at https://github.com/vrtadmin/clamav-devel/blob/master/clamdscan/client.c
* or source at https://github.com/vrtadmin/clamav-devel/blob/master/clamdscan/proto.c * or source at https://github.com/vrtadmin/clamav-devel/blob/master/clamdscan/proto.c
* code could also be copied from https://code.google.com/p/clamavj/source/browse/trunk/src/main/java/com/philvarner/clamavj/ClamScan.java?r=2 * code could also be copied from https://code.google.com/p/clamavj/source/browse/trunk/src/main/java/com/philvarner/clamavj/ClamScan.java?r=2
* *
* @author andreas.romeyke@slub-dresden.de (Andreas Romeyke) * @author andreas.romeyke@slub-dresden.de (Andreas Romeyke)
* @see * @see
*/ */
public class SLUBVirusCheckClamAVPlugin implements VirusCheckPlugin { public class SLUBVirusCheckClamAVPlugin implements VirusCheckPlugin {
//private static final ExLogger log = ExLogger.getExLogger(SLUBVirusCheckClamAVPlugin.class); //private static final ExLogger log = ExLogger.getExLogger(SLUBVirusCheckClamAVPlugin.class);
private static final int DEFAULT_CHUNK_SIZE = 2048; private static final int DEFAULT_CHUNK_SIZE = 2048;
private static final byte[] INSTREAM = "zINSTREAM\0".getBytes(); private static final byte[] INSTREAM = "zINSTREAM\0".getBytes();
private static final byte[] VERSION = "zVERSION\0".getBytes(); private static final byte[] VERSION = "zVERSION\0".getBytes();
private static final String RESPONSEOK = "stream: OK"; private static final String RESPONSEOK = "stream: OK";
private static final String FOUND_SUFFIX = "FOUND"; private static final String FOUND_SUFFIX = "FOUND";
private static final String STREAM_PREFIX = "stream: "; private static final String STREAM_PREFIX = "stream: ";
private enum Status { PASSED, FAILED }; private int timeout;
private String host;
private int timeout; private int port;
private String host; private String response;
private int port; private Status status = Status.FAILED;
private String response; private String signature = "";
private Status status = Status.FAILED; private enum Status {PASSED, FAILED};
private String signature = ""; /** constructor */
SLUBVirusCheckClamAVPlugin(String host, int port, int timeout) {
// constructor this.host = host;
SLUBVirusCheckClamAVPlugin (String host, int port, int timeout) { this.port = port;
this.host = host; this.timeout = timeout;
this.port = port; //log.info("SLUBVirusCheckPlugin instantiated with host=" + host + " port=" + port + " timeout=" + timeout);
this.timeout = timeout; System.out.println("SLUBVirusCheckPlugin instantiated with host=" + host + " port=" + port + " timeout=" + timeout);
//log.info("SLUBVirusCheckPlugin instantiated with host=" + host + " port=" + port + " timeout=" + timeout); }
System.out.println( "SLUBVirusCheckPlugin instantiated with host=" + host + " port=" + port + " timeout=" + timeout);
} // stand alone check
public static void main(String[] args) {
// getter, ex.: get Host, port, timeout SLUBVirusCheckClamAVPlugin plugin = new SLUBVirusCheckClamAVPlugin("127.0.0.1", 3310, 60);
protected String getHost() { return this.host; } System.out.println("Agent: " + plugin.getAgent());
protected int getPort() { return this.port; } for (String file : args) {
protected int getTimeOut() { return this.timeout; } plugin.scan(file);
protected String getSignature() { return this.signature; } System.out.println("RESULT: " + plugin.isVirusFree() + " SIGNATURE: " + plugin.getOutput());
protected Status getStatus() { return status; } }
}
// setter
protected void setSignature(String signature) { // getter, ex.: get Host, port, timeout
this.signature = signature; protected String getHost() {
} return this.host;
protected void setStatus(Status status) { }
this.status = status;
} protected int getPort() {
return this.port;
// scans a given file for viruses }
public void scan(String fileFullPath) {
try { protected int getTimeOut() {
// create a socket return this.timeout;
Socket socket = new Socket (); }
//socket.connect( new InetSocketAddress(getHost()));
socket.connect( new InetSocketAddress(getHost(), getPort())); protected String getSignature() {
try { return this.signature;
socket.setSoTimeout( getTimeOut() ); }
} catch (SocketException e) {
System.out.println( "Could not set socket timeout to " + getTimeOut() + "ms " + e); // setter
//log.error( "Could not set socket timeout to " + getTimeOut() + "ms", e); protected void setSignature(String signature) {
} this.signature = signature;
InputStream in = new FileInputStream( fileFullPath ); }
// send stream
DataOutputStream dos = null; protected Status getStatus() {
response = ""; return status;
try { }
dos = new DataOutputStream(socket.getOutputStream());
dos.write(INSTREAM); protected void setStatus(Status status) {
int read; this.status = status;
byte[] buffer = new byte[DEFAULT_CHUNK_SIZE]; }
while ((read = in.read(buffer))> 0) {
dos.writeInt(read);
dos.write(buffer, 0, read); private void writeStreamToStream(InputStream in, DataOutputStream dos, byte[] buffer) throws IOException {
} int read;
dos.writeInt(0); while ((read = in.read(buffer)) > 0) {
dos.flush(); dos.writeInt(read);
read = socket.getInputStream().read(buffer); dos.write(buffer, 0, read);
if (read > 0) response = new String(buffer, 0, read); }
} finally { dos.writeInt(0);
if (dos != null) try { dos.close(); } catch (IOException e) { }
// log.debug("exception closing DOS", e);
System.out.println( "exception closing DOS "+ e ); private Socket openSocket() throws IOException {
} // create a socket
try { socket.close(); } catch (IOException e) { Socket socket = new Socket();
// log.debug("exception closing socket", e); //socket.connect( new InetSocketAddress(getHost()));
System.out.println( "exception closing socket "+ e); socket.connect(new InetSocketAddress(getHost(), getPort()));
} try {
} socket.setSoTimeout(getTimeOut());
//log.debug( "Response: " + response); } catch (SocketException e) {
System.out.println("Response: " + response); System.out.println("Could not set socket timeout to " + getTimeOut() + "ms " + e);
// parse return code //log.error( "Could not set socket timeout to " + getTimeOut() + "ms", e);
String result = response.trim(); }
if (RESPONSEOK.equals( result ) ) { return socket;
setStatus(Status.PASSED); }
} else if ( result.endsWith(FOUND_SUFFIX) ) {
setStatus(Status.FAILED); private void closeSocket(Socket socket, DataOutputStream dos) {
setSignature(result.substring(STREAM_PREFIX.length(), result.lastIndexOf(FOUND_SUFFIX) - 1 )); if (dos != null) try {
} else { dos.close();
setStatus(Status.FAILED); } catch (IOException e) {
//log.warn("clamd protocol not fully implemented"); // log.debug("exception closing DOS", e);
System.out.println("clamd protocol not fully implemented"); System.out.println("exception closing DOS " + e);
} }
} catch (IOException e) { try {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e); socket.close();
System.out.println ("exception creation socket, clamd not available at host=" + host + "port=" + port + " " + e); } catch (IOException e) {
setStatus(Status.FAILED); // log.debug("exception closing socket", e);
setSignature("ERROR: clamd not available"); System.out.println("exception closing socket " + e);
} }
} }
// outcome of virus check
public String getOutput () { private void callSocketCommand(Socket socket, byte[] command) throws IOException {
return getSignature(); DataOutputStream dos = null;
} try {
dos = new DataOutputStream(socket.getOutputStream());
public String getAgent () { dos.write(command);
try { int read;
// create a socket byte[] buffer = new byte[DEFAULT_CHUNK_SIZE];
Socket socket = new Socket (); dos.flush();
//socket.connect( new InetSocketAddress(getHost())); read = socket.getInputStream().read(buffer);
socket.connect( new InetSocketAddress(getHost(), getPort())); if (read > 0) response = new String(buffer, 0, read);
try { } finally {
socket.setSoTimeout( getTimeOut() ); closeSocket(socket, dos);
} catch (SocketException e) { }
System.out.println( "Could not set socket timeout to " + getTimeOut() + "ms " + e); }
//log.error( "Could not set socket timeout to " + getTimeOut() + "ms", e);
}
DataOutputStream dos = null; private void callSocketCommandStream(Socket socket, byte[] command, InputStream in) throws IOException {
response = ""; DataOutputStream dos = null;
try { try {
dos = new DataOutputStream(socket.getOutputStream()); dos = new DataOutputStream(socket.getOutputStream());
dos.write(VERSION); dos.write(command);
int read; int read;
byte[] buffer = new byte[DEFAULT_CHUNK_SIZE]; byte[] buffer = new byte[DEFAULT_CHUNK_SIZE];
dos.flush(); writeStreamToStream(in, dos, buffer);
read = socket.getInputStream().read(buffer); dos.flush();
if (read > 0) response = new String(buffer, 0, read); read = socket.getInputStream().read(buffer);
} finally { if (read > 0) response = new String(buffer, 0, read);
if (dos != null) try { dos.close(); } catch (IOException e) { } finally {
// log.debug("exception closing DOS", e); closeSocket(socket, dos);
System.out.println( "exception closing DOS "+ e ); }
} }
try { socket.close(); } catch (IOException e) {
// log.debug("exception closing socket", e); // scans a given file for viruses
System.out.println( "exception closing socket "+ e); public void scan(String fileFullPath) {
} try {
} Socket socket = openSocket();
return response;
} catch (IOException e) { InputStream in = new FileInputStream(fileFullPath);
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e); // send stream
System.out.println ("exception creation socket, clamd not available at host=" + host + "port=" + port + " " + e); response = "";
setStatus(Status.FAILED); byte[] command = INSTREAM;
setSignature("ERROR: clamd not available"); callSocketCommandStream(socket, command, in);
return "ERROR: clamd not available"; in.close();
} //log.debug( "Response: " + response);
} System.out.println("Response: " + response);
// parse return code
public boolean isVirusFree() { String result = response.trim();
//return true; // dummy if (RESPONSEOK.equals(result)) {
return (Status.PASSED == getStatus()); setStatus(Status.PASSED);
} } else if (result.endsWith(FOUND_SUFFIX)) {
setStatus(Status.FAILED);
// stand alone check setSignature(result.substring(STREAM_PREFIX.length(), result.lastIndexOf(FOUND_SUFFIX) - 1));
public static void main(String [] args) { } else {
SLUBVirusCheckClamAVPlugin plugin = new SLUBVirusCheckClamAVPlugin( "127.0.0.1", 3310, 60); setStatus(Status.FAILED);
System.out.println("Agent: "+ plugin.getAgent() ); //log.warn("clamd protocol not fully implemented");
for (String file:args) { System.out.println("clamd protocol not fully implemented");
plugin.scan( file ); }
System.out.println("RESULT: " + plugin.isVirusFree() + " SIGNATURE: " + plugin.getOutput()); } catch (IOException e) {
} //log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
} System.out.println("exception creation socket, clamd not available at host=" + host + "port=" + port + " " + e);
setStatus(Status.FAILED);
setSignature("ERROR: clamd not available");
}
}
// outcome of virus check
public String getOutput() {
return getSignature();
}
public String getAgent() {
try {
// create a socket
Socket socket = openSocket();
byte[] command = VERSION;
response = "";
callSocketCommand(socket, command);
return response;
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
System.out.println("exception creation socket, clamd not available at host=" + host + "port=" + port + " " + e);
setStatus(Status.FAILED);
setSignature("ERROR: clamd not available");
return "ERROR: clamd not available";
}
}
public boolean isVirusFree() {
//return true; // dummy
return (Status.PASSED == getStatus());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment