First working version
This commit is contained in:
53
index.mjs
53
index.mjs
@@ -1,6 +1,4 @@
|
||||
import { fs } from 'fs';
|
||||
import { v4 } from 'uuid';
|
||||
import { child_process } from 'child_process';
|
||||
import SSH2Promise from "ssh2-promise";
|
||||
import {
|
||||
SecretsManagerClient,
|
||||
GetSecretValueCommand,
|
||||
@@ -11,42 +9,41 @@ export const handler = async (event) => {
|
||||
if (event.subject.includes("You have deliveries") || event.subject.includes("You have a delivery")) {
|
||||
// Trigger home notification
|
||||
console.info("Matched Subject");
|
||||
const command = `ssh ${process.env.INTERNAL_HOST} "/usr/bin/tmux neww -d \"/usr/bin/mplayer '${process.env.INTERNAL_FILE}'\""`;
|
||||
const tempFile = `./${v4()}.key`;
|
||||
const command = `ssh ${process.env.INTERNAL_HOST} "/home/liz/.sound '${process.env.INTERNAL_FILE}'"`;
|
||||
const keyClient = new SecretsManagerClient({ region: "us-west-2" });
|
||||
let keyResponse;
|
||||
try {
|
||||
console.info("Get Key");
|
||||
keyResponse = await keyClient.send(new GetSecretValueCommand({
|
||||
let keyResponseObj = await keyClient.send(new GetSecretValueCommand({
|
||||
SecretId: process.env.TRIGGER_SECRET
|
||||
}));
|
||||
keyResponse = JSON.parse(keyResponse.SecretString);
|
||||
keyResponse = keyResponse["carbon-key"].replaceAll(" ", "\n");
|
||||
fs.writeFileSync(tempFile, keyResponse);
|
||||
keyResponseObj = JSON.parse(keyResponseObj.SecretString);
|
||||
keyResponse = keyResponseObj["carbon-key"];
|
||||
keyResponse = keyResponse.replaceAll(':', "\n");
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
result = err;
|
||||
}
|
||||
console.info("Run SSH");
|
||||
let SSHResult = child_process.execSync(`ssh -i ${tempFile} ${process.env.TRIGGER_USERNAME}@${process.env.TRIGGER_HOST} "${command}"`);
|
||||
console.info(SSHResult.toString());
|
||||
fs.unlinkSync(tempFile);
|
||||
console.info("Finished SSH");
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
result
|
||||
},
|
||||
};
|
||||
return response;
|
||||
}else{
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
result
|
||||
},
|
||||
};
|
||||
return response;
|
||||
let ssh = new SSH2Promise({
|
||||
host: process.env.TRIGGER_HOST,
|
||||
username: process.env.TRIGGER_USERNAME,
|
||||
privateKey: keyResponse
|
||||
});
|
||||
await ssh.connect();
|
||||
console.info("Connected via SSH");
|
||||
let sshResponse = await ssh.exec(command);
|
||||
console.info("SSH Response: ", sshResponse);
|
||||
result = sshResponse;
|
||||
ssh.close();
|
||||
console.info("Closed SSH");
|
||||
}
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
result
|
||||
},
|
||||
};
|
||||
return response;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user