syncthing/cmd/relaysrv/protocol_listener.go

212 lines
4.7 KiB
Go
Raw Normal View History

2015-06-24 13:39:46 +02:00
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
package main
import (
"crypto/tls"
"log"
"net"
2015-06-28 02:52:01 +02:00
"sync"
2015-06-24 13:39:46 +02:00
"time"
syncthingprotocol "github.com/syncthing/protocol"
"github.com/syncthing/relaysrv/protocol"
)
2015-06-28 02:52:01 +02:00
var (
outboxesMut = sync.RWMutex{}
outboxes = make(map[syncthingprotocol.DeviceID]chan interface{})
)
2015-06-24 13:39:46 +02:00
func protocolListener(addr string, config *tls.Config) {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalln(err)
}
for {
conn, err := listener.Accept()
2015-06-28 02:52:01 +02:00
setTCPOptions(conn)
2015-06-24 13:39:46 +02:00
if err != nil {
if debug {
log.Println(err)
}
continue
}
if debug {
log.Println("Protocol listener accepted connection from", conn.RemoteAddr())
}
go protocolConnectionHandler(conn, config)
}
}
func protocolConnectionHandler(tcpConn net.Conn, config *tls.Config) {
conn := tls.Server(tcpConn, config)
2015-06-28 02:52:01 +02:00
err := conn.Handshake()
2015-06-24 13:39:46 +02:00
if err != nil {
2015-06-28 02:52:01 +02:00
if debug {
log.Println("Protocol connection TLS handshake:", conn.RemoteAddr(), err)
}
2015-06-24 13:39:46 +02:00
conn.Close()
return
}
state := conn.ConnectionState()
if (!state.NegotiatedProtocolIsMutual || state.NegotiatedProtocol != protocol.ProtocolName) && debug {
log.Println("Protocol negotiation error")
}
certs := state.PeerCertificates
if len(certs) != 1 {
2015-06-28 02:52:01 +02:00
if debug {
log.Println("Certificate list error")
}
2015-06-24 13:39:46 +02:00
conn.Close()
return
}
2015-06-28 02:52:01 +02:00
id := syncthingprotocol.NewDeviceID(certs[0].Raw)
2015-06-24 13:39:46 +02:00
2015-06-28 02:52:01 +02:00
messages := make(chan interface{})
errors := make(chan error, 1)
outbox := make(chan interface{})
2015-06-24 13:39:46 +02:00
2015-06-28 02:52:01 +02:00
go func(conn net.Conn, message chan<- interface{}, errors chan<- error) {
for {
msg, err := protocol.ReadMessage(conn)
if err != nil {
errors <- err
return
}
messages <- msg
}
}(conn, messages, errors)
2015-06-24 13:39:46 +02:00
pingTicker := time.NewTicker(pingInterval)
2015-06-28 02:52:01 +02:00
timeoutTicker := time.NewTimer(networkTimeout)
2015-06-24 13:39:46 +02:00
joined := false
for {
select {
2015-06-28 02:52:01 +02:00
case message := <-messages:
timeoutTicker.Reset(networkTimeout)
if debug {
log.Printf("Message %T from %s", message, id)
}
switch msg := message.(type) {
case protocol.JoinRelayRequest:
outboxesMut.RLock()
_, ok := outboxes[id]
outboxesMut.RUnlock()
if ok {
protocol.WriteMessage(conn, protocol.ResponseAlreadyConnected)
if debug {
log.Println("Already have a peer with the same ID", id, conn.RemoteAddr())
}
2015-06-24 13:39:46 +02:00
conn.Close()
continue
}
2015-06-28 02:52:01 +02:00
outboxesMut.Lock()
outboxes[id] = outbox
outboxesMut.Unlock()
joined = true
protocol.WriteMessage(conn, protocol.ResponseSuccess)
case protocol.ConnectRequest:
requestedPeer := syncthingprotocol.DeviceIDFromBytes(msg.ID)
outboxesMut.RLock()
peerOutbox, ok := outboxes[requestedPeer]
outboxesMut.RUnlock()
2015-06-24 13:39:46 +02:00
if !ok {
if debug {
2015-06-28 02:52:01 +02:00
log.Println(id, "is looking", requestedPeer, "which does not exist")
2015-06-24 13:39:46 +02:00
}
2015-06-28 02:52:01 +02:00
protocol.WriteMessage(conn, protocol.ResponseNotFound)
2015-06-24 13:39:46 +02:00
conn.Close()
continue
}
ses := newSession()
2015-06-28 02:52:01 +02:00
go ses.Serve()
clientInvitation := ses.GetClientInvitationMessage(requestedPeer)
serverInvitation := ses.GetServerInvitationMessage(id)
if err := protocol.WriteMessage(conn, clientInvitation); err != nil {
if debug {
log.Printf("Error sending invitation from %s to client: %s", id, err)
}
2015-06-24 13:39:46 +02:00
conn.Close()
continue
}
2015-06-28 02:52:01 +02:00
peerOutbox <- serverInvitation
2015-06-24 13:39:46 +02:00
2015-06-28 02:52:01 +02:00
if debug {
log.Println("Sent invitation from", id, "to", requestedPeer)
2015-06-24 13:39:46 +02:00
}
conn.Close()
2015-06-28 02:52:01 +02:00
case protocol.Pong:
default:
if debug {
log.Printf("Unknown message %s: %T", id, message)
}
protocol.WriteMessage(conn, protocol.ResponseUnexpectedMessage)
conn.Close()
}
case err := <-errors:
if debug {
log.Printf("Closing connection %s: %s", id, err)
2015-06-24 13:39:46 +02:00
}
2015-06-28 02:52:01 +02:00
// Potentially closing a second time.
close(outbox)
conn.Close()
2015-07-17 23:04:02 +02:00
// Only delete the outbox if the client join, as it migth be a
// lookup request coming from the same client.
if joined {
outboxesMut.Lock()
delete(outboxes, id)
outboxesMut.Unlock()
}
2015-06-24 13:39:46 +02:00
return
case <-pingTicker.C:
if !joined {
2015-06-28 02:52:01 +02:00
if debug {
log.Println(id, "didn't join within", pingInterval)
}
2015-06-24 13:39:46 +02:00
conn.Close()
continue
}
2015-06-28 02:52:01 +02:00
if err := protocol.WriteMessage(conn, protocol.Ping{}); err != nil {
if debug {
log.Println(id, err)
}
2015-06-24 13:39:46 +02:00
conn.Close()
}
case <-timeoutTicker.C:
2015-06-28 02:52:01 +02:00
// We should receive a error from the reader loop, which will cause
// us to quit this loop.
if debug {
log.Printf("%s timed out", id)
}
2015-06-24 13:39:46 +02:00
conn.Close()
2015-06-28 02:52:01 +02:00
case msg := <-outbox:
2015-06-24 13:39:46 +02:00
if debug {
2015-06-28 02:52:01 +02:00
log.Printf("Sending message %T to %s", msg, id)
2015-06-24 13:39:46 +02:00
}
2015-06-28 02:52:01 +02:00
if err := protocol.WriteMessage(conn, msg); err != nil {
if debug {
log.Println(id, err)
}
2015-06-24 13:39:46 +02:00
conn.Close()
}
}
}
}