syncthing/cmd/relaysrv/session.go

327 lines
7.0 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"
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"
"github.com/juju/ratelimit"
"github.com/syncthing/syncthing/lib/relay/protocol"
2015-06-28 00:52:01 +00:00
syncthingprotocol "github.com/syncthing/syncthing/lib/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 *ratelimit.Bucket) *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 int64)
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[:],
2015-06-28 00:52:01 +00:00
Key: []byte(s.clientkey),
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[:],
2015-06-28 00:52:01 +00:00
Key: []byte(s.serverkey),
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(int64(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 *ratelimit.Bucket) func(int64) {
// 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 int64) {
globalRateLimit.Wait(bytes)
}
}
if globalRateLimit == nil {
// We only have a session limiter
return func(bytes int64) {
sessionRateLimit.Wait(bytes)
}
}
// We have both. Queue the bytes on both the global and session specific
// rate limiters. Wait for both in parallell, so that the actual send
// happens when both conditions are satisfied. In practice this just means
// wait the longer of the two times.
return func(bytes int64) {
t0 := sessionRateLimit.Take(bytes)
t1 := globalRateLimit.Take(bytes)
if t0 > t1 {
time.Sleep(t0)
} else {
time.Sleep(t1)
}
}
}