2023-04-19 08:05:11 +00:00
|
|
|
package sniff_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2024-07-07 07:45:50 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2023-04-19 08:05:11 +00:00
|
|
|
"github.com/sagernet/sing-box/common/sniff"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSniffHTTP1(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
pkt := "GET / HTTP/1.1\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n"
|
2024-07-07 07:45:50 +00:00
|
|
|
var metadata adapter.InboundContext
|
|
|
|
err := sniff.HTTPHost(context.Background(), &metadata, strings.NewReader(pkt))
|
2023-04-19 08:05:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, metadata.Domain, "www.google.com")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSniffHTTP1WithPort(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
pkt := "GET / HTTP/1.1\r\nHost: www.gov.cn:8080\r\nAccept: */*\r\n\r\n"
|
2024-07-07 07:45:50 +00:00
|
|
|
var metadata adapter.InboundContext
|
|
|
|
err := sniff.HTTPHost(context.Background(), &metadata, strings.NewReader(pkt))
|
2023-04-19 08:05:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, metadata.Domain, "www.gov.cn")
|
|
|
|
}
|