Update balancer.py

This commit is contained in:
xoceunder
2025-09-24 20:50:35 -04:00
parent dc90bd48f3
commit 37593575bc
+61 -17
View File
@@ -6,10 +6,46 @@ from zipfile import ZipFile
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
rDownloadURL = "https://bitbucket.org/xoceunder/x-ui/raw/master/sub_xui_xoceunder.zip"
rPackages = ["cpufrequtils", "iproute2", "python-is-python3", "python3-pip", "net-tools", "dirmngr", "gpg-agent", "software-properties-common", "libcurl4", "libxslt1-dev", "libgeoip-dev", "libonig-dev", "e2fsprogs", "wget", "sysstat", "alsa-utils", "v4l-utils", "mcrypt", "nscd", "htop", "iptables-persistent", "libjpeg-dev", "libpng-dev", "php-ssh2", "xz-utils", "zip", "unzip", "mc", "libpng16-16", "rsync"]
rDownloadURL = "https://bitbucket.org/xoceunder/xtream-ui-install/raw/main/sub_xui_xoceunder.zip"
rPackages = ["cpufrequtils", "iproute2", "python", "net-tools", "dirmngr", "gpg-agent", "software-properties-common", "libcurl4", "libxslt1-dev", "libgeoip-dev", "libonig-dev", "e2fsprogs", "wget", "sysstat", "alsa-utils", "v4l-utils", "mcrypt", "nscd", "htop", "iptables-persistent", "libjpeg-dev", "libpng-dev", "php-ssh2", "xz-utils", "zip", "unzip", "mc", "libpng16-16", "libzip5", "mariadb-server", "rsync", "libmaxminddb0", "libmaxminddb-dev"]
rRemove = ["mysql-server"]
rSysCtl = "# XtreamCodes\n\nnet.ipv4.tcp_congestion_control = bbr\nnet.core.default_qdisc = fq\nnet.ipv4.tcp_rmem = 8192 87380 134217728\nnet.ipv4.udp_rmem_min = 16384\nnet.core.rmem_default = 262144\nnet.core.rmem_max = 268435456\nnet.ipv4.tcp_wmem = 8192 65536 134217728\nnet.ipv4.udp_wmem_min = 16384\nnet.core.wmem_default = 262144\nnet.core.wmem_max = 268435456\nnet.core.somaxconn = 1000000\nnet.core.netdev_max_backlog = 250000\nnet.core.optmem_max = 65535\nnet.ipv4.tcp_max_tw_buckets = 1440000\nnet.ipv4.tcp_max_orphans = 16384\nnet.ipv4.ip_local_port_range = 2000 65000\nnet.ipv4.tcp_no_metrics_save = 1\nnet.ipv4.tcp_slow_start_after_idle = 0\nnet.ipv4.tcp_fin_timeout = 15\nnet.ipv4.tcp_keepalive_time = 300\nnet.ipv4.tcp_keepalive_probes = 5\nnet.ipv4.tcp_keepalive_intvl = 15\nfs.file-max=20970800\nfs.nr_open=20970800\nfs.aio-max-nr=20970800\nnet.ipv4.tcp_timestamps = 1\nnet.ipv4.tcp_window_scaling = 1\nnet.ipv4.tcp_mtu_probing = 1\nnet.ipv4.route.flush = 1\nnet.ipv6.route.flush = 1"
initd_script = """#!/bin/sh
SCRIPT=/home/xtreamcodes/iptv_xtream_codes/xtreamcodes
NAME=xtreamcodes
case "$1" in
start)
$SCRIPT start
;;
stop)
$SCRIPT stop
;;
restart)
$SCRIPT restart
;;
reload)
$SCRIPT reload
;;
status)
pids=$(pgrep -u xtreamcodes nginx | wc -l)
if [ "$pids" -gt 0 ]; then
echo "$NAME is running"
else
echo "$NAME is stopped"
fi
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|status}"
exit 1
;;
esac
exit 0
"""
def getVersion():
try: return os.popen("lsb_release -d").read().strip()
except: return ""
@@ -26,7 +62,7 @@ def is_installed(package_name):
return False
def prepare():
global rPackages
global rPackages, rRemove
for rFile in ["/var/lib/dpkg/lock-frontend","/var/cache/apt/archives/lock","/var/lib/dpkg/lock"]:
if os.path.exists(rFile):
try:
@@ -35,13 +71,12 @@ def prepare():
pass
os.system("apt-get update > /dev/null 2>&1")
os.system("apt-get -y full-upgrade > /dev/null 2>&1")
for rPackage in rRemove:
if is_installed(rPackage):
os.system("sudo DEBIAN_FRONTEND=noninteractive apt-get remove %s > /dev/null" % rPackage)
for rPackage in rPackages:
if not is_installed(rPackage):
subprocess.run(f"sudo DEBIAN_FRONTEND=noninteractive apt-get install {rPackage} -yq > /dev/null 2>&1", shell=True)
if not is_installed("libssl1.1"):
subprocess.run("wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.0g-2ubuntu4_amd64.deb > /dev/null 2>&1 && sudo dpkg -i libssl1.1_1.1.0g-2ubuntu4_amd64.deb > /dev/null 2>&1 && rm -rf libssl1.1_1.1.0g-2ubuntu4_amd64.deb > /dev/null 2>&1", shell=True)
if not is_installed("libzip5"):
subprocess.run("wget http://archive.ubuntu.com/ubuntu/pool/universe/libz/libzip/libzip5_1.5.1-0ubuntu1_amd64.deb > /dev/null 2>&1 && sudo dpkg -i libzip5_1.5.1-0ubuntu1_amd64.deb > /dev/null 2>&1 && rm -rf libzip5_1.5.1-0ubuntu1_amd64.deb > /dev/null 2>&1", shell=True)
os.system("apt-get install -y > /dev/null") # Clean up above
os.system("adduser --system --shell /bin/false --group --disabled-login xtreamcodes > /dev/null")
if not os.path.exists("/home/xtreamcodes"): os.mkdir("/home/xtreamcodes")
@@ -72,11 +107,14 @@ def configure():
rFile.write("tmpfs /home/xtreamcodes/iptv_xtream_codes/streams tmpfs defaults,noatime,nosuid,nodev,noexec,mode=1777,size=90% 0 0\ntmpfs /home/xtreamcodes/iptv_xtream_codes/tmp tmpfs defaults,noatime,nosuid,nodev,noexec,mode=1777,size=2G 0 0")
rFile.close()
if not "xtreamcodes" in open("/etc/sudoers").read(): os.system('echo "xtreamcodes ALL = (root) NOPASSWD: /sbin/iptables, /usr/bin/chattr, /usr/bin/python3, /usr/bin/python" >> /etc/sudoers')
if not os.path.exists("/home/xtreamcodes/iptv_xtream_codes/xtreamcodes"):
os.system("wget -q https://github.com/xoceunder/Xtream-UI-install/raw/main/xtreamcodes -O /home/xtreamcodes/iptv_xtream_codes/xtreamcodes")
os.system("sudo chmod +x /home/xtreamcodes/iptv_xtream_codes/xtreamcodes")
if not os.path.exists("/etc/init.d/xtreamcodes"):
rFile = open("/etc/init.d/xtreamcodes", "w")
rFile.write("#! /bin/bash\n/home/xtreamcodes/iptv_xtream_codes/start_services.sh")
rFile.close()
os.system("chmod +x /etc/init.d/xtreamcodes > /dev/null")
with io.open("/etc/init.d/xtreamcodes", "w", encoding="utf-8") as f:
f.write(initd_script)
os.system("sudo chmod +x /etc/init.d/xtreamcodes")
os.system("sudo update-rc.d xtreamcodes defaults")
os.system("sudo modprobe ip_conntrack")
rFile = open("/etc/sysctl.conf", "w", encoding="utf-8")
rFile.write(rSysCtl)
@@ -87,16 +125,14 @@ def configure():
if not "DefaultLimitNOFILE=655350" in open("/etc/systemd/system.conf").read():
os.system('sudo echo "\nDefaultLimitNOFILE=655350" >> "/etc/systemd/system.conf"')
os.system('sudo echo "\nDefaultLimitNOFILE=655350" >> "/etc/systemd/user.conf"')
if os.path.exists("/home/xtreamcodes/iptv_xtream_codes/sysctl.on"):
os.remove("/home/xtreamcodes/iptv_xtream_codes/sysctl.on")
try: os.remove("/usr/bin/ffmpeg")
except: pass
if not os.path.exists("/home/xtreamcodes/iptv_xtream_codes/tv_archive"): os.mkdir("/home/xtreamcodes/iptv_xtream_codes/tv_archive/")
os.system("ln -s /home/xtreamcodes/iptv_xtream_codes/bin/ffmpeg /usr/bin/")
os.system("wget -q https://git.io/GeoLite2-Country.mmdb -O /home/xtreamcodes/iptv_xtream_codes/GeoLite2.mmdb")
os.system("wget -q https://bitbucket.org/xoceunder/xtream-ui-install/raw/main/GeoLite2.mmdb -O /home/xtreamcodes/iptv_xtream_codes/GeoLite2.mmdb")
os.system("chattr -ai /home/xtreamcodes/iptv_xtream_codes/GeoLite2.mmdb > /dev/null")
os.system("sudo chmod 0777 /home/xtreamcodes/iptv_xtream_codes/GeoLite2.mmdb > /dev/null")
os.system("wget -q https://bitbucket.org/xoceunder/x-ui/raw/master/pid_monitor.php -O /home/xtreamcodes/iptv_xtream_codes/crons/pid_monitor.php")
os.system("wget -q https://bitbucket.org/xoceunder/xtream-ui-install/raw/main/pid_monitor.php -O /home/xtreamcodes/iptv_xtream_codes/crons/pid_monitor.php")
os.system("chown xtreamcodes:xtreamcodes -R /home/xtreamcodes > /dev/null")
os.system("chmod -R 0777 /home/xtreamcodes > /dev/null")
os.system("sed -i 's|chown -R xtreamcodes:xtreamcodes /home/xtreamcodes|chown -R xtreamcodes:xtreamcodes /home/xtreamcodes 2>/dev/null|g' /home/xtreamcodes/iptv_xtream_codes/start_services.sh")
@@ -107,9 +143,9 @@ def configure():
if not "api.xtream-codes.com" in open("/etc/hosts").read(): os.system('echo "127.0.0.1 api.xtream-codes.com" >> /etc/hosts')
if not "downloads.xtream-codes.com" in open("/etc/hosts").read(): os.system('echo "127.0.0.1 downloads.xtream-codes.com" >> /etc/hosts')
if not "xtream-codes.com" in open("/etc/hosts").read(): os.system('echo "127.0.0.1 xtream-codes.com" >> /etc/hosts')
if not "@reboot root /home/xtreamcodes/iptv_xtream_codes/start_services.sh" in open("/etc/crontab").read(): os.system('echo "@reboot root /home/xtreamcodes/iptv_xtream_codes/start_services.sh" >> /etc/crontab')
if not "@reboot root /etc/init.d/xtreamcodes start" in open("/etc/crontab").read(): os.system('echo "@reboot root /etc/init.d/xtreamcodes start" >> /etc/crontab')
def start(): os.system("/home/xtreamcodes/iptv_xtream_codes/start_services.sh > /dev/null")
def start(): os.system("sudo /etc/init.d/xtreamcodes start > /dev/null")
def setPorts(rPorts):
os.system("sed -i 's/listen 25461;/listen %d;/g' /home/xtreamcodes/iptv_xtream_codes/nginx/conf/nginx.conf" % rPorts[0])
@@ -118,6 +154,14 @@ def setPorts(rPorts):
os.system("sed -i 's/listen 25462;/listen %d;/g' /home/xtreamcodes/iptv_xtream_codes/nginx_rtmp/conf/nginx.conf" % rPorts[2])
if __name__ == "__main__":
try: rVersion = os.popen('lsb_release -sr').read().strip()
except: rVersion = None
if rVersion == "20.04":
rPackages.append("libzip5")
elif rVersion == "18.04":
rPackages.append("libzip4")
else:
rPackages.append("libzip5")
rHost = sys.argv[1]
rPort = int(sys.argv[2])
rUsername = sys.argv[3]