syncthing/cmd/strelaysrv/session.go

354 lines
7.6 KiB
Go
Raw Normal View History

2016-06-02 12:16:02 +00:00
// Copyright (C) 2015 Audrius Butkevicius and Contributors.
2015-06-24 11:39:46 +00:00
package main
import (
"crypto/rand"
2015-06-28 00:52:01 +00:00
"encoding/hex"
"fmt"
"log"
"math"
2015-06-24 11:39:46 +00:00
"net"
"sync"
2015-08-20 10:59:44 +00:00
"sync/atomic"
2015-06-24 11:39:46 +00:00
"time"
"golang.org/x/time/rate"
2015-06-28 00:52:01 +00:00
syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/relay/protocol"
2015-06-24 11:39:46 +00:00
)
var (
sessionMut = sync.RWMutex{}
activeSessions = make([]*session, 0)
pendingSessions = make(map[string]*session, 0)
numProxies int64
bytesProxied int64
2015-06-24 11:39:46 +00:00
)
func newSession(serverid, clientid syncthingprotocol.DeviceID, sessionRateLimit, globalRateLimit *rate.Limiter) *session {
2015-06-24 11:39:46 +00:00
serverkey := make([]byte, 32)
_, err := rand.Read(serverkey)
if err != nil {
return nil
}
clientkey := make([]byte, 32)
_, err = rand.Read(clientkey)
if err != nil {
return nil
}
2015-06-28 00:52:01 +00:00
ses := &session{
serverkey: serverkey,
serverid: serverid,
2015-06-28 00:52:01 +00:00
clientkey: clientkey,
clientid: clientid,
rateLimit: makeRateLimitFunc(sessionRateLimit, globalRateLimit),
connsChan: make(chan net.Conn),
conns: make([]net.Conn, 0, 2),
2015-06-24 11:39:46 +00:00
}
2015-06-28 00:52:01 +00:00
if debug {
log.Println("New session", ses)
}
sessionMut.Lock()
pendingSessions[string(ses.serverkey)] = ses
pendingSessions[string(ses.clientkey)] = ses
2015-06-28 00:52:01 +00:00
sessionMut.Unlock()
return ses
2015-06-24 11:39:46 +00:00
}
func findSession(key string) *session {
2015-06-28 00:52:01 +00:00
sessionMut.Lock()
defer sessionMut.Unlock()
ses, ok := pendingSessions[key]
2015-06-24 11:39:46 +00:00
if !ok {
return nil
}
delete(pendingSessions, key)
return ses
}
func dropSessions(id syncthingprotocol.DeviceID) {
sessionMut.RLock()
for _, session := range activeSessions {
if session.HasParticipant(id) {
if debug {
log.Println("Dropping session", session, "involving", id)
}
session.CloseConns()
}
}
sessionMut.RUnlock()
}
func hasSessions(id syncthingprotocol.DeviceID) bool {
sessionMut.RLock()
has := false
for _, session := range activeSessions {
if session.HasParticipant(id) {
has = true
break
}
}
sessionMut.RUnlock()
return has
}
type session struct {
mut sync.Mutex
serverkey []byte
serverid syncthingprotocol.DeviceID
clientkey []byte
clientid syncthingprotocol.DeviceID
rateLimit func(bytes int)
connsChan chan net.Conn
conns []net.Conn
2015-06-24 11:39:46 +00:00
}
2015-06-28 00:52:01 +00:00
func (s *session) AddConnection(conn net.Conn) bool {
if debug {
log.Println("New connection for", s, "from", conn.RemoteAddr())
}
2015-06-24 11:39:46 +00:00
select {
case s.connsChan <- conn:
2015-06-28 00:52:01 +00:00
return true
2015-06-24 11:39:46 +00:00
default:
}
2015-06-28 00:52:01 +00:00
return false
2015-06-24 11:39:46 +00:00
}
2015-06-28 00:52:01 +00:00
func (s *session) Serve() {
2015-06-24 11:39:46 +00:00
timedout := time.After(messageTimeout)
2015-06-28 00:52:01 +00:00
if debug {
log.Println("Session", s, "serving")
}
2015-06-24 11:39:46 +00:00
for {
select {
case conn := <-s.connsChan:
s.mut.Lock()
s.conns = append(s.conns, conn)
s.mut.Unlock()
// We're the only ones mutating s.conns, hence we are free to read it.
if len(s.conns) < 2 {
2015-06-24 11:39:46 +00:00
continue
}
close(s.connsChan)
2015-06-24 11:39:46 +00:00
2015-06-28 00:52:01 +00:00
if debug {
log.Println("Session", s, "starting between", s.conns[0].RemoteAddr(), "and", s.conns[1].RemoteAddr())
2015-06-28 00:52:01 +00:00
}
2015-06-24 11:39:46 +00:00
2015-06-28 00:52:01 +00:00
wg := sync.WaitGroup{}
2015-06-24 11:39:46 +00:00
wg.Add(2)
2015-08-20 12:02:52 +00:00
var err0 error
2015-06-28 00:52:01 +00:00
go func() {
err0 = s.proxy(s.conns[0], s.conns[1])
2015-06-28 00:52:01 +00:00
wg.Done()
}()
2015-08-20 12:02:52 +00:00
var err1 error
2015-06-28 00:52:01 +00:00
go func() {
err1 = s.proxy(s.conns[1], s.conns[0])
2015-06-28 00:52:01 +00:00
wg.Done()
}()
2015-06-24 11:39:46 +00:00
sessionMut.Lock()
activeSessions = append(activeSessions, s)
sessionMut.Unlock()
2015-06-24 11:39:46 +00:00
wg.Wait()
2015-06-28 00:52:01 +00:00
if debug {
2015-08-20 12:02:52 +00:00
log.Println("Session", s, "ended, outcomes:", err0, "and", err1)
2015-06-28 00:52:01 +00:00
}
goto done
2015-08-20 12:02:52 +00:00
2015-06-24 11:39:46 +00:00
case <-timedout:
2015-06-28 00:52:01 +00:00
if debug {
log.Println("Session", s, "timed out")
2015-06-24 11:39:46 +00:00
}
2015-06-28 00:52:01 +00:00
goto done
2015-06-24 11:39:46 +00:00
}
}
2015-06-28 00:52:01 +00:00
done:
// We can end up here in 3 cases:
// 1. Timeout joining, in which case there are potentially entries in pendingSessions
// 2. General session end/timeout, in which case there are entries in activeSessions
// 3. Protocol handler calls dropSession as one of it's clients disconnects.
2015-06-28 00:52:01 +00:00
sessionMut.Lock()
delete(pendingSessions, string(s.serverkey))
delete(pendingSessions, string(s.clientkey))
for i, session := range activeSessions {
if session == s {
l := len(activeSessions) - 1
activeSessions[i] = activeSessions[l]
activeSessions[l] = nil
activeSessions = activeSessions[:l]
}
}
2015-06-28 00:52:01 +00:00
sessionMut.Unlock()
// If we are here because of case 2 or 3, we are potentially closing some or
// all connections a second time.
s.CloseConns()
2015-06-28 00:52:01 +00:00
if debug {
log.Println("Session", s, "stopping")
}
2015-06-24 11:39:46 +00:00
}
func (s *session) GetClientInvitationMessage() protocol.SessionInvitation {
2015-06-28 00:52:01 +00:00
return protocol.SessionInvitation{
From: s.serverid[:],
Key: s.clientkey,
2015-06-28 00:52:01 +00:00
Address: sessionAddress,
Port: sessionPort,
2015-06-24 11:39:46 +00:00
ServerSocket: false,
}
}
func (s *session) GetServerInvitationMessage() protocol.SessionInvitation {
2015-06-28 00:52:01 +00:00
return protocol.SessionInvitation{
From: s.clientid[:],
Key: s.serverkey,
2015-06-28 00:52:01 +00:00
Address: sessionAddress,
Port: sessionPort,
2015-06-24 11:39:46 +00:00
ServerSocket: true,
}
}
func (s *session) HasParticipant(id syncthingprotocol.DeviceID) bool {
return s.clientid == id || s.serverid == id
}
func (s *session) CloseConns() {
s.mut.Lock()
for _, conn := range s.conns {
conn.Close()
}
s.mut.Unlock()
}
func (s *session) proxy(c1, c2 net.Conn) error {
2015-06-28 00:52:01 +00:00
if debug {
log.Println("Proxy", c1.RemoteAddr(), "->", c2.RemoteAddr())
}
2015-08-20 10:59:44 +00:00
atomic.AddInt64(&numProxies, 1)
defer atomic.AddInt64(&numProxies, -1)
buf := make([]byte, 65536)
2015-06-24 11:39:46 +00:00
for {
c1.SetReadDeadline(time.Now().Add(networkTimeout))
n, err := c1.Read(buf)
2015-06-24 11:39:46 +00:00
if err != nil {
2015-06-28 00:52:01 +00:00
return err
}
2015-08-20 10:59:44 +00:00
atomic.AddInt64(&bytesProxied, int64(n))
2015-06-28 00:52:01 +00:00
if debug {
log.Printf("%d bytes from %s to %s", n, c1.RemoteAddr(), c2.RemoteAddr())
2015-06-24 11:39:46 +00:00
}
if s.rateLimit != nil {
s.rateLimit(n)
}
2015-06-24 11:39:46 +00:00
c2.SetWriteDeadline(time.Now().Add(networkTimeout))
_, err = c2.Write(buf[:n])
if err != nil {
2015-06-28 00:52:01 +00:00
return err
2015-06-24 11:39:46 +00:00
}
}
2015-06-28 00:52:01 +00:00
}
func (s *session) String() string {
return fmt.Sprintf("<%s/%s>", hex.EncodeToString(s.clientkey)[:5], hex.EncodeToString(s.serverkey)[:5])
2015-06-24 11:39:46 +00:00
}
func makeRateLimitFunc(sessionRateLimit, globalRateLimit *rate.Limiter) func(int) {
// This may be a case of super duper premature optimization... We build an
// optimized function to do the rate limiting here based on what we need
// to do and then use it in the loop.
if sessionRateLimit == nil && globalRateLimit == nil {
// No limiting needed. We could equally well return a func(int64){} and
// not do a nil check were we use it, but I think the nil check there
// makes it clear that there will be no limiting if none is
// configured...
return nil
}
if sessionRateLimit == nil {
// We only have a global limiter
return func(bytes int) {
take(bytes, globalRateLimit)
}
}
if globalRateLimit == nil {
// We only have a session limiter
return func(bytes int) {
take(bytes, sessionRateLimit)
}
}
// We have both. Queue the bytes on both the global and session specific
// rate limiters.
return func(bytes int) {
take(bytes, sessionRateLimit, globalRateLimit)
}
}
// take is a utility function to consume tokens from a set of rate.Limiters.
// Tokens are consumed in parallel on all limiters, respecting their
// individual burst sizes.
func take(tokens int, ls ...*rate.Limiter) {
// minBurst is the smallest burst size supported by all limiters.
minBurst := int(math.MaxInt32)
for _, l := range ls {
if burst := l.Burst(); burst < minBurst {
minBurst = burst
}
}
for tokens > 0 {
// chunk is how many tokens we can consume at a time
chunk := tokens
if chunk > minBurst {
chunk = minBurst
}
// maxDelay is the longest delay mandated by any of the limiters for
// the chosen chunk size.
var maxDelay time.Duration
for _, l := range ls {
res := l.ReserveN(time.Now(), chunk)
if del := res.Delay(); del > maxDelay {
maxDelay = del
}
}
time.Sleep(maxDelay)
tokens -= chunk
}
}