1
0
Fork 0

global_config -> config and run black

This commit is contained in:
khr 2020-03-29 19:02:45 -07:00
parent c9fc1a37e0
commit 31da40aa4d
1 changed files with 193 additions and 121 deletions

View File

@ -23,11 +23,6 @@ import PIL
import requests import requests
from flask import Flask, jsonify, request from flask import Flask, jsonify, request
#constants
global_config = {}
#
app = Flask(__name__) app = Flask(__name__)
minecraft = None minecraft = None
roomsync = set() roomsync = set()
@ -43,7 +38,7 @@ class socket_util(object):
self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.msglist = [] self.msglist = []
self.addr = None self.addr = None
LOG.info ("Socket Init Complete") LOG.info("Socket Init Complete")
self.proc = None self.proc = None
self.exit = False self.exit = False
atexit.register(self.close_socket) atexit.register(self.close_socket)
@ -65,19 +60,19 @@ class socket_util(object):
self.soc.close() self.soc.close()
def send(self, message): def send(self, message):
#select returns (readable, writable, error) status on the objects passed in the corresponding lists # select returns (readable, writable, error) status on the objects passed in the corresponding lists
r, w, e = select.select([], [self.soc], [], 1) r, w, e = select.select([], [self.soc], [], 1)
#LOG.info("w" + str(w)) # LOG.info("w" + str(w))
if w == []: if w == []:
return 1 return 1
string_message = json.dumps(message) string_message = json.dumps(message)
select.select([], [self.soc], []) select.select([], [self.soc], [])
self.write_int(len(string_message)) self.write_int(len(string_message))
self.write(string_message.encode('utf-8')) self.write(string_message.encode("utf-8"))
return 0 return 0
def write_int(self, integer): def write_int(self, integer):
integer_buf = struct.pack('>i', integer) integer_buf = struct.pack(">i", integer)
self.write(integer_buf) self.write(integer_buf)
def write(self, data: bytes): def write(self, data: bytes):
@ -89,7 +84,7 @@ class socket_util(object):
def receive(self): def receive(self):
if self.soc.fileno() < 0: if self.soc.fileno() < 0:
self.socket_reset() self.socket_reset()
r,s,e = select.select([self.soc], [], [], 1) r, s, e = select.select([self.soc], [], [], 1)
if r == []: if r == []:
return "" return ""
message_size = self.read_int() message_size = self.read_int()
@ -106,21 +101,22 @@ class socket_util(object):
return message return message
def read_int(self): def read_int(self):
int_size = struct.calcsize('>i') int_size = struct.calcsize(">i")
intbuf = self.read(int_size) intbuf = self.read(int_size)
if intbuf == None: if intbuf == None:
return None return None
return struct.unpack('>i', intbuf)[0] return struct.unpack(">i", intbuf)[0]
def read(self, size): def read(self, size):
data = b"" data = b""
while len(data) != size: while len(data) != size:
newdata = self.soc.recv(size - len(data)) newdata = self.soc.recv(size - len(data))
if len(newdata) == 0: if len(newdata) == 0:
return None return None
data = data + newdata data = data + newdata
return data return data
class MinecraftWrapper(socket_util): class MinecraftWrapper(socket_util):
def __init__(self, command, host, port): def __init__(self, command, host, port):
self.logger = LOG.getChild("MinecraftWrapper") self.logger = LOG.getChild("MinecraftWrapper")
@ -156,7 +152,11 @@ class MinecraftWrapper(socket_util):
def exe_mc(self): def exe_mc(self):
self.proc = subprocess.Popen( self.proc = subprocess.Popen(
" ".join(self.command), shell=True, stdout=PIPE, stdin=PIPE, universal_newlines=True " ".join(self.command),
shell=True,
stdout=PIPE,
stdin=PIPE,
universal_newlines=True,
) )
for stdout_line in iter(self.proc.stdout.readline, ""): for stdout_line in iter(self.proc.stdout.readline, ""):
yield stdout_line yield stdout_line
@ -173,17 +173,17 @@ class MinecraftWrapper(socket_util):
status = self.send(self.msglist[-1]) status = self.send(self.msglist[-1])
rcv = self.receive() rcv = self.receive()
if rcv != "" and rcv != None: if rcv != "" and rcv != None:
self.msg_handle(rcv) self.msg_handle(rcv)
if status == 0: self.msglist.pop() if status == 0:
self.msglist.pop()
except Exception as e: except Exception as e:
self.logger.exception(e) self.logger.exception(e)
self.socket_reset() self.socket_reset()
def msg_handle(self, msg): def msg_handle(self, msg):
if len(msg) > 0: if len(msg) > 0:
if msg[0] == '/': if msg[0] == "/":
self.proc.stdin.write(msg + '\n') self.proc.stdin.write(msg + "\n")
else: else:
LOG.info(msg) LOG.info(msg)
@ -199,13 +199,20 @@ class MinecraftWrapper(socket_util):
def cli_poll(self): def cli_poll(self):
prog = re.compile("\[.*\] \[(.*)\] \[(.*)\]: <(.*)> (.*)") prog = re.compile("\[.*\] \[(.*)\] \[(.*)\]: <(.*)> (.*)")
EXAMPLE = "[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: <khr_> test" EXAMPLE = (
"[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: <khr_> test"
)
for line in self.exe_mc(): for line in self.exe_mc():
self.logger.info(line.rstrip('\n')) self.logger.info(line.rstrip("\n"))
result = prog.search(line) result = prog.search(line)
if result: if result:
self.logger.info("user: " + result.group(3) + " msg: " +result.group(4).rstrip('\n')) self.logger.info(
self.msglist.insert(0, {"user":result.group(3),"msg":result.group(4).rstrip('\n')}) "user: " + result.group(3) + " msg: " + result.group(4).rstrip("\n")
)
self.msglist.insert(
0, {"user": result.group(3), "msg": result.group(4).rstrip("\n")}
)
class MinecraftServerBridge(socket_util): class MinecraftServerBridge(socket_util):
def __init__( def __init__(
@ -214,20 +221,22 @@ class MinecraftServerBridge(socket_util):
minecraft_bind_port: int, minecraft_bind_port: int,
matrix_bind_port: int, matrix_bind_port: int,
appservice_token: str, appservice_token: str,
server_name: str server_name: str,
): ):
#starting threads # starting threads
LOG.info ("Starting Appservice Webserver") LOG.info("Starting Appservice Webserver")
flask_thread = threading.Thread(target=app.run,kwargs={ "port": matrix_bind_port }) flask_thread = threading.Thread(
target=app.run, kwargs={"port": matrix_bind_port}
)
flask_thread.daemon = True flask_thread.daemon = True
flask_thread.start() flask_thread.start()
#socket and other init # socket and other init
super().__init__(minecraft_bind_host, minecraft_bind_port) super().__init__(minecraft_bind_host, minecraft_bind_port)
LOG.info ("Calling Matrix Api") LOG.info("Calling Matrix Api")
self.api = MatrixHttpApi("http://localhost:8008", token=appservice_token) self.api = MatrixHttpApi("http://localhost:8008", token=appservice_token)
self.avatar_update_log = {} self.avatar_update_log = {}
LOG.info ("Finished Init") LOG.info("Finished Init")
self.server_name = server_name self.server_name = server_name
@ -258,11 +267,12 @@ class MinecraftServerBridge(socket_util):
LOG.info("Server accepted connection: " + str(self.addr)) LOG.info("Server accepted connection: " + str(self.addr))
def msg_process(self): def msg_process(self):
while(True): while True:
try: try:
if len(self.msglist) > 0: if len(self.msglist) > 0:
status = self.send(self.msglist[-1]) status = self.send(self.msglist[-1])
if status == 0: self.msglist.pop() if status == 0:
self.msglist.pop()
rcv = self.receive() rcv = self.receive()
if rcv != "" and rcv != None: if rcv != "" and rcv != None:
LOG.debug(rcv) LOG.debug(rcv)
@ -272,73 +282,121 @@ class MinecraftServerBridge(socket_util):
self.socket_reset() self.socket_reset()
def msg_handle(self, msg): def msg_handle(self, msg):
#for msg, create user and post as user # for msg, create user and post as user
#add minecraft user to minecraft channel, if this fails, no big deal # add minecraft user to minecraft channel, if this fails, no big deal
try: try:
new_user = "mc_" + msg['user'] new_user = "mc_" + msg["user"]
user_id = "@{}:{}".format(new_user, self.server_name) user_id = "@{}:{}".format(new_user, self.server_name)
LOG.info("trying to create user {}...".format(new_user)) LOG.info("trying to create user {}...".format(new_user))
self.api.register({ self.api.register(
"type": "m.login.application_service", {"type": "m.login.application_service", "username": new_user,}
"username": new_user, )
})
except Exception as e: except Exception as e:
LOG.exception(e) LOG.exception(e)
#for each room we're aware of, post server chat inside. Eventually 1 room should equal 1 server # for each room we're aware of, post server chat inside. Eventually 1 room should equal 1 server
for room in roomsync: for room in roomsync:
#generate a unique transaction id based on the current time # generate a unique transaction id based on the current time
txn_id = str(int(time.time() * 1000)) txn_id = str(int(time.time() * 1000))
#attempt to join room # attempt to join room
LOG.info("trying to join room as user and as bridge manager") LOG.info("trying to join room as user and as bridge manager")
self.api._send("POST", '/rooms/'+room+'/join', query_params={"user_id": user_id}, headers={"Content-Type":"application/json"}) self.api._send(
self.api._send("POST", '/rooms/'+room+'/join', headers={"Content-Type":"application/json"}) "POST",
#set our display name to something nice "/rooms/" + room + "/join",
query_params={"user_id": user_id},
headers={"Content-Type": "application/json"},
)
self.api._send(
"POST",
"/rooms/" + room + "/join",
headers={"Content-Type": "application/json"},
)
# set our display name to something nice
LOG.info("trying to set display name...") LOG.info("trying to set display name...")
self.api._send("PUT", '/profile/'+user_id+'/displayname/', content={"displayname":msg["user"]}, query_params={"user_id": user_id}, headers={"Content-Type":"application/json"}) self.api._send(
"PUT",
"/profile/" + user_id + "/displayname/",
content={"displayname": msg["user"]},
query_params={"user_id": user_id},
headers={"Content-Type": "application/json"},
)
#get our mc skin!! # get our mc skin!!
#backup: #avatar_url = "https://www.minecraftskinstealer.com/face.php?u="+msg['user'] # backup: #avatar_url = "https://www.minecraftskinstealer.com/face.php?u="+msg['user']
#only get this if the user hasn't updated in a long time # only get this if the user hasn't updated in a long time
try: try:
LOG.info("Checking if we need to update avatar...") LOG.info("Checking if we need to update avatar...")
if msg['user'] not in self.avatar_update_log.keys() or abs(self.avatar_update_log[msg['user']] - time.time()) > 180: if (
self.avatar_update_log[msg['user']] = time.time() msg["user"] not in self.avatar_update_log.keys()
avatar_url = self.get_mc_skin(msg['user'], user_id) or abs(self.avatar_update_log[msg["user"]] - time.time()) > 180
):
self.avatar_update_log[msg["user"]] = time.time()
avatar_url = self.get_mc_skin(msg["user"], user_id)
if avatar_url: if avatar_url:
LOG.debug("avatar_url is " + avatar_url) LOG.debug("avatar_url is " + avatar_url)
self.api._send("PUT", '/profile/'+user_id+'/avatar_url/', content={"avatar_url":avatar_url}, query_params={"user_id": user_id}, headers={"Content-Type":"application/json"}) self.api._send(
"PUT",
"/profile/" + user_id + "/avatar_url/",
content={"avatar_url": avatar_url},
query_params={"user_id": user_id},
headers={"Content-Type": "application/json"},
)
except Exception as e: except Exception as e:
LOG.exception(e) LOG.exception(e)
# Not the end of the world if it fails, send the message now. # Not the end of the world if it fails, send the message now.
#attempt to post in room # attempt to post in room
LOG.info("Attempting to post in Room") LOG.info("Attempting to post in Room")
self.api._send("PUT", '/rooms/'+room+'/send/m.room.message/' + txn_id, content={"msgtype":"m.text","body":msg["msg"]}, query_params={"user_id": user_id}, headers={"Content-Type":"application/json"}) self.api._send(
"PUT",
"/rooms/" + room + "/send/m.room.message/" + txn_id,
content={"msgtype": "m.text", "body": msg["msg"]},
query_params={"user_id": user_id},
headers={"Content-Type": "application/json"},
)
def get_mc_skin(self, user, user_id): def get_mc_skin(self, user, user_id):
LOG.info("Getting Minecraft Avatar") LOG.info("Getting Minecraft Avatar")
from PIL import Image from PIL import Image
mojang_info = requests.get('https://api.mojang.com/users/profiles/minecraft/'+user).json() #get uuid
mojang_info = requests.get('https://sessionserver.mojang.com/session/minecraft/profile/'+mojang_info['id']).json() #get more info from uuid mojang_info = requests.get(
mojang_info = json.loads(base64.b64decode(mojang_info['properties'][0]['value'])) "https://api.mojang.com/users/profiles/minecraft/" + user
mojang_url = mojang_info['textures']['SKIN']['url'] ).json() # get uuid
#r = requests.get(mojang_url, stream=True) mojang_info = requests.get(
#r.raw.decode_content = True # handle spurious Content-Encoding "https://sessionserver.mojang.com/session/minecraft/profile/"
+ mojang_info["id"]
).json() # get more info from uuid
mojang_info = json.loads(
base64.b64decode(mojang_info["properties"][0]["value"])
)
mojang_url = mojang_info["textures"]["SKIN"]["url"]
# r = requests.get(mojang_url, stream=True)
# r.raw.decode_content = True # handle spurious Content-Encoding
file = io.BytesIO(urllib.request.urlopen(mojang_url).read()) file = io.BytesIO(urllib.request.urlopen(mojang_url).read())
im = Image.open(file) im = Image.open(file)
img_head = im.crop((8,8,16,16)) img_head = im.crop((8, 8, 16, 16))
img_head = img_head.resize((im.width * 8, im.height * 8), resample=PIL.Image.NEAREST) # Resize with nearest neighbor to get pixels img_head = img_head.resize(
(im.width * 8, im.height * 8), resample=PIL.Image.NEAREST
) # Resize with nearest neighbor to get pixels
image_buffer_head = io.BytesIO() image_buffer_head = io.BytesIO()
img_head.save(image_buffer_head, "PNG") img_head.save(image_buffer_head, "PNG")
#compare to user's current id so we're not uploading the same pic twice # compare to user's current id so we're not uploading the same pic twice
#GET /_matrix/client/r0/profile/{userId}/avatar_url # GET /_matrix/client/r0/profile/{userId}/avatar_url
LOG.info("Getting Current Avatar URL") LOG.info("Getting Current Avatar URL")
curr_url = self.api._send("GET", '/profile/'+user_id+'/avatar_url/', query_params={"user_id": user_id}, headers={"Content-Type":"application/json"}) curr_url = self.api._send(
"GET",
"/profile/" + user_id + "/avatar_url/",
query_params={"user_id": user_id},
headers={"Content-Type": "application/json"},
)
upload = True upload = True
if 'avatar_url' in curr_url.keys(): if "avatar_url" in curr_url.keys():
LOG.info("Checking Avatar...") LOG.info("Checking Avatar...")
file = io.BytesIO(urllib.request.urlopen(self.api.get_download_url(curr_url['avatar_url'])).read()) file = io.BytesIO(
urllib.request.urlopen(
self.api.get_download_url(curr_url["avatar_url"])
).read()
)
im = Image.open(file) im = Image.open(file)
image_buffer_curr = io.BytesIO() image_buffer_curr = io.BytesIO()
im.save(image_buffer_curr, "PNG") im.save(image_buffer_curr, "PNG")
@ -346,16 +404,20 @@ class MinecraftServerBridge(socket_util):
LOG.debug("Image Same") LOG.debug("Image Same")
upload = False upload = False
if upload: if upload:
#upload img # upload img
#POST /_matrix/media/r0/upload # POST /_matrix/media/r0/upload
LOG.debug("Returning updated avatar") LOG.debug("Returning updated avatar")
LOG.debug(image_buffer_head) LOG.debug(image_buffer_head)
return self.api.media_upload(image_buffer_head.getvalue(), "image/png")["content_uri"] return self.api.media_upload(image_buffer_head.getvalue(), "image/png")[
"content_uri"
]
else: else:
return None return None
USER_RE = re.compile("(?<=\@).*(?=\:)") USER_RE = re.compile("(?<=\@).*(?=\:)")
@app.route("/transactions/<transaction>", methods=["PUT"]) @app.route("/transactions/<transaction>", methods=["PUT"])
def on_receive_events(transaction): def on_receive_events(transaction):
LOG.info("got event") LOG.info("got event")
@ -365,35 +427,47 @@ def on_receive_events(transaction):
LOG.info("Event Type: %s" % event["type"]) LOG.info("Event Type: %s" % event["type"])
LOG.info("Content: %s" % event["content"]) LOG.info("Content: %s" % event["content"])
roomsync.add(event["room_id"]) roomsync.add(event["room_id"])
if event['type'] == 'm.room.message' and \ if (
event['content']['msgtype'] == 'm.text' and \ event["type"] == "m.room.message"
event["user_id"].find("@mc_") == -1: and event["content"]["msgtype"] == "m.text"
and event["user_id"].find("@mc_") == -1
):
m_user = USER_RE.search(event["user_id"]).group(0) m_user = USER_RE.search(event["user_id"]).group(0)
m_cont = event['content']['body'] m_cont = event["content"]["body"]
# minecraft.msglist.insert(0, "/tellraw @a {\"text\":\"<" + m_user + "> " + m_cont + "\",\"insertion\":\"/tellraw @p %s\"}") # minecraft.msglist.insert(0, "/tellraw @a {\"text\":\"<" + m_user + "> " + m_cont + "\",\"insertion\":\"/tellraw @p %s\"}")
return jsonify({}) return jsonify({})
@app.route("/rooms/<room>", methods=["GET"]) @app.route("/rooms/<room>", methods=["GET"])
def on_room(room): def on_room(room):
LOG.info("returning: " + str(room)) LOG.info("returning: " + str(room))
return jsonify({}) return jsonify({})
bridge_cfg_skeleton = {"as_token":"", "server_name":"","bridge_mcdata_port":-1, "bridge_matrixapi_port":-1} BRIDGE_CFG_SKELETON = {
wrapper_cfg_skeleton = {"server_name":"","wrapper_mcdata_port":-1,} "as_token": "",
"server_name": "",
"bridge_mcdata_port": -1,
"bridge_matrixapi_port": -1,
}
WRAPPER_CFG_SKELETON = {
"server_name": "",
"wrapper_mcdata_port": -1,
}
def make_config(configfile, server=True): def make_config(configfile, server=True):
if not glob.glob(configfile): if not glob.glob(configfile):
with open(configfile, 'w') as outfile: with open(configfile, "w") as outfile:
if server: if server:
json.dump(bridge_cfg_skeleton, outfile) json.dump(BRIDGE_CFG_SKELETON, outfile)
else: else:
json.dump(wrapper_cfg_skeleton, outfile) json.dump(WRAPPER_CFG_SKELETON, outfile)
LOG.error("Please edit {0} and then run again!".format(configfile)) LOG.error("Please edit {0} and then run again!".format(configfile))
sys.exit(0) sys.exit(0)
else:
elif glob.glob(configfile):
with open(configfile) as config: with open(configfile) as config:
read_config = json.load(config) read_config = json.load(config)
return read_config return read_config
@ -423,25 +497,23 @@ def main():
if args.mode == "wrapper": if args.mode == "wrapper":
LOG.info("Running Minecraft Server Wrapper Mode") LOG.info("Running Minecraft Server Wrapper Mode")
global_config = make_config("wrapper.json", server=False) config = make_config("wrapper.json", server=False)
ip_addr_info = socket.gethostbyname_ex(global_config['server_name']) ip_addr_info = socket.gethostbyname_ex(config["server_name"])
minecraft = MinecraftWrapper( minecraft = MinecraftWrapper(
args.command, args.command, host=ip_addr_info[2][0], port=config["wrapper_mcdata_port"],
host=ip_addr_info[2][0],
port=global_config['wrapper_mcdata_port'],
) )
else: else:
LOG.info("Running Minecraft Matrix Bridge Mode") LOG.info("Running Minecraft Matrix Bridge Mode")
global_config = make_config("server.json", server=True) config = make_config("server.json", server=True)
minecraft = MinecraftServerBridge( minecraft = MinecraftServerBridge(
minecraft_bind_host="0.0.0.0", minecraft_bind_host="0.0.0.0",
minecraft_bind_port=global_config['bridge_mcdata_port'], minecraft_bind_port=config["bridge_mcdata_port"],
matrix_bind_port=global_config["bridge_matrixapi_port"], matrix_bind_port=config["bridge_matrixapi_port"],
appservice_token=global_config["as_token"], appservice_token=config["as_token"],
server_name=global_config["server_name"], server_name=config["server_name"],
) )
LOG.info("All Threads Running") LOG.info("All Threads Running")
while (not minecraft.exit): while not minecraft.exit:
time.sleep(1) time.sleep(1)
LOG.info("Calling exit() in main thread...") LOG.info("Calling exit() in main thread...")
sys.exit() sys.exit()