#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

RED_COLOR="\033[0;31m"
NO_COLOR="\033[0m"
GREEN="\033[32m\033[01m"
BLUE="\033[0;36m"
FUCHSIA="\033[0;35m"

apt update -y && apt install vim curl lsof wget -y
apt install build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev host net-tools dnsutils whois -y
wget -N --no-check-certificate https://zfile.mymisaka.net/directlink/ca/openresty-1.27.1.1.tar.gz && tar -xvzf openresty-1.27.1.1.tar.gz
cd openresty-1.27.1.1
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-file-aio \
--with-threads \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
make && make install
rm /etc/nginx/nginx.conf
cat << 'EOF' | tee /etc/nginx/nginx.conf > /dev/null
worker_priority -20;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 204800;
worker_shutdown_timeout 120s;
error_log /dev/null;

events {
    worker_connections  204800;
    multi_accept on;
    accept_mutex off;
    use epoll;
}

stream {
include /etc/nginx/tunnelconf/*.conf;
}
EOF
cat << 'EOF' | tee /usr/lib/systemd/system/nginx.service > /dev/null
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
EOF
systemctl enable nginx --now
mkdir /etc/nginx/tunnelconf
cat << 'EOF' | tee /etc/nginx/tunnelconf/demo.conf > /dev/null
#server {
#        listen listen_ip:listen_port;
#        tcp_nodelay  on;
#        proxy_pass remote_ip:remote_port;
#        proxy_protocol off;
#        access_log off;
#}
EOF