2022-07-06 04:39:44 +00:00
|
|
|
package sniff
|
|
|
|
|
|
|
|
import (
|
2022-07-11 12:37:57 +00:00
|
|
|
"bytes"
|
2022-07-06 04:39:44 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
2022-07-11 12:37:57 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
2022-07-06 04:39:44 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-18 12:40:14 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-07-11 12:37:57 +00:00
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-06 04:39:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
StreamSniffer = func(ctx context.Context, reader io.Reader) (*adapter.InboundContext, error)
|
|
|
|
PacketSniffer = func(ctx context.Context, packet []byte) (*adapter.InboundContext, error)
|
|
|
|
)
|
|
|
|
|
2022-10-07 12:30:27 +00:00
|
|
|
func PeekStream(ctx context.Context, conn net.Conn, buffer *buf.Buffer, timeout time.Duration, sniffers ...StreamSniffer) (*adapter.InboundContext, error) {
|
|
|
|
if timeout == 0 {
|
|
|
|
timeout = C.ReadPayloadTimeout
|
|
|
|
}
|
|
|
|
err := conn.SetReadDeadline(time.Now().Add(timeout))
|
2022-07-11 12:37:57 +00:00
|
|
|
if err != nil {
|
2023-04-13 01:03:08 +00:00
|
|
|
return nil, E.Cause(err, "set read deadline")
|
2022-07-11 12:37:57 +00:00
|
|
|
}
|
2022-07-26 04:37:03 +00:00
|
|
|
_, err = buffer.ReadOnceFrom(conn)
|
2022-07-11 12:37:57 +00:00
|
|
|
err = E.Errors(err, conn.SetReadDeadline(time.Time{}))
|
|
|
|
if err != nil {
|
2023-04-13 01:03:08 +00:00
|
|
|
return nil, E.Cause(err, "read payload")
|
2022-07-11 12:37:57 +00:00
|
|
|
}
|
|
|
|
var metadata *adapter.InboundContext
|
2022-10-12 05:37:06 +00:00
|
|
|
var errors []error
|
2022-07-06 04:39:44 +00:00
|
|
|
for _, sniffer := range sniffers {
|
2022-07-11 12:37:57 +00:00
|
|
|
metadata, err = sniffer(ctx, bytes.NewReader(buffer.Bytes()))
|
2022-10-12 05:37:06 +00:00
|
|
|
if metadata != nil {
|
|
|
|
return metadata, nil
|
2022-07-06 04:39:44 +00:00
|
|
|
}
|
2022-10-12 05:37:06 +00:00
|
|
|
errors = append(errors, err)
|
2022-07-06 04:39:44 +00:00
|
|
|
}
|
2022-10-12 05:37:06 +00:00
|
|
|
return nil, E.Errors(errors...)
|
2022-07-06 04:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PeekPacket(ctx context.Context, packet []byte, sniffers ...PacketSniffer) (*adapter.InboundContext, error) {
|
2022-10-12 05:37:06 +00:00
|
|
|
var errors []error
|
2022-07-06 04:39:44 +00:00
|
|
|
for _, sniffer := range sniffers {
|
2022-10-12 05:37:06 +00:00
|
|
|
metadata, err := sniffer(ctx, packet)
|
|
|
|
if metadata != nil {
|
|
|
|
return metadata, nil
|
2022-07-06 04:39:44 +00:00
|
|
|
}
|
2022-10-12 05:37:06 +00:00
|
|
|
errors = append(errors, err)
|
2022-07-06 04:39:44 +00:00
|
|
|
}
|
2022-10-12 05:37:06 +00:00
|
|
|
return nil, E.Errors(errors...)
|
2022-07-06 04:39:44 +00:00
|
|
|
}
|