Beispiel #1
0
        /**
         * Initiate a new session for using protected REST APIs.
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_login.html" target="_blank">REST API doc</a>
         *
         * @param userName username to login with
         * @param password password to login with
         * @throws MetadefenderClientException
         */
        public void Login(string userName, string password)
        {
            Requests.Login loginRequest = new Requests.Login();
            loginRequest.user     = userName;
            loginRequest.password = password;

            string       requestStr    = JsonConvert.SerializeObject(loginRequest);
            MemoryStream requestStream = new MemoryStream(Encoding.UTF8.GetBytes(requestStr));

            HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/login", "POST", requestStream, null);

            if (response.responseCode == 200)
            {
                try
                {
                    Responses.Login loginResponse = JsonConvert.DeserializeObject <Responses.Login>(response.response);
                    if (loginResponse != null)
                    {
                        sessionId = loginResponse.session_id;
                    }
                    else
                    {
                        ThrowRequestError(response);
                    }
                }
                catch (JsonSerializationException)
                {
                    ThrowRequestError(response);
                }
            }
            else
            {
                ThrowRequestError(response);
            }
        }
Beispiel #2
0
        /**
         * Fetch Scan Result by File Hash
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_fetch_scan_result_by_file_hash.html" target="_blank">REST API doc</a>
         *
         * @param hash {md5|sha1|sha256 hash}
         * @return File scan result object
         * @throws MetadefenderClientException
         */
        public Responses.FileScanResult FetchScanResultByHash(string hash)
        {
            if (string.IsNullOrEmpty(hash))
            {
                throw new MetadefenderClientException("Hash is required");
            }

            HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/hash/" + hash, "GET");

            if (response.responseCode == 200)
            {
                Responses.FileScanResult fileScanResult = GetObjectFromJson <Responses.FileScanResult>(response.response);

                if (fileScanResult == null)
                {
                    ThrowRequestError(response);
                }
                return(fileScanResult);
            }
            else
            {
                ThrowRequestError(response);
                return(null);
            }
        }
        /**
         * Scan is done asynchronously and each scan request is tracked by data id of which result can be retrieved by API Fetch Scan Result.
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_scan_file.html" target="_blank">REST API doc</a>
         *
         * @param inputStream Stream of data (file) to scan. Required
         * @param fileScanOptions Optional file scan options. Can be NULL.
         * @return unique data id for this file scan
         */
        public string ScanFile(Stream inputStream, FileScanOptions fileScanOptions, out string coreId, long contentLength = 0)
        {
            //if (inputStream == null)
            //{
            //    throw new MetadefenderClientException("Stream cannot be null");
            //}

            if ((fileScanOptions != null && fileScanOptions.GetUserAgent() == null) && user_agent != null)
            {
                fileScanOptions.SetUserAgent(user_agent);
            }

            if (fileScanOptions == null && user_agent != null)
            {
                fileScanOptions = new FileScanOptions().SetUserAgent(user_agent);
            }

            Dictionary <string, string> headers = (fileScanOptions == null) ? null : fileScanOptions.GetOptions();

            cookieContainer = new System.Net.CookieContainer();

            HttpConnector.HttpResponse response =
                httpConnector.SendRequest(apiEndPointUrl + "/file", "POST", inputStream, headers, contentLength, cookieContainer);

            coreId = apiEndPointUrl;
            if (response.responseCode == 200)
            {
                try
                {
                    Responses.ScanRequest scanRequest = JsonConvert.DeserializeObject <Responses.ScanRequest>(response.response);
                    if (scanRequest != null)
                    {
                        if (cookieContainer.Count > 0)
                        {
                            foreach (System.Net.Cookie cookie in response.cookieContainer.GetCookies(new Uri(apiEndPointUrl)))
                            {
                                coreId = cookie.Value;
                            }
                        }

                        return(scanRequest.data_id);
                    }
                    else
                    {
                        ThrowRequestError(response);
                        return(null);
                    }
                }
                catch (JsonSerializationException)
                {
                    ThrowRequestError(response);
                    return(null);
                }
            }
            else
            {
                ThrowRequestError(response);
                return(null);
            }
        }
