spiderfoot-direct-client.js•5.61 kB
// Direct SpiderFoot API Client
import axios from 'axios';
class SpiderFootClient {
constructor(baseURL = 'http://localhost:5001') {
this.client = axios.create({
baseURL,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
}
/**
* Ping the SpiderFoot server
*/
async ping() {
try {
const response = await this.client.get('/ping');
return response.data;
} catch (error) {
this.handleError(error, 'ping');
}
}
/**
* List all available modules
*/
async listModules() {
try {
const response = await this.client.get('/modules');
return response.data;
} catch (error) {
this.handleError(error, 'listModules');
}
}
/**
* List all scans
*/
async listScans() {
try {
const response = await this.client.get('/scans');
return response.data;
} catch (error) {
this.handleError(error, 'listScans');
}
}
/**
* Start a new scan
* @param {string} target - The target to scan
* @param {string} moduleList - Comma-separated list of modules to use (default: all)
* @param {string} scanName - Name for the scan (default: auto-generated)
* @param {string} scanTarget - Type of target (default: ip-address)
*/
async startScan(target, moduleList = 'all', scanName = null, scanTarget = 'ip-address') {
try {
const params = new URLSearchParams();
params.append('scanname', scanName || `scan-${Date.now()}`);
params.append('scantarget', target);
params.append('modulelist', moduleList);
params.append('scantype', scanTarget);
params.append('usecase', 'all');
const response = await this.client.post('/scan/new', params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
return response.data;
} catch (error) {
this.handleError(error, 'startScan');
}
}
/**
* Get scan results
* @param {string} scanId - The ID of the scan
* @param {string} dataType - Type of data to retrieve (default: summary)
*/
async getScanResults(scanId, dataType = 'summary') {
try {
const response = await this.client.get(`/scanresults`, {
params: { id: scanId, type: dataType }
});
return response.data;
} catch (error) {
this.handleError(error, 'getScanResults');
}
}
/**
* Get scan logs
* @param {string} scanId - The ID of the scan
*/
async getScanLogs(scanId) {
try {
const response = await this.client.get(`/scanlog`, {
params: { id: scanId }
});
return response.data;
} catch (error) {
this.handleError(error, 'getScanLogs');
}
}
/**
* Get scan status
* @param {string} scanId - The ID of the scan
*/
async getScanStatus(scanId) {
try {
const response = await this.client.get(`/scanstatus`, {
params: { id: scanId }
});
return response.data;
} catch (error) {
this.handleError(error, 'getScanStatus');
}
}
/**
* Stop a running scan
* @param {string} scanId - The ID of the scan to stop
*/
async stopScan(scanId) {
try {
const response = await this.client.get('/scan/stop', {
params: { id: scanId }
});
return response.data;
} catch (error) {
this.handleError(error, 'stopScan');
}
}
/**
* Delete a scan
* @param {string} scanId - The ID of the scan to delete
*/
async deleteScan(scanId) {
try {
const response = await this.client.get('/scan/delete', {
params: { id: scanId }
});
return response.data;
} catch (error) {
this.handleError(error, 'deleteScan');
}
}
/**
* Handle API errors consistently
*/
handleError(error, method) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const { status, data } = error.response;
const errorMsg = data.error || data.message || 'Unknown error';
throw new Error(`SpiderFoot API error in ${method}: ${status} - ${errorMsg}`);
} else if (error.request) {
// The request was made but no response was received
throw new Error(`No response received from SpiderFoot API in ${method}. Is the server running?`);
} else {
// Something happened in setting up the request that triggered an Error
throw new Error(`Error in ${method}: ${error.message}`);
}
}
}
// Example usage
async function testClient() {
const client = new SpiderFootClient();
try {
console.log('=== Testing SpiderFoot Direct API Client ===');
// 1. Ping the server
console.log('\n1. Pinging server...');
const ping = await client.ping();
console.log('Server response:', ping);
// 2. List available modules
console.log('\n2. Fetching available modules...');
const modules = await client.listModules();
console.log(`Found ${Object.keys(modules).length} modules`);
// 3. List existing scans
console.log('\n3. Listing existing scans...');
const scans = await client.listScans();
console.log('Existing scans:', scans);
console.log('\n✅ SpiderFoot client test completed successfully!');
} catch (error) {
console.error('\n❌ Test failed:', error.message);
process.exit(1);
}
}
// Run the test if this file is executed directly
if (process.argv[1] === new URL(import.meta.url).pathname) {
testClient();
}
export default SpiderFootClient;