fix: On-demand sFTP for wp

This commit is contained in:
Andras Bacsai 2022-04-06 10:29:42 +02:00
parent 3a835b420e
commit 39fa64e20d

View File

@ -21,13 +21,10 @@ export const post: RequestHandler = async (event) => {
const { ftpEnabled } = await event.request.json(); const { ftpEnabled } = await event.request.json();
const publicPort = await getPort({ port: portNumbers(minPort, maxPort) }); const publicPort = await getPort({ port: portNumbers(minPort, maxPort) });
let ftpUser = cuid(); let ftpUser = cuid();
const ftpPassword = generatePassword(); let ftpPassword = generatePassword();
const hostkeyDir = dev ? '/tmp/hostkeys' : '/app/ssl/hostkeys'; const hostkeyDir = dev ? '/tmp/hostkeys' : '/app/ssl/hostkeys';
try { try {
const { stdout: password } = await asyncExecShell(
`echo ${ftpPassword} | openssl passwd -1 -stdin`
);
const data = await db.prisma.wordpress.update({ const data = await db.prisma.wordpress.update({
where: { serviceId: id }, where: { serviceId: id },
data: { ftpEnabled }, data: { ftpEnabled },
@ -37,10 +34,16 @@ export const post: RequestHandler = async (event) => {
service: { destinationDockerId, destinationDocker }, service: { destinationDockerId, destinationDocker },
ftpPublicPort: oldPublicPort, ftpPublicPort: oldPublicPort,
ftpUser: user, ftpUser: user,
ftpPassword: savedPassword,
ftpHostKey, ftpHostKey,
ftpHostKeyPrivate ftpHostKeyPrivate
} = data; } = data;
if (user) ftpUser = user; if (user) ftpUser = user;
if (savedPassword) ftpPassword = decrypt(savedPassword);
const { stdout: password } = await asyncExecShell(
`echo ${ftpPassword} | openssl passwd -1 -stdin`
);
if (destinationDockerId) { if (destinationDockerId) {
try { try {
await fs.stat(hostkeyDir); await fs.stat(hostkeyDir);
@ -74,7 +77,11 @@ export const post: RequestHandler = async (event) => {
if (ftpEnabled) { if (ftpEnabled) {
await db.prisma.wordpress.update({ await db.prisma.wordpress.update({
where: { serviceId: id }, where: { serviceId: id },
data: { ftpPublicPort: publicPort, ftpUser, ftpPassword: encrypt(ftpPassword) } data: {
ftpPublicPort: publicPort,
ftpUser: user ? undefined : ftpUser,
ftpPassword: savedPassword ? undefined : encrypt(ftpPassword)
}
}); });
try { try {
@ -125,6 +132,9 @@ export const post: RequestHandler = async (event) => {
); );
await startTcpProxy(destinationDocker, `${id}-ftp`, publicPort, 22); await startTcpProxy(destinationDocker, `${id}-ftp`, publicPort, 22);
await asyncExecShell(
`rm -f ${hostkeyDir}/${id}-docker-compose.yml ${hostkeyDir}/${id}.ed25519 ${hostkeyDir}/${id}.ed25519.pub ${hostkeyDir}/${id}.rsa ${hostkeyDir}/${id}.rsa.pub`
);
} else { } else {
await db.prisma.wordpress.update({ await db.prisma.wordpress.update({
where: { serviceId: id }, where: { serviceId: id },
@ -141,8 +151,10 @@ export const post: RequestHandler = async (event) => {
console.log(error); console.log(error);
// //
} }
await stopTcpHttpProxy(destinationDocker, oldPublicPort); await stopTcpHttpProxy(destinationDocker, oldPublicPort);
await asyncExecShell(
`rm -f ${hostkeyDir}/${id}-docker-compose.yml ${hostkeyDir}/${id}.ed25519 ${hostkeyDir}/${id}.ed25519.pub ${hostkeyDir}/${id}.rsa ${hostkeyDir}/${id}.rsa.pub `
);
} }
} }
if (ftpEnabled) { if (ftpEnabled) {