Skip to content

Commit

Permalink
idk
Browse files Browse the repository at this point in the history
  • Loading branch information
Zigecek committed Jan 9, 2023
1 parent 2eeb575 commit e1143d5
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 201 deletions.
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

27 changes: 0 additions & 27 deletions backend/drda/mailer.js

This file was deleted.

59 changes: 0 additions & 59 deletions backend/models/Config.js

This file was deleted.

113 changes: 12 additions & 101 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,20 @@
require("dotenv").config();
require("./utils/mongoose").init();
require("./drda/mailer.js");
var hostnamePort;
var host;
var hostPort;

const fs = require("fs");
const express = require("express");
const { createServer } = require("http");
const socketIo = require("socket.io");
var SSHClient = require("ssh2").Client;
var serveStatic = require("serve-static");
const serveStatic = require("serve-static");

const port = process.env.PORT || 80;
console.log("PORT: " + port);

const app = express();
app.use(serveStatic("./frontend/"));
const privateKey = fs.readFileSync("/etc/letsencrypt/live/kozohorsky.xyz/privkey.pem");
const certificate = fs.readFileSync("/etc/letsencrypt/live/kozohorsky.xyz/fullchain.pem");

const httpServer = createServer(app);
httpServer.listen(port);
console.log("PORT: " + port);
app.use(serveStatic("./frontend/"));

const io = socketIo(httpServer, {
cors: {
origin: ["https://kozohorsky.xyz", "https://kozohorsky-xyz.herokuapp.com"],
methods: ["GET", "POST"],
createServer(
{
key: privateKey,
cert: certificate,
},
});

io.on("connection", (socket) => {
console.log(socket.id);
socket.on("getZero", () => {
require("./models/Config").findOne(
{
number: 1,
},
(err, Cres) => {
if (err) {
console.error(err);
error.sendError(err);
return;
}
socket.emit("takeZero", Cres.ngrokZeroUrl);
}
);
});
socket.on("getHost", () => {
require("./models/Config").findOne(
{
number: 1,
},
(err, Cres) => {
if (err) {
console.error(err);
error.sendError(err);
return;
}
hostnamePort = Cres.ngrokRpiSSH;
host = hostnamePort.match(/\d.tcp.eu.ngrok.io/)[0];
hostPort = hostnamePort.match(/\d{4,6}/)[0];
socket.emit("sendHost", { host: host, port: hostPort });
}
);
});
socket.on("hostname", (opt) => {
var conn = new SSHClient();
conn
.on("ready", function () {
socket.emit("conekt");
socket.emit("data", "\r\n*** SSH CONNECTION ESTABLISHED ***\r\n");
conn.shell({ term: "xterm-256color" }, function (err, stream) {
if (err) {
socket.emit("disconekt");
return socket.emit(
"data",
"\r\n*** SSH SHELL ERROR: " + err.message + " ***\r\n"
);
}
socket.on("data", function (data) {
stream.write(data);
});
stream.on("data", function (d) {
socket.emit(
"data",
decodeURIComponent(escape(d.toString("binary")))
);
});
});
})
.on("close", function () {
socket.emit("disconekt");
socket.emit("data", "\r\n*** SSH CONNECTION CLOSED ***\r\n");
socket.disconnect(true);
})
.on("error", function (err) {
socket.emit(
"data",
"\r\n*** SSH CONNECTION ERROR: " + err.message + " ***\r\n"
);
socket.disconnect(true);
})
.connect({
host: opt.host,
port: Number(opt.port),
password: opt.pswd,
username: opt.usr,
});
});
});
app
).listen(port);
8 changes: 8 additions & 0 deletions git_pull.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
cd ~/web-bs
git checkout .
git fetch
git pull
npm run install
npm run reload
chmod +x ~/web-bs/*.sh
6 changes: 6 additions & 0 deletions installation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
npm install pm2@latest -g
npm run install
pm2 startup
npm run start
pm2 save
2 changes: 2 additions & 0 deletions npm_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
npm run install
2 changes: 2 additions & 0 deletions npm_uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
npm run uninstall
21 changes: 8 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@
"version": "1.0.0",
"main": "backend/server.js",
"scripts": {
"start": "npm run node",
"node": "node ./backend/server.js",
"i": "npm i dotenv@latest express@latest finalhandler@latest http@latest [email protected] serve-static@latest [email protected] ssh2@latest fs@latest nodemailer@latest"
"start": "pm2 start ./backend/server.js --time -- trace-warnings",
"start_local": "node ./backend/server.js",
"stop": "pm2 stop server",
"restart": "pm2 restart server",
"reload": "pm2 reload server",
"update": "./git_pull.sh",
"install": "npm i fs@latest express@latest http@latest serve-static@latest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Zigecek/web-bs.git"
},
"author": "kozohorsky.xyz",
"dependencies": {
"dotenv": "^10.0.0",
"express": "^4.17.1",
"http": "^0.0.1-security",
"mongoose": "^5.13.7",
"nodemailer": "^6.7.2",
"serve-static": "^1.14.1",
"socket.io": "^4.3.1",
"ssh2": "^1.5.0"
}
"dependencies": {}
}
2 changes: 2 additions & 0 deletions restart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
npm run restart
2 changes: 2 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
npm run start
2 changes: 2 additions & 0 deletions stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
npm run stop
33 changes: 33 additions & 0 deletions webhook_init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
apt install webhook
cd ~
mkdir -p webhook
cd webhook
touch webhook.yaml
echo "
- id: web-bs
execute-command: $HOME/web-bs/git_pull.sh
command-working-directory: $HOME/web-bs
response-message: \"Web-bs webhook executed successfully\"
" >> webhook.yaml
touch /etc/systemd/system/webhook.service
echo "
[Unit]
Description=Webhook service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
User=$USER
ExecStart=/usr/bin/webhook --hooks $HOME/webhook/webhook.yaml --port 9000 -verbose
[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/webhook.service
systemctl daemon-reload
systemctl enable webhook
systemctl start webhook
systemctl restart webhook
systemctl status webhook

0 comments on commit e1143d5

Please sign in to comment.