github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/quic/gquic-go/server_session.go (about)

     1  package gquic
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/quic/gquic-go/internal/protocol"
     7  	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/quic/gquic-go/internal/utils"
     8  )
     9  
    10  type serverSession struct {
    11  	quicSession
    12  
    13  	config *Config
    14  
    15  	logger utils.Logger
    16  }
    17  
    18  var _ packetHandler = &serverSession{}
    19  
    20  func newServerSession(sess quicSession, config *Config, logger utils.Logger) packetHandler {
    21  	return &serverSession{
    22  		quicSession: sess,
    23  		config:      config,
    24  		logger:      logger,
    25  	}
    26  }
    27  
    28  func (s *serverSession) handlePacket(p *receivedPacket) {
    29  	if err := s.handlePacketImpl(p); err != nil {
    30  		s.logger.Debugf("error handling packet from %s: %s", p.remoteAddr, err)
    31  	}
    32  }
    33  
    34  func (s *serverSession) handlePacketImpl(p *receivedPacket) error {
    35  	hdr := p.header
    36  	// ignore all Public Reset packets
    37  	if hdr.ResetFlag {
    38  		return fmt.Errorf("Received unexpected Public Reset for connection %s", hdr.DestConnectionID)
    39  	}
    40  
    41  	// Probably an old packet that was sent by the client before the version was negotiated.
    42  	// It is safe to drop it.
    43  	if (hdr.VersionFlag || hdr.IsLongHeader) && hdr.Version != s.quicSession.GetVersion() {
    44  		return nil
    45  	}
    46  
    47  	if hdr.IsLongHeader {
    48  		switch hdr.Type {
    49  		case protocol.PacketTypeHandshake, protocol.PacketType0RTT: // 0-RTT accepted for gQUIC 44
    50  			// nothing to do here. Packet will be passed to the session.
    51  		case protocol.PacketTypeInitial:
    52  			if hdr.Version == protocol.Version44 {
    53  				break
    54  			}
    55  			fallthrough
    56  		default:
    57  			// Note that this also drops 0-RTT packets.
    58  			return fmt.Errorf("Received unsupported packet type: %s", hdr.Type)
    59  		}
    60  	}
    61  
    62  	s.quicSession.handlePacket(p)
    63  	return nil
    64  }
    65  
    66  func (s *serverSession) GetPerspective() protocol.Perspective {
    67  	return protocol.PerspectiveServer
    68  }