-
Notifications
You must be signed in to change notification settings - Fork 0
/
Launcher.py
150 lines (132 loc) · 4.98 KB
/
Launcher.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
import boto3
import time
import socket
import struct
import select
from io import BytesIO
from Settings import *
def getInstanceState(ec2):
running_instances = ec2.describe_instances()
for r in running_instances['Reservations']:
for i in r['Instances']:
if i['InstanceId'] == server_instanceId:
return i['State']['Name']
return 'error'
def getAddressTarget(ec2):
live_addresses = ec2.describe_addresses()
for r in live_addresses['Addresses']:
if r['AllocationId'] == ip_allocationId:
return r['InstanceId']
def recvFixed(conn, size):
data = bytearray()
while len(data) < size:
more = conn.recv(size - len(data))
if not more:
raise EOFError()
data.extend(more)
return data
def readByte(data):
if type(data) == socket.socket:
return struct.unpack('B', data.recv(1))[0]
else:
return struct.unpack('B', data.read(1))[0]
def unpack_varint(conn):
total = 0
shift = 0
val = 0x80
while val&0x80:
val = readByte(conn)
total |= ((val&0x7F)<<shift)
shift += 7
if total&(1<<31):
total = total - (1<<32)
return total
def pack_varint(val):
total = b''
if val < 0:
val = (1<<32)+val
while val>=0x80:
bits = val&0x7F
val >>= 7
total += struct.pack('B', (0x80|bits))
bits = val&0x7F
total += struct.pack('B', bits)
return total
ec2 = boto3.client('ec2', region_name=server_regionName)
socks = []
for port in TCP_PORTS:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.settimeout(MINECONN_TIMEOUT)
s.bind((socket.gethostname(), port))
s.listen(1)
socks.append(s)
lastReassocTime = time.time()
while True:
if time.time() > lastReassocTime + REASSOC_TIMEOUT:
lastReassocTime = time.time()
print('Attempt reassociation')
isServerTarget = getAddressTarget(ec2) == server_instanceId
isServerRunning = getInstanceState(ec2) == 'running'
print('isServerTarget: ' + str(isServerTarget))
print('isServerRunning: ' + str(isServerRunning))
if (not isServerTarget) and isServerRunning:
print('Reassociate ip to server')
ec2.associate_address(AllocationId=ip_allocationId, InstanceId=server_instanceId)
if isServerTarget and (not isServerRunning):
print('Reassociate ip to launcher')
ec2.associate_address(AllocationId=ip_allocationId, InstanceId=launcher_instanceId)
try:
read_sockets, write_sockets, error_sockets = select.select(socks, [], [], MINECONN_TIMEOUT)
if len(read_sockets) > 0:
conn, addr = read_sockets[0].accept()
conn.settimeout(MINECONN_TIMEOUT)
print('Connection address:', addr)
length = unpack_varint(conn)
print('len: ' + str(length))
data = BytesIO(recvFixed(conn, length))
pid = unpack_varint(data)
print('pid: ' + str(pid))
version = unpack_varint(data)
print('version: ' + str(version))
length = unpack_varint(conn)
print('len: ' + str(length))
data = BytesIO(recvFixed(conn, length))
pid = unpack_varint(data)
print('pid: ' + str(pid))
if (length == 1) and (pid == 0):
RESP_JSON = RESP_JSON_PREPRO + str(version) + RESP_JSON_POSTPRO
pid = pack_varint(0)
jsonlen = pack_varint(len(RESP_JSON.encode('utf-8')))
length = pack_varint(len(pid) + len(jsonlen) + len(RESP_JSON.encode('utf-8')))
conn.sendall(length)
conn.sendall(pid)
conn.sendall(jsonlen)
conn.sendall(RESP_JSON.encode('utf-8'))
length = unpack_varint(conn)
print('len: ' + str(length))
data = BytesIO(recvFixed(conn, length))
pid = unpack_varint(data)
print('pid: ' + str(pid))
timestamp = struct.unpack('!q', data.read(8))[0]
print('timestamp: ' + str(timestamp))
if (pid == 1):
pid = pack_varint(1)
length = pack_varint(len(pid) + 8)
conn.sendall(length)
conn.sendall(pid)
conn.sendall(struct.pack('!q', timestamp))
instanceState = getInstanceState(ec2)
print('Server is in ' + instanceState + ' state')
if instanceState == 'stopped':
print('Booting server...')
ec2.start_instances(InstanceIds=[server_instanceId])
except KeyboardInterrupt:
raise
except Exception as e:
print(e)
pass
try:
conn.close()
except:
pass