server: Add a config option to set the SSO helper listening port.

This commit is contained in:
Damir Jelić 2019-09-17 19:37:22 +02:00
parent f5f08cba6f
commit 355fa95533
2 changed files with 52 additions and 3 deletions

View file

@ -15,6 +15,7 @@
import asyncio import asyncio
import argparse
import socket import socket
import json import json
from random import choice from random import choice
@ -49,16 +50,20 @@ async def get_token(request):
} }
# Send the token to weechat. # Send the token to weechat.
print(json.dumps(message)) to_weechat(message)
# Initiate a shutdown. # Initiate a shutdown.
shutdown_task = asyncio.ensure_future(shutdown()) shutdown_task = asyncio.ensure_future(shutdown())
# Respond to the browser. # Respond to the browser.
return web.Response(text="Continuing in Weechat.") return web.Response(text="Continuing in Weechat.")
def bind_socket(): def bind_socket(port=None):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if port is not None and port != 0:
sock.bind(("localhost", port))
return sock
while True: while True:
port = choice(port_range) port = choice(port_range)
@ -81,9 +86,38 @@ async def wait_for_shutdown_task(_):
def main(): def main():
parser = argparse.ArgumentParser(
description="Start a web server that waits for a SSO token to be "
"passed with a GET request"
)
parser.add_argument(
"-p", "--port",
help=("the port that the web server will be listening on, if 0 a "
"random port should be chosen"
),
type=int,
default=0
)
args = parser.parse_args()
app = web.Application() app = web.Application()
app.add_routes([web.get('/', get_token)]) app.add_routes([web.get('/', get_token)])
sock = bind_socket()
if not 0 <= args.port <= 65535:
raise ValueError("Port needs to be 0-65535")
try:
sock = bind_socket(args.port)
except OSError as e:
message = {
"type": "error",
"message": str(e),
"code": e.errno
}
to_weechat(message)
return
host, port = sock.getsockname() host, port = sock.getsockname()
message = { message = {

View file

@ -208,6 +208,15 @@ class ServerConfig(ConfigSection):
"10", "10",
("Delay (in seconds) before trying to reconnect to server"), ("Delay (in seconds) before trying to reconnect to server"),
), ),
Option(
"sso_helper_listening_port",
"integer",
"",
0,
65535,
"0",
("The port that the SSO helpers web server should listen on"),
),
] ]
section = W.config_search_section(config_ptr, "server") section = W.config_search_section(config_ptr, "server")
@ -251,6 +260,10 @@ class ServerConfig(ConfigSection):
password = ConfigSection.option_property( password = ConfigSection.option_property(
"password", "string", evaluate=True "password", "string", evaluate=True
) )
sso_helper_listening_port = ConfigSection.option_property(
"sso_helper_listening_port",
"integer"
)
def free(self): def free(self):
W.config_section_free_options(self._ptr) W.config_section_free_options(self._ptr)
@ -807,6 +820,8 @@ class MatrixServer(object):
process_args = { process_args = {
"buffer_flush": "1", "buffer_flush": "1",
"arg1": "--port",
"arg2": str(self.config.sso_helper_listening_port)
} }
self.sso_hook = W.hook_process_hashtable( self.sso_hook = W.hook_process_hashtable(