Skip to content

Commit

Permalink
feat: allow operating on any port within range
Browse files Browse the repository at this point in the history
So long as a client is listening on port 1716 for UDP broadcasts, it
may listen for TCP connection on any port between 1716-1764.

closes #551
closes #1555
closes #1643
  • Loading branch information
andyholmes committed Jul 14, 2023
1 parent 83d041c commit 1dff0a3
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/service/backends/lan.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const Core = imports.service.core;
/**
* TCP Port Constants
*/
const DEFAULT_PORT = 1716;
const PROTOCOL_PORT_DEFAULT = 1716;
const PROTOCOL_PORT_MIN = 1716;
const PROTOCOL_PORT_MAX = 1764;
const TRANSFER_MIN = 1739;
const TRANSFER_MAX = 1764;

Expand Down Expand Up @@ -92,7 +94,7 @@ var ChannelService = GObject.registerClass({
'The port used by the service',
GObject.ParamFlags.READWRITE,
0, GLib.MAXUINT16,
DEFAULT_PORT
PROTOCOL_PORT_DEFAULT
),
},
}, class LanChannelService extends Core.ChannelService {
Expand All @@ -106,6 +108,7 @@ var ChannelService = GObject.registerClass({

//
this._tcp = null;
this._tcpPort = PROTOCOL_PORT_DEFAULT;
this._udp4 = null;
this._udp6 = null;

Expand Down Expand Up @@ -139,7 +142,7 @@ var ChannelService = GObject.registerClass({

get port() {
if (this._port === undefined)
this._port = DEFAULT_PORT;
this._port = PROTOCOL_PORT_DEFAULT;

return this._port;
}
Expand Down Expand Up @@ -190,7 +193,24 @@ var ChannelService = GObject.registerClass({
_initTcpListener() {
try {
this._tcp = new Gio.SocketService();
this._tcp.add_inet_port(this.port, null);

let port = PROTOCOL_PORT_MIN;

while (port <= PROTOCOL_PORT_MAX) {
try {
this._tcp.add_inet_port(port, null);
break;
} catch (e) {
if (port < PROTOCOL_PORT_MAX) {
port++;
continue;
} else {
throw e;
}
}
}

this._tcpPort = port;
this._tcp.connect('incoming', this._onIncomingChannel.bind(this));
} catch (e) {
this._tcp = null;
Expand All @@ -214,7 +234,7 @@ var ChannelService = GObject.registerClass({
// Accept the connection
await channel.accept(connection);
channel.identity.body.tcpHost = channel.host;
channel.identity.body.tcpPort = this.port;
channel.identity.body.tcpPort = this._tcpPort;
channel.allowed = this._allowed.has(host);

this.channel(channel);
Expand Down Expand Up @@ -432,7 +452,7 @@ var ChannelService = GObject.registerClass({
buildIdentity() {
// Chain-up, then add the TCP port
super.buildIdentity();
this.identity.body.tcpPort = this.port;
this.identity.body.tcpPort = this._tcpPort;
}

start() {
Expand Down Expand Up @@ -570,7 +590,7 @@ var Channel = GObject.registerClass({
if (this.identity && this.identity.body.tcpPort)
this._port = this.identity.body.tcpPort;
else
return DEFAULT_PORT;
return PROTOCOL_PORT_DEFAULT;
}

return this._port;
Expand Down

0 comments on commit 1dff0a3

Please sign in to comment.