1
0
Fork 0
matrix-appservice-minecraft/ServerWrapper_v0.1.0.py

440 lines
16 KiB
Python
Raw Normal View History

2020-03-27 07:26:50 +01:00
import argparse
import atexit
import base64
import glob
import io
2017-04-12 02:05:12 +02:00
import json
2020-03-27 07:26:50 +01:00
import logging
2017-04-12 02:05:12 +02:00
import os
2020-03-27 07:26:50 +01:00
import re
2017-04-12 02:05:12 +02:00
import select
import socket
import sys
import subprocess
from subprocess import PIPE
import struct
import threading
import time
2020-03-27 07:26:50 +01:00
import urllib
from urllib.parse import urlparse
2017-04-12 02:05:12 +02:00
from matrix_client.api import MatrixHttpApi
import requests
from flask import Flask, jsonify, request
#constants
global_config = {}
#
app = Flask(__name__)
minecraft = None
roomsync = {}
2020-03-27 07:26:50 +01:00
LOG = logging.getLogger(__name__)
2017-04-12 02:05:12 +02:00
class socket_util(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.msglist = []
self.addr = None
2020-03-27 07:26:50 +01:00
LOG.info ("Socket Init Complete")
2017-04-12 02:05:12 +02:00
self.proc = None
self.exit = False
atexit.register(self.close_socket)
2020-03-27 07:26:50 +01:00
LOG.info("Starting Messaging Thread")
2017-04-12 02:05:12 +02:00
msg_process = threading.Thread(target=self.msg_process)
msg_process.daemon = True
msg_process.start()
def msg_process(self):
raise NotImplementedError("Please Implement this method")
def socket_reset(self):
self.soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.msglist = []
self.addr = None
def close_socket(self):
self.soc.close()
def send(self, message):
#select returns (readable, writable, error) status on the objects passed in the corresponding lists
r, w, e = select.select([], [self.soc], [], 1)
2020-03-27 07:26:50 +01:00
#LOG.info("w" + str(w))
2017-04-12 02:05:12 +02:00
if w == []:
return 1
string_message = json.dumps(message)
select.select([], [self.soc], [])
self.write_int(len(string_message))
self.write(string_message.encode('utf-8'))
2017-04-12 02:05:12 +02:00
return 0
def write_int(self, integer):
integer_buf = struct.pack('>i', integer)
self.write(integer_buf)
def write(self, data: bytes):
2017-04-12 02:05:12 +02:00
#socket.bind(address)
data_len = len(data)
offset = 0
while offset != data_len:
offset += self.soc.send(data[offset:])
def receive(self):
r,s,e = select.select([self.soc], [], [], 1)
2020-03-27 07:26:50 +01:00
#LOG.info("r" + str(r))
2017-04-12 02:05:12 +02:00
if r == []:
return ""
message_size = self.read_int()
if message_size == None:
self.close_socket()
return None
data = self.read(message_size)
if data == None:
2020-03-27 07:26:50 +01:00
LOG.debug("data_none")
2017-04-12 02:05:12 +02:00
return None
message = json.loads(data)
return message
def read_int(self):
int_size = struct.calcsize('>i')
intbuf = self.read(int_size)
if intbuf == None:
return None
return struct.unpack('>i', intbuf)[0]
def read(self, size):
data = ""
while len(data) != size:
newdata = self.soc.recv(size - len(data))
if len(newdata) == 0:
return None
data = data + newdata
return data
class MinecraftWrapper(socket_util):
2020-03-27 07:26:50 +01:00
def __init__(self, command, host, port):
self.logger = LOG.getChild("MinecraftWrapper")
super().__init__(host, port)
self.logger.debug(command)
2020-03-27 07:26:50 +01:00
self.command = command
self.logger.info("Starting Wrapper Polling Thread")
2017-04-12 02:05:12 +02:00
poll_process = threading.Thread(target=self.cli_poll)
poll_process.daemon = True
poll_process.start()
self.socket_reset()
def socket_reset(self):
2020-03-27 07:26:50 +01:00
super().socket_reset()
self.logger.debug("Connecting to {}:{}".format(self.host, self.port))
backoff = 1
while True:
try:
self.soc.connect((self.host, self.port))
break
except OSError as e:
if e.errno == 111:
LOG.warning(
"Connection refused by {}:{}, trying again in {} seconds".format(
self.host, self.port, backoff
)
)
time.sleep(backoff)
backoff *= 2
else:
raise e
self.logger.info("Socket Connected")
2017-04-12 02:05:12 +02:00
def exe_mc(self):
self.proc = subprocess.Popen(
" ".join(self.command), shell=True, stdout=PIPE, stdin=PIPE, universal_newlines=True
)
2017-04-12 02:05:12 +02:00
for stdout_line in iter(self.proc.stdout.readline, ""):
yield stdout_line
return_code = self.proc.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, self.command)
2017-04-12 02:05:12 +02:00
def msg_process(self):
while(not self.exit):
try:
self.proc_monitor()
status = 1
if len(self.msglist) > 0:
status = self.send(self.msglist[-1])
rcv = self.receive()
if rcv != "" and rcv != None:
self.msg_handle(rcv)
if status == 0: self.msglist.pop()
except Exception as e:
self.logger.exception(e)
2017-04-12 02:05:12 +02:00
self.socket_reset()
def msg_handle(self, msg):
if len(msg) > 0:
if msg[0] == '/':
self.proc.stdin.write(msg + '\n')
else:
2020-03-27 07:26:50 +01:00
LOG.info(msg)
2017-04-12 02:05:12 +02:00
def proc_monitor(self):
try:
if self.proc is not None and self.proc.poll() is not None:
2017-04-12 02:05:12 +02:00
self.exit = True
self.close_socket()
sys.exit(0)
except Exception as e:
self.logger.exception("poll error")
2017-04-12 02:05:12 +02:00
pass
def cli_poll(self):
prog = re.compile("\[.*\] \[(.*)\] \[(.*)\]: <(.*)> (.*)")
EXAMPLE = "[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: <khr_> test"
2017-04-12 02:05:12 +02:00
for line in self.exe_mc():
self.logger.info(line.rstrip('\n'))
2017-04-12 02:05:12 +02:00
result = prog.search(line)
if result:
self.logger.info("user: " + result.group(3) + " msg: " +result.group(4).rstrip('\n'))
2017-04-12 02:05:12 +02:00
self.msglist.insert(0, {"user":result.group(3),"msg":result.group(4).rstrip('\n')})
class MinecraftServerBridge(socket_util):
2020-03-27 07:26:50 +01:00
def __init__(
self,
minecraft_bind_host: int,
minecraft_bind_port: int,
matrix_bind_port: int,
appservice_token: str
):
2017-04-12 02:05:12 +02:00
#starting threads
2020-03-27 07:26:50 +01:00
LOG.info ("Starting Appservice Webserver")
flask_thread = threading.Thread(target=app.run,kwargs={ "port": minecraft_bind_port })
2017-04-12 02:05:12 +02:00
flask_thread.daemon = True
flask_thread.start()
#socket and other init
2020-03-27 07:26:50 +01:00
super().__init__(minecraft_bind_host, minecraft_bind_port)
LOG.info ("Calling Matrix Api")
self.api = MatrixHttpApi("http://localhost:8008", token=appservice_token)
2017-04-12 02:05:12 +02:00
self.user_re = re.compile("(?<=\@).*(?=\:)")
self.avatar_update_log = {}
2020-03-27 07:26:50 +01:00
LOG.info ("Finished Init")
2017-04-12 02:05:12 +02:00
def socket_reset(self):
2020-03-27 07:26:50 +01:00
super().socket_reset()
LOG.info("Server Binding to " + self.host + " " + str(self.port))
backoff = 1
2020-03-29 03:25:59 +02:00
while True:
try:
self.soc.bind((self.host, self.port))
break
except OSError as e:
if e.errno == 98:
LOG.warning(
"Unable to bind to port {}, trying again in {} seconds".format(
self.port, backoff
)
)
time.sleep(backoff)
backoff *= 2
else:
raise e
2020-03-27 07:26:50 +01:00
LOG.info("Server Bound")
2017-04-12 02:05:12 +02:00
self.soc.listen(1)
2020-03-27 07:26:50 +01:00
LOG.info("Server listen to host")
2017-04-12 02:05:12 +02:00
self.soc, self.addr = self.soc.accept()
self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
2020-03-27 07:26:50 +01:00
LOG.info("Server accepted connection: " + str(self.addr))
2017-04-12 02:05:12 +02:00
def msg_process(self):
while(True):
try:
if len(self.msglist) > 0:
status = self.send(self.msglist[-1])
if status == 0: self.msglist.pop()
rcv = self.receive()
if rcv != "" and rcv != None:
self.msg_handle(rcv)
except Exception as e:
2020-03-27 07:26:50 +01:00
LOG.info(e)
2017-04-12 02:05:12 +02:00
self.socket_reset()
def msg_handle(self, msg):
#for msg, create user and post as user
#add minecraft user to minecraft channel, if this fails, no big deal
try:
2020-03-27 07:26:50 +01:00
LOG.info("trying to create id...")
2017-04-12 02:05:12 +02:00
new_user = "@mc_" + msg['user']
user_id = new_user + ":" + global_config['server_name']
self.api.register("m.login.application_service",username = "mc_" + msg['user'])
except Exception as e:
LOG.exception(e)
2017-04-12 02:05:12 +02:00
#for each room we're aware of, post server chat inside. Eventually 1 room should equal 1 server
for room in roomsync:
#generate a unique transaction id based on the current time
txn_id = str(int(time.time() * 1000))
#attempt to join room
2020-03-27 07:26:50 +01:00
LOG.info("trying to join room as user and as bridge manager")
2017-04-12 02:05:12 +02:00
self.api._send("POST", '/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
2020-03-27 07:26:50 +01:00
LOG.info("trying to set display name...")
2017-04-12 02:05:12 +02:00
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!!
#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
2020-03-27 07:26:50 +01:00
LOG.info("Checking if we need to update avatar...")
2017-04-12 02:05:12 +02:00
if msg['user'] not in self.avatar_update_log.keys() 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:
2020-03-27 07:26:50 +01:00
LOG.info("avatar_url is " + avatar_url)
2017-04-12 02:05:12 +02:00
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"})
#attempt to post in room
2020-03-27 07:26:50 +01:00
LOG.info("Attempting to post in Room")
2017-04-12 02:05:12 +02:00
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):
2020-03-27 07:26:50 +01:00
LOG.info("Getting Minecraft Avatar")
2017-04-12 02:05:12 +02:00
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 = 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.urlopen(mojang_url).read())
im = Image.open(file)
img_head = im.crop((8,8,16,16))
image_buffer_head = io.BytesIO()
img_head.save(image_buffer_head, "PNG")
#compare to user's current id so we're not uploading the same pic twice
#GET /_matrix/client/r0/profile/{userId}/avatar_url
2020-03-27 07:26:50 +01:00
LOG.info("Getting Current Avatar URL")
2017-04-12 02:05:12 +02:00
curr_url = self.api._send("GET", '/profile/'+user_id+'/avatar_url/', query_params={"user_id": user_id}, headers={"Content-Type":"application/json"})
upload = True
if 'avatar_url' in curr_url.keys():
2020-03-27 07:26:50 +01:00
LOG.info("Checking Avatar...")
2017-04-12 02:05:12 +02:00
file = io.BytesIO(urllib.urlopen(self.api.get_download_url(curr_url['avatar_url'])).read())
im = Image.open(file)
image_buffer_curr = io.BytesIO()
im.save(image_buffer_curr, "PNG")
if (image_buffer_head.getvalue()) == (image_buffer_curr.getvalue()):
2020-03-27 07:26:50 +01:00
LOG.debug("Image Same")
2017-04-12 02:05:12 +02:00
upload = False
if upload:
#upload img
#POST /_matrix/media/r0/upload
2020-03-27 07:26:50 +01:00
LOG.debug("Returning updated avatar")
LOG.debug(image_buffer_head)
2017-04-12 02:05:12 +02:00
return self.api.media_upload(image_buffer_head.getvalue(), "image/png")["content_uri"]
else:
return None
@app.route("/transactions/<transaction>", methods=["PUT"])
def on_receive_events(transaction):
2020-03-27 07:26:50 +01:00
LOG.info("got event")
2017-04-12 02:05:12 +02:00
events = request.get_json()["events"]
for event in events:
2020-03-27 07:26:50 +01:00
LOG.info("User: %s Room: %s" % (event["user_id"], event["room_id"]))
LOG.info("Event Type: %s" % event["type"])
LOG.info("Content: %s" % event["content"])
2017-04-12 02:05:12 +02:00
roomsync[event["room_id"]] = ""
if event['type'] == 'm.room.message' and \
event['content']['msgtype'] == 'm.text' and \
event["user_id"].find("@mc_") == -1:
m_user = minecraft.user_re.search(event["user_id"]).group(0)
m_cont = event['content']['body']
minecraft.msglist.insert(0, "/tellraw @a {\"text\":\"<" + m_user + "> " + m_cont + "\",\"insertion\":\"/tellraw @p %s\"}")
return jsonify({})
@app.route("/rooms/<room>", methods=["GET"])
def on_room(room):
2020-03-27 07:26:50 +01:00
LOG.info("returning: " + str(room))
2017-04-12 02:05:12 +02:00
return jsonify({})
bridge_cfg_skeleton = {"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):
if not glob.glob(configfile):
with open(configfile, 'w') as outfile:
if server:
json.dump(bridge_cfg_skeleton, outfile)
else:
json.dump(wrapper_cfg_skeleton, outfile)
2020-03-27 07:26:50 +01:00
LOG.error("Please edit {0} and then run again!".format(configfile))
2017-04-12 02:05:12 +02:00
sys.exit(0)
elif glob.glob(configfile):
with open(configfile) as config:
read_config = json.load(config)
return read_config
2020-03-27 07:26:50 +01:00
def main():
2020-03-29 03:25:59 +02:00
logging.basicConfig(level=logging.DEBUG)
2020-03-27 07:26:50 +01:00
parser = argparse.ArgumentParser()
mode_group = parser.add_mutually_exclusive_group(required=True)
mode_group.add_argument(
"--minecraft_wrapper",
dest="mode",
action="store_const",
const="wrapper",
help="Run in Minecraft server wrapper mode",
)
mode_group.add_argument(
"--matrix_bridge",
dest="mode",
action="store_const",
const="bridge",
help="Run in Matrix Appservice mode",
)
parser.add_argument("command", nargs=argparse.REMAINDER)
args = parser.parse_args()
if args.mode == "wrapper":
LOG.info("Running Minecraft Server Wrapper Mode")
2017-04-12 02:05:12 +02:00
global_config = make_config("wrapper.json", server=False)
ip_addr_info = socket.gethostbyname_ex(global_config['server_name'])
2020-03-27 07:26:50 +01:00
minecraft = MinecraftWrapper(
args.command,
host=ip_addr_info[2][0],
port=global_config['wrapper_mcdata_port'],
)
2017-04-12 02:05:12 +02:00
else:
2020-03-27 07:26:50 +01:00
LOG.info("Running Minecraft Matrix Bridge Mode")
2017-04-12 02:05:12 +02:00
global_config = make_config("server.json", server=True)
2020-03-27 07:26:50 +01:00
minecraft = MinecraftServerBridge(
minecraft_bind_host="0.0.0.0",
minecraft_bind_port=global_config['bridge_mcdata_port'],
matrix_bind_port=global_config["bridge_matrixapi_port"],
appservice_token=global_config["as_token"],
)
LOG.info("All Threads Running")
while (not minecraft.exit):
time.sleep(1)
2020-03-27 07:26:50 +01:00
LOG.info("Calling exit() in main thread...")
2017-04-12 02:05:12 +02:00
sys.exit()
2020-03-27 07:26:50 +01:00
if __name__ == "__main__":
main()