Exported ClamAV client to 3rd party package
This commit is contained in:
parent
a430d9a25e
commit
63068f6b1d
|
@ -10,6 +10,7 @@ use Utopia\Validator\Text;
|
||||||
use Utopia\Validator\HexColor;
|
use Utopia\Validator\HexColor;
|
||||||
use Utopia\Cache\Cache;
|
use Utopia\Cache\Cache;
|
||||||
use Utopia\Cache\Adapter\Filesystem;
|
use Utopia\Cache\Adapter\Filesystem;
|
||||||
|
use Appwrite\ClamAV\Network;
|
||||||
use Database\Database;
|
use Database\Database;
|
||||||
use Database\Validator\UID;
|
use Database\Validator\UID;
|
||||||
use Storage\Storage;
|
use Storage\Storage;
|
||||||
|
@ -18,7 +19,6 @@ use Storage\Validators\File;
|
||||||
use Storage\Validators\FileSize;
|
use Storage\Validators\FileSize;
|
||||||
use Storage\Compression\Algorithms\GZIP;
|
use Storage\Compression\Algorithms\GZIP;
|
||||||
use Resize\Resize;
|
use Resize\Resize;
|
||||||
use LibreOffice\LibreOffice;
|
|
||||||
use OpenSSL\OpenSSL;
|
use OpenSSL\OpenSSL;
|
||||||
|
|
||||||
Storage::addDevice('local', new Local('app-' . $project->getUid()));
|
Storage::addDevice('local', new Local('app-' . $project->getUid()));
|
||||||
|
@ -529,7 +529,7 @@ $utopia->post('/v1/storage/files')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$antiVirus = new \ClamAV\Network('clamav', 3310);
|
$antiVirus = new Network('clamav', 3310);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Models
|
* Models
|
||||||
|
@ -680,7 +680,7 @@ $utopia->get('/v1/storage/files/:fileId/scan')
|
||||||
|
|
||||||
$source = $compressor->decompress($source);
|
$source = $compressor->decompress($source);
|
||||||
|
|
||||||
$antiVirus = new \ClamAV\Network('clamav', 3310);
|
$antiVirus = new Network('clamav', 3310);
|
||||||
|
|
||||||
//var_dump($antiVirus->ping());
|
//var_dump($antiVirus->ping());
|
||||||
//var_dump($antiVirus->version());
|
//var_dump($antiVirus->version());
|
||||||
|
@ -701,7 +701,7 @@ $utopia->get('/v1/storage/scan')
|
||||||
{
|
{
|
||||||
$device = Storage::getDevice('local');
|
$device = Storage::getDevice('local');
|
||||||
|
|
||||||
$antiVirus = new \ClamAV\Network('clamav', 3310);
|
$antiVirus = new Network('clamav', 3310);
|
||||||
//var_dump($antiVirus->ping());
|
//var_dump($antiVirus->ping());
|
||||||
//var_dump($antiVirus->version());
|
//var_dump($antiVirus->version());
|
||||||
//var_dump($antiVirus->fileScan('/storage/uploads/app-1/5/9/f/e/59fecaed49645.pdf'));
|
//var_dump($antiVirus->fileScan('/storage/uploads/app-1/5/9/f/e/59fecaed49645.pdf'));
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Audit\\": "src/Audit",
|
"Audit\\": "src/Audit",
|
||||||
"Auth\\": "src/Auth",
|
"Auth\\": "src/Auth",
|
||||||
"ClamAV\\": "src/ClamAV",
|
|
||||||
"Database\\": "src/Database",
|
"Database\\": "src/Database",
|
||||||
"Event\\": "src/Event",
|
"Event\\": "src/Event",
|
||||||
"OpenSSL\\": "src/OpenSSL",
|
"OpenSSL\\": "src/OpenSSL",
|
||||||
|
@ -38,6 +37,8 @@
|
||||||
"ext-zlib": "*",
|
"ext-zlib": "*",
|
||||||
"ext-sockets": "*",
|
"ext-sockets": "*",
|
||||||
|
|
||||||
|
"appwrite/php-clamav": "master",
|
||||||
|
|
||||||
"utopia-php/framework": "master",
|
"utopia-php/framework": "master",
|
||||||
"utopia-php/abuse": "master",
|
"utopia-php/abuse": "master",
|
||||||
"utopia-php/cache": "master",
|
"utopia-php/cache": "master",
|
||||||
|
|
47
composer.lock
generated
47
composer.lock
generated
|
@ -4,8 +4,53 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "57d614ec24a3ebc4cee0e6fd1f4cebfb",
|
"content-hash": "c1d8468f36e74c0212272b585e01ffaf",
|
||||||
"packages": [
|
"packages": [
|
||||||
|
{
|
||||||
|
"name": "appwrite/php-clamav",
|
||||||
|
"version": "dev-master",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/appwrite/php-clamav.git",
|
||||||
|
"reference": "9298a2d490e064f0c72d86ecc9f4792a49f4288f"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/appwrite/php-clamav/zipball/9298a2d490e064f0c72d86ecc9f4792a49f4288f",
|
||||||
|
"reference": "9298a2d490e064f0c72d86ecc9f4792a49f4288f",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=7.1"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^7.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Appwrite\\ClamAV\\": "src/ClamAV"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Eldad Fux",
|
||||||
|
"email": "eldad@appwrite.io"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "ClamAV network and pipe client for PHP",
|
||||||
|
"keywords": [
|
||||||
|
"anti virus",
|
||||||
|
"appwrite",
|
||||||
|
"clamav",
|
||||||
|
"php"
|
||||||
|
],
|
||||||
|
"time": "2019-05-11 15:12:28"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "bacon/bacon-qr-code",
|
"name": "bacon/bacon-qr-code",
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
|
|
|
@ -44,7 +44,7 @@ services:
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
clamav:
|
clamav:
|
||||||
image: registry.gitlab.com/appwrite/appwrite/clamav:v1.0.0
|
image: appwrite/clamav:v1.0.2
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
influxdb:
|
influxdb:
|
||||||
|
|
|
@ -97,7 +97,7 @@ services:
|
||||||
- redis
|
- redis
|
||||||
|
|
||||||
clamav:
|
clamav:
|
||||||
image: registry.gitlab.com/appwrite/appwrite/clamav:v1.0.0
|
image: appwrite/clamav:v1.0.2
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
networks:
|
networks:
|
||||||
- clamav
|
- clamav
|
||||||
|
|
|
@ -1,112 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace ClamAV;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class ClamAV
|
|
||||||
*
|
|
||||||
* An abstract class that `ClamdPipe` and `ClamdNetwork` will inherit.
|
|
||||||
*
|
|
||||||
* @package ClamAV
|
|
||||||
*/
|
|
||||||
abstract class ClamAV {
|
|
||||||
|
|
||||||
const CLAMAV_MAXP = 20000;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
abstract protected function getSocket();
|
|
||||||
|
|
||||||
/* Send command to Clamd */
|
|
||||||
private function sendCommand($command)
|
|
||||||
{
|
|
||||||
$return = null;
|
|
||||||
|
|
||||||
$socket = $this->getSocket();
|
|
||||||
|
|
||||||
socket_send($socket, $command, strlen($command), 0);
|
|
||||||
socket_recv($socket, $return, self::CLAMAV_MAXP, 0);
|
|
||||||
socket_close($socket);
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* `ping` command is used to see whether Clamd is alive or not */
|
|
||||||
public function ping()
|
|
||||||
{
|
|
||||||
$return = $this->sendCommand('PING');
|
|
||||||
return strcmp($return, 'PONG') ? true : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* `version` is used to receive the version of Clamd */
|
|
||||||
public function version()
|
|
||||||
{
|
|
||||||
return trim($this->sendCommand('VERSION'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* `reload` Reload Clamd */
|
|
||||||
public function reload()
|
|
||||||
{
|
|
||||||
return $this->sendCommand('RELOAD');
|
|
||||||
}
|
|
||||||
|
|
||||||
/* `shutdown` Shutdown Clamd */
|
|
||||||
public function shutdown()
|
|
||||||
{
|
|
||||||
return $this->sendCommand('SHUTDOWN');
|
|
||||||
}
|
|
||||||
|
|
||||||
/* `fileScan` is used to scan single file. */
|
|
||||||
public function fileScan($file)
|
|
||||||
{
|
|
||||||
$out = $this->sendCommand('SCAN ' . $file);
|
|
||||||
|
|
||||||
list($file, $stats) = explode(':', $out);
|
|
||||||
|
|
||||||
$result = trim($stats);
|
|
||||||
|
|
||||||
return ($result === 'OK');
|
|
||||||
}
|
|
||||||
|
|
||||||
/* `continueScan` is used to scan multiple files/directories. */
|
|
||||||
public function continueScan($file)
|
|
||||||
{
|
|
||||||
$return = array();
|
|
||||||
|
|
||||||
foreach(explode("\n", trim($this->sendCommand('CONTSCAN ' . $file))) as $results ) {
|
|
||||||
list($file, $stats) = explode(':', $results);
|
|
||||||
array_push($return, array( 'file' => $file, 'stats' => trim($stats) ));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* `streamScan` is used to scan a buffer. */
|
|
||||||
public function streamScan($buffer)
|
|
||||||
{
|
|
||||||
$port = null;
|
|
||||||
$socket = null;
|
|
||||||
$command = 'STREAM';
|
|
||||||
$return = null;
|
|
||||||
|
|
||||||
$socket = $this->getSocket();
|
|
||||||
|
|
||||||
socket_send($socket, $command, strlen($command), 0);
|
|
||||||
socket_recv($socket, $return, self::CLAMAV_MAXP, 0);
|
|
||||||
|
|
||||||
sscanf($return, 'PORT %d\n', $port);
|
|
||||||
|
|
||||||
$stream = socket_create(AF_INET, SOCK_STREAM, 0);
|
|
||||||
|
|
||||||
socket_connect($stream, self::CLAMAV_MAXP, $port);
|
|
||||||
socket_send($stream, $buffer, strlen($buffer), 0);
|
|
||||||
socket_close($stream);
|
|
||||||
|
|
||||||
socket_recv($socket, $return, self::CLAMAV_MAXP, 0);
|
|
||||||
|
|
||||||
socket_close($socket);
|
|
||||||
|
|
||||||
return array('stats' => trim(str_replace('stream: ', '', $return)));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,48 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace ClamAV;
|
|
||||||
|
|
||||||
class Network extends ClamAV
|
|
||||||
{
|
|
||||||
const CLAMAV_HOST = '127.0.0.1';
|
|
||||||
const CLAMAV_PORT = 3310;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $host;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
private $port;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Network constructor
|
|
||||||
*
|
|
||||||
* You need to pass the host address and the port the the server
|
|
||||||
*
|
|
||||||
* @param string $host
|
|
||||||
* @param int $port
|
|
||||||
*/
|
|
||||||
public function __construct(string $host = self::CLAMAV_HOST, int $port = self::CLAMAV_PORT)
|
|
||||||
{
|
|
||||||
$this->host = $host;
|
|
||||||
$this->port = $port;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return resource
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
protected function getSocket()
|
|
||||||
{
|
|
||||||
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
|
|
||||||
$status = socket_connect($socket, $this->host, $this->port);
|
|
||||||
|
|
||||||
if(!$status) {
|
|
||||||
throw new \Exception('Unable to connect to ClamAV server');
|
|
||||||
}
|
|
||||||
return $socket;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,30 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace ClamAV;
|
|
||||||
|
|
||||||
class Pipe extends ClamAV
|
|
||||||
{
|
|
||||||
const CLAMAV_HOST = '/var/run/clamav/clamd.ctl';
|
|
||||||
|
|
||||||
private $pip;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pipe constructor.
|
|
||||||
*
|
|
||||||
* This class can be used to connect to local socket.
|
|
||||||
* You need to pass the path to the socket pipe.
|
|
||||||
*
|
|
||||||
* @param string $pip
|
|
||||||
*/
|
|
||||||
public function __construct(string $pip = self::CLAMAV_HOST)
|
|
||||||
{
|
|
||||||
$this->pip = $pip;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getSocket()
|
|
||||||
{
|
|
||||||
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
|
|
||||||
socket_connect($socket, $this->pip);
|
|
||||||
return $socket;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue