2021-11-23 12:45:59 +00:00
|
|
|
package datagramsession
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-21 09:52:19 +00:00
|
|
|
"fmt"
|
2021-11-23 12:45:59 +00:00
|
|
|
"io"
|
2022-03-28 09:06:28 +00:00
|
|
|
"time"
|
2021-11-23 12:45:59 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/rs/zerolog"
|
2022-08-01 12:48:33 +00:00
|
|
|
|
2023-04-17 22:03:34 +00:00
|
|
|
"github.com/cloudflare/cloudflared/management"
|
2022-08-17 15:46:49 +00:00
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
2021-11-23 12:45:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
requestChanCapacity = 16
|
2022-03-28 09:06:28 +00:00
|
|
|
defaultReqTimeout = time.Second * 5
|
2021-11-23 12:45:59 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 09:52:19 +00:00
|
|
|
var (
|
|
|
|
errSessionManagerClosed = fmt.Errorf("session manager closed")
|
|
|
|
)
|
|
|
|
|
2021-11-23 12:45:59 +00:00
|
|
|
// Manager defines the APIs to manage sessions from the same transport.
|
|
|
|
type Manager interface {
|
|
|
|
// Serve starts the event loop
|
|
|
|
Serve(ctx context.Context) error
|
|
|
|
// RegisterSession starts tracking a session. Caller is responsible for starting the session
|
|
|
|
RegisterSession(ctx context.Context, sessionID uuid.UUID, dstConn io.ReadWriteCloser) (*Session, error)
|
|
|
|
// UnregisterSession stops tracking the session and terminates it
|
2021-12-14 22:52:47 +00:00
|
|
|
UnregisterSession(ctx context.Context, sessionID uuid.UUID, message string, byRemote bool) error
|
2022-05-30 12:42:56 +00:00
|
|
|
// UpdateLogger updates the logger used by the Manager
|
|
|
|
UpdateLogger(log *zerolog.Logger)
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type manager struct {
|
|
|
|
registrationChan chan *registerSessionEvent
|
|
|
|
unregistrationChan chan *unregisterSessionEvent
|
2022-08-01 12:48:33 +00:00
|
|
|
sendFunc transportSender
|
2022-08-17 15:46:49 +00:00
|
|
|
receiveChan <-chan *packet.Session
|
2022-08-01 12:48:33 +00:00
|
|
|
closedChan <-chan struct{}
|
2021-11-23 12:45:59 +00:00
|
|
|
sessions map[uuid.UUID]*Session
|
|
|
|
log *zerolog.Logger
|
2022-03-28 09:06:28 +00:00
|
|
|
// timeout waiting for an API to finish. This can be overriden in test
|
|
|
|
timeout time.Duration
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 15:46:49 +00:00
|
|
|
func NewManager(log *zerolog.Logger, sendF transportSender, receiveChan <-chan *packet.Session) *manager {
|
2021-11-23 12:45:59 +00:00
|
|
|
return &manager{
|
|
|
|
registrationChan: make(chan *registerSessionEvent),
|
|
|
|
unregistrationChan: make(chan *unregisterSessionEvent),
|
2022-08-01 12:48:33 +00:00
|
|
|
sendFunc: sendF,
|
|
|
|
receiveChan: receiveChan,
|
|
|
|
closedChan: make(chan struct{}),
|
|
|
|
sessions: make(map[uuid.UUID]*Session),
|
|
|
|
log: log,
|
|
|
|
timeout: defaultReqTimeout,
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 12:42:56 +00:00
|
|
|
func (m *manager) UpdateLogger(log *zerolog.Logger) {
|
|
|
|
// Benign data race, no problem if the old pointer is read or not concurrently.
|
|
|
|
m.log = log
|
|
|
|
}
|
|
|
|
|
2021-11-23 12:45:59 +00:00
|
|
|
func (m *manager) Serve(ctx context.Context) error {
|
2022-08-01 12:48:33 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
m.shutdownSessions(ctx.Err())
|
|
|
|
return ctx.Err()
|
|
|
|
// receiveChan is buffered, so the transport can read more datagrams from transport while the event loop is
|
|
|
|
// processing other events
|
|
|
|
case datagram := <-m.receiveChan:
|
|
|
|
m.sendToSession(datagram)
|
|
|
|
case registration := <-m.registrationChan:
|
|
|
|
m.registerSession(ctx, registration)
|
|
|
|
case unregistration := <-m.unregistrationChan:
|
|
|
|
m.unregisterSession(unregistration)
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
2022-08-01 12:48:33 +00:00
|
|
|
}
|
2022-04-21 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) shutdownSessions(err error) {
|
|
|
|
if err == nil {
|
|
|
|
err = errSessionManagerClosed
|
|
|
|
}
|
|
|
|
closeSessionErr := &errClosedSession{
|
|
|
|
message: err.Error(),
|
|
|
|
// Usually connection with remote has been closed, so set this to true to skip unregistering from remote
|
|
|
|
byRemote: true,
|
|
|
|
}
|
|
|
|
for _, s := range m.sessions {
|
|
|
|
s.close(closeSessionErr)
|
|
|
|
}
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) RegisterSession(ctx context.Context, sessionID uuid.UUID, originProxy io.ReadWriteCloser) (*Session, error) {
|
2022-03-28 09:06:28 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, m.timeout)
|
|
|
|
defer cancel()
|
2021-11-23 12:45:59 +00:00
|
|
|
event := newRegisterSessionEvent(sessionID, originProxy)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-03-28 09:06:28 +00:00
|
|
|
m.log.Error().Msg("Datagram session registration timeout")
|
2021-11-23 12:45:59 +00:00
|
|
|
return nil, ctx.Err()
|
|
|
|
case m.registrationChan <- event:
|
|
|
|
session := <-event.resultChan
|
|
|
|
return session, nil
|
2022-04-21 09:52:19 +00:00
|
|
|
// Once closedChan is closed, manager won't accept more registration because nothing is
|
|
|
|
// reading from registrationChan and it's an unbuffered channel
|
|
|
|
case <-m.closedChan:
|
|
|
|
return nil, errSessionManagerClosed
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) registerSession(ctx context.Context, registration *registerSessionEvent) {
|
2022-04-21 09:52:19 +00:00
|
|
|
session := m.newSession(registration.sessionID, registration.originProxy)
|
2021-11-23 12:45:59 +00:00
|
|
|
m.sessions[registration.sessionID] = session
|
|
|
|
registration.resultChan <- session
|
2023-06-17 00:07:56 +00:00
|
|
|
incrementUDPSessions()
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 09:52:19 +00:00
|
|
|
func (m *manager) newSession(id uuid.UUID, dstConn io.ReadWriteCloser) *Session {
|
2023-04-17 22:03:34 +00:00
|
|
|
logger := m.log.With().
|
|
|
|
Int(management.EventTypeKey, int(management.UDP)).
|
|
|
|
Str("sessionID", id.String()).Logger()
|
2022-04-21 09:52:19 +00:00
|
|
|
return &Session{
|
2022-08-01 12:48:33 +00:00
|
|
|
ID: id,
|
|
|
|
sendFunc: m.sendFunc,
|
|
|
|
dstConn: dstConn,
|
2022-04-21 09:52:19 +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),
|
|
|
|
// capacity is 2 because close() and dstToTransport routine in Serve() can write to this channel
|
|
|
|
closeChan: make(chan error, 2),
|
2022-08-01 12:48:33 +00:00
|
|
|
log: &logger,
|
2022-04-21 09:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 22:52:47 +00:00
|
|
|
func (m *manager) UnregisterSession(ctx context.Context, sessionID uuid.UUID, message string, byRemote bool) error {
|
2022-03-28 09:06:28 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, m.timeout)
|
|
|
|
defer cancel()
|
2021-12-14 22:52:47 +00:00
|
|
|
event := &unregisterSessionEvent{
|
|
|
|
sessionID: sessionID,
|
|
|
|
err: &errClosedSession{
|
|
|
|
message: message,
|
|
|
|
byRemote: byRemote,
|
|
|
|
},
|
|
|
|
}
|
2021-11-23 12:45:59 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-03-28 09:06:28 +00:00
|
|
|
m.log.Error().Msg("Datagram session unregistration timeout")
|
2021-11-23 12:45:59 +00:00
|
|
|
return ctx.Err()
|
|
|
|
case m.unregistrationChan <- event:
|
|
|
|
return nil
|
2022-04-21 09:52:19 +00:00
|
|
|
case <-m.closedChan:
|
|
|
|
return errSessionManagerClosed
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) unregisterSession(unregistration *unregisterSessionEvent) {
|
|
|
|
session, ok := m.sessions[unregistration.sessionID]
|
|
|
|
if ok {
|
|
|
|
delete(m.sessions, unregistration.sessionID)
|
2021-12-14 22:52:47 +00:00
|
|
|
session.close(unregistration.err)
|
2023-06-17 00:07:56 +00:00
|
|
|
decrementUDPActiveSessions()
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:46:49 +00:00
|
|
|
func (m *manager) sendToSession(datagram *packet.Session) {
|
2022-08-01 12:48:33 +00:00
|
|
|
session, ok := m.sessions[datagram.ID]
|
2021-11-23 12:45:59 +00:00
|
|
|
if !ok {
|
2022-08-01 12:48:33 +00:00
|
|
|
m.log.Error().Str("sessionID", datagram.ID.String()).Msg("session not found")
|
2021-11-23 12:45:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// session writes to destination over a connected UDP socket, which should not be blocking, so this call doesn't
|
|
|
|
// need to run in another go routine
|
2022-08-01 12:48:33 +00:00
|
|
|
session.transportToDst(datagram.Payload)
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|