sing-box/common/sniff/stun.go

26 lines
527 B
Go
Raw Permalink Normal View History

2022-07-16 04:01:02 +00:00
package sniff
import (
"context"
"encoding/binary"
"os"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
)
2024-07-07 07:45:50 +00:00
func STUNMessage(_ context.Context, metadata *adapter.InboundContext, packet []byte) error {
2022-07-16 04:01:02 +00:00
pLen := len(packet)
if pLen < 20 {
2024-07-07 07:45:50 +00:00
return os.ErrInvalid
2022-07-16 04:01:02 +00:00
}
if binary.BigEndian.Uint32(packet[4:8]) != 0x2112A442 {
2024-07-07 07:45:50 +00:00
return os.ErrInvalid
2022-07-16 04:01:02 +00:00
}
if len(packet) < 20+int(binary.BigEndian.Uint16(packet[2:4])) {
2024-07-07 07:45:50 +00:00
return os.ErrInvalid
2022-07-16 04:01:02 +00:00
}
2024-07-07 07:45:50 +00:00
metadata.Protocol = C.ProtocolSTUN
return nil
2022-07-16 04:01:02 +00:00
}