Beispiel #4
0
        /**
         * Get the current session state.
         *
         * @return TRUE if the current session is valid, FALSE otherwise.
         * @throws MetadefenderClientException
         */
        public bool ValidateCurrentSession()
        {
            if (string.IsNullOrEmpty(sessionId))
            {
                return(false);
            }

            HttpConnector.HttpResponse response =
                httpConnector.SendRequest(apiEndPointUrl + "/version", "GET", null, GetLoggedInHeader());

            return(response.responseCode == 200);
        }
Beispiel #5
0
        /**
         * You need to be logged in to access this API point.
         *
         * Destroy session for not using protected REST APIs.
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_logout.html" target="_blank">REST API doc</a>
         * @throws MetadefenderClientException
         */
        public void Logout()
        {
            CheckSession();

            HttpConnector.HttpResponse response =
                httpConnector.SendRequest(apiEndPointUrl + "/logout", "POST", null, GetLoggedInHeader());
            sessionId = null;

            if (response.responseCode != 200)
            {
                ThrowRequestError(response);
            }
        }
Beispiel #6
0
        /**
         * Fetching Available Scan Rules
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_fetching_available_scan_workflows.html" target="_blank">REST API doc</a>
         *
         * @return List of Scan rules
         * @throws MetadefenderClientException
         */
        public List <Responses.ScanRule> GetAvailableScanRules()
        {
            HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/file/rules", "GET");

            if (response.responseCode == 200)
            {
                return(GetObjectFromJson <List <Responses.ScanRule> >(response.response));
            }
            else
            {
                ThrowRequestError(response);
                return(null);
            }
        }
Beispiel #7
0
        /**
         * Fetching Engine/Database Versions
         * The response is an array of engines with database information.
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_fetching_engine_database_versions.html" target="_blank">REST API doc</a>
         *
         * @return List of Engine versions
         * @throws MetadefenderClientException
         */
        public List <Responses.EngineVersion> GetEngineVersions()
        {
            HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/stat/engines", "GET");

            if (response.responseCode == 200)
            {
                return(GetObjectFromJson <List <Responses.EngineVersion> >(response.response));
            }
            else
            {
                ThrowRequestError(response);
                return(null);
            }
        }
Beispiel #8
0
        /**
         * Scan is done asynchronously and each scan request is tracked by data id of which result can be retrieved by API Fetch Scan Result.
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_scan_file.html" target="_blank">REST API doc</a>
         *
         * @param inputStream Stream of data (file) to scan. Required
         * @param fileScanOptions Optional file scan options. Can be NULL.
         * @return unique data id for this file scan
         */
        public string ScanFile(Stream inputStream, FileScanOptions fileScanOptions)
        {
            if (inputStream == null)
            {
                throw new MetadefenderClientException("Stream cannot be null");
            }

            if ((fileScanOptions != null && fileScanOptions.GetUserAgent() == null) && user_agent != null)
            {
                fileScanOptions.SetUserAgent(user_agent);
            }

            if (fileScanOptions == null && user_agent != null)
            {
                fileScanOptions = new FileScanOptions().SetUserAgent(user_agent);
            }

            Dictionary <string, string> headers = (fileScanOptions == null) ? null : fileScanOptions.GetOptions();

            HttpConnector.HttpResponse response =
                httpConnector.SendRequest(apiEndPointUrl + "/file", "POST", inputStream, headers);

            if (response.responseCode == 200)
            {
                try
                {
                    Responses.ScanRequest scanRequest = JsonConvert.DeserializeObject <Responses.ScanRequest>(response.response);
                    if (scanRequest != null)
                    {
                        return(scanRequest.data_id);
                    }
                    else
                    {
                        ThrowRequestError(response);
                        return(null);
                    }
                }
                catch (JsonSerializationException)
                {
                    ThrowRequestError(response);
                    return(null);
                }
            }
            else
            {
                ThrowRequestError(response);
                return(null);
            }
        }
