Skip to content

Commit

Permalink
gui, lib/connections: Let the backend decide whether connection is lo…
Browse files Browse the repository at this point in the history
…cal (fixes #8686) (#8694)

* lib/connections: Cache isLAN decision for later external access.

The check whether a remote device's address is on a local network
currently happens when handling the Hello message, to configure the
limiters.  Save the result to the ConnectionInfo and pass it out as
part of the model's ConnectionInfo struct in ConnectionStats().

* gui: Use provided connection attribute to distinguish LAN / WAN.

Replace the dumb IP address check which didn't catch common cases and
actually could contradict what the backend decided.  That could have
been confusing if the GUI says WAN, but the limiter is not actually
applied because the backend thinks it's a LAN.

Add strings for QUIC and relay connections to also differentiate
between LAN and WAN.

* gui: Redefine reception level icons for all connection types.

Move the mapping to the JS code, as it is much easier to handle
multiple switch cases by fall-through there.

QUIC is regarded no less than TCP anymore.  LAN and WAN make the
difference between levels 4 / 3 and 2 / 1:

{TCP,QUIC} LAN --> {TCP,QUIC} WAN --> Relay LAN --> Relay WAN -->
Disconnected.
  • Loading branch information
acolomb committed Nov 28, 2022
1 parent d16c065 commit ab0eb90
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 25 deletions.
10 changes: 3 additions & 7 deletions gui/default/index.html
Expand Up @@ -751,12 +751,8 @@ <h4 class="panel-title">
<span ng-switch-when="disconnected-inactive"><span class="hidden-xs" translate>Disconnected (Inactive)</span><span class="visible-xs" aria-label="{{'Disconnected (Inactive)' | translate}}"><i class="fas fa-fw fa-power-off"></i></span></span>
<span ng-switch-when="unused-disconnected"><span class="hidden-xs" translate>Disconnected (Unused)</span><span class="visible-xs" aria-label="{{'Disconnected (Unused)' | translate}}"><i class="fas fa-fw fa-unlink"></i></span></span>
</span>
<span ng-switch="rdConnType(deviceCfg.deviceID)" class="remote-devices-panel">
<span ng-switch-when="tcplan" class="reception reception-4 reception-theme"></span>
<span ng-switch-when="tcpwan" class="reception reception-3 reception-theme"></span>
<span ng-switch-when="quic" class="reception reception-2 reception-theme"></span>
<span ng-switch-when="relay" class="reception reception-1 reception-theme"></span>
<span ng-switch-when="disconnected" class="reception reception-0 reception-theme"></span>
<span class="remote-devices-panel">
<span ng-class="rdConnTypeIcon(rdConnType(deviceCfg.deviceID))" class="reception reception-theme"></span>
</span>
</span>
<div class="panel-title-text">{{deviceName(deviceCfg)}}</div>
Expand Down Expand Up @@ -849,7 +845,7 @@ <h4 class="panel-title">
</tr>
<tr ng-if="connections[deviceCfg.deviceID].connected">
<th><span class="reception reception-4 reception-theme"></span>&nbsp;<span translate>Connection Type</span></th>
<td ng-if="connections[deviceCfg.deviceID].connected" class="text-right">
<td class="text-right">
<span tooltip data-original-title="{{rdConnDetails(rdConnType(deviceCfg.deviceID))}}">
{{rdConnTypeString(rdConnType(deviceCfg.deviceID))}}
</span>
Expand Down
54 changes: 38 additions & 16 deletions gui/default/syncthing/core/syncthingController.js
Expand Up @@ -1203,24 +1203,27 @@ angular.module('syncthing.core')
$scope.rdConnType = function (deviceID) {
var conn = $scope.connections[deviceID];
if (!conn) return "-1";
if (conn.type.indexOf('relay') === 0) return "relay";
if (conn.type.indexOf('quic') === 0) return "quic";
if (conn.type.indexOf('tcp') === 0) return "tcp" + rdAddrType(conn.address);
return "disconnected";
}

function rdAddrType(address) {
var re = /(^(?:127\.|0?10\.|172\.0?1[6-9]\.|172\.0?2[0-9]\.|172\.0?3[01]\.|192\.168\.|169\.254\.|::1|[fF][cCdD][0-9a-fA-F]{2}:|[fF][eE][89aAbB][0-9a-fA-F]:))/
if (re.test(address)) return "lan";
return "wan";
var type = "disconnected";
if (conn.type.indexOf('relay') === 0) type = "relay";
else if (conn.type.indexOf('quic') === 0) type = "quic";
else if (conn.type.indexOf('tcp') === 0) type = "tcp";
else return type;

if (conn.isLocal) type += "lan";
else type += "wan";
return type;
}

$scope.rdConnTypeString = function (type) {
switch (type) {
case "relay":
return $translate.instant('Relay');
case "quic":
return $translate.instant('QUIC');
case "relaywan":
return $translate.instant('Relay WAN');
case "relaylan":
return $translate.instant('Relay LAN');
case "quicwan":
return $translate.instant('QUIC WAN');
case "quiclan":
return $translate.instant('QUIC LAN');
case "tcpwan":
return $translate.instant('TCP WAN');
case "tcplan":
Expand All @@ -1230,11 +1233,30 @@ angular.module('syncthing.core')
}
}

$scope.rdConnTypeIcon = function (type) {
switch (type) {
case "tcplan":
case "quiclan":
return "reception-4";
case "tcpwan":
case "quicwan":
return "reception-3";
case "relaylan":
return "reception-2";
case "relaywan":
return "reception-1";
case "disconnected":
return "reception-0";
}
}

$scope.rdConnDetails = function (type) {
switch (type) {
case "relay":
case "relaylan":
case "relaywan":
return $translate.instant('Connections via relays might be rate limited by the relay');
case "quic":
case "quiclan":
case "quicwan":
return $translate.instant('QUIC connections are in most cases considered suboptimal');
case "tcpwan":
return $translate.instant('Using a direct TCP connection over WAN');
Expand Down
6 changes: 4 additions & 2 deletions lib/connections/service.go
Expand Up @@ -403,11 +403,13 @@ func (s *service) handleHellos(ctx context.Context) error {
continue
}

// Determine only once whether a connection is considered local
// according to our configuration, then cache the decision.
c.isLocal = s.isLAN(c.RemoteAddr())
// Wrap the connection in rate limiters. The limiter itself will
// keep up with config changes to the rate and whether or not LAN
// connections are limited.
isLAN := s.isLAN(c.RemoteAddr())
rd, wr := s.limiter.getLimiters(remoteID, c, isLAN)
rd, wr := s.limiter.getLimiters(remoteID, c, c.IsLocal())

protoConn := protocol.NewConnection(remoteID, rd, wr, c, s.model, c, deviceCfg.Compression, s.cfg.FolderPasswords(remoteID))
go func() {
Expand Down
5 changes: 5 additions & 0 deletions lib/connections/structs.go
Expand Up @@ -39,6 +39,7 @@ type tlsConn interface {
type internalConn struct {
tlsConn
connType connType
isLocal bool
priority int
establishedAt time.Time
}
Expand Down Expand Up @@ -107,6 +108,10 @@ func (c internalConn) Type() string {
return c.connType.String()
}

func (c internalConn) IsLocal() bool {
return c.isLocal
}

func (c internalConn) Priority() int {
return c.priority
}
Expand Down
2 changes: 2 additions & 0 deletions lib/model/model.go
Expand Up @@ -709,6 +709,7 @@ type ConnectionInfo struct {
Address string `json:"address"`
ClientVersion string `json:"clientVersion"`
Type string `json:"type"`
IsLocal bool `json:"isLocal"`
Crypto string `json:"crypto"`
}

Expand Down Expand Up @@ -739,6 +740,7 @@ func (m *model) ConnectionStats() map[string]interface{} {
}
if conn, ok := m.conn[device]; ok {
ci.Type = conn.Type()
ci.IsLocal = conn.IsLocal()
ci.Crypto = conn.Crypto()
ci.Connected = ok
ci.Statistics = conn.Statistics()
Expand Down
65 changes: 65 additions & 0 deletions lib/protocol/mocked_connection_info_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions lib/protocol/mocks/connection.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab0eb90

Please sign in to comment.