NginxInstaller/app.js
2023-12-21 16:25:34 -05:00

141 lines
5.9 KiB
JavaScript

document.getElementById("submit").addEventListener("click", generateQuickCmd, false);
const debug = false; // DISABLE FOR PRODUCTION
const baseURL = debug ? "http://localhost:8081/ngx" : "https://api.nevets.tech/ngx";
const service = "[Unit]\n" +
"Description=The NGINX HTTP and reverse proxy server\n" +
"After=syslog.target network-online.target remote-fs.target nss-lookup.target\n" +
"Wants=network-online.target\n" +
"\n" +
"[Service]\n" +
"Type=forking\n" +
"PIDFile=/var/run/nginx.pid\n" +
"ExecStartPre=/usr/sbin/nginx -t\n" +
"ExecStart=/usr/sbin/nginx\n" +
"ExecReload=/usr/sbin/nginx -s reload\n" +
"ExecStop=/bin/kill -s QUIT $MAINPID\n" +
"PrivateTmp=true\n" +
"\n" +
"[Install]\n" +
"WantedBy=multi-user.target";
// async function fetch1() {
// let url = "https://thingproxy.freeboard.io/fetch/ " + encodeURIComponent("https://nginx.org/download/");
// await $.get(url, (data, status) => {
// console.log(data);
// return data.body;
// });
// return "ERROR?";
// }
// const sleep = ms => new Promise(r => setTimeout(r, ms));
async function generateQuickCmd() {
let nginxStr = document.getElementById("link").value;
if (nginxStr === "") {
document.getElementById("script").innerHTML = "ERROR!? Did you leave the NGINX Verison field blank?" + "\n<span class=\"tooltiptext\" id=\"myTooltip\">Copy to clipboard</span>";
return;
}
if (extractNginxVersion(nginxStr)===null || extractNginxVersion(nginxStr).includes("null")) {
document.getElementById("script").innerHTML = "ERROR!? Regex could not find the version in the input field!" + "\n<span class=\"tooltiptext\" id=\"myTooltip\">Copy to clipboard</span>";
return;
}
let reqData = "{" +
"\"version\":\"" + encodeURIComponent(extractNginxVersion(nginxStr)) + "\"" + "}";
let continueCycle = true;
await fetch(baseURL + "/dhv", {
method: "POST",
body: reqData,
}).then(res => res.json().then(json => {
if (json.url.includes("false")) {
document.getElementById("script").innerHTML = "ERROR!? Do you have a valid verison of NGINX?" + "\n<span class=\"tooltiptext\" id=\"myTooltip\">Copy to clipboard</span>";
continueCycle = false;
}
}));
if (!continueCycle) {
return;
}
reqData = "{" +
"\"content\":\"" + encodeURI(buildCmd()) + "\"" +
"}";
fetch(baseURL + "/create", {
method: "POST",
body: reqData,
}).then(res => res.json().then(json => {
document.getElementById("script").innerHTML = "curl -s " + json.url + " | bash" + "\n<span class=\"tooltiptext\" id=\"myTooltip\">Copy to clipboard</span>";
}));
}
function outFunc() {
var tooltip = document.getElementById("myTooltip");
tooltip.innerHTML = "Copy to clipboard";
}
function copy() {
var copyText = document.getElementById("script").innerHTML.split("<span")[0];
var tooltip = document.getElementById("myTooltip");
navigator.clipboard.writeText(copyText).then(r => tooltip.innerHTML = "Copied!");
}
function buildCmd() {
let nginxStr = document.getElementById("link").value; //user input
let script = "#!/bin/bash\n" +
"if [ \"$EUID\" -ne 0 ]\n" +
"then echo \"Please run as root (or with sudo)\"\n" +
" exit\n" +
"fi\n\n" +
"apt-get update\n" +
"apt-get install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev wget git\n" +
"wget https://nginx.org/download/nginx-" + extractNginxVersion(nginxStr) + ".tar.gz\n" +
"tar -xzvf " + "nginx-" + extractNginxVersion(nginxStr) + ".tar.gz\n";
if (document.getElementById("extMod").checked === true) {
script += "git clone https://github.com/google/ngx_brotli.git\n" +
"git clone https://github.com/openresty/headers-more-nginx-module.git\n" +
"cd ngx_brotli/\n" +
"git submodule update --init\n" +
"cd ../nginx-" + extractNginxVersion(nginxStr) + "/\n";
} else {
script += "cd nginx-" + extractNginxVersion(nginxStr) + "\n";
}
script += "cp -r conf /etc/nginx\n" +
"mkdir /var/cache/nginx\n" +
"chown www-data:www-data /var/cache/nginx\n" +
getConfigureStr() + "\n" +
"make\n" +
"make install\n" +
"echo \"" + service + "\" >> /lib/systemd/system/nginx.service\n" +
"systemctl enable nginx.service\n" +
"systemctl start nginx.service";
return script;
}
function getConfigureStr() {
let str = "./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/etc/nginx/logs/error.log --http-log-path=/etc/nginx/logs/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=www-data --group=www-data ";
if (document.getElementById("extMod").checked === true) {
str += "--add-module=../headers-more-nginx-module --add-module=../ngx_brotli ";
}
if (document.getElementById("http3").checked === true) {
str += "--with-http_v3_module ";
}
str += "--with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_gunzip_module --with-http_gzip_static_module ";
return str;
}
function extractNginxVersion(url) {
// This regex matches 'nginx' followed by a hyphen and a version number, followed by '.tar.gz'
const regex = /\d+\.\d+\.\d+/;
// Use the regex to search the string
const match = url.match(regex);
// If a match is found, return it. Otherwise, return null or an appropriate message
return match ? match[0] : null;
}