Various fixes
This commit is contained in:
parent
2ad8f3c8f5
commit
2a420d4130
6
init.lua
6
init.lua
@ -146,8 +146,12 @@ function MatrixChat:send(msg)
|
|||||||
function(res)
|
function(res)
|
||||||
if res.code == 200 then
|
if res.code == 200 then
|
||||||
local data = minetest.parse_json(res.data)
|
local data = minetest.parse_json(res.data)
|
||||||
|
if data then
|
||||||
minetest.log("action", "got " .. data["event_id"])
|
minetest.log("action", "got " .. data["event_id"])
|
||||||
self.eventid = data["event_id"]
|
self.eventid = data["event_id"]
|
||||||
|
else
|
||||||
|
minetest.log("error", "matrix_bridge - cannot parse json")
|
||||||
|
end
|
||||||
elseif res.code == 401 then
|
elseif res.code == 401 then
|
||||||
minetest.log("error", "matrix_bridge - not authorized to send messages")
|
minetest.log("error", "matrix_bridge - not authorized to send messages")
|
||||||
elseif res.code == 404 then
|
elseif res.code == 404 then
|
||||||
@ -193,8 +197,8 @@ minetest.register_globalstep(function(dtime)
|
|||||||
local activity = minetest.parse_json(result.data)
|
local activity = minetest.parse_json(result.data)
|
||||||
if activity ~= nil then
|
if activity ~= nil then
|
||||||
MatrixChat:minechat(activity)
|
MatrixChat:minechat(activity)
|
||||||
end
|
|
||||||
MatrixChat.since = activity.next_batch
|
MatrixChat.since = activity.next_batch
|
||||||
|
end
|
||||||
elseif result.code == 0 then
|
elseif result.code == 0 then
|
||||||
elseif result.code == 404 then
|
elseif result.code == 404 then
|
||||||
end
|
end
|
||||||
|
91
proxy.py
91
proxy.py
@ -1,4 +1,4 @@
|
|||||||
import http.client, re, socket, sys, time
|
import http.client, re, socket, sys, time, _thread
|
||||||
|
|
||||||
RECBUF = 1024
|
RECBUF = 1024
|
||||||
|
|
||||||
@ -10,46 +10,7 @@ def getargv(arg, default="", n=1, args=sys.argv):
|
|||||||
|
|
||||||
p_clen = re.compile("\r?\ncontent-length: *(\d+)\r?\n?", re.IGNORECASE)
|
p_clen = re.compile("\r?\ncontent-length: *(\d+)\r?\n?", re.IGNORECASE)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def handle(client):
|
||||||
if "--help" in sys.argv or "-h" in sys.argv:
|
|
||||||
print("""MineTest Matrix Bridge mod proxy
|
|
||||||
GNU AGPLv3, CopyLeft 2022 Pascal Engélibert
|
|
||||||
|
|
||||||
This program is useful for <5.4.0 MineTest servers using the mod matrix_bridge.
|
|
||||||
For security reasons, please listen to a local address (default value is OK) and use a firewall if needed to prevent public access.
|
|
||||||
|
|
||||||
Options: (with defaults)
|
|
||||||
-a 127.0.0.1 Listen address
|
|
||||||
-p 18448 Listen port
|
|
||||||
-A matrix.txmn.tk Matrix address (without protocol and port)
|
|
||||||
-P 8448 Matrix port
|
|
||||||
-s Use HTTP instead of HTTPS (default: no)
|
|
||||||
""")
|
|
||||||
exit()
|
|
||||||
|
|
||||||
listen_addr = getargv("-a", "127.0.0.1")
|
|
||||||
listen_port = int(getargv("-p", 18448))
|
|
||||||
matrix_addr = getargv("-A", "matrix.txmn.tk")
|
|
||||||
matrix_port = int(getargv("-P", 8448))
|
|
||||||
matrix_https = not "-s" in sys.argv
|
|
||||||
|
|
||||||
if matrix_https:
|
|
||||||
import ssl
|
|
||||||
|
|
||||||
ssl_ctx = ssl.create_default_context()
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
||||||
sock.settimeout(60)
|
|
||||||
sock.bind((listen_addr, listen_port))
|
|
||||||
sock.listen(1)
|
|
||||||
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
client, addr = sock.accept()
|
|
||||||
except socket.timeout:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Get request
|
# Get request
|
||||||
paquet = b""
|
paquet = b""
|
||||||
header = b""
|
header = b""
|
||||||
@ -88,7 +49,7 @@ Options: (with defaults)
|
|||||||
httpreq = paquet.split(b" ", 2)
|
httpreq = paquet.split(b" ", 2)
|
||||||
if len(httpreq) != 3:
|
if len(httpreq) != 3:
|
||||||
client.close()
|
client.close()
|
||||||
continue
|
return
|
||||||
method = httpreq[0]
|
method = httpreq[0]
|
||||||
url = httpreq[1]
|
url = httpreq[1]
|
||||||
rest = httpreq[2]
|
rest = httpreq[2]
|
||||||
@ -100,7 +61,11 @@ Options: (with defaults)
|
|||||||
if matrix_https:
|
if matrix_https:
|
||||||
matrix_sock = ssl_ctx.wrap_socket(matrix_sock, server_hostname=matrix_addr)
|
matrix_sock = ssl_ctx.wrap_socket(matrix_sock, server_hostname=matrix_addr)
|
||||||
matrix_sock.settimeout(60)
|
matrix_sock.settimeout(60)
|
||||||
|
try:
|
||||||
matrix_sock.connect((matrix_addr, matrix_port))
|
matrix_sock.connect((matrix_addr, matrix_port))
|
||||||
|
except socket.timeout:
|
||||||
|
sys.stderr.write("timeout connecting to matrix")
|
||||||
|
return
|
||||||
matrix_sock.settimeout(None)
|
matrix_sock.settimeout(None)
|
||||||
|
|
||||||
matrix_sock.sendall(b" ".join([method, url, rest]))
|
matrix_sock.sendall(b" ".join([method, url, rest]))
|
||||||
@ -142,6 +107,48 @@ Options: (with defaults)
|
|||||||
client.sendall(paquet)
|
client.sendall(paquet)
|
||||||
|
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if "--help" in sys.argv or "-h" in sys.argv:
|
||||||
|
print("""MineTest Matrix Bridge mod proxy
|
||||||
|
GNU AGPLv3, CopyLeft 2022 Pascal Engélibert
|
||||||
|
|
||||||
|
This program is useful for <5.4.0 MineTest servers using the mod matrix_bridge.
|
||||||
|
For security reasons, please listen to a local address (default value is OK) and use a firewall if needed to prevent public access.
|
||||||
|
|
||||||
|
Options: (with defaults)
|
||||||
|
-a 127.0.0.1 Listen address
|
||||||
|
-p 18448 Listen port
|
||||||
|
-A matrix.txmn.tk Matrix address (without protocol and port)
|
||||||
|
-P 8448 Matrix port
|
||||||
|
-s Use HTTP instead of HTTPS (default: no)
|
||||||
|
""")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
listen_addr = getargv("-a", "127.0.0.1")
|
||||||
|
listen_port = int(getargv("-p", 18448))
|
||||||
|
matrix_addr = getargv("-A", "matrix.txmn.tk")
|
||||||
|
matrix_port = int(getargv("-P", 8448))
|
||||||
|
matrix_https = not "-s" in sys.argv
|
||||||
|
|
||||||
|
if matrix_https:
|
||||||
|
import ssl
|
||||||
|
|
||||||
|
ssl_ctx = ssl.create_default_context()
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sock.settimeout(60)
|
||||||
|
sock.bind((listen_addr, listen_port))
|
||||||
|
sock.listen(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
client, addr = sock.accept()
|
||||||
|
except socket.timeout:
|
||||||
|
continue
|
||||||
|
|
||||||
|
_thread.start_new_thread(handle, (client,))
|
||||||
sock.close()
|
sock.close()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sock.shutdown(socket.SHUT_WR)
|
sock.shutdown(socket.SHUT_WR)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user