Beispiel #9
0
        /**
         * You need to be logged in to access this API point.
         *
         * @return ApiVersion object
         * @throws MetadefenderClientException
         */
        public Responses.ApiVersion GetVersion()
        {
            CheckSession();

            HttpConnector.HttpResponse response =
                httpConnector.SendRequest(apiEndPointUrl + "/version", "GET", null, GetLoggedInHeader());

            if (response.responseCode == 200)
            {
                return(GetObjectFromJson <Responses.ApiVersion>(response.response));
            }
            else
            {
                ThrowRequestError(response);
                return(null);
            }
        }
Beispiel #10
0
        /**
         * You need to be logged in to access this API point.
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_getlicense.html" target="_blank">REST API doc</a>
         *
         * @return License object
         * @throws MetadefenderClientException
         */
        public Responses.License GetCurrentLicenseInformation()
        {
            CheckSession();

            HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/admin/license", "GET", null,
                                                                            GetLoggedInHeader());

            if (response.responseCode == 200)
            {
                return(GetObjectFromJson <Responses.License>(response.response));
            }
            else
            {
                ThrowRequestError(response);
                return(null);
            }
        }
Beispiel #11
0
 /**
  * Generic error handling for API request where response code != 200
  * @param response actual API response
  * @throws MetadefenderClientException
  */
 private void ThrowRequestError(HttpConnector.HttpResponse response)
 {
     try
     {
         Responses.Error error = JsonConvert.DeserializeObject <Responses.Error>(response.response);
         if (error != null)
         {
             throw new MetadefenderClientException(error.err, response.responseCode);
         }
         else
         {
             throw new MetadefenderClientException("", response.responseCode);
         }
     }
     catch (JsonSerializationException)
     {
         throw new MetadefenderClientException("", response.responseCode);
     }
 }
Beispiel #12
0
        /**
         * Retrieve scan results.
         *
         * @see <a href="http://software.opswat.com/metascan/Documentation/Metascan_v4/documentation/user_guide_metascan_developer_guide_fetch_scan_result.html" target="_blank"v>REST API doc</a>
         *
         * @param data_id Unique file scan id. Required.
         * @return File scan result object
         * @throws MetadefenderClientException
         */
        public Responses.FileScanResult FetchScanResult(string data_id)
        {
            if (string.IsNullOrEmpty(data_id))
            {
                throw new MetadefenderClientException("data_id is required");
            }

            HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/file/" + data_id, "GET");

            if (response.responseCode == 200)
            {
                return(GetObjectFromJson <Responses.FileScanResult>(response.response));
            }
            else
            {
                ThrowRequestError(response);
                return(null);
            }
        }
        /**
         * Download Sanitized Files Using Data Id
         *
         * @see <a href="https://onlinehelp.opswat.com/corev4/Download_Sanitized_Files.html" target="_blank">REST API doc</a>
         *
         * @param data_id Unique file scan id. Required.
         * @return Stream of the sanitized file
         * @throws MetadefenderClientException
         */
        public Stream DownloadSanitizedFile(string data_id, out string errorMessage)
        {
            if (string.IsNullOrEmpty(data_id))
            {
                throw new MetadefenderClientException("data_id is required");
            }

            HttpConnector.HttpResponse response = httpConnector.SendRequest(apiEndPointUrl + "/file/converted/" + data_id, "GET", null, new Dictionary <string, string>(), 0, cookieContainer);

            if (response.responseCode != 200)
            {
                // error: return error message
                errorMessage = GetObjectFromJson <Responses.Error>(response.response).err;
                return(null);
            }
            else
            {
                // success: return stream
                errorMessage = null;
                byte[] byteArray = Encoding.Default.GetBytes(response.response);
                return(new MemoryStream(byteArray));
            }
        }