MCPcopy
hub / github.com/syncthing/syncthing / handleIntroductions

Method handleIntroductions

lib/model/model.go:1653–1711  ·  view source on GitHub ↗

handleIntroductions handles adding devices/folders that are shared by an introducer device

(introducerCfg config.DeviceConfiguration, cm *protocol.ClusterConfig, folders map[string]config.FolderConfiguration, devices map[protocol.DeviceID]config.DeviceConfiguration)

Source from the content-addressed store, hash-verified

1651
1652// handleIntroductions handles adding devices/folders that are shared by an introducer device
1653func (m *model) handleIntroductions(introducerCfg config.DeviceConfiguration, cm *protocol.ClusterConfig, folders map[string]config.FolderConfiguration, devices map[protocol.DeviceID]config.DeviceConfiguration) (map[string]config.FolderConfiguration, map[protocol.DeviceID]config.DeviceConfiguration, folderDeviceSet, bool) {
1654 changed := false
1655
1656 foldersDevices := make(folderDeviceSet)
1657
1658 for _, folder := range cm.Folders {
1659 // Adds devices which we do not have, but the introducer has
1660 // for the folders that we have in common. Also, shares folders
1661 // with devices that we have in common, yet are currently not sharing
1662 // the folder.
1663
1664 fcfg, ok := folders[folder.ID]
1665 if !ok {
1666 // Don't have this folder, carry on.
1667 continue
1668 }
1669
1670 folderChanged := false
1671
1672 for _, device := range folder.Devices {
1673 // No need to share with self.
1674 if device.ID == m.id {
1675 continue
1676 }
1677
1678 foldersDevices.set(device.ID, folder.ID)
1679
1680 if _, ok := devices[device.ID]; !ok {
1681 // The device is currently unknown. Add it to the config.
1682 devices[device.ID] = m.introduceDevice(device, introducerCfg)
1683 } else if fcfg.SharedWith(device.ID) {
1684 // We already share the folder with this device, so
1685 // nothing to do.
1686 continue
1687 }
1688
1689 if fcfg.Type != config.FolderTypeReceiveEncrypted && device.EncryptionPasswordToken != nil {
1690 slog.Warn("Cannot share folder in untrusted mode with introduced device because it requires a password", folder.LogAttr(), slog.Any("device", device.ID), slog.Any("introducer", introducerCfg.DeviceID))
1691 continue
1692 }
1693
1694 // We don't yet share this folder with this device. Add the device
1695 // to sharing list of the folder.
1696 slog.Info("Sharing folder vouched for by introducer", folder.LogAttr(), slog.Any("device", device.ID), slog.Any("introducer", introducerCfg.DeviceID))
1697 fcfg.Devices = append(fcfg.Devices, config.FolderDeviceConfiguration{
1698 DeviceID: device.ID,
1699 IntroducedBy: introducerCfg.DeviceID,
1700 })
1701 folderChanged = true
1702 }
1703
1704 if folderChanged {
1705 folders[fcfg.ID] = fcfg
1706 changed = true
1707 }
1708 }
1709
1710 return folders, devices, foldersDevices, changed

Callers 1

ClusterConfigMethod · 0.95

Calls 4

introduceDeviceMethod · 0.95
SharedWithMethod · 0.80
setMethod · 0.45
LogAttrMethod · 0.45

Tested by

no test coverage detected