github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/internal/qtls/client_session_cache.go (about)

     1  package qtls
     2  
     3  import (
     4  	"crypto/tls"
     5  )
     6  
     7  type clientSessionCache struct {
     8  	getData func(earlyData bool) []byte
     9  	setData func(data []byte, earlyData bool) (allowEarlyData bool)
    10  	wrapped tls.ClientSessionCache
    11  }
    12  
    13  var _ tls.ClientSessionCache = &clientSessionCache{}
    14  
    15  func (c clientSessionCache) Put(key string, cs *tls.ClientSessionState) {
    16  	if cs == nil {
    17  		c.wrapped.Put(key, nil)
    18  		return
    19  	}
    20  	ticket, state, err := cs.ResumptionState()
    21  	if err != nil || state == nil {
    22  		c.wrapped.Put(key, cs)
    23  		return
    24  	}
    25  	state.Extra = append(state.Extra, addExtraPrefix(c.getData(state.EarlyData)))
    26  	newCS, err := tls.NewResumptionState(ticket, state)
    27  	if err != nil {
    28  		// It's not clear why this would error. Just save the original state.
    29  		c.wrapped.Put(key, cs)
    30  		return
    31  	}
    32  	c.wrapped.Put(key, newCS)
    33  }
    34  
    35  func (c clientSessionCache) Get(key string) (*tls.ClientSessionState, bool) {
    36  	cs, ok := c.wrapped.Get(key)
    37  	if !ok || cs == nil {
    38  		return cs, ok
    39  	}
    40  	ticket, state, err := cs.ResumptionState()
    41  	if err != nil {
    42  		// It's not clear why this would error.
    43  		// Remove the ticket from the session cache, so we don't run into this error over and over again
    44  		c.wrapped.Put(key, nil)
    45  		return nil, false
    46  	}
    47  	// restore QUIC transport parameters and RTT stored in state.Extra
    48  	if extra := findExtraData(state.Extra); extra != nil {
    49  		earlyData := c.setData(extra, state.EarlyData)
    50  		if state.EarlyData {
    51  			state.EarlyData = earlyData
    52  		}
    53  	}
    54  	session, err := tls.NewResumptionState(ticket, state)
    55  	if err != nil {
    56  		// It's not clear why this would error.
    57  		// Remove the ticket from the session cache, so we don't run into this error over and over again
    58  		c.wrapped.Put(key, nil)
    59  		return nil, false
    60  	}
    61  	return session, true
    62  }