-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
221 lines (193 loc) · 7.4 KB
/
server.js
File metadata and controls
221 lines (193 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const yaml = require('js-yaml');
const fs = require('fs');
const app = express();
const port = 3000;
// Load default configuration from blackbox_config.yml
let defaultConfig = {};
try {
const configPath = path.join(__dirname, 'blackbox_config.yml');
if (fs.existsSync(configPath)) {
const configContent = fs.readFileSync(configPath, 'utf8');
defaultConfig = yaml.load(configContent);
console.log('Default configuration loaded from blackbox_config.yml');
} else {
console.warn('blackbox_config.yml not found');
}
} catch (error) {
console.error('Error loading blackbox_config.yml:', error);
}
// Set up EJS as the view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Middleware
// Basic CORS to allow requests from other origins (e.g., when the UI is served elsewhere)
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
return res.sendStatus(204);
}
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('docs'));
// Routes
app.get('/', (req, res) => {
res.render('index');
});
// Endpoint to get default configuration
app.get('/api/defaults', (req, res) => {
res.json(defaultConfig);
});
// Endpoint to save configuration to blackbox_config.yml
app.post('/api/save', (req, res) => {
try {
const config = req.body;
// Validate the configuration structure
if (!config || !config.modules) {
return res.status(400).json({ error: 'Invalid configuration structure' });
}
// Convert to YAML
const yamlConfig = yaml.dump(config, {
indent: 2,
lineWidth: -1,
noRefs: true
});
// Save to blackbox_config.yml
const configPath = path.join(__dirname, 'blackbox_config.yml');
fs.writeFileSync(configPath, yamlConfig, 'utf8');
// Reload the default config in memory
defaultConfig = config;
console.log('Configuration saved to blackbox_config.yml');
res.json({ success: true, message: 'Configuration saved successfully' });
} catch (error) {
console.error('Error saving configuration:', error);
res.status(500).json({ error: 'Failed to save configuration' });
}
});
app.post('/generate', (req, res) => {
const config = generateConfig(req.body);
res.json({ config });
});
app.post('/download', (req, res) => {
try {
const config = generateConfig(req.body);
const filename = `${config.modules[0].name}.yml`;
// Convert to YAML
const yamlConfig = yaml.dump(config, {
indent: 2,
lineWidth: -1,
noRefs: true
});
res.setHeader('Content-Type', 'text/yaml');
res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
res.send(yamlConfig);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Error generating YAML');
}
});
function generateConfig(formData) {
// Create the base configuration structure
const config = {
modules: [{
name: formData.moduleName || 'default_module',
version: formData.version || '1.0.0',
description: formData.description || '',
type: formData.probeType,
config: {}
}]
};
// Add type-specific configuration
switch (formData.probeType) {
case 'http':
config.modules[0].config = {
endpoint: formData.httpEndpoint,
method: formData.httpMethod,
valid_status_codes: formData.validStatusCodes.split(',').map(code => parseInt(code.trim())),
timeout: parseInt(formData.timeout) || 5
};
// Add headers if provided
if (formData.headers) {
const headers = {};
formData.headers.split('\n').forEach(line => {
const [key, value] = line.split(':').map(item => item.trim());
if (key && value) {
headers[key] = value;
}
});
if (Object.keys(headers).length > 0) {
config.modules[0].config.headers = headers;
}
}
// Add POST/PUT specific fields
if (formData.httpMethod === 'POST' || formData.httpMethod === 'PUT') {
if (formData.contentType) {
if (!config.modules[0].config.headers) {
config.modules[0].config.headers = {};
}
config.modules[0].config.headers['Content-Type'] = formData.contentType;
}
if (formData.requestBody) {
config.modules[0].config.body = formData.requestBody;
}
}
// Add fail conditions
if (formData.failIfBodyMatches) {
config.modules[0].config.fail_if_body_matches_regexp = formData.failIfBodyMatches.split('\n').filter(line => line.trim());
}
if (formData.failIfBodyNotMatches) {
config.modules[0].config.fail_if_body_not_matches_regexp = formData.failIfBodyNotMatches.split('\n').filter(line => line.trim());
}
if (formData.failIfSSL === 'on') {
config.modules[0].config.fail_if_ssl = true;
}
if (formData.failIfNotSSL === 'on') {
config.modules[0].config.fail_if_not_ssl = true;
}
// Add IP protocol settings
if (formData.preferredIPProtocol) {
config.modules[0].config.preferred_ip_protocol = formData.preferredIPProtocol;
}
if (formData.ipProtocolFallback === 'on') {
config.modules[0].config.ip_protocol_fallback = true;
}
// Add valid HTTP versions if selected
if (formData.validHttpVersions) {
const versions = Array.isArray(formData.validHttpVersions)
? formData.validHttpVersions
: [formData.validHttpVersions];
if (versions.length > 0) {
config.modules[0].config.valid_http_versions = versions;
}
}
// Add redirect settings
if (formData.noFollowRedirects === 'on') {
config.modules[0].config.no_follow_redirects = true;
}
break;
case 'tcp':
config.modules[0].config = {
host: formData.tcpHost,
port: parseInt(formData.tcpPort),
timeout: parseInt(formData.timeout) || 5
};
break;
case 'ping':
config.modules[0].config = {
host: formData.pingHost,
packetCount: parseInt(formData.packetCount) || 4,
timeout: parseInt(formData.timeout) || 5
};
break;
}
return config;
}
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});