allow 2p url config, origin limiting for server

This commit is contained in:
Bui 2022-03-24 02:32:31 +00:00
parent 0c39e54d90
commit 7f511abb4f
4 changed files with 14 additions and 4 deletions

3
app.py
View File

@ -135,7 +135,8 @@ def get_config(credentials=False):
'accounts': take_config('ACCOUNTS'),
'custom_js': take_config('CUSTOM_JS'),
'plugins': take_config('PLUGINS') and [x for x in take_config('PLUGINS') if x['url']],
'preview_type': take_config('PREVIEW_TYPE') or 'mp3'
'preview_type': take_config('PREVIEW_TYPE') or 'mp3',
'multiplayer_url': take_config('MULTIPLAYER_URL')
}
if credentials:
google_credentials = take_config('GOOGLE_CREDENTIALS')

View File

@ -4,6 +4,9 @@ ASSETS_BASEURL = '/assets/'
# The full URL base song URL, with trailing slash.
SONGS_BASEURL = '/songs/'
# Multiplayer websocket URL. Defaults to /p2 if blank.
MULTIPLAYER_URL = ''
# The email address to display in the "About Simulator" menu.
EMAIL = None

View File

@ -32,7 +32,7 @@ class P2Connection{
if(this.closed && !this.disabled){
this.closed = false
var wsProtocol = location.protocol == "https:" ? "wss:" : "ws:"
this.socket = new WebSocket(wsProtocol + "//" + location.host + "/p2")
this.socket = new WebSocket(gameConfig.multiplayer_url ? gameConfig.multiplayer_url : wsProtocol + "//" + location.host + "/p2")
pageEvents.race(this.socket, "open", "close").then(response => {
if(response.type === "open"){
return this.openEvent()

View File

@ -1,11 +1,17 @@
#!/usr/bin/env python3
import argparse
import asyncio
import websockets
import json
import random
import sys
parser = argparse.ArgumentParser(description='Run the taiko-web multiplayer server.')
parser.add_argument('port', type=int, metavar='PORT', nargs='?', default=34802, help='Port to listen on.')
parser.add_argument('-o', '--allow-origin', action='append', help='Limit incoming connections to the specified origin. Can be specified multiple times.')
args = parser.parse_args()
server_status = {
"waiting": {},
"users": [],
@ -372,11 +378,11 @@ async def connection(ws, path):
elif user["action"] == "invite" and user["session"] in server_status["invites"]:
del server_status["invites"][user["session"]]
port = int(sys.argv[1]) if len(sys.argv) > 1 else 34802
port = args.port
print('Starting server on port %d' % port)
loop = asyncio.get_event_loop()
tasks = asyncio.gather(
websockets.serve(connection, "localhost", port)
websockets.serve(connection, "localhost", port, origins=args.allow_origin)
)
try:
loop.run_until_complete(tasks)