分享一个现在用的“远程控制服务器”代码


Published:   Comment: No Comments

奇迹MU架设

之前有不少人问我怎么实现的网站控制游戏的开关(通过网站远程重启游戏、关闭游戏),其实这个并不难,asp是可以执行exe文件的(以前这种方法常见用于服务器提权比较多,此代码个别服务器可能会报木马删掉,毕竟很久远的时候都是通过此方法来进行服务器的入侵提权操作),通过下面代码执行一键启动游戏(很多游戏都已经做成了一键启动,所以只要执行一个文件即可远程对游戏进行开启、关闭),群服网站上的“远程启动、关闭游戏”就是通过以下代码实现的,如图所示
QQ图片20250406152017.jpg
群服的游戏远程控制代码为:

<!DOCTYPE html>
<html lang="zh-CN">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/@tailwindcss/browser@4"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <title>回首MU 远程控制</title>
</head>
    <body class="bg-gray-100 font-sans">
    <div class="container mx-auto p-8">
        <h1 class="text-3xl font-bold text-center text-blue-600 mb-8">回首MU 远程控制</h1>
        <div class="flex justify-center">
            <select id="action" name="action" class="p-2 border border-gray-300 rounded mr-4">
                <option value="start">强制重启</option>
                <option value="stop">强制关机</option>
            </select>
            <button id="execute" onclick="submitForm()"
                class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded transition duration-300">执行</button>
        </div>
        <%
        Dim action
        action = Request.Form("action")
        
        If action <> "" Then
            Dim exePath
            If action = "start" Then
                exePath = "C:\MuServer\一键启动.exe" 
            ElseIf action = "stop" Then
                exePath = "C:\MuServer\一键关闭.exe"
            End If
                            Dim shell
            Set shell = Server.CreateObject("WScript.Shell")
            shell.Run(exePath, 0, true)
            Set shell = Nothing
            Response.Write "<p class='text-green-600 text-center mt-4'>操作完成。</p>"
        End If
        %>
    </div>
    <script>
        function submitForm() {
            const form = document.createElement('form');
            form.method = 'post';
            const actionInput = document.createElement('input');
            actionInput.type = 'hidden';
            actionInput.name = 'action';
            actionInput.value = document.getElementById('action').value;
            form.appendChild(actionInput);
            document.body.appendChild(form);
            form.submit();
        }
    </script>
</body>
</html>   

还有一些更加智能的方案比如:

  1. 写一个进程检测程序,当检测到gameserver.exe进程消失时,就自动运行进程(此方法针对一些端不稳定,出现闪退等情况,比如一些破解端、残端会不定时重启,这个方案是最优不影响游戏体验)
  2. 当进程不存在时,通过api给网站发送通知,让玩家自行启动游戏,此方案影响游戏体验,目前群服所用方案。

我为什么不使用方案1,而选择方案2?原因是之前架设的游戏版本太多了,同时架设了,97高(低)倍、103H、S6飞跃、S6修仙 、S8。版本过多,有些版本没有人玩,可以通过网站让玩家自动开启、关闭服务器,玩自己想要玩的游戏,操作性更强。
如果你只是架设一个端,优先使用方案1,让程序 24小时检测进程,当进程不存在就马上运行“一键启动”,即可将游戏的体验保持到最佳。
方案2代码如下,使用Python代码,简单方便

import psutil
import subprocess
import time

# 要监测的进程名
process_name = "gameserver.exe"
# 要启动的程序路径
program_path = "C:\\MuServer\\gameserver.exe"

def is_process_running(process_name):
    for proc in psutil.process_iter(['name']):
        if proc.info['name'] == process_name:
            return True
    return False

while True:
    if not is_process_running(process_name):
        try:
            subprocess.Popen(program_path)
            print(f"{process_name} 进程不存在,已启动 {program_path}")
        except Exception as e:
            print(f"启动 {program_path} 时出错: {e}")
    time.sleep(1) 

如果你对编写程序完全不懂,还有更简单的方案,直接使用win自带的bat功能

@echo off
set "process_name=gameserver.exe"
set "program_path=C:\MuServer\gameserver.exe"

:check_process
tasklist | find /i "%process_name%" >nul
if %errorlevel% equ 1 (
    start "" "%program_path%"
    echo %process_name% 进程不存在,已启动 %program_path%
)
ping -n 2 127.0.0.1 >nul
goto check_process  

选择以上任意方法让你的游戏服务更加智能。

none
Last Modified:2025-04-11 23:34:26

我有话说