-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hibernate.py
252 lines (213 loc) · 13.2 KB
/
Hibernate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import base64
import socket
import time
from json import loads
from re import findall, sub
import yaml
from mcdreforged.api.all import *
PLUGIN_METADATA = {
'id': 'hibernate',
'version': '0.0.1',
'name': 'Hibernate',
'description': "Hibernate your server when no one's online, and resume it when someone login",
'author': 'Sciroccogti',
'link': 'https://github.com/Dark-Night-Base/AutoSleep',
}
waitmin = 10 # wait 10min before shutting server down
server_property = {"Version": "", "Protocol": 0, "MaxPlayers": 0}
@new_thread
def stop_server_later(server: ServerInterface, waitmin: int):
'''After waitmin, shutdown the server and turn on the FakeServer'''
time.sleep(waitmin * 60)
# check if there's someone login in the waiting time
if (Client().getResultNew())["OnlinePlayers"] == 0:
server.logger.info("Hibernate!")
server.stop()
while server.is_server_running():
pass
FakeServer(server)
@new_thread
def query_playernum(server: ServerInterface):
global waitmin
time.sleep(1) # in case of server is not ready yet
playerCount = Client().getResultNew()["OnlinePlayers"]
if playerCount == 0:
stop_server_later(server, waitmin)
def on_load(server: ServerInterface, old_module):
global waitmin
try:
with open("config/Hibernate.yml") as file:
confYaml = yaml.load(file, Loader=yaml.CLoader)
waitmin = confYaml["waitmin"]
except:
server.logger.info('fail to read config file, using default value')
with open("config/Hibernate.yml", 'w') as file:
config = {"waitmin": waitmin}
yaml.dump(config, file)
server.register_help_message(
"!!hibernate", "Hibernate server at %dmin after no one's online" % waitmin)
if server.is_server_startup():
query_playernum(server)
def on_server_startup(server: ServerInterface):
query_playernum(server)
def on_player_left(server: ServerInterface, player: str):
query_playernum(server)
def pic2base() -> bytes:
'''encode the server-icon.png to base64'''
image_path = "server/server-icon.png"
with open(image_path, "rb") as f:
image = f.read()
image_base64 = base64.b64encode(image)
return image_base64
def genStatus() -> bytes:
msg = '{"description":{"text":"server is sleeping, connect to wake it up"},\
"players":{"max":%d,"online":0},\
"version":{"name":"%s","protocol":%d},\
"favicon":"data:image/png;base64,' % (server_property["MaxPlayers"], server_property["Version"], server_property["Protocol"])
# the favicon is https://minecraft-zh.gamepedia.com/File:White_Bed_JE3_BE2.png
faviconBytes = b'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAMuklEQVR42u2aSXAc133Gf73MvmAjCIIgSJEUJdgiKVKUxVhSItuKnJyck1Iu35Kyq3Lxyb7klBxTlRxSVsVOonJFrEp540FxUomWmEwM0ZYUiSQELqAFEvs2M5itMd3Te+fQ0z09WEiKgkg6wXea7n79+n3f+97//f+vBnawgx3sYAc72MEO/p9CeNADuBtMzS94we9Dw/u2dczigyZ3O6hq841CadUDGOjtBWB6YdFTVe3d7frGQ+kATdO+53l8O3qvVqszs7oKwIlHDwPged5KNpsZ/D8jwNT8QiGbTu9OJxIUq1V29/Rg2zZjYx8BEOvpwarXwXUB2H/4U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Lajv6PEjdlRcikPUZRwY8l2VPfQRz9aSvEZo2MJAJh6MFsCF8YXATh6IE+89C7lioLUc5DEgRco1pXQEXdCQH5N03BaxYpcu0JjdQGAiiUyX2i75LOw+pYC6JqCKLUrsVXFX4u78hLTBY2BvgxXZxWYVTg4kCFdnUarTpN/9AXM7EGKdYVETO4IfusR1OgAkjaP1PCj+lrTZlUJ0+PC6NjSHu4zZA9WXcfquLnWNFhrJWNnz00KAC+/eESfLqgJgMeHcsQql4lVLmMMvIBBX8fS2Ix4UI8begPbdrk2HR6trY6OLfXfb+IBQqv95Z+c+ua1OeXVQtlPMERX+OJ/jy++t/6FaFr5eyf2AaBpBlZmP/Hh5wAw9Sbzc/Mt4jeQHL/PqqIxOdte//fT6ncUIMDzx/edvjC+8P6dXgyE6M5m2NsXR/D8pZM/9CyKNMT81XdIW/MkklmWS/WwHgfU0bGl7IMmvqUAnxTf+qMTBhAHGOqV6OvNU2/oTM6UyGaTzCyprDUNEJgfvby0/0ETXo9P/S+xV38+lqip2l8F18l0F4IoIqaSNBp+nTA6tiQ8jOS3RYAAtUa7OIkn2nXEWtO4l+7uG+7+TOs26Mmk8FqrSdfqOJZFNiaiND9lx/cB2+KAutpmmkz522Bw5vawY1sEcIksAUEIRfhtwLYIcPYXH/85CG+CvwT0ptI+c/N480GTvB22PRF55btf/ke1aX1rsajwytnxB57o7GAHO9jBDnawgx3sYAv8L+XvjEwK+4EwAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIxLTAxLTE5VDE2OjIzOjE5KzA4OjAwhLwWPAAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyMS0wMS0xOVQxNjoyMzoxOSswODowMPXhroAAAAAASUVORK5CYII='
statusBytes = msg.encode("utf-8") + faviconBytes + '"}'.encode("utf-8")
statusBytes = bytes(encode_varint(len(statusBytes)),
"latin-1") + statusBytes
statusBytes = b'\x00' + statusBytes
statusBytes = bytes(encode_varint(len(statusBytes)),
"latin-1") + statusBytes
return statusBytes
# def genLoginReturn() -> bytes:
# msg = 'Server is starting... Please wait for a minute and try again!!\
# For more information, check website https://mc.sciroccogti.top/!'
# ret = msg.encode("utf-8")
# ret = bytes(encode_varint(len(ret)), "latin-1") + ret
# return ret
@new_thread
def FakeServer(server: ServerInterface):
'''
Act as a server when the Java server is shut down
ping request1: L1 \x00\xc2\x04 HOST c \xdd\x01\x01\x00
L1: length from \x00 to \x01 (included)
e.g. \x16\x00\xc2\x04\x12tis.union.worldc\xdd\x01\x01\x00
ping request2: \xfe\x01\xfa\x00\x0b MC|PingHost+HOST \x00\x00 c \xdd
e.g. \xfe\x01\xfa\x00\x0bMC|PingHost+tis.union.world\x00\x00c\xdd
login request: L1 \x00\xc2\x04 HOST c \xdd\x02\r\x00 L2 USER
L1: length from \x00 to \x02 (included)
L2: length of USER
e.g. \x16\x00\xc2\x04\x12tis.union.worldc\xdd\x02\r\x00\x0bSciroccogti
'''
server.logger.info("FakeServer start!")
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# TODO: read port from server.properties
while True: # waiting for port exiting TIME_WAIT
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(
socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
server_socket.bind((socket.gethostname(), 25565))
except Exception as err:
server.logger.warn("port is been using", err)
server_socket.close()
time.sleep(1)
else:
server.logger.info("FakeServer is ready")
break
server_socket.listen(128)
try:
while True:
client_socket, client_addr = server_socket.accept() # wait for client
recv_data = client_socket.recv(50)
# print(recv_data)
# print(recv_data.decode("unicode-escape"))
dataLength = decode_varint(recv_data[:2].decode("latin-1")) + 2
# Judge if is "MC|PingHost+`ip`" or ping request 1
if ((recv_data[0:2] == b"\xfe\x01" # frame head
and recv_data[5:27].decode("utf-16-be") == "MC|PingHost")
or len(recv_data) < dataLength + 2):
server.logger.info("FakeServer get a ping")
client_socket.send(genStatus())
client_socket.close()
else: # login request
# start server here
server.logger.info("FakeServer get a login request")
server.start()
client_socket.close()
server_socket.close()
break
# client_socket.send(genLoginReturn())
except:
server_socket.close()
class Client(object):
'''https://github.com/Saren-Arterius/MinecraftServerQueryPythonTCP'''
def __init__(self, host=socket.gethostname(), port=25565, timeout=5):
self.host = host
self.port = port # 0 to use any available port
self.timeout = timeout
def __getDataNew(self):
from struct import pack
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
s.settimeout(self.timeout)
s.connect((self.host, self.port))
handShake = b"\x00"
handShake += b"\x04"
handShake += bytes(encode_varint(len(self.host)), "UTF-8")
handShake += bytes(self.host, "UTF-8")
handShake += pack("H", self.port)
handShake += b"\x01"
handShake = bytes(encode_varint(len(handShake)), "UTF-8") + handShake
s.sendall(handShake)
s.sendall(b'\x01\x00')
data = s.recv(1024)
dataLength = decode_varint(data[:2].decode("latin-1")) + 2
while 1:
recv = s.recv(1024)
data += recv
if len(data) == dataLength:
break
s.close()
assert data[2:3] == b"\x00"
assert decode_varint(data[3:5].decode("latin-1")) == len(data[5:])
return data[5:].decode("UTF-8")
def getResultNew(self):
global server_property
status = {"Version": "", "MOTD": "",
"OnlinePlayers": 0, "MaxPlayers": 0}
try:
jsonData = loads(self.__getDataNew())
status["Version"] = findall(
"(\d.\d+.\d)", jsonData["version"]["name"])[0]
status["Protocol"] = jsonData["version"]["protocol"]
status["MOTD"] = jsonData["description"]
status["OnlinePlayers"] = int(jsonData["players"]["online"])
status["MaxPlayers"] = int(jsonData["players"]["max"])
status["ServerIcon"] = sub("\n", "", jsonData["favicon"])
except Exception as e:
print("Error in getResultNew:", e)
finally:
server_property["Version"] = status["Version"]
server_property["MaxPlayers"] = status["MaxPlayers"]
server_property["Protocol"] = status["Protocol"]
return status
def encode_varint(value):
return "".join(encode_varint_stream([value]))
def decode_varint(value):
return decode_varint_stream(value).__next__()
def encode_varint_stream(values):
for value in values:
while True:
if value > 127:
yield chr((1 << 7) | (value & 0x7f))
value >>= 7
else:
yield chr(value)
break
def decode_varint_stream(stream):
value = 0
base = 1
for raw_byte in stream:
val_byte = ord(raw_byte)
value += (val_byte & 0x7f) * base
if (val_byte & 0x80):
base *= 128
else:
yield value
value = 0
base = 1