github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/common/sniff/stun.go (about)

     1  package sniff
     2  
     3  import (
     4  	"context"
     5  	"encoding/binary"
     6  	"os"
     7  
     8  	"github.com/inazumav/sing-box/adapter"
     9  	C "github.com/inazumav/sing-box/constant"
    10  )
    11  
    12  func STUNMessage(ctx context.Context, packet []byte) (*adapter.InboundContext, error) {
    13  	pLen := len(packet)
    14  	if pLen < 20 {
    15  		return nil, os.ErrInvalid
    16  	}
    17  	if binary.BigEndian.Uint32(packet[4:8]) != 0x2112A442 {
    18  		return nil, os.ErrInvalid
    19  	}
    20  	if len(packet) < 20+int(binary.BigEndian.Uint16(packet[2:4])) {
    21  		return nil, os.ErrInvalid
    22  	}
    23  	return &adapter.InboundContext{Protocol: C.ProtocolSTUN}, nil
    24  }