From 669a3a7673137c1b7e1202ef4e32cbe6cd18e69a Mon Sep 17 00:00:00 2001 From: Aditya Toshniwal Date: Tue, 15 Nov 2022 13:51:12 +0530 Subject: [PATCH] Fixed SocketIO calls when pgAdmin 4 server is running from a sub directory. #5521 --- web/pgadmin/__init__.py | 11 +++++------ web/pgadmin/static/js/socket_instance.js | 10 ++++++---- web/pgadmin/tools/psql/__init__.py | 4 ++++ web/pgadmin/tools/psql/static/js/psql_module.js | 6 +++++- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py index 382123aeb..cbead1642 100644 --- a/web/pgadmin/__init__.py +++ b/web/pgadmin/__init__.py @@ -123,7 +123,7 @@ class PgAdmin(Flask): # like 'localhost/pgadmin4' then we have to append '/pgadmin4' # into endpoints ############################################################# - wsgi_root_path = None + wsgi_root_path = '' if url_for('browser.index') != '/browser/': wsgi_root_path = url_for('browser.index').replace( '/browser/', '' @@ -133,10 +133,7 @@ class PgAdmin(Flask): """ Generate endpoint URL at per WSGI alias """ - if wsgi_root_path is not None and url: - return wsgi_root_path + url - else: - return url + return wsgi_root_path + url # Fetch all endpoints and their respective url for rule in current_app.url_map.iter_rules('static'): @@ -147,6 +144,8 @@ class PgAdmin(Flask): for rule in current_app.url_map.iter_rules(endpoint): yield rule.endpoint, get_full_url_path(rule.rule) + yield 'pgadmin.root', wsgi_root_path + @property def javascripts(self): scripts = [] @@ -928,5 +927,5 @@ def create_app(app_name=None): ########################################################################## # All done! ########################################################################## - socketio.init_app(app) + socketio.init_app(app, cors_allowed_origins="*") return app diff --git a/web/pgadmin/static/js/socket_instance.js b/web/pgadmin/static/js/socket_instance.js index 24dde5da6..41c95c27d 100644 --- a/web/pgadmin/static/js/socket_instance.js +++ b/web/pgadmin/static/js/socket_instance.js @@ -8,10 +8,12 @@ ////////////////////////////////////////////////////////////// import { io } from 'socketio'; import gettext from 'sources/gettext'; +import url_for from 'sources/url_for'; export function openSocket(namespace, options) { return new Promise((resolve, reject)=>{ const socketObj = io(namespace, { + path: `${url_for('pgadmin.root')}/socket.io`, pingTimeout: 120000, pingInterval: 25000, ...options, @@ -24,11 +26,11 @@ export function openSocket(namespace, options) { socketObj.on('connected', ()=>{ resolve(socketObj); }); - socketObj.on('connect_error', ()=>{ - reject(); + socketObj.on('connect_error', (err)=>{ + reject(err); }); - socketObj.on('disconnect', ()=>{ - reject(); + socketObj.on('disconnect', (err)=>{ + reject(err); }); }); } diff --git a/web/pgadmin/tools/psql/__init__.py b/web/pgadmin/tools/psql/__init__.py index 6ece64c99..ee27c3a44 100644 --- a/web/pgadmin/tools/psql/__init__.py +++ b/web/pgadmin/tools/psql/__init__.py @@ -25,6 +25,8 @@ from pgadmin.utils.constants import MIMETYPE_APP_JS from pgadmin.utils.driver import get_driver from ... import socketio as sio from pgadmin.utils import get_complete_file_path +from pgadmin.authenticate import socket_login_required + if _platform == 'win32': # Check Windows platform support for WinPty api, Disable psql @@ -149,6 +151,7 @@ def set_term_size(fd, row, col, xpix=0, ypix=0): @sio.on('connect', namespace='/pty') +@socket_login_required def connect(): """ Connect to the server through socket. @@ -276,6 +279,7 @@ def pty_handel_io(connection_data, data, sid): @sio.on('start_process', namespace='/pty') +@socket_login_required def start_process(data): """ Start the pty terminal and execute psql command and emit results to user. diff --git a/web/pgadmin/tools/psql/static/js/psql_module.js b/web/pgadmin/tools/psql/static/js/psql_module.js index bb45579b2..8dbc67c7a 100644 --- a/web/pgadmin/tools/psql/static/js/psql_module.js +++ b/web/pgadmin/tools/psql/static/js/psql_module.js @@ -275,7 +275,11 @@ export function initialize(gettext, url_for, $, _, pgAdmin, csrfToken, Browser) return new SearchAddon(); }, psql_socket: function() { - return io('/pty', {pingTimeout: 120000, pingInterval: 25000}); + return io('/pty', { + path: `${url_for('pgadmin.root')}/socket.io`, + pingTimeout: 120000, + pingInterval: 25000 + }); }, set_theme: function(term) { let theme = {