sing-box/common/sniff/dtls.go

33 lines
656 B
Go
Raw Normal View History

2024-07-07 15:45:50 +08:00
package sniff
import (
"context"
"os"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
)
2024-07-07 15:45:50 +08:00
func DTLSRecord(ctx context.Context, metadata *adapter.InboundContext, packet []byte) error {
2024-07-07 15:45:50 +08:00
const fixedHeaderSize = 13
if len(packet) < fixedHeaderSize {
2024-07-07 15:45:50 +08:00
return os.ErrInvalid
2024-07-07 15:45:50 +08:00
}
contentType := packet[0]
switch contentType {
case 20, 21, 22, 23, 25:
default:
2024-07-07 15:45:50 +08:00
return os.ErrInvalid
2024-07-07 15:45:50 +08:00
}
versionMajor := packet[1]
if versionMajor != 0xfe {
2024-07-07 15:45:50 +08:00
return os.ErrInvalid
2024-07-07 15:45:50 +08:00
}
versionMinor := packet[2]
if versionMinor != 0xff && versionMinor != 0xfd {
2024-07-07 15:45:50 +08:00
return os.ErrInvalid
2024-07-07 15:45:50 +08:00
}
2024-07-07 15:45:50 +08:00
metadata.Protocol = C.ProtocolDTLS
return nil
2024-07-07 15:45:50 +08:00
}