stun.go 552 B

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