- Port forwaring using ngrok
- Send IP to a discord channel
- Stop ngrok after server stopped
import subprocess
import os
import time
from discordwebhook import Discord
from datetime import timedelta
WEBHOOK = "YOUR WEBHOOK URL"
MEMORY = 16384
SERVERJAR = "sv.jar"
PORT = 25565
COMMAND = f"java -Xms{MEMORY}M -Xmx{MEMORY}M --add-modules=jdk.incubator.vector -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -XX:G1NewSizePercent=40 -XX:G1MaxNewSizePercent=50 -XX:G1HeapRegionSize=16M -XX:G1ReservePercent=15 -jar {SERVERJAR} --nogui"
webhook = Discord(url=WEBHOOK)
ngrok = subprocess.Popen(["ngrok", "tcp", str(PORT), "--log=stdout"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
starttime = time.time()
while True:
line = ngrok.stdout.readline().decode().rstrip()
if 'msg="started tunnel"' in line:
ip = line.split()[-1].replace('url=tcp://','')
print(f'Got IP: {ip}')
webhook.post(content=f'Server IP: `{ip}`')
break
os.system(COMMAND)
ngrok.terminate()
uptime = int(time.time() - starttime)
webhook.post(content=f"Server stopped after `{timedelta(seconds=uptime)}` of uptime")