-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
112 lines (95 loc) · 2.64 KB
/
Copy pathindex.ts
File metadata and controls
112 lines (95 loc) · 2.64 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
import express from "express";
import winston from "winston";
import categories from "./apps.json";
import Hashids from "hashids";
const log = winston.createLogger({
level: "debug",
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
winston.format.printf(info => `[${info.timestamp}][${info.level}]: ${info.message}`)),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: "linite.log" })
]
});
const appList = [];
const salt = "linite420";
const hasher = new Hashids(salt);
const app = express();
const port = 8080;
generateAppList();
app.set("view engine", "pug");
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.get("/", (req, res) => {
res.render("index", categories);
log.log("info", `${req.connection.remoteAddress} accessing /`)
});
app.post("/app-code", (req, res) => {
const code = generateAppCode(req.body.apps);
res.send(code);
log.log("debug", `${req.connection.remoteAddress} asking for app code`)
});
app.get("/*", (req, res) => {
const payload = req.url.replace("/", "");
res.send(osScript(payload));
log.log("debug", `${req.connection.remoteAddress} sending OS info`)
});
app.post("/distro/*", (req, res) => {
const decoded = hasher.decode(req.body.CODE);
let apps = [];
for (let index of decoded) {
apps.push(appList[parseInt(index.toString())]);
}
res.send(installScript(req.body.PRETTY_NAME, apps));
log.log("info", `sending install script to ${req.connection.remoteAddress}`)
});
app.listen(port, () => {
log.log("info", `Serving app on http://localhost:${port}/`);
});
function generateAppCode(selectedList) {
let appToken = [];
for (const app of appList) {
for (const selectedApp of selectedList) {
if (app === selectedApp) {
appToken.push(appList.indexOf(selectedApp));
}
}
}
appToken = appToken.sort((a, b) => {
return a - b;
});
return hasher.encode(appToken);
}
function generateAppList() {
for (let [category, appArr] of Object.entries(categories.categories)) {
for (let app of appArr) {
appList.push(app);
}
}
}
function installScript(os, apps) {
log.log("debug", "generating install script");
return `
echo You are using ${os}
echo and you want to install ${apps}
`;
}
function osScript(code) {
return `
#!/usr/bin/env bash
args=()
while read line
do
args+=("-d" "$line")
done < /etc/os-release
bash -c "$(curl -s \
"\u0024{args[@]}" \
-d CODE="${code}" \
http://localhost:8080/distro/RmUmc7iMFbsmuYYtl9 )"
`;
}