From f6910409361530a06a4c5a61ad7834e89f206f4f Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 23 Aug 2015 08:43:33 +0200 Subject: [PATCH 1/3] Refactor: s/Basic/Direct/ on connection type --- cmd/syncthing/connections.go | 2 +- cmd/syncthing/connections_tcp.go | 2 +- lib/model/connection.go | 14 +++++++------- lib/model/model_test.go | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/syncthing/connections.go b/cmd/syncthing/connections.go index 2ad8e86c8..c1021997a 100644 --- a/cmd/syncthing/connections.go +++ b/cmd/syncthing/connections.go @@ -290,7 +290,7 @@ func (s *connectionSvc) connect() { } s.conns <- model.IntermediateConnection{ - conn, model.ConnectionTypeBasicDial, + conn, model.ConnectionTypeDirectDial, } continue nextDevice } diff --git a/cmd/syncthing/connections_tcp.go b/cmd/syncthing/connections_tcp.go index 1c051e293..515df6096 100644 --- a/cmd/syncthing/connections_tcp.go +++ b/cmd/syncthing/connections_tcp.go @@ -99,7 +99,7 @@ func tcpListener(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.Intermedia } conns <- model.IntermediateConnection{ - tc, model.ConnectionTypeBasicAccept, + tc, model.ConnectionTypeDirectAccept, } } } diff --git a/lib/model/connection.go b/lib/model/connection.go index 194a71a16..d7c2bda0a 100644 --- a/lib/model/connection.go +++ b/lib/model/connection.go @@ -25,8 +25,8 @@ type Connection struct { } const ( - ConnectionTypeBasicAccept ConnectionType = iota - ConnectionTypeBasicDial + ConnectionTypeDirectAccept ConnectionType = iota + ConnectionTypeDirectDial ConnectionTypeRelayAccept ConnectionTypeRelayDial ) @@ -35,10 +35,10 @@ type ConnectionType int func (t ConnectionType) String() string { switch t { - case ConnectionTypeBasicAccept: - return "basic-accept" - case ConnectionTypeBasicDial: - return "basic-dial" + case ConnectionTypeDirectAccept: + return "direct-accept" + case ConnectionTypeDirectDial: + return "direct-dial" case ConnectionTypeRelayAccept: return "relay-accept" case ConnectionTypeRelayDial: @@ -48,5 +48,5 @@ func (t ConnectionType) String() string { } func (t ConnectionType) IsDirect() bool { - return t == ConnectionTypeBasicAccept || t == ConnectionTypeBasicDial + return t == ConnectionTypeDirectAccept || t == ConnectionTypeDirectDial } diff --git a/lib/model/model_test.go b/lib/model/model_test.go index 08f3889a9..31df11f4d 100644 --- a/lib/model/model_test.go +++ b/lib/model/model_test.go @@ -285,7 +285,7 @@ func BenchmarkRequest(b *testing.B) { m.AddConnection(Connection{ &net.TCPConn{}, fc, - ConnectionTypeBasicAccept, + ConnectionTypeDirectAccept, }) m.Index(device1, "default", files, 0, nil) @@ -328,7 +328,7 @@ func TestDeviceRename(t *testing.T) { m.AddConnection(Connection{ &net.TCPConn{}, fc, - ConnectionTypeBasicAccept, + ConnectionTypeDirectAccept, }) m.ServeBackground() From aec143b8824d151d26000111f0ef198ff245cb3e Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 23 Aug 2015 08:55:32 +0200 Subject: [PATCH 2/3] Refactor: make IntermediateConnection more like Connection --- cmd/syncthing/connections.go | 12 +++++------- lib/model/connection.go | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/cmd/syncthing/connections.go b/cmd/syncthing/connections.go index c1021997a..2c47b3b3c 100644 --- a/cmd/syncthing/connections.go +++ b/cmd/syncthing/connections.go @@ -143,7 +143,7 @@ next: s.mut.RLock() ct, ok := s.connType[remoteID] s.mut.RUnlock() - if ok && !ct.IsDirect() && c.ConnType.IsDirect() { + if ok && !ct.IsDirect() && c.Type.IsDirect() { if debugNet { l.Debugln("Switching connections", remoteID) } @@ -194,7 +194,7 @@ next: rd = &limitedReader{c.Conn, readRateLimit} } - name := fmt.Sprintf("%s-%s (%s)", c.Conn.LocalAddr(), c.Conn.RemoteAddr(), c.ConnType) + name := fmt.Sprintf("%s-%s (%s)", c.Conn.LocalAddr(), c.Conn.RemoteAddr(), c.Type) protoConn := protocol.NewConnection(remoteID, rd, wr, s.model, name, deviceCfg.Compression) l.Infof("Established secure connection to %s at %s", remoteID, name) @@ -205,10 +205,10 @@ next: s.model.AddConnection(model.Connection{ c.Conn, protoConn, - c.ConnType, + c.Type, }) s.mut.Lock() - s.connType[remoteID] = c.ConnType + s.connType[remoteID] = c.Type s.mut.Unlock() continue next } @@ -219,11 +219,9 @@ next: "device": remoteID.String(), "address": c.Conn.RemoteAddr().String(), }) - l.Infof("Connection from %s (%s) with unknown device ID %s", c.Conn.RemoteAddr(), c.ConnType, remoteID) - } else { - l.Infof("Connection from %s (%s) with ignored device ID %s", c.Conn.RemoteAddr(), c.ConnType, remoteID) } + l.Infof("Connection from %s (%s) with ignored device ID %s", c.Conn.RemoteAddr(), c.Type, remoteID) c.Conn.Close() } } diff --git a/lib/model/connection.go b/lib/model/connection.go index d7c2bda0a..78e9f0f2c 100644 --- a/lib/model/connection.go +++ b/lib/model/connection.go @@ -14,8 +14,8 @@ import ( ) type IntermediateConnection struct { - Conn *tls.Conn - ConnType ConnectionType + *tls.Conn + Type ConnectionType } type Connection struct { From 21adf752c89538b13ea2f80074515f144e45b8a4 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 23 Aug 2015 09:39:53 +0200 Subject: [PATCH 3/3] Refactor: slightly simplify relay.Svc --- lib/relay/relay.go | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/relay/relay.go b/lib/relay/relay.go index ec7974f95..7b7029c35 100644 --- a/lib/relay/relay.go +++ b/lib/relay/relay.go @@ -54,9 +54,10 @@ func NewSvc(cfg *config.Wrapper, tlsCfg *tls.Config, conns chan<- model.Intermed tlsCfg: tlsCfg, conns: conns, invitations: svc.invitations, + stop: make(chan struct{}), } - svc.receiverToken = svc.Add(receiver) + svc.Add(receiver) return svc } @@ -66,11 +67,10 @@ type Svc struct { cfg *config.Wrapper tlsCfg *tls.Config - receiverToken suture.ServiceToken - tokens map[string]suture.ServiceToken - clients map[string]*client.ProtocolClient - mut sync.RWMutex - invitations chan protocol.SessionInvitation + tokens map[string]suture.ServiceToken + clients map[string]*client.ProtocolClient + mut sync.RWMutex + invitations chan protocol.SessionInvitation } func (s *Svc) VerifyConfiguration(from, to config.Configuration) error { @@ -123,6 +123,7 @@ func (s *Svc) CommitConfiguration(from, to config.Configuration) bool { } continue } + for _, relayAnn := range ann.Relays { ruri, err := url.Parse(relayAnn.URL) if err != nil { @@ -138,6 +139,8 @@ func (s *Svc) CommitConfiguration(from, to config.Configuration) bool { } } + s.mut.Lock() + for key, uri := range existing { _, ok := s.tokens[key] if !ok { @@ -146,9 +149,7 @@ func (s *Svc) CommitConfiguration(from, to config.Configuration) bool { } c := client.NewProtocolClient(uri, s.tlsCfg.Certificates, s.invitations) s.tokens[key] = s.Add(c) - s.mut.Lock() s.clients[key] = c - s.mut.Unlock() } } @@ -157,15 +158,15 @@ func (s *Svc) CommitConfiguration(from, to config.Configuration) bool { if !ok { err := s.Remove(token) delete(s.tokens, key) - s.mut.Lock() delete(s.clients, key) - s.mut.Unlock() if debug { l.Debugln("Disconnecting from relay", key, err) } } } + s.mut.Unlock() + return true } @@ -187,11 +188,6 @@ type invitationReceiver struct { } func (r *invitationReceiver) Serve() { - if r.stop != nil { - return - } - r.stop = make(chan struct{}) - for { select { case inv := <-r.invitations: @@ -227,6 +223,7 @@ func (r *invitationReceiver) Serve() { r.conns <- model.IntermediateConnection{ tc, model.ConnectionTypeRelayAccept, } + case <-r.stop: return } @@ -234,17 +231,13 @@ func (r *invitationReceiver) Serve() { } func (r *invitationReceiver) Stop() { - if r.stop == nil { - return - } - r.stop <- struct{}{} - r.stop = nil + close(r.stop) } +// This is the announcement recieved from the relay server; +// {"relays": [{"url": "relay://10.20.30.40:5060"}, ...]} type dynamicAnnouncement struct { - Relays []relayAnnouncement `json:"relays"` -} - -type relayAnnouncement struct { - URL string `json:"url"` + Relays []struct { + URL string + } }