@@ -23,6 +23,7 @@ def __init__(self):
23
23
self .rcon_command = self .conf ['rcon_command' ]
24
24
self .daemon_time = self .conf ['daemon_time' ]
25
25
self .arguments = self .conf .get ('arguments' , '' ).split ()
26
+ self .is_first_run = True
26
27
27
28
# 修改rcon源代码,忽略SessionTimeout异常
28
29
def patched_run (self , command : str , * args : str , encoding : str = "utf-8" ) -> str :
@@ -39,10 +40,34 @@ def patched_run(self, command: str, *args: str, encoding: str = "utf-8") -> str:
39
40
# Apply the monkey patch
40
41
Client .run = patched_run
41
42
43
+ def check_rcon (self ):
44
+ try :
45
+ with Client (
46
+ host = self .conf ['rcon_host' ],
47
+ port = self .conf ['rcon_port' ],
48
+ passwd = self .conf ['rcon_password' ],
49
+ timeout = 1 ):
50
+
51
+ INFO .logger .info ("[ RCON ] RCON连接正常" )
52
+ print ("[ RCON ] RCON连接正常" )
53
+ except TimeoutError :
54
+ INFO .logger .error ("[ RCON ] RCON连接超时,请检查IP和端口是否填写正确" )
55
+ print ("[ RCON ] RCON连接超时,请检查IP和端口是否填写正确" )
56
+ time .sleep (2 )
57
+ subprocess .run (['taskkill' , '/f' , '/im' , self .appName ], stderr = subprocess .DEVNULL )
58
+ exit (0 )
59
+ except rcon .exceptions .WrongPassword :
60
+ INFO .logger .error ("[ RCON ] RCON密码错误,请检查相关设置" )
61
+ print ("[ RCON ] RCON密码错误,请检查相关设置" )
62
+ time .sleep (2 )
63
+ subprocess .run (['taskkill' , '/f' , '/im' , self .appName ], stderr = subprocess .DEVNULL )
64
+ exit (0 )
65
+
42
66
def start_program (self ):
43
67
INFO .logger .info ("[ 启动任务 ] 正在启动程序......" )
44
68
print ("[ 启动任务 ] 正在启动程序......" )
45
69
program_args = [self .conf ['program_path' ]]
70
+
46
71
if self .conf ['arguments' ]:
47
72
INFO .logger .info ("[ 启动任务 ] 已配置额外参数" )
48
73
print ("[ 启动任务 ] 已配置额外参数" )
@@ -52,16 +77,30 @@ def start_program(self):
52
77
print ("[ 启动任务 ] 已开启多核选项" )
53
78
program_args .extend (["-useperfthreads" , "-NoAsyncLoadingThread" , "-UseMultithreadForDS" ])
54
79
print ("[ 启动任务 ] 启动参数:" , self .conf ['arguments' ].split ())
80
+
55
81
subprocess .Popen (program_args )
56
82
83
+ if self .conf ['rcon_enabled' ]:
84
+ if self .is_first_run : # 只有在首次运行时才检查RCON连接
85
+ INFO .logger .info ("[ RCON ] 已开启RCON功能" )
86
+ print ("[ RCON ] 已开启RCON功能" )
87
+ INFO .logger .info ("[ RCON ] 正在检查RCON连接,请等待5秒......" )
88
+ print ("[ RCON ] 正在检查RCON连接,请等待5秒......" )
89
+ time .sleep (5 )
90
+ self .check_rcon ()
91
+ self .is_first_run = False # 首次启动后,将状态标志设置为False
92
+ else :
93
+ INFO .logger .info ("[ RCON ] 未开启RCON功能" )
94
+ print ("[ RCON ] 未开启RCON功能" )
95
+
57
96
# 轮询任务(固定延迟执行)
58
97
def polling_task (self ):
59
98
60
99
while True :
61
100
if self .conf ['restart_interval' ] < 60 :
62
101
INFO .logger .error ("[ 轮询任务 ] 服务器重启时间 restart_interval 必须大于等于1分钟,请重新设置!" )
63
102
print ("[ 轮询任务 ] 服务器重启时间 restart_interval 必须大于等于1分钟,请重新设置!" )
64
- time .sleep (3 )
103
+ time .sleep (2 )
65
104
exit (0 )
66
105
67
106
# 启动程序前检查, 如果存在服务端则不再进行启动操作,改为每次循环结尾关闭进程
@@ -92,7 +131,7 @@ def polling_task(self):
92
131
else :
93
132
INFO .logger .error ("[ 内存监控 ] 轮询间隔 polling_interval_seconds 必须大于等于5秒,请重新设置!" )
94
133
print ("[ 内存监控 ] 轮询间隔 polling_interval_seconds 必须大于5秒,请重新设置!" )
95
- time .sleep (3 )
134
+ time .sleep (2 )
96
135
exit (0 )
97
136
98
137
print (f'\r [ 轮询任务 ] 服务器将在 { i } 秒后重启......' , end = '' )
@@ -111,8 +150,8 @@ def polling_task(self):
111
150
timeout = 1 ) as client :
112
151
message = self .conf ['shutdown_notices' ][str (i )]
113
152
response = client .run (f"{ self .conf ['rcon_command' ]} { message } " , 'utf-8' )
114
- INFO .logger .info ('Response: {0}' .format (response ))
115
- print ('Response: ' , response )
153
+ INFO .logger .info ('[指令发送] {0}' .format (response ))
154
+ print ('[指令发送] ' , response )
116
155
except TimeoutError :
117
156
INFO .logger .error ("RCON连接超时,请检查IP和端口是否填写正确" )
118
157
print ("\r \033 [K" , end = '' )
0 commit comments