ваше сообщение коммита
This commit is contained in:
98
scripts/ssh-key-server.js
Executable file
98
scripts/ssh-key-server.js
Executable file
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env node
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const url = require('url');
|
||||
|
||||
const port = 3001;
|
||||
|
||||
const SSH_DIR = path.join(process.env.HOME, '.ssh');
|
||||
const DEFAULT_KEY_PATH = path.join(SSH_DIR, 'id_rsa');
|
||||
const DEFAULT_PUB_KEY_PATH = path.join(SSH_DIR, 'id_rsa.pub');
|
||||
|
||||
// Путь к папке с ключами шифрования
|
||||
const KEYS_DIR = path.join(__dirname, '../ssl/keys');
|
||||
const ENCRYPTION_KEY_PATH = path.join(KEYS_DIR, 'full_db_encryption.key');
|
||||
|
||||
// Helper to read SSH key
|
||||
const readSshKey = (keyPath) => {
|
||||
try {
|
||||
return fs.readFileSync(keyPath, 'utf8');
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper to read encryption key
|
||||
const readEncryptionKey = (keyPath) => {
|
||||
try {
|
||||
return fs.readFileSync(keyPath, 'utf8');
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// CORS headers
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
const parsedUrl = url.parse(req.url, true);
|
||||
const pathname = parsedUrl.pathname;
|
||||
|
||||
// Handle CORS preflight
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.writeHead(200, corsHeaders);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// Set CORS headers
|
||||
Object.keys(corsHeaders).forEach(key => {
|
||||
res.setHeader(key, corsHeaders[key]);
|
||||
});
|
||||
|
||||
if (pathname === '/ssh-key') {
|
||||
const privateKey = readSshKey(DEFAULT_KEY_PATH);
|
||||
const publicKey = readSshKey(DEFAULT_PUB_KEY_PATH);
|
||||
|
||||
if (privateKey) {
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify({ success: true, sshKey: privateKey, publicKey: publicKey, keyType: 'rsa' }));
|
||||
} else {
|
||||
res.writeHead(404);
|
||||
res.end(JSON.stringify({ success: false, message: 'SSH private key not found' }));
|
||||
}
|
||||
} else if (pathname === '/ssh-key/public') {
|
||||
const publicKey = readSshKey(DEFAULT_PUB_KEY_PATH);
|
||||
|
||||
if (publicKey) {
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify({ success: true, publicKey: publicKey, keyType: 'rsa' }));
|
||||
} else {
|
||||
res.writeHead(404);
|
||||
res.end(JSON.stringify({ success: false, message: 'SSH public key not found' }));
|
||||
}
|
||||
} else if (pathname === '/encryption-key') {
|
||||
const encryptionKey = readEncryptionKey(ENCRYPTION_KEY_PATH);
|
||||
|
||||
if (encryptionKey) {
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify({ success: true, encryptionKey: encryptionKey }));
|
||||
} else {
|
||||
res.writeHead(404);
|
||||
res.end(JSON.stringify({ success: false, message: 'Encryption key not found' }));
|
||||
}
|
||||
} else {
|
||||
res.writeHead(404);
|
||||
res.end(JSON.stringify({ success: false, message: 'Not found' }));
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`SSH Key Server running on port ${port}`);
|
||||
});
|
||||
Reference in New Issue
Block a user