📄 clamavscan.java
字号:
setAddresses(InetAddress.getAllByName(host)); nextAddressIndex = 0; } /** * Initializer for property port. */ protected void initPort() { String portParam = getInitParameter("port"); setPort((portParam == null) ? DEFAULT_PORT : Integer.parseInt(portParam)); if (isDebug()) { log("port: " + getPort()); } } /** * Getter for property port. * @return Value of property port. */ public int getPort() { return this.port; } /** * Setter for property port. * @param port New value of property port. */ public void setPort(int port) { this.port = port; } /** * Initializer for property maxPings. */ protected void initMaxPings() { String maxPingsParam = getInitParameter("maxPings"); setMaxPings((maxPingsParam == null) ? DEFAULT_MAX_PINGS : Integer.parseInt(maxPingsParam)); if (isDebug()) { log("maxPings: " + getMaxPings()); } } /** * Getter for property maxPings. * @return Value of property maxPings. */ public int getMaxPings() { return this.maxPings; } /** * Setter for property maxPings. * @param maxPings New value of property maxPings. */ public void setMaxPings(int maxPings) { this.maxPings = maxPings; } /** * Initializer for property pingIntervalMilli. */ protected void initPingIntervalMilli() { String pingIntervalMilliParam = getInitParameter("pingIntervalMilli"); setPingIntervalMilli((pingIntervalMilliParam == null) ? DEFAULT_PING_INTERVAL_MILLI : Integer.parseInt(pingIntervalMilliParam)); if (isDebug()) { log("pingIntervalMilli: " + getPingIntervalMilli()); } } /** * Getter for property pingIntervalMilli. * @return Value of property pingIntervalMilli. */ public int getPingIntervalMilli() { return this.pingIntervalMilli; } /** * Setter for property pingIntervalMilli. * @param pingIntervalMilli New value of property pingIntervalMilli. */ public void setPingIntervalMilli(int pingIntervalMilli) { this.pingIntervalMilli = pingIntervalMilli; } /** * Initializer for property streamBufferSize. */ protected void initStreamBufferSize() { String streamBufferSizeParam = getInitParameter("streamBufferSize"); setStreamBufferSize((streamBufferSizeParam == null) ? DEFAULT_STREAM_BUFFER_SIZE : Integer.parseInt(streamBufferSizeParam)); if (isDebug()) { log("streamBufferSize: " + getStreamBufferSize()); } } /** * Getter for property streamBufferSize. * @return Value of property streamBufferSize. */ public int getStreamBufferSize() { return this.streamBufferSize; } /** * Setter for property streamBufferSize. * @param streamBufferSize New value of property streamBufferSize. */ public void setStreamBufferSize(int streamBufferSize) { this.streamBufferSize = streamBufferSize; } /** * Indexed getter for property addresses. * @param index Index of the property. * @return Value of the property at <CODE>index</CODE>. */ protected InetAddress getAddresses(int index) { return this.addresses[index]; } /** * Getter for property addresses. * @return Value of property addresses. */ protected InetAddress[] getAddresses() { return this.addresses; } /** * Setter for property addresses. * @param addresses New value of property addresses. */ protected void setAddresses(InetAddress[] addresses) { this.addresses = addresses; } /** * Getter for property nextAddress. * * Gets the address of the next CLAMD server to connect to in this round, using round-robin. * Increments the nextAddressIndex for the next round. * @return Value of property address. */ protected synchronized InetAddress getNextAddress() { InetAddress address = getAddresses(nextAddressIndex); nextAddressIndex++; if (nextAddressIndex >= getAddressesCount()) { nextAddressIndex = 0; } return address; } /** * Getter for property addressesCount. * @return Value of property addressesCount. */ public int getAddressesCount() { return getAddresses().length; } /** * Gets a Socket connected to CLAMD. * * Will loop though the round-robin address list until the first one accepts * the connection. * @return a socket connected to CLAMD * @throws MessagingException if no CLAMD in the round-robin address list has accepted the connection */ protected Socket getClamdSocket() throws MessagingException { InetAddress address = null; Set usedAddresses = new HashSet(getAddressesCount()); for (;;) { // this do-while loop is needed because other threads could in the meantime // calling getNextAddress(), and because of that the current thread may skip // some working address do { if (usedAddresses.size() >= getAddressesCount()) { String logText = "Unable to connect to CLAMD. All addresses failed."; log(logText + " Giving up."); throw new MessagingException(logText); } address = getNextAddress(); } while (!usedAddresses.add(address)); try { // get the socket return new Socket(address, getPort()); } catch (IOException ioe) { log("Exception caught acquiring main socket to CLAMD on " + address + " on port " + getPort() + ": " + ioe.getMessage()); address = getNextAddress(); // retry continue; } } } /** * Mailet initialization routine. */ public void init() throws MessagingException { // check that all init parameters have been declared in allowedInitParameters checkInitParameters(getAllowedInitParameters()); try { initDebug(); if (isDebug()) { log("Initializing"); } initHost(); initPort(); initMaxPings(); initPingIntervalMilli(); initStreamBufferSize(); // If "maxPings is > ping the CLAMD server to check if it is up if (getMaxPings() > 0) { ping(); } } catch (Exception e) { log("Exception thrown", e); throw new MessagingException("Exception thrown", e); } } /** * Scans the mail. * * @param mail the mail to scan * @throws MessagingException if a problem arises */ public void service(Mail mail) throws MessagingException { // if already checked no action if (mail.getAttribute(MAIL_ATTRIBUTE_NAME) != null) { return; } MimeMessage mimeMessage = mail.getMessage(); if (mimeMessage == null) { log("Null MimeMessage. Will send to ghost"); // write mail info to log logMailInfo(mail); mail.setState(Mail.GHOST); return; } // get the socket Socket socket = getClamdSocket(); BufferedReader reader = null; PrintWriter writer = null; Socket streamSocket = null; BufferedOutputStream bos = null; try { // prepare the reader and writer for the commands reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ASCII")); writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); // write a request for a port to use for streaming out the data to scan writer.println("STREAM"); writer.flush(); // parse and get the "stream" port# int streamPort = getStreamPortFromAnswer(reader.readLine()); // get the "stream" socket and the related (buffered) output stream streamSocket = new Socket(socket.getInetAddress(), streamPort); bos = new BufferedOutputStream(streamSocket.getOutputStream(), getStreamBufferSize()); // stream out the message to the scanner mimeMessage.writeTo(bos); bos.flush(); bos.close(); streamSocket.close(); String answer = null; boolean virusFound = false; String logMessage = ""; for (;;) { answer = reader.readLine(); if (answer != null) { answer = answer.trim(); // if a virus is found the answer will be '... FOUND' if (answer.substring(answer.length() - FOUND_STRING.length()).equals(FOUND_STRING)) { virusFound = true; logMessage = answer + " (by CLAMD on " + socket.getInetAddress() + ")"; log(logMessage); } } else { break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -