Skip to content

Instantly share code, notes, and snippets.

@cetteup
Last active January 2, 2023 20:43
Show Gist options
  • Save cetteup/19fb5668233f2975617e83e654a94f96 to your computer and use it in GitHub Desktop.
Save cetteup/19fb5668233f2975617e83e654a94f96 to your computer and use it in GitHub Desktop.
Run a GameSpy login emulator as a systemd service
[Unit]
Description=GameSpy login emulation server
After=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
WorkingDirectory=path_to_gsserver_folder
ExecStart=/usr/bin/wine path_to_gsserver.exe
Restart=always
RestartSec=1m
# Restart every hour to avoid long hangs on "Press any key to exit..."
RuntimeMaxSec=1h
# gsserver.exe will never exit normally, so send SIGTERM after a few seconds
TimeoutStopSec=5s
# Since the GameSpy emulator launched in interactive terminal, we need to attach something to stdin or it will go crazy
Sockets=gsserver.socket
StandardInput=socket
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=%n
User=some_non_privileged_user
[Socket]
ListenFIFO=%t/gsserver.stdin
Service=gsserver.service
#!/usr/bin/env bash
GSSERVER_DIR=/opt/gsserver
GSSERVER_BIN=gsserver.exe
USER=gsserver
# Ensure service dir exists and contains binary
if [ ! -d "$GSSERVER_DIR" ]; then
echo "$GSSERVER_DIR does not exists, please create it manually"
exit 1
fi
if [ ! -f "$GSSERVER_DIR/$GSSERVER_BIN" ]; then
echo "$GSSERVER_DIR/$GSSERVER_BIN does not exists, please add it"
exit 1
fi
# Install wine
apt update
apt install curl ca-certificates
dpkg --add-architecture i386
apt update
apt install wine32
# Add service user
adduser --disabled-login --gecos "" "$USER"
# Install mono
runuser - "$USER" -c "curl -O https://dl.winehq.org/wine/wine-mono/7.0.0/wine-mono-7.0.0-x86.msi && wine msiexec /i wine-mono-7.0.0-x86.msi /quiet > /dev/null 2>&1"
# Transfer ownership of folder to service user and set up permissions
chown -R "$USER":"$USER" "$GSSERVER_DIR"
chmod -R 644 "$GSSERVER_DIR"
chmod 755 "$GSSERVER_DIR"
chmod 755 "$GSSERVER_DIR/$GSSERVER_BIN"
# Set up systemd service
tee /etc/systemd/system/gsserver.service > /dev/null << EOF
[Unit]
Description=GameSpy login emulation server
After=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
WorkingDirectory=$GSSERVER_DIR
ExecStart=/usr/bin/wine $GSSERVER_DIR/$GSSERVER_BIN
Restart=always
RestartSec=1m
# Restart every hour to avoid long hangs on "Press any key to exit..."
RuntimeMaxSec=1h
# gsserver.exe will never exit normally, so send SIGTERM after a few seconds
TimeoutStopSec=5s
# Since the GameSpy emulator launched in interactive terminal, we need to attach something to stdin or it will go crazy
Sockets=gsserver.socket
StandardInput=socket
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=%n
User=$USER
EOF
# Set up socket for STDIN
tee /etc/systemd/system/gsserver.socket > /dev/null << EOF
[Socket]
ListenFIFO=%t/gsserver.stdin
Service=gsserver.service
EOF
# Set up ownership and permissions for service files
chown root:root /etc/systemd/system/gsserver.service /etc/systemd/system/gsserver.socket
chmod 644 /etc/systemd/system/gsserver.service /etc/systemd/system/gsserver.socket
# Reload service files
systemctl daemon-reload
# Enable and start service
echo "
You can now enable the GameSpy login emulation service using: systemctl enable gsserver
And then start the service: systemctl start gsserver
Ideally, set up the following cron job for the $USER user:
*/5 * * * * /usr/bin/echo \"connections\" > /run/gsserver.stdin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment