Add switch to disable relays

This commit is contained in:
Audrius Butkevicius 2015-07-24 20:07:26 +01:00
parent 031804827f
commit 1e8b185377
5 changed files with 16 additions and 4 deletions

View File

@ -45,8 +45,9 @@ type connectionSvc struct {
lastRelayCheck map[protocol.DeviceID]time.Time lastRelayCheck map[protocol.DeviceID]time.Time
mut sync.RWMutex mut sync.RWMutex
connType map[protocol.DeviceID]model.ConnectionType connType map[protocol.DeviceID]model.ConnectionType
relaysEnabled bool
} }
func newConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl *model.Model, tlsCfg *tls.Config) *connectionSvc { func newConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl *model.Model, tlsCfg *tls.Config) *connectionSvc {
@ -59,6 +60,7 @@ func newConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl *model.Mo
conns: make(chan model.IntermediateConnection), conns: make(chan model.IntermediateConnection),
connType: make(map[protocol.DeviceID]model.ConnectionType), connType: make(map[protocol.DeviceID]model.ConnectionType),
relaysEnabled: cfg.Options().RelaysEnabled,
lastRelayCheck: make(map[protocol.DeviceID]time.Time), lastRelayCheck: make(map[protocol.DeviceID]time.Time),
} }
cfg.Subscribe(svc) cfg.Subscribe(svc)
@ -239,6 +241,7 @@ func (s *connectionSvc) connect() {
s.mut.RLock() s.mut.RLock()
ct, ok := s.connType[deviceID] ct, ok := s.connType[deviceID]
relaysEnabled := s.relaysEnabled
s.mut.RUnlock() s.mut.RUnlock()
if connected && ok && ct.IsDirect() { if connected && ok && ct.IsDirect() {
continue continue
@ -296,7 +299,8 @@ func (s *connectionSvc) connect() {
// Also, do not set lastRelayCheck time if we have no relays, // Also, do not set lastRelayCheck time if we have no relays,
// as otherwise when we do discover relays, we might have to // as otherwise when we do discover relays, we might have to
// wait up to RelayReconnectIntervalM to connect again. // wait up to RelayReconnectIntervalM to connect again.
if connected || len(relays) == 0 { // Also, do not try relays if we are explicitly told not to.
if connected || len(relays) == 0 || !relaysEnabled {
continue nextDevice continue nextDevice
} }
@ -394,6 +398,10 @@ func (s *connectionSvc) VerifyConfiguration(from, to config.Configuration) error
} }
func (s *connectionSvc) CommitConfiguration(from, to config.Configuration) bool { func (s *connectionSvc) CommitConfiguration(from, to config.Configuration) bool {
s.mut.Lock()
s.relaysEnabled = to.Options.RelaysEnabled
s.mut.Unlock()
// We require a restart if a device as been removed. // We require a restart if a device as been removed.
newDevices := make(map[protocol.DeviceID]bool, len(to.Devices)) newDevices := make(map[protocol.DeviceID]bool, len(to.Devices))

View File

@ -676,7 +676,7 @@ func syncthingMain() {
connectionSvc := newConnectionSvc(cfg, myID, m, tlsCfg) connectionSvc := newConnectionSvc(cfg, myID, m, tlsCfg)
mainSvc.Add(connectionSvc) mainSvc.Add(connectionSvc)
if opts.GlobalAnnEnabled || opts.RelayWithoutGlobalAnn { if opts.RelaysEnabled && (opts.GlobalAnnEnabled || opts.RelayWithoutGlobalAnn) {
relaySvc = relay.NewSvc(cfg, tlsCfg, connectionSvc.conns) relaySvc = relay.NewSvc(cfg, tlsCfg, connectionSvc.conns)
connectionSvc.Add(relaySvc) connectionSvc.Add(relaySvc)
} }

View File

@ -223,6 +223,7 @@ type OptionsConfiguration struct {
MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"` MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"`
MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"` MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`
ReconnectIntervalS int `xml:"reconnectionIntervalS" json:"reconnectionIntervalS" default:"60"` ReconnectIntervalS int `xml:"reconnectionIntervalS" json:"reconnectionIntervalS" default:"60"`
RelaysEnabled bool `xml:"relaysEnabled" json:"relaysEnabled" default:"true"`
RelayReconnectIntervalM int `xml:"relayReconnectIntervalM" json:"relayReconnectIntervalM" default:"10"` RelayReconnectIntervalM int `xml:"relayReconnectIntervalM" json:"relayReconnectIntervalM" default:"10"`
RelayWithoutGlobalAnn bool `xml:"relayWithoutGlobalAnn" json:"relayWithoutGlobalAnn" default:"false"` RelayWithoutGlobalAnn bool `xml:"relayWithoutGlobalAnn" json:"relayWithoutGlobalAnn" default:"false"`
StartBrowser bool `xml:"startBrowser" json:"startBrowser" default:"true"` StartBrowser bool `xml:"startBrowser" json:"startBrowser" default:"true"`

View File

@ -41,6 +41,7 @@ func TestDefaultValues(t *testing.T) {
MaxSendKbps: 0, MaxSendKbps: 0,
MaxRecvKbps: 0, MaxRecvKbps: 0,
ReconnectIntervalS: 60, ReconnectIntervalS: 60,
RelaysEnabled: true,
RelayReconnectIntervalM: 10, RelayReconnectIntervalM: 10,
RelayWithoutGlobalAnn: false, RelayWithoutGlobalAnn: false,
StartBrowser: true, StartBrowser: true,
@ -155,6 +156,7 @@ func TestOverriddenValues(t *testing.T) {
MaxSendKbps: 1234, MaxSendKbps: 1234,
MaxRecvKbps: 2341, MaxRecvKbps: 2341,
ReconnectIntervalS: 6000, ReconnectIntervalS: 6000,
RelaysEnabled: false,
RelayReconnectIntervalM: 20, RelayReconnectIntervalM: 20,
RelayWithoutGlobalAnn: true, RelayWithoutGlobalAnn: true,
StartBrowser: false, StartBrowser: false,

View File

@ -13,6 +13,7 @@
<maxSendKbps>1234</maxSendKbps> <maxSendKbps>1234</maxSendKbps>
<maxRecvKbps>2341</maxRecvKbps> <maxRecvKbps>2341</maxRecvKbps>
<reconnectionIntervalS>6000</reconnectionIntervalS> <reconnectionIntervalS>6000</reconnectionIntervalS>
<relaysEnabled>false</relaysEnabled>
<relayReconnectIntervalM>20</relayReconnectIntervalM> <relayReconnectIntervalM>20</relayReconnectIntervalM>
<relayWithoutGlobalAnn>true</relayWithoutGlobalAnn> <relayWithoutGlobalAnn>true</relayWithoutGlobalAnn>
<startBrowser>false</startBrowser> <startBrowser>false</startBrowser>