forked from dunaj-dev/docker-mailserver-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockerMailserver.js
More file actions
360 lines (311 loc) · 10.9 KB
/
dockerMailserver.js
File metadata and controls
360 lines (311 loc) · 10.9 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const Docker = require('dockerode');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
// Docker container name for docker-mailserver
const DOCKER_CONTAINER = process.env.DOCKER_CONTAINER || 'mailserver';
// Debug flag
const DEBUG = process.env.DEBUG_DOCKER === 'true';
/**
* Debug logger that only logs if DEBUG is true
* @param {string} message - Message to log
* @param {any} data - Optional data to log
*/
function debugLog(message, data = null) {
if (DEBUG) {
if (data) {
console.log(`[DOCKER-DEBUG] ${message}`, data);
} else {
console.log(`[DOCKER-DEBUG] ${message}`);
}
}
}
/**
* Escapes a string for safe use in shell commands by wrapping it in single quotes
* and escaping any single quotes within the string
* @param {string} arg - Argument to escape
* @return {string} Escaped argument safe for shell execution
*/
function escapeShellArg(arg) {
// Replace single quotes with '\'' (end quote, escaped quote, start quote)
// Then wrap the entire string in single quotes
return `'${arg.replace(/'/g, "'\\''")}'`;
}
/**
* Executes a command in the docker-mailserver container
* @param {string} command Command to execute
* @return {Promise<string>} stdout from the command
*/
async function execInContainer(command) {
try {
debugLog(`Executing command in container ${DOCKER_CONTAINER}: ${command}`);
// Get container instance
const container = docker.getContainer(DOCKER_CONTAINER);
// Create exec instance
const exec = await container.exec({
Cmd: ['sh', '-c', command],
AttachStdout: true,
AttachStderr: true,
});
// Start exec instance
const stream = await exec.start();
// Collect output
return new Promise((resolve, reject) => {
let stdoutData = '';
let stderrData = '';
stream.on('data', (chunk) => {
// Docker multiplexes stdout/stderr in the same stream
// First 8 bytes contain header, actual data starts at 8th byte
stdoutData += chunk.slice(8).toString();
});
stream.on('end', () => {
debugLog(`Command completed. Output:`, stdoutData);
resolve(stdoutData);
});
stream.on('error', (err) => {
debugLog(`Command error:`, err);
reject(err);
});
});
} catch (error) {
console.error(`Error executing command in container: ${command}`, error);
debugLog(`Execution error:`, error);
throw error;
}
}
/**
* Executes a setup.sh command in the docker-mailserver container
* @param {string} setupCommand Command to pass to setup.sh
* @return {Promise<string>} stdout from the command
*/
async function execSetup(setupCommand) {
// The setup.sh script is usually located at /usr/local/bin/setup.sh or /usr/local/bin/setup in docker-mailserver
debugLog(`Executing setup command: ${setupCommand}`);
return execInContainer(`/usr/local/bin/setup ${setupCommand}`);
}
// Function to retrieve email accounts
async function getAccounts() {
try {
debugLog('Getting email accounts list');
const stdout = await execSetup('email list');
// Parse multiline output with regex to extract email and size information
const accounts = [];
const accountLineRegex =
/\* ([\w\-\.@]+) \( ([\w\.\~]+) \/ ([\w\.\~]+) \) \[(\d+)%\](.*)$/;
// Process each line individually
const lines = stdout.split('\n').filter((line) => line.trim().length > 0);
debugLog('Raw email list response:', lines);
for (let i = 0; i < lines.length; i++) {
// Clean the line from binary control characters
const line = lines[i].replace(/[\x00-\x1F\x7F-\x9F]/g, '').trim();
// Check if line contains * which indicates an account entry
if (line.includes('*')) {
const match = line.match(accountLineRegex);
if (match) {
const email = match[1];
const usedSpace = match[2];
const totalSpace = match[3] === '~' ? 'unlimited' : match[3];
const usagePercent = match[4];
debugLog(
`Parsed account: ${email}, Storage: ${usedSpace}/${totalSpace} [${usagePercent}%]`
);
accounts.push({
email,
storage: {
used: usedSpace,
total: totalSpace,
percent: usagePercent + '%',
},
});
} else {
debugLog(`Failed to parse account line: ${line}`);
}
}
}
debugLog(`Found ${accounts.length} accounts`);
return accounts;
} catch (error) {
console.error('Error retrieving accounts:', error);
debugLog('Account retrieval error:', error);
throw new Error('Unable to retrieve account list');
}
}
// Function to add a new email account
async function addAccount(email, password) {
try {
debugLog(`Adding new email account: ${email}`);
await execSetup(
`email add ${escapeShellArg(email)} ${escapeShellArg(password)}`
);
debugLog(`Account created: ${email}`);
return { success: true, email };
} catch (error) {
console.error('Error adding account:', error);
debugLog('Account creation error:', error);
throw new Error('Unable to add email account');
}
}
// Function to update an email account password
async function updateAccountPassword(email, password) {
try {
debugLog(`Updating password for account: ${email}`);
await execSetup(
`email update ${escapeShellArg(email)} ${escapeShellArg(password)}`
);
debugLog(`Password updated for account: ${email}`);
return { success: true, email };
} catch (error) {
console.error('Error updating account password:', error);
debugLog('Account password update error:', error);
throw new Error('Unable to update email account password');
}
}
// Function to delete an email account
async function deleteAccount(email) {
try {
debugLog(`Deleting email account: ${email}`);
await execSetup(`email del ${escapeShellArg(email)}`);
debugLog(`Account deleted: ${email}`);
return { success: true, email };
} catch (error) {
console.error('Error deleting account:', error);
debugLog('Account deletion error:', error);
throw new Error('Unable to delete email account');
}
}
// Function to retrieve aliases
async function getAliases() {
try {
debugLog('Getting aliases list');
const stdout = await execSetup('alias list');
const aliases = [];
// Parse each line in the format "* source destination"
const lines = stdout.split('\n').filter((line) => line.trim().length > 0);
debugLog('Raw alias list response:', lines);
// Modified regex to be more tolerant of control characters that might appear in the output
const aliasRegex = /\* ([\w\-\.@]+) ([\w\-\.@]+)$/;
for (let i = 0; i < lines.length; i++) {
// Clean the line from binary control characters
const line = lines[i].replace(/[\x00-\x1F\x7F-\x9F]/g, '').trim();
if (line.includes('*')) {
const match = line.match(aliasRegex);
if (match) {
const source = match[1];
const destination = match[2];
debugLog(`Parsed alias: ${source} -> ${destination}`);
aliases.push({
source,
destination,
});
} else {
debugLog(`Failed to parse alias line: ${line}`);
}
}
}
debugLog(`Found ${aliases.length} aliases`);
return aliases;
} catch (error) {
console.error('Error retrieving aliases:', error);
debugLog('Alias retrieval error:', error);
throw new Error('Unable to retrieve alias list');
}
}
// Function to add an alias
async function addAlias(source, destination) {
try {
debugLog(`Adding new alias: ${source} -> ${destination}`);
await execSetup(
`alias add ${escapeShellArg(source)} ${escapeShellArg(destination)}`
);
debugLog(`Alias created: ${source} -> ${destination}`);
return { success: true, source, destination };
} catch (error) {
console.error('Error adding alias:', error);
debugLog('Alias creation error:', error);
throw new Error('Unable to add alias');
}
}
// Function to delete an alias
async function deleteAlias(source, destination) {
try {
debugLog(`Deleting alias: ${source} => ${destination}`);
await execSetup(
`alias del ${escapeShellArg(source)} ${escapeShellArg(destination)}`
);
debugLog(`Alias deleted: ${source} => ${destination}`);
return { success: true, source, destination };
} catch (error) {
console.error('Error deleting alias:', error);
debugLog('Alias deletion error:', error);
throw new Error('Unable to delete alias');
}
}
// Function to check server status
async function getServerStatus() {
try {
debugLog('Getting server status');
// Get container info
const container = docker.getContainer(DOCKER_CONTAINER);
const containerInfo = await container.inspect();
// Check if container is running
const isRunning = containerInfo.State.Running === true;
debugLog(`Container running: ${isRunning}`);
let diskUsage = '0%';
let cpuUsage = '0%';
let memoryUsage = '0MB';
if (isRunning) {
// Get container stats
debugLog('Getting container stats');
const stats = await container.stats({ stream: false });
// Calculate CPU usage percentage
const cpuDelta =
stats.cpu_stats.cpu_usage.total_usage -
stats.precpu_stats.cpu_usage.total_usage;
const systemCpuDelta =
stats.cpu_stats.system_cpu_usage - stats.precpu_stats.system_cpu_usage;
const cpuPercent =
(cpuDelta / systemCpuDelta) * stats.cpu_stats.online_cpus * 100;
cpuUsage = `${cpuPercent.toFixed(2)}%`;
// Calculate memory usage
const memoryUsageBytes = stats.memory_stats.usage;
memoryUsage = formatMemorySize(memoryUsageBytes);
debugLog(`Resources - CPU: ${cpuUsage}, Memory: ${memoryUsage}`);
// For disk usage, we would need to run a command inside the container
// This could be a more complex operation involving checking specific directories
// For simplicity, we'll set this to "N/A" or implement a basic check
diskUsage = 'N/A';
}
const result = {
status: isRunning ? 'running' : 'stopped',
resources: {
cpu: cpuUsage,
memory: memoryUsage,
disk: diskUsage,
},
};
debugLog('Server status result:', result);
return result;
} catch (error) {
console.error('Error checking server status:', error);
debugLog('Server status error:', error);
return {
status: 'unknown',
error: error.message,
};
}
}
// Helper function to format memory size
function formatMemorySize(bytes) {
if (bytes === 0) return '0B';
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + sizes[i];
}
module.exports = {
getAccounts,
addAccount,
updateAccountPassword,
deleteAccount,
getAliases,
addAlias,
deleteAlias,
getServerStatus,
};