2021-11-23 12:45:59 +00:00
|
|
|
package datagramsession
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2021-12-02 11:02:27 +00:00
|
|
|
"time"
|
2021-11-23 12:45:59 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2021-12-02 11:02:27 +00:00
|
|
|
const (
|
|
|
|
defaultCloseIdleAfter = time.Second * 210
|
|
|
|
)
|
|
|
|
|
2021-11-23 12:45:59 +00:00
|
|
|
// Each Session is a bidirectional pipe of datagrams between transport and dstConn
|
|
|
|
// Currently the only implementation of transport is quic DatagramMuxer
|
|
|
|
// Destination can be a connection with origin or with eyeball
|
|
|
|
// When the destination is origin:
|
|
|
|
// - Datagrams from edge are read by Manager from the transport. Manager finds the corresponding Session and calls the
|
|
|
|
// write method of the Session to send to origin
|
|
|
|
// - Datagrams from origin are read from conn and SentTo transport. Transport will return them to eyeball
|
|
|
|
// When the destination is eyeball:
|
|
|
|
// - Datagrams from eyeball are read from conn and SentTo transport. Transport will send them to cloudflared
|
|
|
|
// - Datagrams from cloudflared are read by Manager from the transport. Manager finds the corresponding Session and calls the
|
|
|
|
// write method of the Session to send to eyeball
|
|
|
|
type Session struct {
|
|
|
|
id uuid.UUID
|
|
|
|
transport transport
|
|
|
|
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
|
|
|
|
doneChan chan struct{}
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSession(id uuid.UUID, transport transport, dstConn io.ReadWriteCloser) *Session {
|
|
|
|
return &Session{
|
|
|
|
id: id,
|
|
|
|
transport: transport,
|
|
|
|
dstConn: dstConn,
|
2021-12-02 11:02:27 +00:00
|
|
|
// activeAtChan has low capacity. It can be full when there are many concurrent read/write. markActive() will
|
|
|
|
// drop instead of blocking because last active time only needs to be an approximation
|
|
|
|
activeAtChan: make(chan time.Time, 2),
|
|
|
|
doneChan: make(chan struct{}),
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 11:02:27 +00:00
|
|
|
func (s *Session) Serve(ctx context.Context, closeAfterIdle time.Duration) error {
|
2021-11-23 12:45:59 +00:00
|
|
|
serveCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2021-12-02 11:02:27 +00:00
|
|
|
go s.waitForCloseCondition(serveCtx, closeAfterIdle)
|
2021-11-23 12:45:59 +00:00
|
|
|
// 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-02 11:02:27 +00:00
|
|
|
readBuffer := make([]byte, s.transport.MTU())
|
|
|
|
for {
|
|
|
|
if err := s.dstToTransport(readBuffer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) waitForCloseCondition(ctx context.Context, closeAfterIdle time.Duration) {
|
|
|
|
if closeAfterIdle == 0 {
|
|
|
|
// provide deafult is caller doesn't specify one
|
|
|
|
closeAfterIdle = defaultCloseIdleAfter
|
|
|
|
}
|
|
|
|
// Closing dstConn cancels read so Serve function can return
|
|
|
|
defer s.dstConn.Close()
|
|
|
|
|
|
|
|
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():
|
|
|
|
return
|
|
|
|
case <-s.doneChan:
|
|
|
|
return
|
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-02 11:02:27 +00:00
|
|
|
return
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) dstToTransport(buffer []byte) error {
|
|
|
|
n, err := s.dstConn.Read(buffer)
|
|
|
|
s.markActive()
|
|
|
|
if n > 0 {
|
|
|
|
if err := s.transport.SendTo(s.id, buffer[:n]); err != nil {
|
2021-11-23 12:45:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 11:02:27 +00:00
|
|
|
return 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()
|
2021-11-23 12:45:59 +00:00
|
|
|
return s.dstConn.Write(payload)
|
|
|
|
}
|
|
|
|
|
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-11-23 12:45:59 +00:00
|
|
|
func (s *Session) close() {
|
|
|
|
close(s.doneChan)
|
|
|
|
}
|