Implemented Synapse Ping as heartbeat for connection between synapse and RMASS Bridge. Also implemented set online action to update RMASS presence
This commit is contained in:
@@ -7,14 +7,6 @@ from core.runtime.createruntime import outputs
|
|||||||
|
|
||||||
|
|
||||||
def buildListener(listener, self):
|
def buildListener(listener, self):
|
||||||
|
|
||||||
if self.path == "/_matrix/app/v1/ping":
|
|
||||||
self.send_response(200)
|
|
||||||
self.send_header("Content-Type", "application/json")
|
|
||||||
self.end_headers()
|
|
||||||
self.wfile.write(b"{}")
|
|
||||||
return
|
|
||||||
|
|
||||||
if self.path.startswith(listener.acceptpath):
|
if self.path.startswith(listener.acceptpath):
|
||||||
|
|
||||||
#Prints can used buildlog for listener runtime
|
#Prints can used buildlog for listener runtime
|
||||||
@@ -32,12 +24,13 @@ def buildListener(listener, self):
|
|||||||
allow, status_code = buildFirewall(listener, outputs)
|
allow, status_code = buildFirewall(listener, outputs)
|
||||||
|
|
||||||
if allow:
|
if allow:
|
||||||
from core.execution.createthread import buildThread
|
#from core.execution.createthread import buildThread
|
||||||
|
from core.execution.executioncontract import executeContract
|
||||||
|
|
||||||
logging.info(outputs["firewall_allowed"])
|
logging.info(outputs["firewall_allowed"])
|
||||||
|
|
||||||
thread = buildThread(listener.execute)
|
#thread = buildThread(listener.execute)
|
||||||
|
executeContract(listener.execute)
|
||||||
|
|
||||||
|
|
||||||
self.send_response(status_code)
|
self.send_response(status_code)
|
||||||
|
|||||||
+69
-56
@@ -8,90 +8,103 @@ from .dispatcher import dispatchRequest
|
|||||||
from .createresponse import buildResponse
|
from .createresponse import buildResponse
|
||||||
from plugins.rmass.listeners import synapselistener
|
from plugins.rmass.listeners import synapselistener
|
||||||
from core.server.createlistener import buildListener
|
from core.server.createlistener import buildListener
|
||||||
#from plugins.rmass.actions import pingsynapse
|
from plugins.rmass.actions import synapsesetbridgeonline
|
||||||
|
|
||||||
port = int(os.environ.get("SERVER_PORT", 19001))
|
port = int(os.environ.get("SERVER_PORT", 19001))
|
||||||
|
|
||||||
|
|
||||||
class RequestHandler(BaseHTTPRequestHandler):
|
class RequestHandler(BaseHTTPRequestHandler):
|
||||||
def do_POST(self):
|
def do_POST(self):
|
||||||
#lifecycles = {}
|
if self.path == "/_matrix/app/v1/ping":
|
||||||
print("POST request received")
|
|
||||||
|
|
||||||
max_request_size = 1024
|
from core.execution.createthread import buildThread
|
||||||
|
thread = buildThread(synapsesetbridgeonline)
|
||||||
|
|
||||||
content_length = self.headers.get("Content-Length")
|
self.send_response(200)
|
||||||
|
self.send_header("Content-Type", "application/json")
|
||||||
# Check whether Content-Length exists
|
self.send_header("Content-Length", "2")
|
||||||
if content_length is None:
|
self.end_headers()
|
||||||
response = {
|
self.wfile.write(b"{}")
|
||||||
"status": "error",
|
return
|
||||||
"message": "Content length required"
|
|
||||||
}
|
|
||||||
status = 411
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Attempt to convert the header into an integer
|
#lifecycles = {}
|
||||||
try:
|
print("POST request received")
|
||||||
length = int(content_length)
|
|
||||||
except ValueError:
|
|
||||||
length = None
|
|
||||||
|
|
||||||
# Validate the converted length
|
max_request_size = 1024
|
||||||
if length is None:
|
|
||||||
|
content_length = self.headers.get("Content-Length")
|
||||||
|
|
||||||
|
# Check whether Content-Length exists
|
||||||
|
if content_length is None:
|
||||||
response = {
|
response = {
|
||||||
"status": "error",
|
"status": "error",
|
||||||
"message": "Invalid Content-Length"
|
"message": "Content length required"
|
||||||
}
|
}
|
||||||
status = 400
|
status = 411
|
||||||
|
|
||||||
elif length < 0:
|
|
||||||
response = {
|
|
||||||
"status": "error",
|
|
||||||
"message": "Bad Request"
|
|
||||||
}
|
|
||||||
status = 400
|
|
||||||
|
|
||||||
elif length > max_request_size:
|
|
||||||
response = {
|
|
||||||
"status": "error",
|
|
||||||
"message": "Content exceeds maximum request size"
|
|
||||||
}
|
|
||||||
status = 413
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Content-Length is valid, so read the body
|
# Attempt to convert the header into an integer
|
||||||
data = self.rfile.read(length)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
request = json.loads(data)
|
length = int(content_length)
|
||||||
|
except ValueError:
|
||||||
|
length = None
|
||||||
|
|
||||||
except json.JSONDecodeError:
|
# Validate the converted length
|
||||||
|
if length is None:
|
||||||
response = {
|
response = {
|
||||||
"status": "error",
|
"status": "error",
|
||||||
"message": "Invalid JSON received"
|
"message": "Invalid Content-Length"
|
||||||
}
|
}
|
||||||
status = 400
|
status = 400
|
||||||
|
|
||||||
|
elif length < 0:
|
||||||
|
response = {
|
||||||
|
"status": "error",
|
||||||
|
"message": "Bad Request"
|
||||||
|
}
|
||||||
|
status = 400
|
||||||
|
|
||||||
|
elif length > max_request_size:
|
||||||
|
response = {
|
||||||
|
"status": "error",
|
||||||
|
"message": "Content exceeds maximum request size"
|
||||||
|
}
|
||||||
|
status = 413
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Only valid JSON reaches the dispatcher
|
# Content-Length is valid, so read the body
|
||||||
thread, lifecycles, module, mode = dispatchRequest(
|
data = self.rfile.read(length)
|
||||||
request, lifecycles
|
|
||||||
)
|
|
||||||
|
|
||||||
response = buildResponse(module, thread, mode)
|
try:
|
||||||
status = 200
|
request = json.loads(data)
|
||||||
|
|
||||||
# Serialize the response
|
except json.JSONDecodeError:
|
||||||
responsebytes = json.dumps(response).encode("utf-8")
|
response = {
|
||||||
|
"status": "error",
|
||||||
|
"message": "Invalid JSON received"
|
||||||
|
}
|
||||||
|
status = 400
|
||||||
|
|
||||||
# Send one HTTP response
|
else:
|
||||||
self.send_response(status)
|
# Only valid JSON reaches the dispatcher
|
||||||
self.send_header("Content-Type", "application/json")
|
thread, lifecycles, module, mode = dispatchRequest(
|
||||||
self.send_header("Content-Length", str(len(responsebytes)))
|
request, lifecycles
|
||||||
self.end_headers()
|
)
|
||||||
|
|
||||||
self.wfile.write(responsebytes)
|
response = buildResponse(module, thread, mode)
|
||||||
|
status = 200
|
||||||
|
|
||||||
|
# Serialize the response
|
||||||
|
responsebytes = json.dumps(response).encode("utf-8")
|
||||||
|
|
||||||
|
# Send one HTTP response
|
||||||
|
self.send_response(status)
|
||||||
|
self.send_header("Content-Type", "application/json")
|
||||||
|
self.send_header("Content-Length", str(len(responsebytes)))
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
self.wfile.write(responsebytes)
|
||||||
|
|
||||||
|
|
||||||
def do_PUT(self):
|
def do_PUT(self):
|
||||||
|
|||||||
+9
-7
@@ -5,8 +5,8 @@ import os
|
|||||||
from core.server.server import startServer
|
from core.server.server import startServer
|
||||||
from core.execution.createthread import buildThread
|
from core.execution.createthread import buildThread
|
||||||
from plugins.rmass.monitors import rmassmonitor
|
from plugins.rmass.monitors import rmassmonitor
|
||||||
#from plugins.rmass.actions import refresh_tokens
|
|
||||||
from plugins.rmass.operations import token_update
|
from plugins.rmass.operations import token_update
|
||||||
|
from plugins.rmass.actions import pingsynapse
|
||||||
|
|
||||||
from core.console import console
|
from core.console import console
|
||||||
from rich.logging import RichHandler
|
from rich.logging import RichHandler
|
||||||
@@ -28,6 +28,13 @@ logging.basicConfig(
|
|||||||
import login
|
import login
|
||||||
import synapsemessagepush
|
import synapsemessagepush
|
||||||
|
|
||||||
|
thread = threading.Thread(
|
||||||
|
target=startServer,
|
||||||
|
name="RMASS Bridge Server"
|
||||||
|
)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
pingsynapse.mode = "continuous"
|
||||||
rmassmonitor.mode = "continuous"
|
rmassmonitor.mode = "continuous"
|
||||||
token_update.mode = "continuous"
|
token_update.mode = "continuous"
|
||||||
#refresh_tokens.mode = "continuous"
|
#refresh_tokens.mode = "continuous"
|
||||||
@@ -38,9 +45,4 @@ thread = buildThread(rmassmonitor)
|
|||||||
|
|
||||||
thread = buildThread(token_update)
|
thread = buildThread(token_update)
|
||||||
|
|
||||||
thread = threading.Thread(
|
thread = buildThread(pingsynapse)
|
||||||
target=startServer,
|
|
||||||
#args=(module, lifecycles)
|
|
||||||
name="RMASS Bridge Server"
|
|
||||||
)
|
|
||||||
thread.start()
|
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
name = "Synapse Wake"
|
name = "Synapse Ping"
|
||||||
|
|
||||||
kind = "action"
|
kind = "action"
|
||||||
|
|
||||||
alias = "synapseping"
|
alias = "pingsynapse"
|
||||||
|
|
||||||
showstatus = True
|
showstatus = True
|
||||||
|
|
||||||
|
sleeptime = 15
|
||||||
|
|
||||||
isrequest = True
|
isrequest = True
|
||||||
|
|
||||||
method = "POST"
|
method = "POST"
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
name = "Set RMASS Bridge Online"
|
||||||
|
|
||||||
|
kind = "action"
|
||||||
|
|
||||||
|
alias = "setrmassbridgeonline"
|
||||||
|
|
||||||
|
showstatus = True
|
||||||
|
|
||||||
|
#sleeptime = 15
|
||||||
|
|
||||||
|
isrequest = True
|
||||||
|
|
||||||
|
method = "PUT"
|
||||||
|
|
||||||
|
path = [
|
||||||
|
"https://loxthelion.com/_matrix/client/v3/presence/@rmass_bridge:loxthelion.com/status",
|
||||||
|
]
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"Authorization": {
|
||||||
|
"os": "AS_TOKEN",
|
||||||
|
"prefix": "Bearer ",
|
||||||
|
},
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
}
|
||||||
|
|
||||||
|
keys = [
|
||||||
|
{
|
||||||
|
"keytitle": "presence",
|
||||||
|
"keyvalue": "online",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"keytitle": "status_msg",
|
||||||
|
"keyvalue": "RMASS Bridge Connected",
|
||||||
|
},
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user