From f9637af4e31e7500d619f8e59e3884dc4beb5ce0 Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Mon, 7 Feb 2022 04:59:48 +0100 Subject: [PATCH] Use local sockets for client-spawned servers This looks much less suspicious to the Windows firewall. --- client/clinet.cpp | 32 +++++++++++--------------------- client/connectdlg_common.cpp | 12 +++++++----- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/client/clinet.cpp b/client/clinet.cpp index 52ddc8b56d..f511226d54 100644 --- a/client/clinet.cpp +++ b/client/clinet.cpp @@ -115,33 +115,23 @@ static void error_on_socket() } /** - Try to connect to a server: - - try to create a TCP socket to the given URL (default to - localhost:DEFAULT_SOCK_PORT). - - if successful: - - start monitoring the socket for packets from the server - - send a "login request" packet to the server - and - return 0 - - if unable to create the connection, close the socket, put an error - message in ERRBUF and return the Unix error code (ie., errno, which - will be non-zero). + * Try to connect to a server: + * - try to create a TCP socket to the given URL + * - if successful: + * - start monitoring the socket for packets from the server + * - send a "login request" packet to the server + * and - return 0 + * - if unable to create the connection, close the socket, put an error + * message in ERRBUF and return the Unix error code (ie., errno, which + * will be non-zero). */ static int try_to_connect(const QUrl &url, char *errbuf, int errbufsize) { - // Apply defaults - auto url_copy = url; - if (url_copy.host().isEmpty()) { - url_copy.setHost(QStringLiteral("localhost")); - } - if (url_copy.port() <= 0) { - url_copy.setPort(DEFAULT_SOCK_PORT); - } - connections_set_close_callback(client_conn_close_callback); // connection in progress? wait. if (client.conn.used) { - if (url_copy != connect_to) { + if (url != connect_to) { (void) fc_strlcpy( errbuf, _("Canceled previous connection, trying new connection."), errbufsize); @@ -151,7 +141,7 @@ static int try_to_connect(const QUrl &url, char *errbuf, int errbufsize) qobject_cast(client.conn.sock)) { local->abort(); } - connect_to = url_copy; + connect_to = url; } else { (void) fc_strlcpy(errbuf, _("Connection in progress."), errbufsize); return -1; diff --git a/client/connectdlg_common.cpp b/client/connectdlg_common.cpp index 280a416ee4..08ab90ec69 100644 --- a/client/connectdlg_common.cpp +++ b/client/connectdlg_common.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -238,6 +239,9 @@ bool client_start_server(const QString &user_name) return false; } + // Unique name for this game + auto uuid = QUuid::createUuid().toString(QUuid::WithoutBraces); + ruleset = QString::fromUtf8(tileset_what_ruleset(tileset)); // Set up the command-line parameters. @@ -245,8 +249,7 @@ bool client_start_server(const QString &user_name) savesdir = QStringLiteral("%1/saves").arg(storage); scensdir = QStringLiteral("%1/scenarios").arg(storage); - arguments << QStringLiteral("-p") << port_buf << QStringLiteral("--bind") - << QStringLiteral("localhost") << QStringLiteral("-q") + arguments << QStringLiteral("--local") << uuid << QStringLiteral("-q") << QStringLiteral("1") << QStringLiteral("-e") << QStringLiteral("--saves") << savesdir << QStringLiteral("--scenarios") << scensdir @@ -297,10 +300,9 @@ bool client_start_server(const QString &user_name) // Local server URL auto url = QUrl(); - url.setScheme(QStringLiteral("fc21")); + url.setScheme(QStringLiteral("fc21+local")); url.setUserName(user_name); - url.setHost(QStringLiteral("localhost")); - url.setPort(internal_server_port); + url.setPath(uuid); // a reasonable number of tries while (connect_to_server(