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"
|
2017-01-02 11:29:20 +00:00
|
|
|
"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"
|
|
|
|
|
2017-01-02 11:29:20 +00:00
|
|
|
"golang.org/x/time/rate"
|
2015-06-28 00:52:01 +00:00
|
|
|
|
2015-09-22 17:54:02 +00:00
|
|
|
syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
|
2017-01-02 11:29:20 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
2015-06-24 11:39:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-11 21:29:50 +00:00
|
|
|
sessionMut = sync.RWMutex{}
|
|
|
|
activeSessions = make([]*session, 0)
|
2019-02-02 11:09:07 +00:00
|
|
|
pendingSessions = make(map[string]*session)
|
2023-02-07 11:07:34 +00:00
|
|
|
numProxies atomic.Int64
|
|
|
|
bytesProxied atomic.Int64
|
2015-06-24 11:39:46 +00:00
|
|
|
)
|
|
|
|
|
2017-01-02 11:29:20 +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,
|
2015-09-11 21:29:50 +00:00
|
|
|
serverid: serverid,
|
2015-06-28 00:52:01 +00:00
|
|
|
clientkey: clientkey,
|
2015-09-11 21:29:50 +00:00
|
|
|
clientid: clientid,
|
2015-07-20 11:25:08 +00:00
|
|
|
rateLimit: makeRateLimitFunc(sessionRateLimit, globalRateLimit),
|
2015-09-11 21:29:50 +00:00
|
|
|
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()
|
2015-09-11 21:29:50 +00:00
|
|
|
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()
|
2015-09-11 21:29:50 +00:00
|
|
|
ses, ok := pendingSessions[key]
|
2015-06-24 11:39:46 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2015-09-11 21:29:50 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2015-11-21 00:08:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-11 21:29:50 +00:00
|
|
|
type session struct {
|
|
|
|
mut sync.Mutex
|
|
|
|
|
|
|
|
serverkey []byte
|
|
|
|
serverid syncthingprotocol.DeviceID
|
|
|
|
|
|
|
|
clientkey []byte
|
|
|
|
clientid syncthingprotocol.DeviceID
|
|
|
|
|
2017-01-02 11:29:20 +00:00
|
|
|
rateLimit func(bytes int)
|
2015-09-11 21:29:50 +00:00
|
|
|
|
|
|
|
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 {
|
2015-09-11 21:29:50 +00:00
|
|
|
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 {
|
2015-09-11 21:29:50 +00:00
|
|
|
case conn := <-s.connsChan:
|
|
|
|
s.mut.Lock()
|
|
|
|
s.conns = append(s.conns, conn)
|
|
|
|
s.mut.Unlock()
|
2015-11-21 00:08:09 +00:00
|
|
|
// We're the only ones mutating s.conns, hence we are free to read it.
|
2015-09-11 21:29:50 +00:00
|
|
|
if len(s.conns) < 2 {
|
2015-06-24 11:39:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-11 21:29:50 +00:00
|
|
|
close(s.connsChan)
|
2015-06-24 11:39:46 +00:00
|
|
|
|
2015-06-28 00:52:01 +00:00
|
|
|
if debug {
|
2015-09-11 21:29:50 +00:00
|
|
|
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() {
|
2015-09-11 21:29:50 +00:00
|
|
|
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() {
|
2015-09-11 21:29:50 +00:00
|
|
|
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
|
|
|
|
2015-09-11 21:29:50 +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:
|
2015-09-11 21:29:50 +00:00
|
|
|
// 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
|
2017-11-04 07:20:11 +00:00
|
|
|
// 3. Protocol handler calls dropSession as one of its clients disconnects.
|
2015-09-11 21:29:50 +00:00
|
|
|
|
2015-06-28 00:52:01 +00:00
|
|
|
sessionMut.Lock()
|
2015-09-11 21:29:50 +00:00
|
|
|
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()
|
|
|
|
|
2015-09-11 21:29:50 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-09-11 21:29:50 +00:00
|
|
|
func (s *session) GetClientInvitationMessage() protocol.SessionInvitation {
|
2015-06-28 00:52:01 +00:00
|
|
|
return protocol.SessionInvitation{
|
2015-09-11 21:29:50 +00:00
|
|
|
From: s.serverid[:],
|
2016-12-17 14:37:11 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 21:29:50 +00:00
|
|
|
func (s *session) GetServerInvitationMessage() protocol.SessionInvitation {
|
2015-06-28 00:52:01 +00:00
|
|
|
return protocol.SessionInvitation{
|
2015-09-11 21:29:50 +00:00
|
|
|
From: s.clientid[:],
|
2016-12-17 14:37:11 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 21:29:50 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2015-07-20 11:25:08 +00:00
|
|
|
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-07-20 11:25:08 +00:00
|
|
|
|
2023-02-07 11:07:34 +00:00
|
|
|
numProxies.Add(1)
|
|
|
|
defer numProxies.Add(-1)
|
2015-08-20 10:59:44 +00:00
|
|
|
|
2017-08-30 16:52:28 +00:00
|
|
|
buf := make([]byte, networkBufferSize)
|
2015-06-24 11:39:46 +00:00
|
|
|
for {
|
|
|
|
c1.SetReadDeadline(time.Now().Add(networkTimeout))
|
2015-07-20 11:25:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-07 11:07:34 +00:00
|
|
|
bytesProxied.Add(int64(n))
|
2015-08-20 10:59:44 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-20 11:25:08 +00:00
|
|
|
if s.rateLimit != nil {
|
2017-01-02 11:29:20 +00:00
|
|
|
s.rateLimit(n)
|
2015-07-20 11:25:08 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2015-07-20 11:25:08 +00:00
|
|
|
|
2017-01-02 11:29:20 +00:00
|
|
|
func makeRateLimitFunc(sessionRateLimit, globalRateLimit *rate.Limiter) func(int) {
|
2015-07-20 11:25:08 +00:00
|
|
|
// 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
|
2017-01-02 11:29:20 +00:00
|
|
|
return func(bytes int) {
|
|
|
|
take(bytes, globalRateLimit)
|
2015-07-20 11:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if globalRateLimit == nil {
|
|
|
|
// We only have a session limiter
|
2017-01-02 11:29:20 +00:00
|
|
|
return func(bytes int) {
|
|
|
|
take(bytes, sessionRateLimit)
|
2015-07-20 11:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have both. Queue the bytes on both the global and session specific
|
2017-01-02 11:29:20 +00:00
|
|
|
// 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
|
|
|
|
}
|
2015-07-20 11:25:08 +00:00
|
|
|
}
|
2017-01-02 11:29:20 +00:00
|
|
|
|
|
|
|
time.Sleep(maxDelay)
|
|
|
|
tokens -= chunk
|
2015-07-20 11:25:08 +00:00
|
|
|
}
|
|
|
|
}
|