2015-02-19 02:44:37 -07:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-02-19 02:44:37 -07:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2015-06-23 05:55:30 -07:00
|
|
|
"net/url"
|
2015-06-28 08:05:29 -07:00
|
|
|
"sync"
|
2015-02-19 02:44:37 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/protocol"
|
2015-06-28 13:09:03 -07:00
|
|
|
|
|
|
|
"github.com/syncthing/relaysrv/client"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
|
|
|
"github.com/syncthing/syncthing/lib/model"
|
2015-06-28 13:09:03 -07:00
|
|
|
|
2015-04-25 03:31:10 -07:00
|
|
|
"github.com/thejerf/suture"
|
2015-02-19 02:44:37 -07:00
|
|
|
)
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
type DialerFactory func(*url.URL, *tls.Config) (*tls.Conn, error)
|
2015-06-28 08:05:29 -07:00
|
|
|
type ListenerFactory func(*url.URL, *tls.Config, chan<- intermediateConnection)
|
2015-06-23 05:55:30 -07:00
|
|
|
|
|
|
|
var (
|
|
|
|
dialers = make(map[string]DialerFactory, 0)
|
|
|
|
listeners = make(map[string]ListenerFactory, 0)
|
|
|
|
)
|
|
|
|
|
2015-04-25 03:31:10 -07:00
|
|
|
// The connection service listens on TLS and dials configured unconnected
|
2015-04-28 08:34:55 -07:00
|
|
|
// devices. Successful connections are handed to the model.
|
2015-04-25 03:31:10 -07:00
|
|
|
type connectionSvc struct {
|
|
|
|
*suture.Supervisor
|
|
|
|
cfg *config.Wrapper
|
|
|
|
myID protocol.DeviceID
|
|
|
|
model *model.Model
|
|
|
|
tlsCfg *tls.Config
|
2015-06-28 08:05:29 -07:00
|
|
|
conns chan intermediateConnection
|
|
|
|
|
2015-06-28 13:09:03 -07:00
|
|
|
lastRelayCheck map[protocol.DeviceID]time.Time
|
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
mut sync.RWMutex
|
|
|
|
connType map[protocol.DeviceID]model.ConnectionType
|
|
|
|
}
|
|
|
|
|
|
|
|
type intermediateConnection struct {
|
|
|
|
conn *tls.Conn
|
|
|
|
connType model.ConnectionType
|
2015-04-25 03:31:10 -07:00
|
|
|
}
|
2015-02-19 02:44:37 -07:00
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
func newConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl *model.Model, tlsCfg *tls.Config) *connectionSvc {
|
2015-04-25 03:31:10 -07:00
|
|
|
svc := &connectionSvc{
|
|
|
|
Supervisor: suture.NewSimple("connectionSvc"),
|
|
|
|
cfg: cfg,
|
|
|
|
myID: myID,
|
2015-06-28 08:05:29 -07:00
|
|
|
model: mdl,
|
2015-04-25 03:31:10 -07:00
|
|
|
tlsCfg: tlsCfg,
|
2015-06-28 08:05:29 -07:00
|
|
|
conns: make(chan intermediateConnection),
|
|
|
|
|
2015-06-28 13:09:03 -07:00
|
|
|
connType: make(map[protocol.DeviceID]model.ConnectionType),
|
|
|
|
lastRelayCheck: make(map[protocol.DeviceID]time.Time),
|
2015-02-19 02:44:37 -07:00
|
|
|
}
|
2015-06-28 12:09:53 -07:00
|
|
|
cfg.Subscribe(svc)
|
2015-02-19 02:44:37 -07:00
|
|
|
|
2015-04-25 03:31:10 -07:00
|
|
|
// There are several moving parts here; one routine per listening address
|
|
|
|
// to handle incoming connections, one routine to periodically attempt
|
2015-06-28 12:09:53 -07:00
|
|
|
// outgoing connections, one routine to the the common handling
|
|
|
|
// regardless of whether the connection was incoming or outgoing.
|
|
|
|
// Furthermore, a relay service which handles incoming requests to connect
|
|
|
|
// via the relays.
|
2015-04-25 03:31:10 -07:00
|
|
|
//
|
|
|
|
// TODO: Clean shutdown, and/or handling config changes on the fly. We
|
|
|
|
// partly do this now - new devices and addresses will be picked up, but
|
|
|
|
// not new listen addresses and we don't support disconnecting devices
|
|
|
|
// that are removed and so on...
|
|
|
|
|
|
|
|
svc.Add(serviceFunc(svc.connect))
|
|
|
|
for _, addr := range svc.cfg.Options().ListenAddress {
|
2015-06-23 05:55:30 -07:00
|
|
|
uri, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
l.Infoln("Failed to parse listen address:", addr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
listener, ok := listeners[uri.Scheme]
|
|
|
|
if !ok {
|
|
|
|
l.Infoln("Unknown listen address scheme:", uri.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if debugNet {
|
|
|
|
l.Debugln("listening on", uri.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
svc.Add(serviceFunc(func() {
|
|
|
|
listener(uri, svc.tlsCfg, svc.conns)
|
|
|
|
}))
|
2015-04-25 03:31:10 -07:00
|
|
|
}
|
|
|
|
svc.Add(serviceFunc(svc.handle))
|
2015-02-19 02:44:37 -07:00
|
|
|
|
2015-04-25 03:31:10 -07:00
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *connectionSvc) handle() {
|
2015-02-19 02:44:37 -07:00
|
|
|
next:
|
2015-06-28 08:05:29 -07:00
|
|
|
for c := range s.conns {
|
|
|
|
cs := c.conn.ConnectionState()
|
2015-03-05 08:09:20 -07:00
|
|
|
|
|
|
|
// We should have negotiated the next level protocol "bep/1.0" as part
|
2015-03-15 09:49:47 -07:00
|
|
|
// of the TLS handshake. Unfortunately this can't be a hard error,
|
|
|
|
// because there are implementations out there that don't support
|
|
|
|
// protocol negotiation (iOS for one...).
|
2015-03-05 07:58:16 -07:00
|
|
|
if !cs.NegotiatedProtocolIsMutual || cs.NegotiatedProtocol != bepProtocolName {
|
2015-06-28 08:05:29 -07:00
|
|
|
l.Infof("Peer %s did not negotiate bep/1.0", c.conn.RemoteAddr())
|
2015-03-05 07:58:16 -07:00
|
|
|
}
|
|
|
|
|
2015-03-05 08:09:20 -07:00
|
|
|
// We should have received exactly one certificate from the other
|
|
|
|
// side. If we didn't, they don't have a device ID and we drop the
|
|
|
|
// connection.
|
2015-03-05 07:58:16 -07:00
|
|
|
certs := cs.PeerCertificates
|
2015-02-19 02:44:37 -07:00
|
|
|
if cl := len(certs); cl != 1 {
|
2015-06-28 08:05:29 -07:00
|
|
|
l.Infof("Got peer certificate list of length %d != 1 from %s; protocol error", cl, c.conn.RemoteAddr())
|
|
|
|
c.conn.Close()
|
2015-02-19 02:44:37 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
remoteCert := certs[0]
|
|
|
|
remoteID := protocol.NewDeviceID(remoteCert.Raw)
|
|
|
|
|
2015-03-05 08:09:20 -07:00
|
|
|
// The device ID should not be that of ourselves. It can happen
|
2015-04-28 08:34:55 -07:00
|
|
|
// though, especially in the presence of NAT hairpinning, multiple
|
2015-03-05 08:09:20 -07:00
|
|
|
// clients between the same NAT gateway, and global discovery.
|
2015-02-19 02:44:37 -07:00
|
|
|
if remoteID == myID {
|
|
|
|
l.Infof("Connected to myself (%s) - should not happen", remoteID)
|
2015-06-28 08:05:29 -07:00
|
|
|
c.conn.Close()
|
2015-02-19 02:44:37 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-28 13:09:03 -07:00
|
|
|
// If we have a relay connection, and the new incoming connection is
|
|
|
|
// not a relay connection, we should drop that, and prefer the this one.
|
|
|
|
s.mut.RLock()
|
|
|
|
ct, ok := s.connType[remoteID]
|
|
|
|
s.mut.RUnlock()
|
|
|
|
if ok && !ct.IsDirect() && c.connType.IsDirect() {
|
|
|
|
if debugNet {
|
|
|
|
l.Debugln("Switching connections", remoteID)
|
|
|
|
}
|
|
|
|
s.model.Close(remoteID, fmt.Errorf("switching connections"))
|
|
|
|
} else if s.model.ConnectedTo(remoteID) {
|
|
|
|
// We should not already be connected to the other party. TODO: This
|
|
|
|
// could use some better handling. If the old connection is dead but
|
|
|
|
// hasn't timed out yet we may want to drop *that* connection and keep
|
|
|
|
// this one. But in case we are two devices connecting to each other
|
|
|
|
// in parallel we don't want to do that or we end up with no
|
|
|
|
// connections still established...
|
2015-02-19 02:44:37 -07:00
|
|
|
l.Infof("Connected to already connected device (%s)", remoteID)
|
2015-06-28 08:05:29 -07:00
|
|
|
c.conn.Close()
|
2015-02-19 02:44:37 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-25 03:31:10 -07:00
|
|
|
for deviceID, deviceCfg := range s.cfg.Devices() {
|
2015-02-19 02:44:37 -07:00
|
|
|
if deviceID == remoteID {
|
|
|
|
// Verify the name on the certificate. By default we set it to
|
|
|
|
// "syncthing" when generating, but the user may have replaced
|
|
|
|
// the certificate and used another name.
|
|
|
|
certName := deviceCfg.CertName
|
|
|
|
if certName == "" {
|
|
|
|
certName = tlsDefaultCommonName
|
|
|
|
}
|
|
|
|
err := remoteCert.VerifyHostname(certName)
|
|
|
|
if err != nil {
|
|
|
|
// Incorrect certificate name is something the user most
|
|
|
|
// likely wants to know about, since it's an advanced
|
|
|
|
// config. Warn instead of Info.
|
2015-06-28 08:05:29 -07:00
|
|
|
l.Warnf("Bad certificate from %s (%v): %v", remoteID, c.conn.RemoteAddr(), err)
|
|
|
|
c.conn.Close()
|
2015-02-19 02:44:37 -07:00
|
|
|
continue next
|
|
|
|
}
|
|
|
|
|
2015-03-08 11:36:59 -07:00
|
|
|
// If rate limiting is set, and based on the address we should
|
|
|
|
// limit the connection, then we wrap it in a limiter.
|
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
limit := s.shouldLimit(c.conn.RemoteAddr())
|
2015-03-08 11:36:59 -07:00
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
wr := io.Writer(c.conn)
|
2015-03-08 11:36:59 -07:00
|
|
|
if limit && writeRateLimit != nil {
|
2015-06-28 08:05:29 -07:00
|
|
|
wr = &limitedWriter{c.conn, writeRateLimit}
|
2015-02-19 02:44:37 -07:00
|
|
|
}
|
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
rd := io.Reader(c.conn)
|
2015-03-08 11:36:59 -07:00
|
|
|
if limit && readRateLimit != nil {
|
2015-06-28 08:05:29 -07:00
|
|
|
rd = &limitedReader{c.conn, readRateLimit}
|
2015-02-19 02:44:37 -07:00
|
|
|
}
|
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
name := fmt.Sprintf("%s-%s (%s)", c.conn.LocalAddr(), c.conn.RemoteAddr(), c.connType)
|
2015-04-25 03:31:10 -07:00
|
|
|
protoConn := protocol.NewConnection(remoteID, rd, wr, s.model, name, deviceCfg.Compression)
|
2015-02-19 02:44:37 -07:00
|
|
|
|
|
|
|
l.Infof("Established secure connection to %s at %s", remoteID, name)
|
|
|
|
if debugNet {
|
2015-06-28 08:05:29 -07:00
|
|
|
l.Debugf("cipher suite: %04X in lan: %t", c.conn.ConnectionState().CipherSuite, !limit)
|
2015-02-19 02:44:37 -07:00
|
|
|
}
|
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
s.model.AddConnection(model.Connection{
|
|
|
|
c.conn,
|
|
|
|
protoConn,
|
|
|
|
c.connType,
|
|
|
|
})
|
|
|
|
s.mut.Lock()
|
|
|
|
s.connType[remoteID] = c.connType
|
|
|
|
s.mut.Unlock()
|
2015-02-19 02:44:37 -07:00
|
|
|
continue next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 03:31:10 -07:00
|
|
|
if !s.cfg.IgnoredDevice(remoteID) {
|
2015-02-19 02:44:37 -07:00
|
|
|
events.Default.Log(events.DeviceRejected, map[string]string{
|
|
|
|
"device": remoteID.String(),
|
2015-06-28 08:05:29 -07:00
|
|
|
"address": c.conn.RemoteAddr().String(),
|
2015-02-19 02:44:37 -07:00
|
|
|
})
|
2015-06-28 08:05:29 -07:00
|
|
|
l.Infof("Connection from %s (%s) with unknown device ID %s", c.conn.RemoteAddr(), c.connType, remoteID)
|
2015-02-19 02:44:37 -07:00
|
|
|
} else {
|
2015-06-28 08:05:29 -07:00
|
|
|
l.Infof("Connection from %s (%s) with ignored device ID %s", c.conn.RemoteAddr(), c.connType, remoteID)
|
2015-02-19 02:44:37 -07:00
|
|
|
}
|
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
c.conn.Close()
|
2015-02-19 02:44:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 03:31:10 -07:00
|
|
|
func (s *connectionSvc) connect() {
|
2015-02-19 02:44:37 -07:00
|
|
|
delay := time.Second
|
|
|
|
for {
|
|
|
|
nextDevice:
|
2015-04-25 03:31:10 -07:00
|
|
|
for deviceID, deviceCfg := range s.cfg.Devices() {
|
2015-02-19 02:44:37 -07:00
|
|
|
if deviceID == myID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-28 13:09:03 -07:00
|
|
|
connected := s.model.ConnectedTo(deviceID)
|
|
|
|
|
|
|
|
s.mut.RLock()
|
|
|
|
ct, ok := s.connType[deviceID]
|
|
|
|
s.mut.RUnlock()
|
|
|
|
if connected && ok && ct.IsDirect() {
|
2015-02-19 02:44:37 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var addrs []string
|
2015-06-28 13:09:03 -07:00
|
|
|
var relays []string
|
2015-02-19 02:44:37 -07:00
|
|
|
for _, addr := range deviceCfg.Addresses {
|
|
|
|
if addr == "dynamic" {
|
|
|
|
if discoverer != nil {
|
2015-06-28 13:09:03 -07:00
|
|
|
t, r := discoverer.Lookup(deviceID)
|
|
|
|
relays = append(relays, r...)
|
2015-02-19 02:44:37 -07:00
|
|
|
if len(t) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs = append(addrs, t...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
2015-06-23 05:55:30 -07:00
|
|
|
uri, err := url.Parse(addr)
|
2015-02-19 02:44:37 -07:00
|
|
|
if err != nil {
|
2015-06-23 05:55:30 -07:00
|
|
|
l.Infoln("Failed to parse connection url:", addr, err)
|
2015-02-19 02:44:37 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
dialer, ok := dialers[uri.Scheme]
|
|
|
|
if !ok {
|
|
|
|
l.Infoln("Unknown address schema", uri.String())
|
2015-02-19 02:44:37 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
if debugNet {
|
|
|
|
l.Debugln("dial", deviceCfg.DeviceID, uri.String())
|
|
|
|
}
|
|
|
|
conn, err := dialer(uri, s.tlsCfg)
|
2015-02-19 02:44:37 -07:00
|
|
|
if err != nil {
|
2015-06-23 05:55:30 -07:00
|
|
|
if debugNet {
|
|
|
|
l.Debugln("dial failed", deviceCfg.DeviceID, uri.String(), err)
|
|
|
|
}
|
2015-02-19 02:44:37 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-28 13:09:03 -07:00
|
|
|
if connected {
|
|
|
|
s.model.Close(deviceID, fmt.Errorf("switching connections"))
|
|
|
|
}
|
|
|
|
|
2015-06-28 08:05:29 -07:00
|
|
|
s.conns <- intermediateConnection{
|
|
|
|
conn, model.ConnectionTypeBasicDial,
|
|
|
|
}
|
2015-02-19 02:44:37 -07:00
|
|
|
continue nextDevice
|
|
|
|
}
|
2015-06-28 13:09:03 -07:00
|
|
|
|
|
|
|
// Only connect via relays if not already connected
|
|
|
|
// Also, do not set lastRelayCheck time if we have no relays,
|
|
|
|
// as otherwise when we do discover relays, we might have to
|
|
|
|
// wait up to RelayReconnectIntervalM to connect again.
|
|
|
|
if connected || len(relays) == 0 {
|
|
|
|
continue nextDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
reconIntv := time.Duration(s.cfg.Options().RelayReconnectIntervalM) * time.Minute
|
|
|
|
if last, ok := s.lastRelayCheck[deviceID]; ok && time.Since(last) < reconIntv {
|
|
|
|
if debugNet {
|
|
|
|
l.Debugln("Skipping connecting via relay to", deviceID, "last checked at", last)
|
|
|
|
}
|
|
|
|
continue nextDevice
|
|
|
|
} else if debugNet {
|
|
|
|
l.Debugln("Trying relay connections to", deviceID, relays)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.lastRelayCheck[deviceID] = time.Now()
|
|
|
|
|
|
|
|
for _, addr := range relays {
|
|
|
|
uri, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
l.Infoln("Failed to parse relay connection url:", addr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
inv, err := client.GetInvitationFromRelay(uri, deviceID, s.tlsCfg.Certificates)
|
|
|
|
if err != nil {
|
|
|
|
if debugNet {
|
|
|
|
l.Debugf("Failed to get invitation for %s from %s: %v", deviceID, uri, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if debugNet {
|
|
|
|
l.Debugln("Succesfully retrieved relay invitation", inv, "from", uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := client.JoinSession(inv)
|
|
|
|
if err != nil {
|
|
|
|
if debugNet {
|
|
|
|
l.Debugf("Failed to join relay session %s: %v", inv, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if debugNet {
|
|
|
|
l.Debugln("Sucessfully joined relay session", inv)
|
|
|
|
}
|
|
|
|
|
|
|
|
setTCPOptions(conn.(*net.TCPConn))
|
|
|
|
|
|
|
|
var tc *tls.Conn
|
|
|
|
|
|
|
|
if inv.ServerSocket {
|
|
|
|
tc = tls.Server(conn, s.tlsCfg)
|
|
|
|
} else {
|
|
|
|
tc = tls.Client(conn, s.tlsCfg)
|
|
|
|
}
|
|
|
|
err = tc.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
l.Infof("TLS handshake (BEP/relay %s): %v", inv, err)
|
|
|
|
tc.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.conns <- intermediateConnection{
|
|
|
|
tc, model.ConnectionTypeRelayDial,
|
|
|
|
}
|
|
|
|
continue nextDevice
|
|
|
|
}
|
2015-02-19 02:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(delay)
|
|
|
|
delay *= 2
|
2015-04-25 03:31:10 -07:00
|
|
|
if maxD := time.Duration(s.cfg.Options().ReconnectIntervalS) * time.Second; delay > maxD {
|
2015-02-19 02:44:37 -07:00
|
|
|
delay = maxD
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 03:31:10 -07:00
|
|
|
func (s *connectionSvc) shouldLimit(addr net.Addr) bool {
|
|
|
|
if s.cfg.Options().LimitBandwidthInLan {
|
2015-03-08 11:36:59 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpaddr, ok := addr.(*net.TCPAddr)
|
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, lan := range lans {
|
|
|
|
if lan.Contains(tcpaddr.IP) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !tcpaddr.IP.IsLoopback()
|
|
|
|
}
|
2015-06-03 00:47:39 -07:00
|
|
|
|
|
|
|
func (s *connectionSvc) VerifyConfiguration(from, to config.Configuration) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *connectionSvc) CommitConfiguration(from, to config.Configuration) bool {
|
|
|
|
// We require a restart if a device as been removed.
|
|
|
|
|
|
|
|
newDevices := make(map[protocol.DeviceID]bool, len(to.Devices))
|
|
|
|
for _, dev := range to.Devices {
|
|
|
|
newDevices[dev.DeviceID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dev := range from.Devices {
|
|
|
|
if !newDevices[dev.DeviceID] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2015-06-23 05:55:30 -07:00
|
|
|
|
|
|
|
func setTCPOptions(conn *net.TCPConn) {
|
|
|
|
var err error
|
|
|
|
if err = conn.SetLinger(0); err != nil {
|
|
|
|
l.Infoln(err)
|
|
|
|
}
|
|
|
|
if err = conn.SetNoDelay(false); err != nil {
|
|
|
|
l.Infoln(err)
|
|
|
|
}
|
|
|
|
if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
|
|
|
|
l.Infoln(err)
|
|
|
|
}
|
|
|
|
if err = conn.SetKeepAlive(true); err != nil {
|
|
|
|
l.Infoln(err)
|
|
|
|
}
|
|
|
|
}
|