Skip to content

Commit

Permalink
Use local sockets for client-spawned servers
Browse files Browse the repository at this point in the history
This looks much less suspicious to the Windows firewall.
  • Loading branch information
lmoureaux committed Feb 12, 2022
1 parent fd0c6af commit f9637af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
32 changes: 11 additions & 21 deletions client/clinet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -151,7 +141,7 @@ static int try_to_connect(const QUrl &url, char *errbuf, int errbufsize)
qobject_cast<QLocalSocket *>(client.conn.sock)) {
local->abort();
}
connect_to = url_copy;
connect_to = url;
} else {
(void) fc_strlcpy(errbuf, _("Connection in progress."), errbufsize);
return -1;
Expand Down
12 changes: 7 additions & 5 deletions client/connectdlg_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <QStandardPaths>
#include <QTcpServer>
#include <QUrl>
#include <QUuid>

#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -238,15 +239,17 @@ 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.
port_buf = QString::number(internal_server_port);
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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit f9637af

Please sign in to comment.