2021-11-23 12:45:59 +00:00
|
|
|
package datagramsession
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-14 22:52:47 +00:00
|
|
|
"fmt"
|
2021-11-23 12:45:59 +00:00
|
|
|
"io"
|
2022-08-01 12:48:33 +00:00
|
|
|
"net"
|
2021-12-02 11:02:27 +00:00
|
|
|
"time"
|
2021-11-23 12:45:59 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2021-12-22 23:18:22 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-08-17 15:46:49 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
2021-11-23 12:45:59 +00:00
|
|
|
)
|
|
|
|
|
2021-12-02 11:02:27 +00:00
|
|
|
const (
|
|
|
|
defaultCloseIdleAfter = time.Second * 210
|
|
|
|
)
|
|
|
|
|
2021-12-14 22:52:47 +00:00
|
|
|
func SessionIdleErr(timeout time.Duration) error {
|
|
|
|
return fmt.Errorf("session idle for %v", timeout)
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:46:49 +00:00
|
|
|
type transportSender func(session *packet.Session) error
|
2022-08-01 12:48:33 +00:00
|
|
|
|
2021-12-22 23:18:22 +00:00
|
|
|
// Session is a bidirectional pipe of datagrams between transport and dstConn
|
2021-11-23 12:45:59 +00:00
|
|
|
// Destination can be a connection with origin or with eyeball
|
|
|
|
// When the destination is origin:
|
2022-08-01 12:48:33 +00:00
|
|
|
// - Manager receives datagrams from receiveChan and calls the transportToDst method of the Session to send to origin
|
|
|
|
// - Datagrams from origin are read from conn and Send to transport using the transportSender callback. Transport will return them to eyeball
|
2021-11-23 12:45:59 +00:00
|
|
|
// When the destination is eyeball:
|
2022-08-01 12:48:33 +00:00
|
|
|
// - Datagrams from eyeball are read from conn and Send to transport. Transport will send them to cloudflared using the transportSender callback.
|
|
|
|
// - Manager receives datagrams from receiveChan and calls the transportToDst method of the Session to send to the eyeball
|
2021-11-23 12:45:59 +00:00
|
|
|
type Session struct {
|
2022-08-01 12:48:33 +00:00
|
|
|
ID uuid.UUID
|
|
|
|
sendFunc transportSender
|
|
|
|
dstConn io.ReadWriteCloser
|
2021-12-02 11:02:27 +00:00
|
|
|
// activeAtChan is used to communicate the last read/write time
|
|
|
|
activeAtChan chan time.Time
|
2021-12-14 22:52:47 +00:00
|
|
|
closeChan chan error
|
2021-12-22 23:18:22 +00:00
|
|
|
log *zerolog.Logger
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 22:52:47 +00:00
|
|
|
func (s *Session) Serve(ctx context.Context, closeAfterIdle time.Duration) (closedByRemote bool, err error) {
|
|
|
|
go func() {
|
|
|
|
// QUIC implementation copies data to another buffer before returning https://github.com/lucas-clemente/quic-go/blob/v0.24.0/session.go#L1967-L1975
|
|
|
|
// This makes it safe to share readBuffer between iterations
|
2021-12-22 23:18:22 +00:00
|
|
|
const maxPacketSize = 1500
|
|
|
|
readBuffer := make([]byte, maxPacketSize)
|
2021-12-14 22:52:47 +00:00
|
|
|
for {
|
2022-08-01 12:48:33 +00:00
|
|
|
if closeSession, err := s.dstToTransport(readBuffer); err != nil {
|
|
|
|
if err != net.ErrClosed {
|
|
|
|
s.log.Error().Err(err).Msg("Failed to send session payload from destination to transport")
|
|
|
|
} else {
|
|
|
|
s.log.Debug().Msg("Session cannot read from destination because the connection is closed")
|
|
|
|
}
|
|
|
|
if closeSession {
|
|
|
|
s.closeChan <- err
|
|
|
|
return
|
|
|
|
}
|
2021-12-14 22:52:47 +00:00
|
|
|
}
|
2021-12-02 11:02:27 +00:00
|
|
|
}
|
2021-12-14 22:52:47 +00:00
|
|
|
}()
|
|
|
|
err = s.waitForCloseCondition(ctx, closeAfterIdle)
|
|
|
|
if closeSession, ok := err.(*errClosedSession); ok {
|
|
|
|
closedByRemote = closeSession.byRemote
|
2021-12-02 11:02:27 +00:00
|
|
|
}
|
2021-12-14 22:52:47 +00:00
|
|
|
return closedByRemote, err
|
2021-12-02 11:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 22:52:47 +00:00
|
|
|
func (s *Session) waitForCloseCondition(ctx context.Context, closeAfterIdle time.Duration) error {
|
|
|
|
// Closing dstConn cancels read so dstToTransport routine in Serve() can return
|
|
|
|
defer s.dstConn.Close()
|
2021-12-02 11:02:27 +00:00
|
|
|
if closeAfterIdle == 0 {
|
|
|
|
// provide deafult is caller doesn't specify one
|
|
|
|
closeAfterIdle = defaultCloseIdleAfter
|
|
|
|
}
|
|
|
|
|
|
|
|
checkIdleFreq := closeAfterIdle / 8
|
|
|
|
checkIdleTicker := time.NewTicker(checkIdleFreq)
|
|
|
|
defer checkIdleTicker.Stop()
|
|
|
|
|
|
|
|
activeAt := time.Now()
|
2021-11-23 12:45:59 +00:00
|
|
|
for {
|
2021-12-02 11:02:27 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-12-14 22:52:47 +00:00
|
|
|
return ctx.Err()
|
|
|
|
case reason := <-s.closeChan:
|
|
|
|
return reason
|
2021-12-13 13:35:40 +00:00
|
|
|
// TODO: TUN-5423 evaluate if using atomic is more efficient
|
|
|
|
case now := <-checkIdleTicker.C:
|
2021-12-02 11:02:27 +00:00
|
|
|
// The session is considered inactive if current time is after (last active time + allowed idle time)
|
2021-12-13 13:35:40 +00:00
|
|
|
if now.After(activeAt.Add(closeAfterIdle)) {
|
2021-12-14 22:52:47 +00:00
|
|
|
return SessionIdleErr(closeAfterIdle)
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
2021-12-02 11:02:27 +00:00
|
|
|
case activeAt = <-s.activeAtChan: // Update last active time
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
2021-12-02 11:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 12:48:33 +00:00
|
|
|
func (s *Session) dstToTransport(buffer []byte) (closeSession bool, err error) {
|
2021-12-02 11:02:27 +00:00
|
|
|
n, err := s.dstConn.Read(buffer)
|
|
|
|
s.markActive()
|
2022-01-19 18:24:16 +00:00
|
|
|
// https://pkg.go.dev/io#Reader suggests caller should always process n > 0 bytes
|
2022-08-01 12:48:33 +00:00
|
|
|
if n > 0 || err == nil {
|
2022-08-17 15:46:49 +00:00
|
|
|
session := packet.Session{
|
|
|
|
ID: s.ID,
|
|
|
|
Payload: buffer[:n],
|
|
|
|
}
|
|
|
|
if sendErr := s.sendFunc(&session); sendErr != nil {
|
2022-08-01 12:48:33 +00:00
|
|
|
return false, sendErr
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-01 12:48:33 +00:00
|
|
|
return err != nil, err
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 11:02:27 +00:00
|
|
|
func (s *Session) transportToDst(payload []byte) (int, error) {
|
|
|
|
s.markActive()
|
2022-08-01 12:48:33 +00:00
|
|
|
n, err := s.dstConn.Write(payload)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Err(err).Msg("Failed to write payload to session")
|
|
|
|
}
|
|
|
|
return n, err
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 11:02:27 +00:00
|
|
|
// Sends the last active time to the idle checker loop without blocking. activeAtChan will only be full when there
|
|
|
|
// are many concurrent read/write. It is fine to lose some precision
|
|
|
|
func (s *Session) markActive() {
|
|
|
|
select {
|
|
|
|
case s.activeAtChan <- time.Now():
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 22:52:47 +00:00
|
|
|
func (s *Session) close(err *errClosedSession) {
|
|
|
|
s.closeChan <- err
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|